1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
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\Promise; |
12
|
|
|
|
13
|
|
|
use Cubiche\Core\Async\Loop\Loop; |
14
|
|
|
use Cubiche\Core\Async\Promise\Promise; |
15
|
|
|
use Cubiche\Core\Async\Promise\PromiseInterface; |
16
|
|
|
use Cubiche\Core\Async\Promise\Promises; |
17
|
|
|
use Cubiche\Core\Async\Promise\State; |
18
|
|
|
use Cubiche\Core\Async\Tests\Units\TestCase; |
19
|
|
|
use Cubiche\Core\Async\Promise\DeferredInterface; |
20
|
|
|
use Cubiche\Core\Async\Promise\FulfilledPromise; |
21
|
|
|
use Cubiche\Core\Async\Promise\RejectedPromise; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Promises Tests class. |
25
|
|
|
* |
26
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class PromisesTests extends TestCase |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Test defer. |
32
|
|
|
*/ |
33
|
|
|
public function testDefer() |
34
|
|
|
{ |
35
|
|
|
$this |
36
|
|
|
->when($deferred = Promises::defer()) |
37
|
|
|
->then() |
38
|
|
|
->object($deferred) |
39
|
|
|
->isInstanceOf(DeferredInterface::class) |
40
|
|
|
->boolean($deferred->promise()->state()->equals(State::PENDING())) |
41
|
|
|
->isTrue() |
42
|
|
|
; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Test fulfilled. |
47
|
|
|
*/ |
48
|
|
|
public function testFulfilled() |
49
|
|
|
{ |
50
|
|
|
$this |
51
|
|
|
->when($fulfilled = Promises::fulfilled('foo')) |
52
|
|
|
->then() |
53
|
|
|
->object($fulfilled) |
54
|
|
|
->isInstanceOf(FulfilledPromise::class) |
55
|
|
|
; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Test rejected. |
60
|
|
|
*/ |
61
|
|
|
public function testRejected() |
62
|
|
|
{ |
63
|
|
|
$this |
64
|
|
|
->when($rejected = Promises::rejected()) |
65
|
|
|
->then() |
66
|
|
|
->object($rejected) |
67
|
|
|
->isInstanceOf(RejectedPromise::class) |
68
|
|
|
; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Test all. |
73
|
|
|
*/ |
74
|
|
|
public function testAll() |
75
|
|
|
{ |
76
|
|
|
$this |
77
|
|
|
->given( |
78
|
|
|
$deferred = Promises::defer(), |
79
|
|
|
$promise1 = Promises::fulfilled(1), |
80
|
|
|
$promise2 = Promises::fulfilled(2) |
81
|
|
|
) |
82
|
|
|
->when($all = Promises::all(array($deferred->promise(), $promise1, $promise2))) |
83
|
|
|
->then() |
84
|
|
|
->object($all) |
85
|
|
|
->isInstanceOf(PromiseInterface::class) |
86
|
|
|
->boolean($all->state()->equals(State::PENDING())) |
87
|
|
|
->isTrue() |
88
|
|
|
; |
89
|
|
|
|
90
|
|
|
$this |
91
|
|
|
->when($deferred->resolve(0)) |
92
|
|
|
->then() |
93
|
|
|
->boolean($all->state()->equals(State::FULFILLED())) |
94
|
|
|
->isTrue() |
95
|
|
|
; |
96
|
|
|
|
97
|
|
|
$this |
98
|
|
|
->given($onFulfilled = $this->delegateMock()) |
99
|
|
|
->when($all->then($onFulfilled)) |
100
|
|
|
->then() |
101
|
|
|
->delegateCall($onFulfilled) |
102
|
|
|
->withArguments(array(0, 1, 2)) |
103
|
|
|
->once() |
104
|
|
|
; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Test map. |
109
|
|
|
*/ |
110
|
|
|
public function testMap() |
111
|
|
|
{ |
112
|
|
|
$this |
113
|
|
|
->given( |
114
|
|
|
$promises = array(Promises::fulfilled(0), Promises::fulfilled(1), Promises::fulfilled(2)), |
115
|
|
|
$onFulfilled = $this->delegateMock() |
116
|
|
|
) |
117
|
|
|
->when(Promises::map($promises, function ($value) { |
118
|
|
|
return $value + 1; |
119
|
|
|
})->then($onFulfilled)) |
120
|
|
|
->then() |
121
|
|
|
->delegateCall($onFulfilled) |
122
|
|
|
->withArguments(array(1, 2, 3)) |
123
|
|
|
->once() |
124
|
|
|
; |
125
|
|
|
|
126
|
|
|
$this |
127
|
|
|
->given($onFulfilled = $this->delegateMock()) |
128
|
|
|
->when(Promises::map(array())->then($onFulfilled)) |
129
|
|
|
->then() |
130
|
|
|
->delegateCall($onFulfilled) |
131
|
|
|
->withArguments(array()) |
132
|
|
|
->once() |
133
|
|
|
; |
134
|
|
|
|
135
|
|
|
$this |
136
|
|
|
->given( |
137
|
|
|
$reason = new \Exception(), |
138
|
|
|
$onRejected = $this->delegateMock() |
139
|
|
|
) |
140
|
|
|
->when(Promises::map(array(Promises::rejected($reason)))->otherwise($onRejected)) |
141
|
|
|
->then() |
142
|
|
|
->delegateCall($onRejected) |
143
|
|
|
->withArguments($reason) |
144
|
|
|
->once() |
145
|
|
|
; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Test timeout. |
150
|
|
|
*/ |
151
|
|
|
public function testTimeout() |
152
|
|
|
{ |
153
|
|
|
$this |
154
|
|
|
->given( |
155
|
|
|
$loop = new Loop(), |
156
|
|
|
$deferred = Promises::defer() |
157
|
|
|
) |
158
|
|
|
->when($timeout = Promises::timeout($deferred->promise(), 0.01, $loop)) |
159
|
|
|
->then() |
160
|
|
|
->object($timeout) |
161
|
|
|
->isInstanceOf(PromiseInterface::class) |
162
|
|
|
; |
163
|
|
|
|
164
|
|
|
$this |
165
|
|
|
->given($onRejected = $this->delegateMock()) |
166
|
|
|
->when(function () use ($timeout, $onRejected, $loop) { |
167
|
|
|
$timeout->otherwise($onRejected); |
168
|
|
|
$loop->run(); |
169
|
|
|
}) |
170
|
|
|
->then() |
171
|
|
|
->delegateCall($onRejected) |
172
|
|
|
->once() |
173
|
|
|
; |
174
|
|
|
|
175
|
|
|
$this |
176
|
|
|
->given( |
177
|
|
|
$onFulfilled = $this->delegateMock(), |
178
|
|
|
$timeout = Promises::timeout(Promises::fulfilled('foo'), 0.01, $loop) |
179
|
|
|
) |
180
|
|
|
->when(function () use ($timeout, $onFulfilled, $loop) { |
181
|
|
|
$timeout->then($onFulfilled); |
182
|
|
|
$loop->run(); |
183
|
|
|
}) |
184
|
|
|
->then() |
185
|
|
|
->delegateCall($onFulfilled) |
186
|
|
|
->withArguments('foo') |
187
|
|
|
->once() |
188
|
|
|
; |
189
|
|
|
|
190
|
|
|
$this |
191
|
|
|
->given( |
192
|
|
|
$reason = new \Exception(), |
193
|
|
|
$onRejected = $this->delegateMock(), |
194
|
|
|
$timeout = Promises::timeout(Promises::rejected($reason), 0.01, $loop) |
195
|
|
|
) |
196
|
|
|
->when(function () use ($timeout, $onRejected, $loop) { |
197
|
|
|
$timeout->otherwise($onRejected); |
198
|
|
|
$loop->run(); |
199
|
|
|
}) |
200
|
|
|
->then() |
201
|
|
|
->delegateCall($onRejected) |
202
|
|
|
->withArguments($reason) |
203
|
|
|
->once() |
204
|
|
|
; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Test get. |
209
|
|
|
*/ |
210
|
|
|
public function testGet() |
211
|
|
|
{ |
212
|
|
|
$this |
213
|
|
|
->given($loop = new Loop()) |
214
|
|
|
->when($value = Promises::get(Promises::fulfilled('foo'), $loop)) |
215
|
|
|
->then() |
216
|
|
|
->variable($value) |
217
|
|
|
->isEqualTo('foo') |
218
|
|
|
; |
219
|
|
|
|
220
|
|
|
$this |
221
|
|
|
->given($deferred = Promises::defer()) |
222
|
|
|
->let($value = null) |
223
|
|
|
->when(function () use ($loop, $deferred, &$value) { |
224
|
|
|
$loop->enqueue(function () use ($deferred) { |
225
|
|
|
$deferred->resolve('bar'); |
226
|
|
|
}); |
227
|
|
|
$value = Promises::get($deferred->promise(), $loop); |
228
|
|
|
}) |
229
|
|
|
->then() |
230
|
|
|
->variable($value) |
231
|
|
|
->isEqualTo('bar') |
232
|
|
|
; |
233
|
|
|
|
234
|
|
|
$this |
235
|
|
|
->given($deferred = Promises::defer()) |
236
|
|
|
->exception(function () use ($loop, $deferred) { |
237
|
|
|
Promises::get($deferred->promise(), $loop, 0.01); |
238
|
|
|
}) |
239
|
|
|
; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|