Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
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);
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);
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));
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);
0 ignored issues
show
$mockedEvent is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Kunstmaan\VotingB...e\Event\EventInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array<integer|boolean>[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
89
     */
90 View Code Duplication
    public function dataTestOnVote()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        return array(
93
            array(2, 2, true),
94
            array(2, 1, false),
95
            array(2, 3, true),
96
        );
97
    }
98
}
99