ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
PromiseTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Sabre\Event\Promise;
4 
7 
9 
10  function testSuccess() {
11 
12  $finalValue = 0;
13  $promise = new Promise();
14  $promise->fulfill(1);
15 
16  $promise->then(function($value) use (&$finalValue) {
17  $finalValue = $value + 2;
18  });
19  Loop\run();
20 
21  $this->assertEquals(3, $finalValue);
22 
23  }
24 
25  function testFail() {
26 
27  $finalValue = 0;
28  $promise = new Promise();
29  $promise->reject(1);
30 
31  $promise->then(null, function($value) use (&$finalValue) {
32  $finalValue = $value + 2;
33  });
34  Loop\run();
35 
36  $this->assertEquals(3, $finalValue);
37 
38  }
39 
40  function testChain() {
41 
42  $finalValue = 0;
43  $promise = new Promise();
44  $promise->fulfill(1);
45 
46  $promise->then(function($value) use (&$finalValue) {
47  $finalValue = $value + 2;
48  return $finalValue;
49  })->then(function($value) use (&$finalValue) {
50  $finalValue = $value + 4;
51  return $finalValue;
52  });
53  Loop\run();
54 
55  $this->assertEquals(7, $finalValue);
56 
57  }
58  function testChainPromise() {
59 
60  $finalValue = 0;
61  $promise = new Promise();
62  $promise->fulfill(1);
63 
64  $subPromise = new Promise();
65 
66  $promise->then(function($value) use ($subPromise) {
67  return $subPromise;
68  })->then(function($value) use (&$finalValue) {
69  $finalValue = $value + 4;
70  return $finalValue;
71  });
72 
73  $subPromise->fulfill(2);
74  Loop\run();
75 
76  $this->assertEquals(6, $finalValue);
77 
78  }
79 
80  function testPendingResult() {
81 
82  $finalValue = 0;
83  $promise = new Promise();
84 
85  $promise->then(function($value) use (&$finalValue) {
86  $finalValue = $value + 2;
87  });
88 
89  $promise->fulfill(4);
90  Loop\run();
91 
92  $this->assertEquals(6, $finalValue);
93 
94  }
95 
96  function testPendingFail() {
97 
98  $finalValue = 0;
99  $promise = new Promise();
100 
101  $promise->then(null, function($value) use (&$finalValue) {
102  $finalValue = $value + 2;
103  });
104 
105  $promise->reject(4);
106  Loop\run();
107 
108  $this->assertEquals(6, $finalValue);
109 
110  }
111 
112  function testExecutorSuccess() {
113 
114  $promise = (new Promise(function($success, $fail) {
115 
116  $success('hi');
117 
118  }))->then(function($result) use (&$realResult) {
119 
120  $realResult = $result;
121 
122  });
123  Loop\run();
124 
125  $this->assertEquals('hi', $realResult);
126 
127  }
128 
129  function testExecutorFail() {
130 
131  $promise = (new Promise(function($success, $fail) {
132 
133  $fail('hi');
134 
135  }))->then(function($result) use (&$realResult) {
136 
137  $realResult = 'incorrect';
138 
139  }, function($reason) use (&$realResult) {
140 
141  $realResult = $reason;
142 
143  });
144  Loop\run();
145 
146  $this->assertEquals('hi', $realResult);
147 
148  }
149 
153  function testFulfillTwice() {
154 
155  $promise = new Promise();
156  $promise->fulfill(1);
157  $promise->fulfill(1);
158 
159  }
160 
164  function testRejectTwice() {
165 
166  $promise = new Promise();
167  $promise->reject(1);
168  $promise->reject(1);
169 
170  }
171 
173 
174  $ok = 0;
175  $promise = new Promise();
176  $promise->otherwise(function($reason) {
177 
178  $this->assertEquals('foo', $reason);
179  throw new \Exception('hi');
180 
181  })->then(function() use (&$ok) {
182 
183  $ok = -1;
184 
185  }, function() use (&$ok) {
186 
187  $ok = 1;
188 
189  });
190 
191  $this->assertEquals(0, $ok);
192  $promise->reject('foo');
193  Loop\run();
194 
195  $this->assertEquals(1, $ok);
196 
197  }
198 
199  function testAll() {
200 
201  $promise1 = new Promise();
202  $promise2 = new Promise();
203 
204  $finalValue = 0;
205  Promise::all([$promise1, $promise2])->then(function($value) use (&$finalValue) {
206 
207  $finalValue = $value;
208 
209  });
210 
211  $promise1->fulfill(1);
212  Loop\run();
213  $this->assertEquals(0, $finalValue);
214 
215  $promise2->fulfill(2);
216  Loop\run();
217  $this->assertEquals([1, 2], $finalValue);
218 
219  }
220 
221  function testAllReject() {
222 
223  $promise1 = new Promise();
224  $promise2 = new Promise();
225 
226  $finalValue = 0;
227  Promise::all([$promise1, $promise2])->then(
228  function($value) use (&$finalValue) {
229  $finalValue = 'foo';
230  return 'test';
231  },
232  function($value) use (&$finalValue) {
233  $finalValue = $value;
234  }
235  );
236 
237  $promise1->reject(1);
238  Loop\run();
239  $this->assertEquals(1, $finalValue);
240  $promise2->reject(2);
241  Loop\run();
242  $this->assertEquals(1, $finalValue);
243 
244  }
245 
247 
248  $promise1 = new Promise();
249  $promise2 = new Promise();
250 
251  $finalValue = 0;
252  Promise::all([$promise1, $promise2])->then(
253  function($value) use (&$finalValue) {
254  $finalValue = 'foo';
255  return 'test';
256  },
257  function($value) use (&$finalValue) {
258  $finalValue = $value;
259  }
260  );
261 
262  $promise1->reject(1);
263  Loop\run();
264  $this->assertEquals(1, $finalValue);
265  $promise2->fulfill(2);
266  Loop\run();
267  $this->assertEquals(1, $finalValue);
268 
269  }
270 
271  function testWaitResolve() {
272 
273  $promise = new Promise();
274  Loop\nextTick(function() use ($promise) {
275  $promise->fulfill(1);
276  });
277  $this->assertEquals(
278  1,
279  $promise->wait()
280  );
281 
282  }
283 
288 
289  $promise = new Promise();
290  $promise->wait();
291 
292  }
293 
295 
296  $promise = new Promise();
297  Loop\nextTick(function() use ($promise) {
298  $promise->reject(new \OutOfBoundsException('foo'));
299  });
300  try {
301  $promise->wait();
302  $this->fail('We did not get the expected exception');
303  } catch (\Exception $e) {
304  $this->assertInstanceOf('OutOfBoundsException', $e);
305  $this->assertEquals('foo', $e->getMessage());
306  }
307 
308  }
309 
311 
312  $promise = new Promise();
313  Loop\nextTick(function() use ($promise) {
314  $promise->reject('foo');
315  });
316  try {
317  $promise->wait();
318  $this->fail('We did not get the expected exception');
319  } catch (\Exception $e) {
320  $this->assertInstanceOf('Exception', $e);
321  $this->assertEquals('foo', $e->getMessage());
322  }
323 
324  }
325 
327 
328  $promise = new Promise();
329  Loop\nextTick(function() use ($promise) {
330  $promise->reject([]);
331  });
332  try {
333  $promise->wait();
334  $this->fail('We did not get the expected exception');
335  } catch (\Exception $e) {
336  $this->assertInstanceOf('Exception', $e);
337  $this->assertEquals('Promise was rejected with reason of type: array', $e->getMessage());
338  }
339 
340  }
341 }
$result
An implementation of the Promise pattern.
Definition: Promise.php:23
then(callable $onFulfilled=null, callable $onRejected=null)
This method allows you to specify the callback that will be called after the promise has been fulfill...
Definition: Promise.php:92
$promise
This example shows demonstrates the Promise api.
Definition: promise.php:16
static all(array $promises)
Deprecated.
Definition: Promise.php:314
run()
Runs the loop.
Definition: functions.php:130
$success
Definition: Utf8Test.php:86
nextTick(callable $cb)
Runs a function immediately at the next iteration of the loop.
Definition: functions.php:52