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