Completed
Push — master ( 298e09...565401 )
by Ivannis Suárez
02:36
created

LoopTests::testTick()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the Cubiche/Async component.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Cubiche\Core\Async\Tests\Units\Loop;
12
13
use Cubiche\Core\Async\Promise\PromiseInterface;
14
use Cubiche\Core\Async\Promise\State;
15
use mageekguy\atoum\mock\stream as Stream;
16
use React\EventLoop\LoopInterface as BaseLoopInterface;
17
use Cubiche\Core\Async\Loop\Timer\TimerInterface;
18
19
/**
20
 * Loop Tests class.
21
 *
22
 * @author Karel Osorio Ramírez <[email protected]>
23
 */
24
class LoopTests extends LoopInterfaceTestCase
25
{
26
    /**
27
     * Test addReadStream method.
28
     */
29
    public function testAddReadStream()
30
    {
31
        $this->proxyMethodTest('addReadStream', array(Stream::get(), $this->delegateMock()));
32
    }
33
34
    /**
35
     * Test addWriteStream method.
36
     */
37
    public function testAddWriteStream()
38
    {
39
        $this->proxyMethodTest('addWriteStream', array(Stream::get(), $this->delegateMock()));
40
    }
41
42
    /**
43
     * Test removeReadStream method.
44
     */
45
    public function testRemoveReadStream()
46
    {
47
        $this->proxyMethodTest('removeReadStream', array(Stream::get()));
48
    }
49
50
    /**
51
     * Test removeWriteStream method.
52
     */
53
    public function testRemoveWriteStream()
54
    {
55
        $this->proxyMethodTest('removeWriteStream', array(Stream::get()));
56
    }
57
58
    /**
59
     * Test RemoveStream method.
60
     */
61
    public function testRemoveStream()
62
    {
63
        $this->proxyMethodTest('removeStream', array(Stream::get()));
64
    }
65
66
    /**
67
     * Test Timeout method.
68
     */
69
    public function testTimeout()
70
    {
71
        $this
72
            ->given(
73
                /** @var \Cubiche\Core\Async\Loop\LoopInterface $loop */
74
                $loop = $this->newDefaultTestedInstance(),
75
                $task = $this->delegateMockWithReturn('foo')
76
            )
77
            /* @var \Cubiche\Core\Async\Loop\Timer\TimerInterface $timer */
78
            ->when($timer = $loop->timeout($task, 0.001))
79
            ->then()
80
                ->object($timer)
81
                    ->isInstanceOf(TimerInterface::class)
82
                ->boolean($timer->state()->equals(State::PENDING()))
83
                    ->isTrue()
84
                ->float($timer->interval())
85
                    ->isEqualTo(0.001)
86
                ->integer($timer->iterations())
87
                    ->isEqualTo(0)
88
                ->integer($timer->maxIterations())
89
                    ->isEqualTo(1)
90
                ->boolean($timer->isActive())
91
                    ->isTrue()
92
        ;
93
94
        $this
95
            ->given($onFulfilled = $this->delegateMock())
96
            ->when(function () use ($loop, $timer, $onFulfilled) {
97
                $timer->then($onFulfilled);
98
                $loop->tick();
99
            })
100
            ->then()
101
                ->delegateCall($task)
102
                    ->once()
103
                ->delegateCall($onFulfilled)
104
                    ->withArguments('foo')
105
                    ->once()
106
        ;
107
    }
108
109
    /**
110
     * Test Timer method.
111
     */
112
    public function testTimer()
113
    {
114
        $this
115
            ->given(
116
                /** @var \Cubiche\Core\Async\Loop\LoopInterface $loop */
117
                $loop = $this->newDefaultTestedInstance(),
118
                $task = $this->delegateMockWithReturn('foo')
119
            )
120
            /* @var \Cubiche\Core\Async\Loop\Timer\TimerInterface $timer */
121
            ->when($timer = $loop->timer($task, 0.001, 2))
122
            ->then()
123
                ->object($timer)
124
                    ->isInstanceOf(TimerInterface::class)
125
                ->boolean($timer->state()->equals(State::PENDING()))
126
                    ->isTrue()
127
                ->float($timer->interval())
128
                    ->isEqualTo(0.001)
129
                ->integer($timer->iterations())
130
                    ->isEqualTo(0)
131
                ->integer($timer->maxIterations())
132
                    ->isEqualTo(2)
133
                ->boolean($timer->isActive())
134
                    ->isTrue()
135
        ;
136
137
        $this
138
        ->given(
139
            $onRejected = $this->delegateMock(),
140
            $onNotify = $this->delegateMock()
141
        )
142
        ->when(function () use ($loop, $timer, $onRejected, $onNotify) {
143
            $timer->then(null, $onRejected, $onNotify);
144
            $loop->run();
145
        })
146
        ->then()
147
            ->delegateCall($task)
148
                ->twice()
149
            ->delegateCall($onRejected)
150
                ->once()
151
            ->delegateCall($onNotify)
152
                ->withArguments('foo')
153
                ->twice()
154
        ;
155
    }
156
157
    /**
158
     * Test Next method.
159
     */
160
    public function testNext()
161
    {
162
        $this->scheduleTickTest('next');
163
    }
164
165
    /**
166
     * Test Enqueue method.
167
     */
168
    public function testEnqueue()
169
    {
170
        $this->scheduleTickTest('enqueue');
171
    }
172
173
    /**
174
     * Test Tick method.
175
     */
176
    public function testTick()
177
    {
178
        $this->proxyMethodTest('tick');
179
    }
180
181
    /**
182
     * Test Run method.
183
     */
184
    public function testRun()
185
    {
186
        $this->proxyMethodTest('run');
187
    }
188
189
    /**
190
     * Test Stop method.
191
     */
192
    public function testStop()
193
    {
194
        $this->proxyMethodTest('stop');
195
    }
196
197
    /**
198
     * @param string $method
199
     *
200
     * @return string
201
     */
202
    protected function scheduleTickTest($method)
203
    {
204
        $this
205
            ->given(
206
                /** @var \Cubiche\Core\Async\Loop\LoopInterface $loop */
207
                $loop = $this->newDefaultMockTestedInstance(),
208
                $task = $this->delegateMockWithReturn('foo')
209
            )
210
            /* @var \Cubiche\Core\Async\Promise\PromiseInterface $promise */
211
            ->when($promise = $loop->$method($task))
212
            ->then()
213
                ->object($promise)
214
                    ->isInstanceOf(PromiseInterface::class)
215
                ->boolean($promise->state()->equals(State::PENDING()))
216
                    ->isTrue()
217
        ;
218
219
        $this
220
            ->given($onFulfilled = $this->delegateMock())
221
            ->when(function () use ($loop, $promise, $onFulfilled) {
222
                $promise->then($onFulfilled);
223
                $loop->tick();
224
            })
225
            ->then()
226
                ->delegateCall($task)
227
                    ->once()
228
                ->delegateCall($onFulfilled)
229
                    ->withArguments('foo')
230
                    ->once()
231
        ;
232
    }
233
234
    /**
235
     * @param string $method
236
     * @param array  $arguments
237
     */
238
    protected function proxyMethodTest($method, array $arguments = array())
239
    {
240
        $methodCall = $this
241
            ->given(
242
                $baseLoopMock = $this->newMockInstance(BaseLoopInterface::class),
243
                /** @var \Cubiche\Core\Async\Loop\LoopInterface $loop */
244
                $loop = $this->newTestedInstance($baseLoopMock)
245
            )
246
            ->when(\call_user_func_array(array($loop, $method), $arguments))
247
            ->then()
248
                ->mock($baseLoopMock)
249
                    ->call($method)
250
        ;
251
        \call_user_func_array(array($methodCall, 'withArguments'), $arguments);
252
        $methodCall->once();
253
    }
254
}
255