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); |
|
|
|
|
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
|
|
View Code Duplication |
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
|
|
|
|
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: