VotingManagerTest.php 4.78 KB
<?php

namespace FootyRoom\Tests\Integration\Vote;

use FootyRoom\Core\Vote\Choice;
use FootyRoom\Core\Vote\VotingManager;
use FootyRoom\Queries\VoteQuery;
use FootyRoom\Repositories\VoteRepository;
use FootyRoom\Tests\TestCase;
use Illuminate\Contracts\Events\Dispatcher;

class VotingManagerTest extends TestCase
{
    public function setUp()
    {
        $this->mockVoteRepo = $this->getMockBuilder(VoteRepository::class)
                            ->disableOriginalConstructor()
                            ->setMethods(['deleteBy', 'createMany'])
                            ->getMock();

        $this->mockVoteQuery = $this->getMockBuilder(VoteQuery::class)
                            ->disableOriginalConstructor()
                            ->setMethods(['findByUserId', 'findByTrackerOnly'])
                            ->getMock();

        $this->mockDispactcher = $this->getMockBuilder(Dispatcher::class)
                            ->disableOriginalConstructor()
                            ->getMock();

        $this->voteManager = new VotingManager($this->mockVoteRepo, $this->mockVoteQuery, $this->mockDispactcher);
    }

    /**
     * @test
     * @expectedException        \FootyRoom\Core\CoreException
     */
    public function pollIdMustNotBeEmpty()
    {
        $pollId = null;
        $choices = [new Choice('1')];
        $tracker = 'test';
        $userId = null;

        $this->voteManager->vote($pollId, $choices, $tracker, $userId);
    }

    /**
     * @test
     * @expectedException        \FootyRoom\Core\CoreException
     */
    public function ChoiceMustNotLessThanOne()
    {
        $pollId = 'poll-1234';
        $choices = [];
        $tracker = 'test';
        $userId = null;

        $this->voteManager->vote($pollId, $choices, $tracker, $userId);
    }

    /**
     * @test
     * @expectedException        \FootyRoom\Core\CoreException
     */
    public function CookiesMustBeEnabled_TrackerMustNotBeEmpty()
    {
        $pollId = 'poll-1234';
        $choices = [new Choice('1')];
        $tracker = '';
        $userId = null;

        $this->voteManager->vote($pollId, $choices, $tracker, $userId);
    }

    /**
     * @test
     */
    public function deleteWasCalledIfUpdateablePollVoteWasUpdated()
    {
        $pollId = 'poll-1234';
        $choices = [new Choice('1')];
        $tracker = 'test';
        $userId = 41301;

        $this->mockVoteRepo->expects($this->once())
                           ->method('deleteBy');

        $this->voteManager->vote($pollId, $choices, $tracker, $userId);
    }

    /**
     * @test
     */
    public function deleteWasCalledIfOtherUpdateablePollVoteWasUpdated()
    {
        $pollId = 'motm-1234';
        $choices = [new Choice('1')];
        $tracker = 'test';
        $userId = 41301;

        $this->mockVoteRepo->expects($this->once())
                           ->method('deleteBy');

        $this->voteManager->vote($pollId, $choices, $tracker, $userId);
    }

    /**
     * @test
     * @expectedException        \FootyRoom\Core\CoreException
     */
    public function cantUpdateImmutablePollVotes()
    {
        $pollId = 'not-updatable-poll-1234';
        $choices = [new Choice('1')];
        $tracker = 'test';
        $userId = 41301;

        $this->mockVoteQuery->method('findByUserId')
                            ->will($this->returnValue([[]]));

        $this->voteManager->vote($pollId, $choices, $tracker, $userId);
    }

    /**
     * @test
     * @expectedException        \FootyRoom\Core\CoreException
     */
    public function hasVoted()
    {
        $pollId = 'not-updatable-poll-1234';
        $choices = [new Choice('1')];
        $tracker = 'test';
        $userId = 41301;

        $this->mockVoteQuery->method('findByUserId')
                            ->will($this->returnValue([[]]));

        $this->voteManager->vote($pollId, $choices, $tracker, $userId);
    }

    /**
     * @test
     * @expectedException        \FootyRoom\Core\CoreException
     */
    public function ifUserVotingThenCountByUserId()
    {
        $pollId = 'not-updatable-poll-1234';
        $choices = [new Choice('1')];
        $tracker = 'test';
        $userId = 41301;

        $this->mockVoteQuery->method('findByUserId')
                            ->will($this->returnValue([[]]));

        $this->voteManager->vote($pollId, $choices, $tracker, $userId);
    }

    /**
     * @test
     * @expectedException        \FootyRoom\Core\CoreException
     */
    public function ifGuestVotingThenCountByTracker()
    {
        $pollId = 'not-updatable-poll-1234';
        $choices = [new Choice('1')];
        $tracker = 'test';
        $userId = '';

        $this->mockVoteQuery->method('findByTrackerOnly')
                            ->will($this->returnValue([[]]));

        $this->voteManager->vote($pollId, $choices, $tracker, $userId);
    }
}