Passed
Push — issue#702 ( 0b5bf0...f20c81 )
by Guilherme
06:32
created

SmsStatusServiceTest::testGetSmsStatus()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 26
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\Service;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use LoginCidadao\PhoneVerificationBundle\Entity\SentVerification;
15
use LoginCidadao\PhoneVerificationBundle\Entity\SentVerificationRepository;
16
use LoginCidadao\PhoneVerificationBundle\Event\UpdateStatusEvent;
17
use LoginCidadao\PhoneVerificationBundle\Service\SmsStatusService;
18
use LoginCidadao\PhoneVerificationBundle\Model\SmsStatusInterface;
19
use Symfony\Component\Console\Style\SymfonyStyle;
20
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
21
22
class SmsStatusServiceTest extends \PHPUnit_Framework_TestCase
23
{
24
    public function testNoPendingUpdate()
25
    {
26
        $repo = $this->getRepository([]);
27
        $updater = $this->getSmsStatusService($this->getEntityManager(), $this->getDispatcher(), $repo, $this->getIo());
28
        $updater->updateSentVerificationStatus();
29
    }
30
31
    public function testOnePendingUpdate()
32
    {
33
        $date = new \DateTime();
34
35
        /** @var SmsStatusInterface|\PHPUnit_Framework_MockObject_MockObject $status */
36
        $status = $this->getMock('LoginCidadao\PhoneVerificationBundle\Model\SmsStatusInterface');
37
        $status->expects($this->once())->method('getDateSent')->willReturn($date);
38
        $status->expects($this->once())->method('getDateDelivered')->willReturn($date);
39
40
        $dispatcher = $this->getDispatcher();
41
        $dispatcher->expects($this->atMost(1))
42
            ->method('dispatch')
43
            ->willReturnCallback(
44
                function ($eventName, UpdateStatusEvent $event) use ($status) {
45
                    $event->setDeliveryStatus($status);
46
                }
47
            );
48
49
        $sentVerification = new SentVerification();
50
        $sentVerification->setTransactionId('0123456');
51
52
        $items = [
53
            [
54
                $sentVerification,
55
            ],
56
        ];
57
58
        $repo = $this->getRepository($items);
59
        $updater = $this->getSmsStatusService($this->getEntityManager(), $dispatcher, $repo, $this->getIo());
60
        $updater->updateSentVerificationStatus();
61
62
        $this->assertEquals($date, $sentVerification->getActuallySentAt());
63
        $this->assertEquals($date, $sentVerification->getDeliveredAt());
64
        $this->assertTrue($sentVerification->isFinished());
65
    }
66
67
    public function testOnePendingUpdateNoListener()
68
    {
69
        $dispatcher = $this->getDispatcher();
70
71
        $sentVerification = new SentVerification();
72
        $sentVerification->setTransactionId('0123456');
73
74
        $items = [
75
            [
76
                $sentVerification,
77
            ],
78
        ];
79
80
        $repo = $this->getRepository($items);
81
        $updater = $this->getSmsStatusService($this->getEntityManager(), $dispatcher, $repo, $this->getIo());
82
        $updater->updateSentVerificationStatus();
83
84
        $this->assertFalse($sentVerification->isFinished());
85
    }
86
87
    public function testOnePendingUpdateNoListenerNoIo()
88
    {
89
        $dispatcher = $this->getDispatcher();
90
91
        $sentVerification = new SentVerification();
92
        $sentVerification->setTransactionId('0123456');
93
94
        $items = [
95
            [
96
                $sentVerification,
97
            ],
98
        ];
99
100
        $repo = $this->getRepository($items);
101
        $updater = $this->getSmsStatusService($this->getEntityManager(), $dispatcher, $repo);
102
        $updater->updateSentVerificationStatus();
103
104
        $this->assertFalse($sentVerification->isFinished());
105
    }
106
107
    public function testAverageTime()
108
    {
109
        $sentVerification1 = new SentVerification();
110
        $sentVerification1->setSentAt(new \DateTime())
111
            ->setDeliveredAt(new \DateTime('+30 seconds'))
112
            ->setActuallySentAt(new \DateTime('+5 seconds'));
113
114
        $sentVerification2 = new SentVerification();
115
        $sentVerification2->setSentAt(new \DateTime())
116
            ->setDeliveredAt(new \DateTime('+30 seconds'))
117
            ->setActuallySentAt(new \DateTime('+5 seconds'));
118
119
        $verifications = [
120
            $sentVerification1,
121
            $sentVerification2,
122
        ];
123
124
        $repo = $this->getRepository();
125
        $repo->expects($this->once())->method('getLastDeliveredVerifications')
126
            ->willReturn($verifications);
127
128
        $updater = $this->getSmsStatusService($this->getEntityManager(), $this->getDispatcher(), $repo, $this->getIo());
129
        $avg = $updater->getAverageDeliveryTime(2);
130
131
        $this->assertEquals(30, $avg);
132
    }
133
134
    public function testNoAverageTime()
135
    {
136
        $repo = $this->getRepository();
137
        $repo->expects($this->once())->method('getLastDeliveredVerifications')
138
            ->willReturn([]);
139
140
        $updater = $this->getSmsStatusService($this->getEntityManager(), $this->getDispatcher(), $repo, $this->getIo());
141
        $avg = $updater->getAverageDeliveryTime(2);
142
143
        $this->assertEquals(0, $avg);
144
    }
145
146
    public function testGetDelayedDeliveryTransactions()
147
    {
148
        $sentVerification = new SentVerification();
149
        $sentVerification->setSentAt(new \DateTime())
150
            ->setTransactionId('0123456');
151
152
        $repo = $this->getRepository();
153
        $repo->expects($this->once())->method('getNotDeliveredSince')->willReturn([$sentVerification]);
154
155
        $updater = $this->getSmsStatusService($this->getEntityManager(), $this->getDispatcher(), $repo, $this->getIo());
156
        $notDelivered = $updater->getDelayedDeliveryTransactions(0);
157
158
        $transaction = reset($notDelivered);
159
160
        $this->assertEquals($sentVerification->getTransactionId(), $transaction['transaction_id']);
161
        $this->assertEquals($sentVerification->getSentAt()->format('c'), $transaction['sent_at']);
162
    }
163
164
    public function testGetSmsStatus()
165
    {
166
        $id = '1234';
167
        $date = new \DateTime();
168
169
        /** @var SmsStatusInterface|\PHPUnit_Framework_MockObject_MockObject $status */
170
        $status = $this->getMock('LoginCidadao\PhoneVerificationBundle\Model\SmsStatusInterface');
171
        $status->expects($this->once())->method('getDateSent')->willReturn($date);
172
        $status->expects($this->once())->method('getDateDelivered')->willReturn($date);
173
174
        $dispatcher = $this->getDispatcher();
175
        $dispatcher->expects($this->atMost(1))
176
            ->method('dispatch')
177
            ->willReturnCallback(
178
                function ($eventName, UpdateStatusEvent $event) use ($status) {
179
                    $event->setDeliveryStatus($status);
180
                }
181
            );
182
183
        $updater = $this->getSmsStatusService($this->getEntityManager(), $dispatcher, $this->getRepository(),
184
            $this->getIo());
185
        $response = $updater->getSmsStatus($id);
186
187
        $this->assertSame($status, $response);
188
        $this->assertSame($date, $response->getDateSent());
189
        $this->assertSame($date, $response->getDateDelivered());
190
    }
191
192
    private function getSmsStatusService(
193
        EntityManagerInterface $em,
194
        EventDispatcherInterface $dispatcher,
195
        SentVerificationRepository $repo,
196
        SymfonyStyle $io = null
197
    ) {
198
        $updater = new SmsStatusService($em, $dispatcher, $repo);
199
        if ($io !== null) {
200
            $updater->setSymfonyStyle($io);
201
        }
202
203
        return $updater;
204
    }
205
206
    /**
207
     * @return \PHPUnit_Framework_MockObject_MockObject|EventDispatcherInterface
208
     */
209
    private function getDispatcher()
210
    {
211
        $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
212
213
        return $dispatcher;
214
    }
215
216
    private function getQuery($items = [])
217
    {
218
        $query = $this->getMockBuilder('\Doctrine\ORM\AbstractQuery')
219
            ->disableOriginalConstructor()
220
            ->getMock();
221
        $query->expects($this->atMost(1))->method('iterate')
222
            ->willReturn(new \ArrayIterator($items));
223
224
        return $query;
225
    }
226
227
    /**
228
     * @param null $items
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $items is correct as it would always require null to be passed?
Loading history...
229
     * @return \PHPUnit_Framework_MockObject_MockObject|SentVerificationRepository
230
     */
231
    private function getRepository($items = null)
232
    {
233
        $repo = $this->getMockBuilder('LoginCidadao\PhoneVerificationBundle\Entity\SentVerificationRepository')
234
            ->disableOriginalConstructor()
235
            ->getMock();
236
237
        if ($items !== null) {
238
            $repo->expects($this->atMost(1))
239
                ->method('getPendingUpdateSentVerificationQuery')
240
                ->willReturn($this->getQuery($items));
241
242
            $repo->expects($this->atMost(1))
243
                ->method('countPendingUpdateSentVerification')
244
                ->willReturn(count($items));
245
        }
246
247
        return $repo;
248
    }
249
250
    /**
251
     * @return \PHPUnit_Framework_MockObject_MockObject|EntityManagerInterface
252
     */
253
    private function getEntityManager()
254
    {
255
        $em = $this->getMock('Doctrine\ORM\EntityManagerInterface');
256
257
        return $em;
258
    }
259
260
    /**
261
     * @return \PHPUnit_Framework_MockObject_MockObject|SymfonyStyle
262
     */
263
    private function getIo()
264
    {
265
        $io = $this->getMockBuilder('Symfony\Component\Console\Style\SymfonyStyle')
266
            ->disableOriginalConstructor()
267
            ->getMock();
268
269
        return $io;
270
    }
271
}
272