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

TaskSubscriberTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 144
rs 10
c 0
b 0
f 0
wmc 13
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\PhoneNumberType;
14
use LoginCidadao\PhoneVerificationBundle\Event\TaskSubscriber;
15
use LoginCidadao\PhoneVerificationBundle\Exception\VerificationNotSentException;
16
use LoginCidadao\TaskStackBundle\TaskStackEvents;
17
use PHPUnit\Framework\TestCase;
18
19
class TaskSubscriberTest extends TestCase
20
{
21
    private function getUser($userClass = 'LoginCidadao\CoreBundle\Model\PersonInterface')
22
    {
23
        return $this->createMock($userClass);
24
    }
25
26
    private function getTokenStorage($shouldBeUsed = true, $user = null)
27
    {
28
        $tokenStorage = $this->createMock(
29
            'Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface'
30
        );
31
        if ($shouldBeUsed) {
32
            $user = $user ?: $this->getUser();
33
34
            $token = $this->createMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
35
            $token->expects($this->atLeastOnce())->method('getUser')->willReturn($user);
36
37
            $tokenStorage->expects($this->atLeastOnce())->method('getToken')->willReturn($token);
38
        }
39
40
        return $tokenStorage;
41
    }
42
43
    private function getPhoneVerificationService()
44
    {
45
        $class = 'LoginCidadao\PhoneVerificationBundle\Service\PhoneVerificationServiceInterface';
46
        $phoneVerificationService = $this->createMock($class);
47
48
        return $phoneVerificationService;
49
    }
50
51
    public function testGetSubscribedEvents()
52
    {
53
        $this->assertEquals([TaskStackEvents::GET_TASKS => ['onGetTasks', 100]], TaskSubscriber::getSubscribedEvents());
54
    }
55
56
    public function testVerificationDisabled()
57
    {
58
        $tokenStorage = $this->getTokenStorage(false);
59
        $phoneVerificationService = $this->getPhoneVerificationService();
60
        $phoneVerificationService->expects($this->never())->method('getAllPendingPhoneVerification');
61
62
        $event = $this->getMockBuilder('LoginCidadao\TaskStackBundle\Event\GetTasksEvent')
63
            ->disableOriginalConstructor()->getMock();
64
65
        $subscriber = new TaskSubscriber($tokenStorage, $phoneVerificationService, false);
66
        $subscriber->onGetTasks($event);
67
    }
68
69
    public function testOnGetTasks()
70
    {
71
        $phoneVerification = $this->createMock('LoginCidadao\PhoneVerificationBundle\Model\PhoneVerificationInterface');
72
        $tokenStorage = $this->getTokenStorage();
73
74
        $phoneVerificationService = $this->getPhoneVerificationService();
75
        $phoneVerificationService->expects($this->once())->method('getAllPendingPhoneVerification')
76
            ->willReturn([$phoneVerification]);
77
78
        $event = $this->getMockBuilder('LoginCidadao\TaskStackBundle\Event\GetTasksEvent')
79
            ->disableOriginalConstructor()->getMock();
80
        $event->expects($this->once())->method('addTaskIfStackEmpty');
81
82
        $subscriber = new TaskSubscriber($tokenStorage, $phoneVerificationService, true);
83
        $subscriber->onGetTasks($event);
84
    }
85
86
    public function testOnGetTasksOAuthToken()
87
    {
88
        $token = $this->createMock('FOS\OAuthServerBundle\Security\Authentication\Token\OAuthToken');
89
        $tokenStorage = $this->getTokenStorage(false);
90
        $tokenStorage->expects($this->once())->method('getToken')->willReturn($token);
91
92
        $phoneVerificationService = $this->getPhoneVerificationService();
93
        $event = $this->getMockBuilder('LoginCidadao\TaskStackBundle\Event\GetTasksEvent')
94
            ->disableOriginalConstructor()->getMock();
95
96
        $subscriber = new TaskSubscriber($tokenStorage, $phoneVerificationService, true);
97
        $subscriber->onGetTasks($event);
98
    }
99
100
    public function testOnGetTasksOrphanVerification()
101
    {
102
        $user = $this->getUser();
103
        $user->expects($this->once())->method('getMobile')->willReturn(null);
104
105
        $phoneVerification = $this->createMock('LoginCidadao\PhoneVerificationBundle\Model\PhoneVerificationInterface');
106
        $phoneVerification->expects($this->once())->method('getPhone')->willReturn(new PhoneNumberType());
107
        $tokenStorage = $this->getTokenStorage(true, $user);
108
109
        $phoneVerificationService = $this->getPhoneVerificationService();
110
        $phoneVerificationService->expects($this->once())->method('getAllPendingPhoneVerification')
111
            ->willReturn([$phoneVerification]);
112
113
        $event = $this->getMockBuilder('LoginCidadao\TaskStackBundle\Event\GetTasksEvent')
114
            ->disableOriginalConstructor()->getMock();
115
116
        $subscriber = new TaskSubscriber($tokenStorage, $phoneVerificationService, true);
117
        $subscriber->onGetTasks($event);
118
    }
119
120
    public function testOnGetTasksSendFailed()
121
    {
122
        $phoneVerification = $this->createMock('LoginCidadao\PhoneVerificationBundle\Model\PhoneVerificationInterface');
123
        $tokenStorage = $this->getTokenStorage();
124
125
        $phoneVerificationService = $this->getPhoneVerificationService();
126
        $phoneVerificationService->expects($this->once())->method('getAllPendingPhoneVerification')
127
            ->willReturn([$phoneVerification]);
128
        $phoneVerificationService->expects($this->once())->method('sendVerificationCode')->willThrowException(
129
            new VerificationNotSentException()
130
        );
131
132
        $event = $this->getMockBuilder('LoginCidadao\TaskStackBundle\Event\GetTasksEvent')
133
            ->disableOriginalConstructor()->getMock();
134
135
        $subscriber = new TaskSubscriber($tokenStorage, $phoneVerificationService, true);
136
        $subscriber->onGetTasks($event);
137
    }
138
139
    public function testUnsupportedUser()
140
    {
141
        $tokenStorage = $this->getTokenStorage(
142
            true,
143
            $this->getUser('Symfony\Component\Security\Core\User\UserInterface')
144
        );
145
        $phoneVerificationService = $this->getPhoneVerificationService();
146
147
        $event = $this->getMockBuilder('LoginCidadao\TaskStackBundle\Event\GetTasksEvent')
148
            ->disableOriginalConstructor()->getMock();
149
150
        $subscriber = new TaskSubscriber($tokenStorage, $phoneVerificationService, true);
151
        $subscriber->onGetTasks($event);
152
    }
153
154
    public function testNoPendingVerification()
155
    {
156
        $tokenStorage = $this->getTokenStorage();
157
        $phoneVerificationService = $this->getPhoneVerificationService();
158
159
        $event = $this->getMockBuilder('LoginCidadao\TaskStackBundle\Event\GetTasksEvent')
160
            ->disableOriginalConstructor()->getMock();
161
162
        $subscriber = new TaskSubscriber($tokenStorage, $phoneVerificationService, true);
163
        $subscriber->onGetTasks($event);
164
    }
165
166
    public function testOAuthRequest()
167
    {
168
        $tokenStorage = $this->getTokenStorage(false);
169
        $token = $this->createMock('FOS\OAuthServerBundle\Security\Authentication\Token\OAuthToken');
170
        $tokenStorage->expects($this->once())->method('getToken')->willReturn($token);
171
172
        $phoneVerificationService = $this->getPhoneVerificationService();
173
174
        $event = $this->getMockBuilder('LoginCidadao\TaskStackBundle\Event\GetTasksEvent')
175
            ->disableOriginalConstructor()->getMock();
176
        $subscriber = new TaskSubscriber($tokenStorage, $phoneVerificationService, true);
177
        $subscriber->onGetTasks($event);
178
    }
179
}
180