CommentKarmaShouldBeUpdatedOnVoteTest.php 1.54 KB
<?php

namespace FootyRoom\Tests;

use Illuminate\Database\Connection;

class CommentKarmaShouldBeUpdatedOnVoteTest extends TestCase
{
    use SetsUpFixtures;

    public function setup()
    {
        parent::setup();

        $this->mysql = $this->app->make(Connection::class);
    }

    public function testVoteUpShouldSeeCommentKarmaIncremented()
    {
        $commentId = 1;
        $commentBefore = $this->mysql->table('comments')->where('id', '=', $commentId)->first();

        $user = factory('FootyRoom\User\User')->make(['user_id' => 2]);
        $response = $this->actingAs($user)->json('POST', "/comments/$commentId/vote", ['vote' => 1]);

        $commentAfter = $this->mysql->table('comments')->where('id', '=', $commentId)->first();

        $this->assertEquals(200, $response->status());
        $this->assertEquals($commentBefore->karma + 1, $commentAfter->karma);
    }

    public function testVoteDownShouldSeeCommentKarmaDecremented()
    {
        $commentId = 1;
        $commentBefore = $this->mysql->table('comments')->where('id', '=', $commentId)->first();

        // We will use different user , so we don't get already voted exception
        $user = factory('FootyRoom\User\User')->make(['user_id' => 3]);
        $response = $this->actingAs($user)->json('POST', "/comments/$commentId/vote", ['vote' => -1]);

        $commentAfter = $this->mysql->table('comments')->where('id', '=', $commentId)->first();

        $this->assertEquals(200, $response->status());
        $this->assertEquals($commentBefore->karma - 1, $commentAfter->karma);
    }
}