Passed
Push — issue#778 ( 66f8fa...dfad2e )
by Guilherme
05:44
created

LongPollingUtilsTest::testRunTimeLimited()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 17
rs 9.4285
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\CoreBundle\Tests\LongPolling;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use LoginCidadao\CoreBundle\Entity\PersonRepository;
15
use LoginCidadao\CoreBundle\LongPolling\LongPollingUtils;
16
use LoginCidadao\CoreBundle\Model\PersonInterface;
17
use Symfony\Component\Stopwatch\Stopwatch;
18
19
/**
20
 * @group time-sensitive
21
 */
22
class LongPollingUtilsTest extends \PHPUnit_Framework_TestCase
23
{
24
    public function testRunTimeLimited()
25
    {
26
        $timer = 'timeLimited';
27
        $stopwatch = new Stopwatch();
28
        $stopwatch->start($timer);
29
30
        $longPolling = new LongPollingUtils($this->getEntityManager(), 60);
31
32
        $count = 5;
33
        $response = $longPolling->runTimeLimited(function () use (&$count) {
34
            return --$count <= 0;
35
        });
36
        $duration = $stopwatch->stop($timer)->getDuration();
37
38
        $this->assertTrue($response);
39
        $this->assertEquals(0, $count);
40
        $this->assertLessThan(60, $duration);
41
    }
42
43
    public function testTimeout()
44
    {
45
        $this->setExpectedException('LoginCidadao\APIBundle\Exception\RequestTimeoutException');
46
47
        $longPolling = new LongPollingUtils($this->getEntityManager(), 30);
48
        $longPolling->runTimeLimited(function () {
49
            return false;
50
        });
51
    }
52
53
    public function testEntityUpdateCheckerCallback()
54
    {
55
        $updatedAt = new \DateTime('-1 hour');
56
        $currentUpdatedAt = new \DateTime('-1 hour');
57
58
        $person = $this->getPerson();
59
        $person->expects($this->any())->method('getUpdatedAt')
60
            ->willReturnCallback(function () use (&$currentUpdatedAt) {
61
                return $currentUpdatedAt;
62
            });
63
64
        $em = $this->getEntityManager();
65
        $em->expects($this->once())->method('getRepository')
66
            ->willReturn($this->getEvolvingPersonRepo($person, $currentUpdatedAt));
67
68
        $longPolling = new LongPollingUtils($em);
69
        $callback = $longPolling->getEntityUpdateCheckerCallback($person, $updatedAt);
70
71
        call_user_func($callback);
72
        $response = call_user_func($callback);
73
74
        $this->assertNotFalse($response);
75
        $this->assertNotEquals($updatedAt, $currentUpdatedAt);
76
        $this->assertEquals($person, $response);
77
    }
78
79
    public function testEntityUpdateCheckerCallbackNeverUpdated()
80
    {
81
        $person = $this->getPerson();
82
        $person->expects($this->once())->method('getUpdatedAt')->willReturn(null);
83
84
        $repo = $this->getPersonRepository();
85
        $repo->expects($this->any())->method('find')->willReturn($person);
86
87
        $em = $this->getEntityManager();
88
        $em->expects($this->once())->method('getRepository')->willReturn($repo);
89
90
        $longPolling = new LongPollingUtils($em);
91
        $callback = $longPolling->getEntityUpdateCheckerCallback($person, new \DateTime());
92
        $response = call_user_func($callback);
93
94
        $this->assertFalse($response);
95
    }
96
97
    public function testObviouslyValidEmail()
98
    {
99
        $person = $this->getPerson();
100
        $person->expects($this->once())->method('getEmailConfirmedAt')->willReturn(new \DateTime());
101
102
        $longPolling = new LongPollingUtils($this->getEntityManager());
103
        $response = $longPolling->waitValidEmail($person, new \DateTime());
104
105
        $this->assertTrue($response);
106
    }
107
108
    public function testWaitValidEmail()
109
    {
110
        if (version_compare(phpversion(), '7.1', '>=')) {
111
            $this->markTestSkipped("This won't work on PHP 7.1");
112
113
            return;
114
        }
115
116
        $updatedAt = new \DateTime('-1 hour');
117
        $currentUpdatedAt = new \DateTime('-1 hour');
118
119
        $person = $this->getPerson();
120
        $person->expects($this->any())->method('getUpdatedAt')
121
            ->willReturnCallback(function () use (&$currentUpdatedAt) {
122
                return $currentUpdatedAt;
123
            });
124
125
        $repo = $this->getEvolvingPersonRepo($person, $currentUpdatedAt);
126
127
        $em = $this->getEntityManager();
128
        $em->expects($this->once())->method('getRepository')->willReturn($repo);
129
130
        $longPolling = new LongPollingUtils($em);
131
        $response = $longPolling->waitValidEmail($person, $updatedAt);
132
133
        $this->assertTrue($response);
134
    }
135
136
    /**
137
     * @return \PHPUnit_Framework_MockObject_MockObject|EntityManagerInterface
138
     */
139
    private function getEntityManager()
140
    {
141
        $em = $this->getMock('Doctrine\ORM\EntityManagerInterface');
142
143
        return $em;
144
    }
145
146
    /**
147
     * @return \PHPUnit_Framework_MockObject_MockObject|PersonRepository
148
     */
149
    private function getPersonRepository()
150
    {
151
        $repo = $this->getMockBuilder('LoginCidadao\CoreBundle\Entity\PersonRepository')
152
            ->disableOriginalConstructor()->getMock();
153
154
        return $repo;
155
    }
156
157
    /**
158
     * @return PersonInterface|\PHPUnit_Framework_MockObject_MockObject
159
     */
160
    private function getPerson()
161
    {
162
        /** @var \PHPUnit_Framework_MockObject_MockObject|PersonInterface $person */
163
        $person = $this->getMock('LoginCidadao\CoreBundle\Model\PersonInterface');
164
        $person->expects($this->any())->method('getId')->willReturn(123);
165
166
        return $person;
167
    }
168
169
    /**
170
     * @param PersonInterface|\PHPUnit_Framework_MockObject_MockObject $person
171
     * @param \DateTime|null $currentUpdatedAt
172
     * @return PersonRepository|\PHPUnit_Framework_MockObject_MockObject
173
     */
174
    private function getEvolvingPersonRepo(&$person, &$currentUpdatedAt)
175
    {
176
        $repo = $this->getPersonRepository();
177
        $repo->expects($this->at(0))->method('find')->willReturn($person);
178
        $repo->expects($this->at(1))->method('find')
179
            ->willReturnCallback(function () use (&$person, &$currentUpdatedAt) {
180
                $currentUpdatedAt = new \DateTime();
181
                $person->expects($this->any())->method('getEmailConfirmedAt')->willReturn($currentUpdatedAt);
182
183
                return $person;
184
            });
185
186
        return $repo;
187
    }
188
}
189