Failed Conditions
Pull Request — master (#790)
by Guilherme
10:36 queued 05:09
created

UserRegistrationSubscriberTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 6
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 FOS\UserBundle\FOSUserEvents;
14
use LoginCidadao\PhoneVerificationBundle\Event\UserRegistrationSubscriber;
15
use PHPUnit\Framework\TestCase;
16
17
class UserRegistrationSubscriberTest extends TestCase
18
{
19
    public function testGetSubscribedEvents()
20
    {
21
        $this->assertArrayHasKey(
22
            FOSUserEvents::REGISTRATION_COMPLETED,
23
            UserRegistrationSubscriber::getSubscribedEvents()
24
        );
25
    }
26
27
    public function testOnRegistrationCompletedWithPhone()
28
    {
29
        $phone = $this->createMock('libphonenumber\PhoneNumber');
30
        $user = $this->createMock('LoginCidadao\CoreBundle\Model\PersonInterface');
31
        $user->expects($this->exactly(2))->method('getMobile')->willReturn($phone);
32
33
        $verificationService = $this->createMock(
34
            'LoginCidadao\PhoneVerificationBundle\Service\PhoneVerificationServiceInterface'
35
        );
36
        $verificationService->expects($this->once())
37
            ->method('enforcePhoneVerification')->with($user, $phone)
38
            ->willReturn($this->getPhoneVerification());
39
40
        $event = $this->getMockBuilder('FOS\UserBundle\Event\FilterUserResponseEvent')
41
            ->disableOriginalConstructor()
42
            ->getMock();
43
        $event->expects($this->once())->method('getUser')->willReturn($user);
44
45
        $subscriber = new UserRegistrationSubscriber($verificationService, $this->getStackManager(true));
46
        $subscriber->onRegistrationCompleted($event);
47
    }
48
49
    public function testOnRegistrationCompletedWithoutPhone()
50
    {
51
        $user = $this->createMock('LoginCidadao\CoreBundle\Model\PersonInterface');
52
        $user->expects($this->once())->method('getMobile')->willReturn(null);
53
54
        $verificationService = $this->createMock(
55
            'LoginCidadao\PhoneVerificationBundle\Service\PhoneVerificationServiceInterface'
56
        );
57
58
        $event = $this->getMockBuilder('FOS\UserBundle\Event\FilterUserResponseEvent')
59
            ->disableOriginalConstructor()
60
            ->getMock();
61
        $event->expects($this->once())->method('getUser')->willReturn($user);
62
63
        $subscriber = new UserRegistrationSubscriber($verificationService, $this->getStackManager(false));
64
        $subscriber->onRegistrationCompleted($event);
65
    }
66
67
    private function getStackManager($setTaskSkipped = false)
68
    {
69
        $taskStackManager = $this->createMock('LoginCidadao\TaskStackBundle\Service\TaskStackManagerInterface');
70
71
        if ($setTaskSkipped) {
72
            $taskStackManager->expects($this->once())->method('setTaskSkipped')
73
                ->with($this->isInstanceOf('LoginCidadao\TaskStackBundle\Model\TaskInterface'));
74
        }
75
76
        return $taskStackManager;
77
    }
78
79
    private function getPhoneVerification()
80
    {
81
        $phoneVerification = $this->createMock('LoginCidadao\PhoneVerificationBundle\Model\PhoneVerificationInterface');
82
        $phoneVerification->expects($this->once())->method('getId')->willReturn('some_task');
83
84
        return $phoneVerification;
85
    }
86
}
87