ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
PromiseTest.php
Go to the documentation of this file.
1<?php
2
3namespace Sabre\Event;
4
6
7 function testSuccess() {
8
9 $finalValue = 0;
10 $promise = new Promise();
11 $promise->fulfill(1);
12
13 $promise->then(function($value) use (&$finalValue) {
14 $finalValue = $value + 2;
15 });
16 Loop\run();
17
18 $this->assertEquals(3, $finalValue);
19
20 }
21
22 function testFail() {
23
24 $finalValue = 0;
25 $promise = new Promise();
26 $promise->reject(1);
27
28 $promise->then(null, function($value) use (&$finalValue) {
29 $finalValue = $value + 2;
30 });
31 Loop\run();
32
33 $this->assertEquals(3, $finalValue);
34
35 }
36
37 function testChain() {
38
39 $finalValue = 0;
40 $promise = new Promise();
41 $promise->fulfill(1);
42
43 $promise->then(function($value) use (&$finalValue) {
44 $finalValue = $value + 2;
45 return $finalValue;
46 })->then(function($value) use (&$finalValue) {
47 $finalValue = $value + 4;
48 return $finalValue;
49 });
50 Loop\run();
51
52 $this->assertEquals(7, $finalValue);
53
54 }
55 function testChainPromise() {
56
57 $finalValue = 0;
58 $promise = new Promise();
59 $promise->fulfill(1);
60
61 $subPromise = new Promise();
62
63 $promise->then(function($value) use ($subPromise) {
64 return $subPromise;
65 })->then(function($value) use (&$finalValue) {
66 $finalValue = $value + 4;
67 return $finalValue;
68 });
69
70 $subPromise->fulfill(2);
71 Loop\run();
72
73 $this->assertEquals(6, $finalValue);
74
75 }
76
77 function testPendingResult() {
78
79 $finalValue = 0;
80 $promise = new Promise();
81
82 $promise->then(function($value) use (&$finalValue) {
83 $finalValue = $value + 2;
84 });
85
86 $promise->fulfill(4);
87 Loop\run();
88
89 $this->assertEquals(6, $finalValue);
90
91 }
92
93 function testPendingFail() {
94
95 $finalValue = 0;
96 $promise = new Promise();
97
98 $promise->then(null, function($value) use (&$finalValue) {
99 $finalValue = $value + 2;
100 });
101
102 $promise->reject(4);
103 Loop\run();
104
105 $this->assertEquals(6, $finalValue);
106
107 }
108
110
111 $promise = (new Promise(function($success, $fail) {
112
113 $success('hi');
114
115 }))->then(function($result) use (&$realResult) {
116
117 $realResult = $result;
118
119 });
120 Loop\run();
121
122 $this->assertEquals('hi', $realResult);
123
124 }
125
126 function testExecutorFail() {
127
128 $promise = (new Promise(function($success, $fail) {
129
130 $fail('hi');
131
132 }))->then(function($result) use (&$realResult) {
133
134 $realResult = 'incorrect';
135
136 }, function($reason) use (&$realResult) {
137
138 $realResult = $reason;
139
140 });
141 Loop\run();
142
143 $this->assertEquals('hi', $realResult);
144
145 }
146
150 function testFulfillTwice() {
151
152 $promise = new Promise();
153 $promise->fulfill(1);
154 $promise->fulfill(1);
155
156 }
157
161 function testRejectTwice() {
162
163 $promise = new Promise();
164 $promise->reject(1);
165 $promise->reject(1);
166
167 }
168
170
171 $ok = 0;
172 $promise = new Promise();
173 $promise->otherwise(function($reason) {
174
175 $this->assertEquals('foo', $reason);
176 throw new \Exception('hi');
177
178 })->then(function() use (&$ok) {
179
180 $ok = -1;
181
182 }, function() use (&$ok) {
183
184 $ok = 1;
185
186 });
187
188 $this->assertEquals(0, $ok);
189 $promise->reject('foo');
190 Loop\run();
191
192 $this->assertEquals(1, $ok);
193
194 }
195
196 function testAll() {
197
198 $promise1 = new Promise();
199 $promise2 = new Promise();
200
201 $finalValue = 0;
202 Promise::all([$promise1, $promise2])->then(function($value) use (&$finalValue) {
203
204 $finalValue = $value;
205
206 });
207
208 $promise1->fulfill(1);
209 Loop\run();
210 $this->assertEquals(0, $finalValue);
211
212 $promise2->fulfill(2);
213 Loop\run();
214 $this->assertEquals([1, 2], $finalValue);
215
216 }
217
218 function testAllReject() {
219
220 $promise1 = new Promise();
221 $promise2 = new Promise();
222
223 $finalValue = 0;
224 Promise::all([$promise1, $promise2])->then(
225 function($value) use (&$finalValue) {
226 $finalValue = 'foo';
227 return 'test';
228 },
229 function($value) use (&$finalValue) {
230 $finalValue = $value;
231 }
232 );
233
234 $promise1->reject(1);
235 Loop\run();
236 $this->assertEquals(1, $finalValue);
237 $promise2->reject(2);
238 Loop\run();
239 $this->assertEquals(1, $finalValue);
240
241 }
242
244
245 $promise1 = new Promise();
246 $promise2 = new Promise();
247
248 $finalValue = 0;
249 Promise::all([$promise1, $promise2])->then(
250 function($value) use (&$finalValue) {
251 $finalValue = 'foo';
252 return 'test';
253 },
254 function($value) use (&$finalValue) {
255 $finalValue = $value;
256 }
257 );
258
259 $promise1->reject(1);
260 Loop\run();
261 $this->assertEquals(1, $finalValue);
262 $promise2->fulfill(2);
263 Loop\run();
264 $this->assertEquals(1, $finalValue);
265
266 }
267
268 function testRace() {
269
270 $promise1 = new Promise();
271 $promise2 = new Promise();
272
273 $finalValue = 0;
274 Promise\race([$promise1, $promise2])->then(
275 function($value) use (&$finalValue) {
276 $finalValue = $value;
277 },
278 function($value) use (&$finalValue) {
279 $finalValue = $value;
280 }
281 );
282
283 $promise1->fulfill(1);
284 Loop\run();
285 $this->assertEquals(1, $finalValue);
286 $promise2->fulfill(2);
287 Loop\run();
288 $this->assertEquals(1, $finalValue);
289
290 }
291
292 function testRaceReject() {
293
294 $promise1 = new Promise();
295 $promise2 = new Promise();
296
297 $finalValue = 0;
298 Promise\race([$promise1, $promise2])->then(
299 function($value) use (&$finalValue) {
300 $finalValue = $value;
301 },
302 function($value) use (&$finalValue) {
303 $finalValue = $value;
304 }
305 );
306
307 $promise1->reject(1);
308 Loop\run();
309 $this->assertEquals(1, $finalValue);
310 $promise2->reject(2);
311 Loop\run();
312 $this->assertEquals(1, $finalValue);
313
314 }
315
316 function testWaitResolve() {
317
318 $promise = new Promise();
319 Loop\nextTick(function() use ($promise) {
320 $promise->fulfill(1);
321 });
322 $this->assertEquals(
323 1,
324 $promise->wait()
325 );
326
327 }
328
333
334 $promise = new Promise();
335 $promise->wait();
336
337 }
338
340
341 $promise = new Promise();
342 Loop\nextTick(function() use ($promise) {
343 $promise->reject(new \OutOfBoundsException('foo'));
344 });
345 try {
346 $promise->wait();
347 $this->fail('We did not get the expected exception');
348 } catch (\Exception $e) {
349 $this->assertInstanceOf('OutOfBoundsException', $e);
350 $this->assertEquals('foo', $e->getMessage());
351 }
352
353 }
354
356
357 $promise = new Promise();
358 Loop\nextTick(function() use ($promise) {
359 $promise->reject('foo');
360 });
361 try {
362 $promise->wait();
363 $this->fail('We did not get the expected exception');
364 } catch (\Exception $e) {
365 $this->assertInstanceOf('Exception', $e);
366 $this->assertEquals('foo', $e->getMessage());
367 }
368
369 }
370
372
373 $promise = new Promise();
374 Loop\nextTick(function() use ($promise) {
375 $promise->reject([]);
376 });
377 try {
378 $promise->wait();
379 $this->fail('We did not get the expected exception');
380 } catch (\Exception $e) {
381 $this->assertInstanceOf('Exception', $e);
382 $this->assertEquals('Promise was rejected with reason of type: array', $e->getMessage());
383 }
384
385 }
386}
$result
$success
Definition: Utf8Test.php:86
An exception for terminatinating execution or to throw for unit testing.
testWaitWillNeverResolve()
@expectedException \LogicException
testRejectTwice()
@expectedException \Sabre\Event\PromiseAlreadyResolvedException
testFulfillTwice()
@expectedException \Sabre\Event\PromiseAlreadyResolvedException
An implementation of the Promise pattern.
Definition: Promise.php:23
static all(array $promises)
Deprecated.
Definition: Promise.php:314
run()
Runs the loop.
Definition: functions.php:130
nextTick(callable $cb)
Runs a function immediately at the next iteration of the loop.
Definition: functions.php:52
race(array $promises)
The race function returns a promise that resolves or rejects as soon as one of the promises in the ar...
Definition: functions.php:71
$promise
This example shows demonstrates the Promise api.
Definition: promise.php:16