|
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\PhoneVerificationBundle\Tests\Event; |
|
12
|
|
|
|
|
13
|
|
|
use libphonenumber\PhoneNumberUtil; |
|
14
|
|
|
use LoginCidadao\CoreBundle\Entity\Person; |
|
15
|
|
|
use LoginCidadao\PhoneVerificationBundle\Event\PhoneVerificationSubscriber; |
|
16
|
|
|
use LoginCidadao\PhoneVerificationBundle\PhoneVerificationEvents; |
|
17
|
|
|
use PHPUnit\Framework\TestCase; |
|
18
|
|
|
use Symfony\Component\Security\Http\SecurityEvents; |
|
19
|
|
|
|
|
20
|
|
|
class PhoneVerificationSubscriberTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
private function getPhoneVerification() |
|
23
|
|
|
{ |
|
24
|
|
|
$phoneVerification = $this->createMock('LoginCidadao\PhoneVerificationBundle\Model\PhoneVerificationInterface'); |
|
25
|
|
|
|
|
26
|
|
|
return $phoneVerification; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
private function getPhoneVerificationService() |
|
30
|
|
|
{ |
|
31
|
|
|
$phoneVerificationServiceClass = 'LoginCidadao\PhoneVerificationBundle\Service\PhoneVerificationServiceInterface'; |
|
32
|
|
|
$phoneVerificationService = $this->createMock($phoneVerificationServiceClass); |
|
33
|
|
|
|
|
34
|
|
|
return $phoneVerificationService; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
private function getDispatcher() |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface') |
|
40
|
|
|
->disableOriginalConstructor() |
|
41
|
|
|
->getMock(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testGetSubscribedEvents() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->assertArrayHasKey( |
|
47
|
|
|
PhoneVerificationEvents::PHONE_CHANGED, |
|
48
|
|
|
PhoneVerificationSubscriber::getSubscribedEvents() |
|
49
|
|
|
); |
|
50
|
|
|
$this->assertArrayHasKey( |
|
51
|
|
|
PhoneVerificationEvents::PHONE_VERIFICATION_REQUESTED, |
|
52
|
|
|
PhoneVerificationSubscriber::getSubscribedEvents() |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @throws \libphonenumber\NumberParseException |
|
58
|
|
|
*/ |
|
59
|
|
|
public function testOnPhoneChange() |
|
60
|
|
|
{ |
|
61
|
|
|
$person = new Person(); |
|
62
|
|
|
$person->setMobile(PhoneNumberUtil::getInstance()->parse('+5551999999999', 'BR')); |
|
63
|
|
|
|
|
64
|
|
|
$oldPhone = PhoneNumberUtil::getInstance()->parse('+5551999998888', 'BR'); |
|
65
|
|
|
$event = $this->getMockBuilder('LoginCidadao\PhoneVerificationBundle\Event\PhoneChangedEvent') |
|
66
|
|
|
->setConstructorArgs([$person, $oldPhone]) |
|
67
|
|
|
->getMock(); |
|
68
|
|
|
|
|
69
|
|
|
$event->expects($this->any())->method('getPerson')->willReturn($person); |
|
70
|
|
|
$event->expects($this->any())->method('getOldPhone')->willReturn($oldPhone); |
|
71
|
|
|
|
|
72
|
|
|
$phoneVerification = $this->getPhoneVerification(); |
|
73
|
|
|
|
|
74
|
|
|
$phoneVerificationService = $this->getPhoneVerificationService(); |
|
75
|
|
|
$phoneVerificationService->expects($this->once())->method('getPhoneVerification') |
|
76
|
|
|
->willReturn($phoneVerification); |
|
77
|
|
|
$phoneVerificationService->expects($this->once())->method('createPhoneVerification') |
|
78
|
|
|
->willReturn($phoneVerification); |
|
79
|
|
|
|
|
80
|
|
|
$dispatcher = $this->getDispatcher(); |
|
81
|
|
|
$dispatcher->expects($this->once())->method('dispatch'); |
|
82
|
|
|
|
|
83
|
|
|
$listener = new PhoneVerificationSubscriber($phoneVerificationService); |
|
84
|
|
|
$listener->onPhoneChange($event, PhoneVerificationEvents::PHONE_CHANGED, $dispatcher); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @throws \libphonenumber\NumberParseException |
|
89
|
|
|
*/ |
|
90
|
|
|
public function testOnPhoneSet() |
|
91
|
|
|
{ |
|
92
|
|
|
$person = new Person(); |
|
93
|
|
|
$person->setMobile(PhoneNumberUtil::getInstance()->parse('+5551999999999', 'BR')); |
|
94
|
|
|
|
|
95
|
|
|
$event = $this->getMockBuilder('LoginCidadao\PhoneVerificationBundle\Event\PhoneChangedEvent') |
|
96
|
|
|
->setConstructorArgs([$person, null]) |
|
97
|
|
|
->getMock(); |
|
98
|
|
|
|
|
99
|
|
|
$event->expects($this->any())->method('getPerson')->willReturn($person); |
|
100
|
|
|
$event->expects($this->any())->method('getOldPhone')->willReturn(null); |
|
101
|
|
|
|
|
102
|
|
|
$phoneVerification = $this->getPhoneVerification(); |
|
103
|
|
|
|
|
104
|
|
|
$phoneVerificationService = $this->getPhoneVerificationService(); |
|
105
|
|
|
$phoneVerificationService->expects($this->once())->method('createPhoneVerification') |
|
106
|
|
|
->willReturn($phoneVerification); |
|
107
|
|
|
|
|
108
|
|
|
$dispatcher = $this->getDispatcher(); |
|
109
|
|
|
$dispatcher->expects($this->once())->method('dispatch'); |
|
110
|
|
|
|
|
111
|
|
|
$listener = new PhoneVerificationSubscriber($phoneVerificationService); |
|
112
|
|
|
$listener->onPhoneChange($event, PhoneVerificationEvents::PHONE_CHANGED, $dispatcher); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @throws \libphonenumber\NumberParseException |
|
117
|
|
|
*/ |
|
118
|
|
|
public function testOnPhoneUnset() |
|
119
|
|
|
{ |
|
120
|
|
|
$oldPhone = PhoneNumberUtil::getInstance()->parse('+5551999999999', 'BR'); |
|
121
|
|
|
$person = new Person(); |
|
122
|
|
|
$person->setMobile(null); |
|
123
|
|
|
|
|
124
|
|
|
$event = $this->getMockBuilder('LoginCidadao\PhoneVerificationBundle\Event\PhoneChangedEvent') |
|
125
|
|
|
->setConstructorArgs([$person, null]) |
|
126
|
|
|
->getMock(); |
|
127
|
|
|
|
|
128
|
|
|
$event->expects($this->any())->method('getPerson')->willReturn($person); |
|
129
|
|
|
$event->expects($this->any())->method('getOldPhone')->willReturn($oldPhone); |
|
130
|
|
|
|
|
131
|
|
|
$phoneVerificationService = $this->getPhoneVerificationService(); |
|
132
|
|
|
|
|
133
|
|
|
$dispatcher = $this->getDispatcher(); |
|
134
|
|
|
$dispatcher->expects($this->never())->method('dispatch'); |
|
135
|
|
|
|
|
136
|
|
|
$listener = new PhoneVerificationSubscriber($phoneVerificationService); |
|
137
|
|
|
$listener->onPhoneChange($event, PhoneVerificationEvents::PHONE_CHANGED, $dispatcher); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @throws \libphonenumber\NumberParseException |
|
142
|
|
|
*/ |
|
143
|
|
|
public function testOnVerificationRequest() |
|
144
|
|
|
{ |
|
145
|
|
|
$phoneVerificationServiceClass = 'LoginCidadao\PhoneVerificationBundle\Service\PhoneVerificationServiceInterface'; |
|
146
|
|
|
$phoneVerificationService = $this->createMock($phoneVerificationServiceClass); |
|
147
|
|
|
|
|
148
|
|
|
$person = new Person(); |
|
149
|
|
|
$person->setMobile(PhoneNumberUtil::getInstance()->parse('+5551999999999', 'BR')); |
|
150
|
|
|
|
|
151
|
|
|
$phoneVerification = $this->getPhoneVerification(); |
|
152
|
|
|
$phoneVerification->expects($this->any())->method('getPerson')->willReturn($person); |
|
153
|
|
|
|
|
154
|
|
|
$event = $this->getMockBuilder('LoginCidadao\PhoneVerificationBundle\Event\SendPhoneVerificationEvent') |
|
155
|
|
|
->setConstructorArgs([$phoneVerification]) |
|
156
|
|
|
->getMock(); |
|
157
|
|
|
$event->expects($this->once())->method('getPhoneVerification')->willReturn($phoneVerification); |
|
158
|
|
|
|
|
159
|
|
|
$logger = $this->createMock('Psr\Log\LoggerInterface'); |
|
160
|
|
|
$logger->expects($this->atLeastOnce())->method('log'); |
|
161
|
|
|
|
|
162
|
|
|
$listener = new PhoneVerificationSubscriber($phoneVerificationService); |
|
163
|
|
|
$listener->setLogger($logger); |
|
164
|
|
|
$listener->onVerificationRequest($event); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function testOnCodeSent() |
|
168
|
|
|
{ |
|
169
|
|
|
$sentVerification = $this->createMock('LoginCidadao\PhoneVerificationBundle\Model\SentVerificationInterface'); |
|
170
|
|
|
|
|
171
|
|
|
$phoneVerificationServiceClass = 'LoginCidadao\PhoneVerificationBundle\Service\PhoneVerificationServiceInterface'; |
|
172
|
|
|
$phoneVerificationService = $this->createMock($phoneVerificationServiceClass); |
|
173
|
|
|
$phoneVerificationService->expects($this->once())->method('registerVerificationSent')->with($sentVerification); |
|
174
|
|
|
|
|
175
|
|
|
$event = $this->getMockBuilder('LoginCidadao\PhoneVerificationBundle\Event\SendPhoneVerificationEvent') |
|
176
|
|
|
->disableOriginalConstructor() |
|
177
|
|
|
->getMock(); |
|
178
|
|
|
$event->expects($this->once())->method('getSentVerification')->willReturn($sentVerification); |
|
179
|
|
|
|
|
180
|
|
|
$listener = new PhoneVerificationSubscriber($phoneVerificationService); |
|
181
|
|
|
$listener->onCodeSent($event); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
public function testOnLoginNoPhone() |
|
185
|
|
|
{ |
|
186
|
|
|
$person = $this->createMock('LoginCidadao\CoreBundle\Model\PersonInterface'); |
|
187
|
|
|
$person->expects($this->once())->method('getMobile')->willReturn(null); |
|
188
|
|
|
|
|
189
|
|
|
$token = $this->createMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); |
|
190
|
|
|
$token->expects($this->once())->method('getUser')->willReturn($person); |
|
191
|
|
|
|
|
192
|
|
|
$phoneVerificationService = $this->getPhoneVerificationService(); |
|
193
|
|
|
$dispatcher = $this->getDispatcher(); |
|
194
|
|
|
|
|
195
|
|
|
$event = $this->getMockBuilder('Symfony\Component\Security\Http\Event\InteractiveLoginEvent') |
|
196
|
|
|
->disableOriginalConstructor() |
|
197
|
|
|
->getMock(); |
|
198
|
|
|
$event->expects($this->once())->method('getAuthenticationToken')->willReturn($token); |
|
199
|
|
|
|
|
200
|
|
|
$listener = new PhoneVerificationSubscriber($phoneVerificationService); |
|
201
|
|
|
$listener->onLogin($event, SecurityEvents::INTERACTIVE_LOGIN, $dispatcher); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
public function testOnLoginWithPhone() |
|
205
|
|
|
{ |
|
206
|
|
|
$phone = $this->getMockBuilder('libphonenumber\PhoneNumber') |
|
207
|
|
|
->disableOriginalConstructor() |
|
208
|
|
|
->getMock(); |
|
209
|
|
|
|
|
210
|
|
|
$person = $this->createMock('LoginCidadao\CoreBundle\Model\PersonInterface'); |
|
211
|
|
|
$person->expects($this->once())->method('getMobile')->willReturn($phone); |
|
212
|
|
|
|
|
213
|
|
|
$token = $this->createMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); |
|
214
|
|
|
$token->expects($this->once())->method('getUser')->willReturn($person); |
|
215
|
|
|
|
|
216
|
|
|
$verification = $this->getPhoneVerification(); |
|
217
|
|
|
$phoneVerificationService = $this->getPhoneVerificationService(); |
|
218
|
|
|
$phoneVerificationService->expects($this->once())->method('enforcePhoneVerification') |
|
219
|
|
|
->willReturn($verification); |
|
220
|
|
|
|
|
221
|
|
|
$dispatcher = $this->getDispatcher(); |
|
222
|
|
|
$dispatcher->expects($this->once())->method('dispatch') |
|
223
|
|
|
->with( |
|
224
|
|
|
PhoneVerificationEvents::PHONE_VERIFICATION_REQUESTED, |
|
225
|
|
|
$this->isInstanceOf('LoginCidadao\PhoneVerificationBundle\Event\SendPhoneVerificationEvent') |
|
226
|
|
|
); |
|
227
|
|
|
|
|
228
|
|
|
$event = $this->getMockBuilder('Symfony\Component\Security\Http\Event\InteractiveLoginEvent') |
|
229
|
|
|
->disableOriginalConstructor() |
|
230
|
|
|
->getMock(); |
|
231
|
|
|
$event->expects($this->once())->method('getAuthenticationToken')->willReturn($token); |
|
232
|
|
|
|
|
233
|
|
|
$listener = new PhoneVerificationSubscriber($phoneVerificationService); |
|
234
|
|
|
$listener->onLogin($event, SecurityEvents::INTERACTIVE_LOGIN, $dispatcher); |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|