Failed Conditions
Push — issue#785 ( 0adb82...cc35f8 )
by Guilherme
04:03
created

ProfileEditSubscriberTest::testOnProfileEditSuccess()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 24
nc 1
nop 0
dl 0
loc 35
rs 8.8571
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 FOS\UserBundle\FOSUserEvents;
14
use libphonenumber\PhoneNumberUtil;
15
use LoginCidadao\PhoneVerificationBundle\Event\ProfileEditSubscriber;
16
use PHPUnit\Framework\TestCase;
17
18
class ProfileEditSubscriberTest extends TestCase
19
{
20
    public function testGetSubscribedEvents()
21
    {
22
        $this->assertArrayHasKey(
23
            FOSUserEvents::PROFILE_EDIT_INITIALIZE,
24
            ProfileEditSubscriber::getSubscribedEvents()
25
        );
26
        $this->assertArrayHasKey(
27
            FOSUserEvents::PROFILE_EDIT_SUCCESS,
28
            ProfileEditSubscriber::getSubscribedEvents()
29
        );
30
    }
31
32
    public function testOnProfileEditInitialize()
33
    {
34
        $person = $this->createMock('LoginCidadao\CoreBundle\Model\PersonInterface');
35
        $person->expects($this->once())->method('getMobile');
36
37
        $event = $this->getMockBuilder('FOS\UserBundle\Event\GetResponseUserEvent')
38
            ->disableOriginalConstructor()
39
            ->getMock();
40
        $event->expects($this->once())->method('getUser')->willReturn($person);
41
42
        $subscriber = new ProfileEditSubscriber();
43
        $subscriber->onProfileEditInitialize($event);
44
    }
45
46
    public function testSkipOnProfileEditInitialize()
47
    {
48
        $event = $this->getMockBuilder('FOS\UserBundle\Event\GetResponseUserEvent')
49
            ->disableOriginalConstructor()
50
            ->getMock();
51
        $event->expects($this->once())->method('getUser')->willReturn(null);
52
53
        $subscriber = new ProfileEditSubscriber();
54
        $subscriber->onProfileEditInitialize($event);
55
    }
56
57
    public function testOnProfileEditSuccess()
58
    {
59
        $person = $this->createMock('LoginCidadao\CoreBundle\Model\PersonInterface');
60
        $person->expects($this->any())->method('getMobile')
61
            ->willReturn(PhoneNumberUtil::getInstance()->parse('+5551999999999', 'BR'));
62
63
        $form = $this->getMockBuilder('Symfony\Component\Form\FormInterface')
64
            ->disableOriginalConstructor()
65
            ->getMock();
66
        $form->expects($this->once())->method('getData')->willReturn($person);
67
68
        $event = $this->getMockBuilder('FOS\UserBundle\Event\FormEvent')
69
            ->disableOriginalConstructor()
70
            ->getMock();
71
        $event->expects($this->once())->method('getForm')->willReturn($form);
72
73
        $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')
74
            ->disableOriginalConstructor()
75
            ->getMock();
76
77
        $subscriber = new ProfileEditSubscriber();
78
79
        // Prepare oldPhone
80
        $person2 = $this->createMock('LoginCidadao\CoreBundle\Model\PersonInterface');
81
        $person2->expects($this->any())->method('getMobile')
82
            ->willReturn(PhoneNumberUtil::getInstance()->parse('+5551999998888', 'BR'));
83
84
        $event1 = $this->getMockBuilder('FOS\UserBundle\Event\GetResponseUserEvent')
85
            ->disableOriginalConstructor()
86
            ->getMock();
87
        $event1->expects($this->once())->method('getUser')->willReturn($person2);
88
        $subscriber->onProfileEditInitialize($event1);
89
        // ----------------
90
91
        $subscriber->onProfileEditSuccess($event, FOSUserEvents::PROFILE_EDIT_SUCCESS, $dispatcher);
92
    }
93
94
    public function testSkipOnProfileEditSuccess()
95
    {
96
        $form = $this->getMockBuilder('Symfony\Component\Form\FormInterface')
97
            ->disableOriginalConstructor()
98
            ->getMock();
99
        $form->expects($this->once())->method('getData')->willReturn(null);
100
101
        $event = $this->getMockBuilder('FOS\UserBundle\Event\FormEvent')
102
            ->disableOriginalConstructor()
103
            ->getMock();
104
        $event->expects($this->once())->method('getForm')->willReturn($form);
105
106
        $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')
107
            ->disableOriginalConstructor()
108
            ->getMock();
109
110
        $subscriber = new ProfileEditSubscriber();
111
        $subscriber->onProfileEditSuccess($event, FOSUserEvents::PROFILE_EDIT_SUCCESS, $dispatcher);
112
    }
113
}
114