|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace LoginCidadao\CoreBundle\Tests\LongPolling; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
14
|
|
|
use LoginCidadao\CoreBundle\Entity\PersonRepository; |
|
15
|
|
|
use LoginCidadao\CoreBundle\LongPolling\LongPollingUtils; |
|
16
|
|
|
use LoginCidadao\CoreBundle\Model\PersonInterface; |
|
17
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @group time-sensitive |
|
21
|
|
|
*/ |
|
22
|
|
|
class LongPollingUtilsTest extends \PHPUnit_Framework_TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
public function testRunTimeLimited() |
|
25
|
|
|
{ |
|
26
|
|
|
$timer = 'timeLimited'; |
|
27
|
|
|
$stopwatch = new Stopwatch(); |
|
28
|
|
|
$stopwatch->start($timer); |
|
29
|
|
|
|
|
30
|
|
|
$longPolling = new LongPollingUtils($this->getEntityManager(), 60); |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
$count = 5; |
|
33
|
|
|
$response = $longPolling->runTimeLimited(function () use (&$count) { |
|
34
|
|
|
return --$count <= 0; |
|
35
|
|
|
}); |
|
36
|
|
|
$duration = $stopwatch->stop($timer)->getDuration(); |
|
37
|
|
|
|
|
38
|
|
|
$this->assertTrue($response); |
|
39
|
|
|
$this->assertEquals(0, $count); |
|
40
|
|
|
$this->assertLessThan(60, $duration); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testTimeout() |
|
44
|
|
|
{ |
|
45
|
|
|
$this->setExpectedException('LoginCidadao\APIBundle\Exception\RequestTimeoutException'); |
|
46
|
|
|
|
|
47
|
|
|
$longPolling = new LongPollingUtils($this->getEntityManager(), 30); |
|
|
|
|
|
|
48
|
|
|
$longPolling->runTimeLimited(function () { |
|
49
|
|
|
return false; |
|
50
|
|
|
}); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testEntityUpdateCheckerCallback() |
|
54
|
|
|
{ |
|
55
|
|
|
$updatedAt = new \DateTime('-1 hour'); |
|
56
|
|
|
$currentUpdatedAt = new \DateTime('-1 hour'); |
|
57
|
|
|
|
|
58
|
|
|
$person = $this->getPerson(); |
|
59
|
|
|
$person->expects($this->any())->method('getUpdatedAt') |
|
|
|
|
|
|
60
|
|
|
->willReturnCallback(function () use (&$currentUpdatedAt) { |
|
61
|
|
|
return $currentUpdatedAt; |
|
62
|
|
|
}); |
|
63
|
|
|
|
|
64
|
|
|
$em = $this->getEntityManager(); |
|
65
|
|
|
$em->expects($this->once())->method('getRepository') |
|
|
|
|
|
|
66
|
|
|
->willReturn($this->getEvolvingPersonRepo($person, $currentUpdatedAt)); |
|
67
|
|
|
|
|
68
|
|
|
$longPolling = new LongPollingUtils($em); |
|
|
|
|
|
|
69
|
|
|
$callback = $longPolling->getEntityUpdateCheckerCallback($person, $updatedAt); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
call_user_func($callback); |
|
72
|
|
|
$response = call_user_func($callback); |
|
73
|
|
|
|
|
74
|
|
|
$this->assertNotFalse($response); |
|
75
|
|
|
$this->assertNotEquals($updatedAt, $currentUpdatedAt); |
|
76
|
|
|
$this->assertEquals($person, $response); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testEntityUpdateCheckerCallbackNeverUpdated() |
|
80
|
|
|
{ |
|
81
|
|
|
$person = $this->getPerson(); |
|
82
|
|
|
$person->expects($this->once())->method('getUpdatedAt')->willReturn(null); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
$repo = $this->getPersonRepository(); |
|
85
|
|
|
$repo->expects($this->any())->method('find')->willReturn($person); |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
$em = $this->getEntityManager(); |
|
88
|
|
|
$em->expects($this->once())->method('getRepository')->willReturn($repo); |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
$longPolling = new LongPollingUtils($em); |
|
|
|
|
|
|
91
|
|
|
$callback = $longPolling->getEntityUpdateCheckerCallback($person, new \DateTime()); |
|
|
|
|
|
|
92
|
|
|
$response = call_user_func($callback); |
|
93
|
|
|
|
|
94
|
|
|
$this->assertFalse($response); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function testObviouslyValidEmail() |
|
98
|
|
|
{ |
|
99
|
|
|
$person = $this->getPerson(); |
|
100
|
|
|
$person->expects($this->once())->method('getEmailConfirmedAt')->willReturn(new \DateTime()); |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
$longPolling = new LongPollingUtils($this->getEntityManager()); |
|
|
|
|
|
|
103
|
|
|
$response = $longPolling->waitValidEmail($person, new \DateTime()); |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
$this->assertTrue($response); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function testWaitValidEmail() |
|
109
|
|
|
{ |
|
110
|
|
|
$updatedAt = new \DateTime('-1 hour'); |
|
111
|
|
|
$currentUpdatedAt = new \DateTime('-1 hour'); |
|
112
|
|
|
|
|
113
|
|
|
$person = $this->getPerson(); |
|
114
|
|
|
$person->expects($this->any())->method('getUpdatedAt') |
|
|
|
|
|
|
115
|
|
|
->willReturnCallback(function () use (&$currentUpdatedAt) { |
|
116
|
|
|
return $currentUpdatedAt; |
|
117
|
|
|
}); |
|
118
|
|
|
|
|
119
|
|
|
$repo = $this->getEvolvingPersonRepo($person, $currentUpdatedAt); |
|
120
|
|
|
|
|
121
|
|
|
$em = $this->getEntityManager(); |
|
122
|
|
|
$em->expects($this->once())->method('getRepository')->willReturn($repo); |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
$longPolling = new LongPollingUtils($em); |
|
|
|
|
|
|
125
|
|
|
$response = $longPolling->waitValidEmail($person, $updatedAt); |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
$this->assertTrue($response); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|EntityManagerInterface |
|
132
|
|
|
*/ |
|
133
|
|
|
private function getEntityManager() |
|
134
|
|
|
{ |
|
135
|
|
|
$em = $this->getMock('Doctrine\ORM\EntityManagerInterface'); |
|
136
|
|
|
|
|
137
|
|
|
return $em; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|PersonRepository |
|
142
|
|
|
*/ |
|
143
|
|
|
private function getPersonRepository() |
|
144
|
|
|
{ |
|
145
|
|
|
$repo = $this->getMockBuilder('LoginCidadao\CoreBundle\Entity\PersonRepository') |
|
146
|
|
|
->disableOriginalConstructor()->getMock(); |
|
147
|
|
|
|
|
148
|
|
|
return $repo; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @return PersonInterface|\PHPUnit_Framework_MockObject_MockObject |
|
153
|
|
|
*/ |
|
154
|
|
|
private function getPerson() |
|
155
|
|
|
{ |
|
156
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|PersonInterface $person */ |
|
157
|
|
|
$person = $this->getMock('LoginCidadao\CoreBundle\Model\PersonInterface'); |
|
158
|
|
|
$person->expects($this->any())->method('getId')->willReturn(123); |
|
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
return $person; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param PersonInterface|\PHPUnit_Framework_MockObject_MockObject $person |
|
165
|
|
|
* @param \DateTime|null $currentUpdatedAt |
|
166
|
|
|
* @return PersonRepository|\PHPUnit_Framework_MockObject_MockObject |
|
167
|
|
|
*/ |
|
168
|
|
|
private function getEvolvingPersonRepo(&$person, &$currentUpdatedAt) |
|
169
|
|
|
{ |
|
170
|
|
|
$repo = $this->getPersonRepository(); |
|
171
|
|
|
$repo->expects($this->at(0))->method('find')->willReturn($person); |
|
|
|
|
|
|
172
|
|
|
$repo->expects($this->at(1))->method('find') |
|
173
|
|
|
->willReturnCallback(function () use (&$person, &$currentUpdatedAt) { |
|
174
|
|
|
$currentUpdatedAt = new \DateTime(); |
|
175
|
|
|
$person->expects($this->any())->method('getEmailConfirmedAt')->willReturn($currentUpdatedAt); |
|
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
return $person; |
|
178
|
|
|
}); |
|
179
|
|
|
|
|
180
|
|
|
return $repo; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.