Failed Conditions
Push — issue#764 ( 39afc8 )
by Guilherme
11:43
created

PhoneVerificationSubscriberTest::getDispatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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