ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
CoroutineTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Sabre\Event;
4 
6 
10  function testNonGenerator() {
11 
12  coroutine(function() {});
13 
14  }
15 
16  function testBasicCoroutine() {
17 
18  $start = 0;
19 
20  coroutine(function() use (&$start) {
21 
22  $start += 1;
23  yield;
24 
25  });
26 
27  $this->assertEquals(1, $start);
28 
29  }
30 
31  function testFulfilledPromise() {
32 
33  $start = 0;
34  $promise = new Promise(function($fulfill, $reject) {
35  $fulfill(2);
36  });
37 
38  coroutine(function() use (&$start, $promise) {
39 
40  $start += 1;
41  $start += (yield $promise);
42 
43  });
44 
45  Loop\run();
46  $this->assertEquals(3, $start);
47 
48  }
49 
50  function testRejectedPromise() {
51 
52  $start = 0;
53  $promise = new Promise(function($fulfill, $reject) {
54  $reject(2);
55  });
56 
57  coroutine(function() use (&$start, $promise) {
58 
59  $start += 1;
60  try {
61  $start += (yield $promise);
62  // This line is unreachable, but it's our control
63  $start += 4;
64  } catch (\Exception $e) {
65  $start += $e->getMessage();
66  }
67 
68  });
69 
70  Loop\run();
71  $this->assertEquals(3, $start);
72 
73  }
74 
76 
77  $start = 0;
78  $promise = new Promise(function($fulfill, $reject) {
79  $reject(new \LogicException('2'));
80  });
81 
82  coroutine(function() use (&$start, $promise) {
83 
84  $start += 1;
85  try {
86  $start += (yield $promise);
87  // This line is unreachable, but it's our control
88  $start += 4;
89  } catch (\LogicException $e) {
90  $start += $e->getMessage();
91  }
92 
93  });
94 
95  Loop\run();
96  $this->assertEquals(3, $start);
97 
98  }
99 
101 
102  $start = 0;
103  $promise = new Promise(function($fulfill, $reject) {
104  $reject([]);
105  });
106 
107  coroutine(function() use (&$start, $promise) {
108 
109  $start += 1;
110  try {
111  $start += (yield $promise);
112  // This line is unreachable, but it's our control
113  $start += 4;
114  } catch (\Exception $e) {
115  $this->assertTrue(strpos($e->getMessage(), 'Promise was rejected with') === 0);
116  $start += 2;
117  }
118 
119  })->wait();
120 
121  $this->assertEquals(3, $start);
122 
123  }
124 
126 
127  $start = 0;
128  $promise = new Promise();
129  coroutine(function() use (&$start, $promise) {
130 
131  $start += 1;
132  $start += (yield $promise);
133 
134  });
135  Loop\run();
136 
137  $this->assertEquals(1, $start);
138 
139  $promise->fulfill(2);
140  Loop\run();
141 
142  $this->assertEquals(3, $start);
143 
144  }
145 
147 
148  $start = 0;
149  $promise = new Promise();
150  coroutine(function() use (&$start, $promise) {
151 
152  $start += 1;
153  try {
154  $start += (yield $promise);
155  // This line is unreachable, but it's our control
156  $start += 4;
157  } catch (\Exception $e) {
158  $start += $e->getMessage();
159  }
160 
161  });
162 
163  $this->assertEquals(1, $start);
164 
165  $promise->reject(new \Exception(2));
166  Loop\run();
167 
168  $this->assertEquals(3, $start);
169 
170  }
171 
173 
174  $start = 0;
175  coroutine(function() use (&$start) {
176 
177  $start += 1;
178  $start += (yield 2);
179 
180  throw new \Exception('4');
181 
182  })->error(function($e) use (&$start) {
183 
184  $start += $e->getMessage();
185 
186  });
187  Loop\run();
188 
189  $this->assertEquals(7, $start);
190 
191  }
192 
193  function testDeepException() {
194 
195  $start = 0;
196  $promise = new Promise();
197  coroutine(function() use (&$start, $promise) {
198 
199  $start += 1;
200  $start += (yield $promise);
201 
202  })->error(function($e) use (&$start) {
203 
204  $start += $e->getMessage();
205 
206  });
207 
208  $this->assertEquals(1, $start);
209 
210  $promise->reject(new \Exception(2));
211  Loop\run();
212 
213  $this->assertEquals(3, $start);
214 
215  }
216 
218 
219  $ok = false;
220  coroutine(function() {
221 
222  yield 1;
223  yield 2;
224  $hello = 'hi';
225 
226  })->then(function($value) use (&$ok) {
227  $this->assertEquals(2, $value);
228  $ok = true;
229  })->error(function($reason) {
230  $this->fail($reason);
231  });
232  Loop\run();
233 
234  $this->assertTrue($ok);
235 
236  }
237 
239 
240  $ok = false;
241 
242  $promise = new Promise();
243 
244  coroutine(function() use ($promise) {
245 
246  yield 'fail';
247  yield $promise;
248  $hello = 'hi';
249 
250  })->then(function($value) use (&$ok) {
251  $ok = $value;
252  $this->fail($reason);
253  });
254 
255  $promise->fulfill('omg it worked');
256  Loop\run();
257 
258  $this->assertEquals('omg it worked', $ok);
259 
260  }
261 
262 }
An implementation of the Promise pattern.
Definition: Promise.php:23
$promise
This example shows demonstrates the Promise api.
Definition: promise.php:16
coroutine(callable $gen)
Turn asynchronous promise-based code into something that looks synchronous again, through the use of ...
Definition: coroutine.php:47
$start
Definition: bench.php:8
run()
Runs the loop.
Definition: functions.php:130