Passed
Push — master ( 7438ce...69adc8 )
by Guilherme
01:29 queued 11s
created

ProfileEditSubscriberTest   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 340
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 340
ccs 0
cts 267
cp 0
rs 10
c 0
b 0
f 0
wmc 19
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 PROCERGS\LoginCidadao\NfgBundle\Tests\EventListener;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use FOS\UserBundle\Event\FormEvent;
15
use FOS\UserBundle\Event\GetResponseUserEvent;
16
use LoginCidadao\CoreBundle\Entity\Person;
17
use PHPUnit\Framework\MockObject\MockObject;
18
use PHPUnit\Framework\TestCase;
19
use PROCERGS\LoginCidadao\CoreBundle\Entity\PersonMeuRS;
20
use PROCERGS\LoginCidadao\CoreBundle\Helper\MeuRSHelper;
21
use PROCERGS\LoginCidadao\NfgBundle\Entity\NfgProfile;
22
use PROCERGS\LoginCidadao\NfgBundle\EventListener\ProfileEditSubscriber;
23
use PROCERGS\LoginCidadao\NfgBundle\Exception\NfgServiceUnavailableException;
24
use PROCERGS\LoginCidadao\NfgBundle\Service\Nfg;
25
use Symfony\Component\Form\FormInterface;
26
use Symfony\Component\HttpFoundation\Request;
27
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
28
use Symfony\Component\Security\Core\User\UserInterface;
29
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
30
use Symfony\Component\Translation\TranslatorInterface;
31
32
class ProfileEditSubscriberTest extends TestCase
33
{
34
    public function testImplementsSubscriberInterface()
35
    {
36
        $events = ProfileEditSubscriber::getSubscribedEvents();
37
38
        $this->assertNotEmpty($events);
39
        $this->assertTrue(is_array($events));
40
    }
41
42
    public function testEditInitialize()
43
    {
44
        $person = new Person();
45
46
        $meuRSHelper = $this->getMeuRSHelper();
47
        $meuRSHelper->expects($this->once())->method('getVoterRegistration')->with($person)->willReturn('123456789012');
48
49
        $subscriber = new ProfileEditSubscriber(
50
            $this->getEntityManager(),
51
            $meuRSHelper,
52
            $this->getNfgService(),
53
            $this->getTokenStorage($person),
54
            $this->getTranslator()
55
        );
56
        $event = new GetResponseUserEvent($person);
57
        $subscriber->onProfileEditInitialize($event);
58
    }
59
60
    public function testEditSuccessNoChanges()
61
    {
62
        $voterRegistration = '123456789012';
63
        $personMeuRS = new PersonMeuRS();
64
        $personMeuRS->setVoterRegistration($voterRegistration);
65
        $person = new Person();
66
        $person->personMeuRS = $personMeuRS;
67
68
        $form = $this->getForm($person);
69
70
        $meuRSHelper = $this->getMeuRSHelper();
71
        $meuRSHelper->expects($this->once())->method('getVoterRegistration')
72
            ->with($person)->willReturn($voterRegistration);
73
        $meuRSHelper->expects($this->once())->method('getPersonMeuRS')->with($person)->willReturn($personMeuRS);
74
75
        $subscriber = new ProfileEditSubscriber(
76
            $this->getEntityManager(),
77
            $meuRSHelper,
78
            $this->getNfgService(),
79
            $this->getTokenStorage($person),
80
            $this->getTranslator()
81
        );
82
83
84
        $event = new GetResponseUserEvent($person);
85
        $subscriber->onProfileEditInitialize($event);
86
87
        $subscriber->onProfileDocEditSuccess(new FormEvent($form, $this->getRequest()));
88
    }
89
90
    public function testEditSuccessNoVoterRegistration()
91
    {
92
        $personMeuRS = new PersonMeuRS();
93
        $person = new Person();
94
        $person->personMeuRS = $personMeuRS;
95
96
        $form = $this->getForm($person);
97
98
        $meuRSHelper = $this->getMeuRSHelper();
99
        $meuRSHelper->expects($this->once())->method('getPersonMeuRS')->with($person)->willReturn($personMeuRS);
100
101
        $subscriber = new ProfileEditSubscriber(
102
            $this->getEntityManager(),
103
            $meuRSHelper,
104
            $this->getNfgService(),
105
            $this->getTokenStorage($person),
106
            $this->getTranslator()
107
        );
108
109
110
        $event = new GetResponseUserEvent($person);
111
        $subscriber->onProfileEditInitialize($event);
112
113
        $subscriber->onProfileDocEditSuccess(new FormEvent($form, $this->getRequest()));
114
    }
115
116
    public function testEditSuccessWithNfgNoConflict()
117
    {
118
        $voterRegistration = '123456789012';
119
        $personMeuRS = new PersonMeuRS();
120
        $personMeuRS->setVoterRegistration(strrev($voterRegistration));
121
        $personMeuRS->setNfgAccessToken('access_token');
122
        $person = new Person();
123
        $person->personMeuRS = $personMeuRS;
124
125
        $form = $this->getForm($person);
126
127
        $meuRSHelper = $this->getMeuRSHelper();
128
        $meuRSHelper->expects($this->once())->method('getVoterRegistration')
129
            ->with($person)->willReturn($voterRegistration);
130
        $meuRSHelper->expects($this->once())->method('getPersonMeuRS')->with($person)->willReturn($personMeuRS);
131
132
        $nfgProfile = new NfgProfile();
133
        $nfgProfile->setVoterRegistrationSit(1);
134
        $nfg = $this->getNfgService();
135
        $nfg->expects($this->once())->method('getUserInfo')->willReturn($nfgProfile);
136
137
        $subscriber = new ProfileEditSubscriber(
138
            $this->getEntityManager(),
139
            $meuRSHelper,
140
            $nfg,
141
            $this->getTokenStorage($person),
142
            $this->getTranslator()
143
        );
144
145
        $event = new GetResponseUserEvent($person);
146
        $subscriber->onProfileEditInitialize($event);
147
148
        $subscriber->onProfileDocEditSuccess(new FormEvent($form, $this->getRequest()));
149
    }
150
151
    public function testEditSuccessWithNfgNoConflictAndInvalidVoterRegistration()
152
    {
153
        $voterRegistration = '123456789012';
154
        $personMeuRS = new PersonMeuRS();
155
        $personMeuRS->setVoterRegistration(strrev($voterRegistration));
156
        $personMeuRS->setNfgAccessToken('access_token');
157
        $person = new Person();
158
        $person->personMeuRS = $personMeuRS;
159
160
        $form = $this->prepareFormError($this->getForm($person), $personMeuRS, $voterRegistration);
161
162
        $meuRSHelper = $this->getMeuRSHelper();
163
        $meuRSHelper->expects($this->once())->method('getVoterRegistration')
164
            ->with($person)->willReturn($voterRegistration);
165
        $meuRSHelper->expects($this->once())->method('getPersonMeuRS')->with($person)->willReturn($personMeuRS);
166
167
        $nfgProfile = new NfgProfile();
168
        $nfgProfile->setVoterRegistrationSit(0);
169
        $nfg = $this->getNfgService();
170
        $nfg->expects($this->once())->method('getUserInfo')->willReturn($nfgProfile);
171
172
        $subscriber = new ProfileEditSubscriber(
173
            $this->getEntityManager(),
174
            $meuRSHelper,
175
            $nfg,
176
            $this->getTokenStorage($person),
177
            $this->getTranslator()
178
        );
179
180
        $event = new GetResponseUserEvent($person);
181
        $subscriber->onProfileEditInitialize($event);
182
183
        $subscriber->onProfileDocEditSuccess(new FormEvent($form, $this->getRequest()));
184
    }
185
186
    public function testEditSuccessWithNfgDownNoConflict()
187
    {
188
        $voterRegistration = '123456789012';
189
        $personMeuRS = new PersonMeuRS();
190
        $personMeuRS->setVoterRegistration(strrev($voterRegistration));
191
        $personMeuRS->setNfgAccessToken('access_token');
192
        $person = new Person();
193
        $person->personMeuRS = $personMeuRS;
194
195
        $form = $this->getForm($person);
196
197
        $meuRSHelper = $this->getMeuRSHelper();
198
        $meuRSHelper->expects($this->once())->method('getVoterRegistration')
199
            ->with($person)->willReturn($voterRegistration);
200
        $meuRSHelper->expects($this->once())->method('getPersonMeuRS')->with($person)->willReturn($personMeuRS);
201
202
        $nfg = $this->getNfgService();
203
        $nfg->expects($this->once())->method('getUserInfo')->willThrowException(new NfgServiceUnavailableException());
204
205
        $subscriber = new ProfileEditSubscriber(
206
            $this->getEntityManager(),
207
            $meuRSHelper,
208
            $nfg,
209
            $this->getTokenStorage($person),
210
            $this->getTranslator()
211
        );
212
        $logger = $this->createMock('Psr\Log\LoggerInterface');
213
        $logger->expects($this->once())->method('notice');
214
        $subscriber->setLogger($logger);
215
216
        $event = new GetResponseUserEvent($person);
217
        $subscriber->onProfileEditInitialize($event);
218
219
        $subscriber->onProfileDocEditSuccess(new FormEvent($form, $this->getRequest()));
220
    }
221
222
    public function testEditSuccessWithNfgAndConflict()
223
    {
224
        $voterRegistration = '123456789012';
225
        $personMeuRS = new PersonMeuRS();
226
        $personMeuRS->setVoterRegistration(strrev($voterRegistration));
227
        $personMeuRS->setNfgAccessToken('access_token');
228
        $person = new Person();
229
        $person->personMeuRS = $personMeuRS;
230
231
        $otherPersonMeuRS = new PersonMeuRS();
232
        $otherPersonMeuRS->setVoterRegistration('0000');
233
234
        $form = $this->getForm($person);
235
236
        $meuRSHelper = $this->getMeuRSHelper();
237
        $meuRSHelper->expects($this->once())->method('getVoterRegistration')
238
            ->with($person)->willReturn($voterRegistration);
239
        $meuRSHelper->expects($this->once())->method('getPersonMeuRS')->with($person)->willReturn($personMeuRS);
240
        $meuRSHelper->expects($this->once())
241
            ->method('findPersonMeuRSByVoterRegistration')->willReturn($otherPersonMeuRS);
242
243
        $nfgProfile = new NfgProfile();
244
        $nfgProfile->setVoterRegistrationSit(1);
245
        $nfg = $this->getNfgService();
246
        $nfg->expects($this->once())->method('getUserInfo')->willReturn($nfgProfile);
247
248
        $subscriber = new ProfileEditSubscriber(
249
            $this->getEntityManager(),
250
            $meuRSHelper,
251
            $nfg,
252
            $this->getTokenStorage($person),
253
            $this->getTranslator()
254
        );
255
256
        $event = new GetResponseUserEvent($person);
257
        $subscriber->onProfileEditInitialize($event);
258
259
        $subscriber->onProfileDocEditSuccess(new FormEvent($form, $this->getRequest()));
260
261
        $this->assertNull($otherPersonMeuRS->getVoterRegistration());
262
    }
263
264
    public function testEditSuccessWithNfgDownAndConflict()
265
    {
266
        $voterRegistration = '123456789012';
267
        $personMeuRS = new PersonMeuRS();
268
        $personMeuRS->setVoterRegistration(strrev($voterRegistration));
269
        $personMeuRS->setNfgAccessToken('access_token');
270
        $person = new Person();
271
        $person->personMeuRS = $personMeuRS;
272
273
        $otherPersonMeuRS = new PersonMeuRS();
274
        $otherPersonMeuRS->setVoterRegistration('0000');
275
276
        $form = $this->prepareFormError($this->getForm($person), $personMeuRS, $voterRegistration);
277
278
        $meuRSHelper = $this->getMeuRSHelper();
279
        $meuRSHelper->expects($this->once())->method('getVoterRegistration')
280
            ->with($person)->willReturn($voterRegistration);
281
        $meuRSHelper->expects($this->once())->method('getPersonMeuRS')->with($person)->willReturn($personMeuRS);
282
        $meuRSHelper->expects($this->once())
283
            ->method('findPersonMeuRSByVoterRegistration')->willReturn($otherPersonMeuRS);
284
285
        $nfgProfile = new NfgProfile();
286
        $nfgProfile->setVoterRegistrationSit(1);
287
        $nfg = $this->getNfgService();
288
        $nfg->expects($this->once())->method('getUserInfo')->willThrowException(new NfgServiceUnavailableException());
289
290
        $subscriber = new ProfileEditSubscriber(
291
            $this->getEntityManager(),
292
            $meuRSHelper,
293
            $nfg,
294
            $this->getTokenStorage($person),
295
            $this->getTranslator()
296
        );
297
298
        $event = new GetResponseUserEvent($person);
299
        $subscriber->onProfileEditInitialize($event);
300
301
        $subscriber->onProfileDocEditSuccess(new FormEvent($form, $this->getRequest()));
302
    }
303
304
    /**
305
     * @return MockObject|EntityManagerInterface
306
     */
307
    private function getEntityManager()
308
    {
309
        return $this->createMock(EntityManagerInterface::class);
310
    }
311
312
    /**
313
     * @return MockObject|MeuRSHelper
314
     */
315
    private function getMeuRSHelper()
316
    {
317
        return $this->getMockBuilder(MeuRSHelper::class)->disableOriginalConstructor()->getMock();
318
    }
319
320
    /**
321
     * @return MockObject|Nfg
322
     */
323
    private function getNfgService()
324
    {
325
        return $this->getMockBuilder(Nfg::class)->disableOriginalConstructor()->getMock();
326
    }
327
328
    /**
329
     * @param null|UserInterface $currentUser
330
     * @return MockObject|TokenStorageInterface
331
     */
332
    private function getTokenStorage($currentUser = null)
333
    {
334
        $tokenStorage = $this->createMock(TokenStorageInterface::class);
335
336
        if ($currentUser) {
337
            $token = $this->createMock(TokenInterface::class);
338
            $token->expects($this->atLeastOnce())->method('getUser')->willReturn($currentUser);
339
            $tokenStorage->expects($this->atLeastOnce())->method('getToken')->willReturn($token);
340
        }
341
342
        return $tokenStorage;
343
    }
344
345
    /**
346
     * @return MockObject|TranslatorInterface
347
     */
348
    private function getTranslator()
349
    {
350
        return $this->createMock(TranslatorInterface::class);
351
    }
352
353
    /**
354
     * @return Request|MockObject
355
     */
356
    private function getRequest()
357
    {
358
        return $this->createMock(Request::class);
359
    }
360
361
    /**
362
     * @param null $data
363
     * @return MockObject|FormInterface
364
     */
365
    private function getForm($data = null)
366
    {
367
        $form = $this->createMock('Symfony\Component\Form\FormInterface');
368
        if ($data) {
369
            $form->expects($this->any())->method('getData')->willReturn($data);
370
        }
371
372
        return $form;
373
    }
374
375
    /**
376
     * @param FormInterface|MockObject $form
377
     * @param $personMeuRS
378
     * @param $voterRegistration
379
     * @return mixed
380
     */
381
    private function prepareFormError($form, $personMeuRS, $voterRegistration)
382
    {
383
        $voterRegForm = $this->getForm($voterRegistration);
384
        $voterRegForm->expects($this->once())->method('addError');
385
        $personMeuRSForm = $this->getForm($personMeuRS);
386
        $personMeuRSForm->expects($this->once())->method('get')->with('voterRegistration')->willReturn($voterRegForm);
387
        $form->expects($this->once())->method('get')->with('personMeuRS')->willReturn($personMeuRSForm);
388
389
        return $form;
390
    }
391
}
392