Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

Security/MaxNumberByIpEventListenerTest.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\VotingBundle\Tests\EventListener\Security;
4
5
use Kunstmaan\VotingBundle\Event\Facebook\FacebookLikeEvent;
6
use Kunstmaan\VotingBundle\EventListener\Security\MaxNumberByIpEventListener;
7
use PHPUnit\Framework\TestCase;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
11
/**
12
 * Test Max Number by Ip event listener
13
 */
14
class MaxNumberByIpEventListenerTest extends TestCase
15
{
16
    /**
17
     * @param $returnNull
18
     * @param int $voteNumber
19
     *
20
     * @return \Kunstmaan\VotingBundle\Services\RepositoryResolver
21
     */
22
    protected function mockRepositoryResolver($returnNull, $voteNumber = 0)
23
    {
24
        $mockedRepository = null;
25
26
        if (!$returnNull) {
27
            $mockedRepository = $this->createMock('Kunstmaan\VotingBundle\Repository\AbstractVoteRepository'); //, array('countByReferenceAndByIp'), array(), 'MockedRepository', false);
0 ignored issues
show
Unused Code Comprehensibility introduced by
79% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
28
29
            $mockedRepository->expects($this->any())
30
             ->method('countByReferenceAndByIp')
31
             ->willReturn($voteNumber);
32
        }
33
34
        $mockedResolver = $this->createMock('Kunstmaan\VotingBundle\Services\RepositoryResolver'); //, array('getRepositoryForEvent'), array(), 'MockedResolver', false);
0 ignored issues
show
Unused Code Comprehensibility introduced by
79% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
35
36
        $mockedResolver->expects($this->any())
37
             ->method('getRepositoryForEvent')
38
             ->willReturn($mockedRepository);
39
40
        /* @var \Kunstmaan\VotingBundle\Services\RepositoryResolver $mockedResolver */
41
        return $mockedResolver;
42
    }
43
44
    /**
45
     * @dataProvider dataTestOnVote
46
     */
47
    public function testOnVote($maxNumber, $number, $stopPropagation)
48
    {
49
        $mockedEvent = $this->createMock('Kunstmaan\VotingBundle\Event\UpDown\UpVoteEvent'); //, array('stopPropagation'), array(new Request(), null, null));
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
50
51
        if ($stopPropagation) {
52
            $mockedEvent->expects($this->once())
53
                ->method('stopPropagation');
54
        } else {
55
            $mockedEvent->expects($this->never())
56
                ->method('stopPropagation');
57
        }
58
59
        $mockedEvent->expects($this->any())
60
            ->method('getRequest')
61
            ->willReturn(new Request());
62
63
        $resolver = $this->mockRepositoryResolver(false, $number);
64
65
        $listener = new MaxNumberByIpEventListener($resolver, $maxNumber);
66
67
        $listener->onVote($mockedEvent);
68
    }
69
70
    /**
71
     * @dataProvider dataTestOnVote
72
     */
73
    public function testOnVoteReturnsNothing($maxNumber, $number, $stopPropagation)
74
    {
75
        $event = new FacebookLikeEvent(new Request(), new Response(), 2);
76
77
        $resolver = $this->mockRepositoryResolver(false, $number);
78
79
        $listener = new MaxNumberByIpEventListener($resolver, $maxNumber);
80
81
        $this->assertNull($listener->onVote($event));
82
        $this->assertSame($stopPropagation, $event->isPropagationStopped());
83
    }
84
85
    /**
86
     * Data for test on vote
87
     *
88
     * @return array
89
     */
90
    public function dataTestOnVote()
91
    {
92
        return array(
93
            array(2, 2, true),
94
            array(2, 1, false),
95
            array(2, 3, true),
96
        );
97
    }
98
}
99