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