| 1 | <?php
|
|---|
| 2 | namespace GuzzleHttp\Promise\Tests;
|
|---|
| 3 |
|
|---|
| 4 | use GuzzleHttp\Promise as P;
|
|---|
| 5 | use GuzzleHttp\Promise\FulfilledPromise;
|
|---|
| 6 | use GuzzleHttp\Promise\Promise;
|
|---|
| 7 | use GuzzleHttp\Promise\RejectedPromise;
|
|---|
| 8 |
|
|---|
| 9 | class FunctionsTest extends \PHPUnit_Framework_TestCase
|
|---|
| 10 | {
|
|---|
| 11 | public function testCreatesPromiseForValue()
|
|---|
| 12 | {
|
|---|
| 13 | $p = \GuzzleHttp\Promise\promise_for('foo');
|
|---|
| 14 | $this->assertInstanceOf('GuzzleHttp\Promise\FulfilledPromise', $p);
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | public function testReturnsPromiseForPromise()
|
|---|
| 18 | {
|
|---|
| 19 | $p = new Promise();
|
|---|
| 20 | $this->assertSame($p, \GuzzleHttp\Promise\promise_for($p));
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | public function testReturnsPromiseForThennable()
|
|---|
| 24 | {
|
|---|
| 25 | $p = new Thennable();
|
|---|
| 26 | $wrapped = \GuzzleHttp\Promise\promise_for($p);
|
|---|
| 27 | $this->assertNotSame($p, $wrapped);
|
|---|
| 28 | $this->assertInstanceOf('GuzzleHttp\Promise\PromiseInterface', $wrapped);
|
|---|
| 29 | $p->resolve('foo');
|
|---|
| 30 | P\queue()->run();
|
|---|
| 31 | $this->assertEquals('foo', $wrapped->wait());
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | public function testReturnsRejection()
|
|---|
| 35 | {
|
|---|
| 36 | $p = \GuzzleHttp\Promise\rejection_for('fail');
|
|---|
| 37 | $this->assertInstanceOf('GuzzleHttp\Promise\RejectedPromise', $p);
|
|---|
| 38 | $this->assertEquals('fail', $this->readAttribute($p, 'reason'));
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | public function testReturnsPromisesAsIsInRejectionFor()
|
|---|
| 42 | {
|
|---|
| 43 | $a = new Promise();
|
|---|
| 44 | $b = \GuzzleHttp\Promise\rejection_for($a);
|
|---|
| 45 | $this->assertSame($a, $b);
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | public function testWaitsOnAllPromisesIntoArray()
|
|---|
| 49 | {
|
|---|
| 50 | $e = new \Exception();
|
|---|
| 51 | $a = new Promise(function () use (&$a) { $a->resolve('a'); });
|
|---|
| 52 | $b = new Promise(function () use (&$b) { $b->reject('b'); });
|
|---|
| 53 | $c = new Promise(function () use (&$c, $e) { $c->reject($e); });
|
|---|
| 54 | $results = \GuzzleHttp\Promise\inspect_all([$a, $b, $c]);
|
|---|
| 55 | $this->assertEquals([
|
|---|
| 56 | ['state' => 'fulfilled', 'value' => 'a'],
|
|---|
| 57 | ['state' => 'rejected', 'reason' => 'b'],
|
|---|
| 58 | ['state' => 'rejected', 'reason' => $e]
|
|---|
| 59 | ], $results);
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | /**
|
|---|
| 63 | * @expectedException \GuzzleHttp\Promise\RejectionException
|
|---|
| 64 | */
|
|---|
| 65 | public function testUnwrapsPromisesWithNoDefaultAndFailure()
|
|---|
| 66 | {
|
|---|
| 67 | $promises = [new FulfilledPromise('a'), new Promise()];
|
|---|
| 68 | \GuzzleHttp\Promise\unwrap($promises);
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | public function testUnwrapsPromisesWithNoDefault()
|
|---|
| 72 | {
|
|---|
| 73 | $promises = [new FulfilledPromise('a')];
|
|---|
| 74 | $this->assertEquals(['a'], \GuzzleHttp\Promise\unwrap($promises));
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | public function testUnwrapsPromisesWithKeys()
|
|---|
| 78 | {
|
|---|
| 79 | $promises = [
|
|---|
| 80 | 'foo' => new FulfilledPromise('a'),
|
|---|
| 81 | 'bar' => new FulfilledPromise('b'),
|
|---|
| 82 | ];
|
|---|
| 83 | $this->assertEquals([
|
|---|
| 84 | 'foo' => 'a',
|
|---|
| 85 | 'bar' => 'b'
|
|---|
| 86 | ], \GuzzleHttp\Promise\unwrap($promises));
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | public function testAllAggregatesSortedArray()
|
|---|
| 90 | {
|
|---|
| 91 | $a = new Promise();
|
|---|
| 92 | $b = new Promise();
|
|---|
| 93 | $c = new Promise();
|
|---|
| 94 | $d = \GuzzleHttp\Promise\all([$a, $b, $c]);
|
|---|
| 95 | $b->resolve('b');
|
|---|
| 96 | $a->resolve('a');
|
|---|
| 97 | $c->resolve('c');
|
|---|
| 98 | $d->then(
|
|---|
| 99 | function ($value) use (&$result) { $result = $value; },
|
|---|
| 100 | function ($reason) use (&$result) { $result = $reason; }
|
|---|
| 101 | );
|
|---|
| 102 | P\queue()->run();
|
|---|
| 103 | $this->assertEquals(['a', 'b', 'c'], $result);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | public function testAllThrowsWhenAnyRejected()
|
|---|
| 107 | {
|
|---|
| 108 | $a = new Promise();
|
|---|
| 109 | $b = new Promise();
|
|---|
| 110 | $c = new Promise();
|
|---|
| 111 | $d = \GuzzleHttp\Promise\all([$a, $b, $c]);
|
|---|
| 112 | $b->resolve('b');
|
|---|
| 113 | $a->reject('fail');
|
|---|
| 114 | $c->resolve('c');
|
|---|
| 115 | $d->then(
|
|---|
| 116 | function ($value) use (&$result) { $result = $value; },
|
|---|
| 117 | function ($reason) use (&$result) { $result = $reason; }
|
|---|
| 118 | );
|
|---|
| 119 | P\queue()->run();
|
|---|
| 120 | $this->assertEquals('fail', $result);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | public function testSomeAggregatesSortedArrayWithMax()
|
|---|
| 124 | {
|
|---|
| 125 | $a = new Promise();
|
|---|
| 126 | $b = new Promise();
|
|---|
| 127 | $c = new Promise();
|
|---|
| 128 | $d = \GuzzleHttp\Promise\some(2, [$a, $b, $c]);
|
|---|
| 129 | $b->resolve('b');
|
|---|
| 130 | $c->resolve('c');
|
|---|
| 131 | $a->resolve('a');
|
|---|
| 132 | $d->then(function ($value) use (&$result) { $result = $value; });
|
|---|
| 133 | P\queue()->run();
|
|---|
| 134 | $this->assertEquals(['b', 'c'], $result);
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | public function testSomeRejectsWhenTooManyRejections()
|
|---|
| 138 | {
|
|---|
| 139 | $a = new Promise();
|
|---|
| 140 | $b = new Promise();
|
|---|
| 141 | $d = \GuzzleHttp\Promise\some(2, [$a, $b]);
|
|---|
| 142 | $a->reject('bad');
|
|---|
| 143 | $b->resolve('good');
|
|---|
| 144 | P\queue()->run();
|
|---|
| 145 | $this->assertEquals($a::REJECTED, $d->getState());
|
|---|
| 146 | $d->then(null, function ($reason) use (&$called) {
|
|---|
| 147 | $called = $reason;
|
|---|
| 148 | });
|
|---|
| 149 | P\queue()->run();
|
|---|
| 150 | $this->assertInstanceOf('GuzzleHttp\Promise\AggregateException', $called);
|
|---|
| 151 | $this->assertContains('bad', $called->getReason());
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | public function testCanWaitUntilSomeCountIsSatisfied()
|
|---|
| 155 | {
|
|---|
| 156 | $a = new Promise(function () use (&$a) { $a->resolve('a'); });
|
|---|
| 157 | $b = new Promise(function () use (&$b) { $b->resolve('b'); });
|
|---|
| 158 | $c = new Promise(function () use (&$c) { $c->resolve('c'); });
|
|---|
| 159 | $d = \GuzzleHttp\Promise\some(2, [$a, $b, $c]);
|
|---|
| 160 | $this->assertEquals(['a', 'b'], $d->wait());
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | /**
|
|---|
| 164 | * @expectedException \GuzzleHttp\Promise\AggregateException
|
|---|
| 165 | * @expectedExceptionMessage Not enough promises to fulfill count
|
|---|
| 166 | */
|
|---|
| 167 | public function testThrowsIfImpossibleToWaitForSomeCount()
|
|---|
| 168 | {
|
|---|
| 169 | $a = new Promise(function () use (&$a) { $a->resolve('a'); });
|
|---|
| 170 | $d = \GuzzleHttp\Promise\some(2, [$a]);
|
|---|
| 171 | $d->wait();
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | /**
|
|---|
| 175 | * @expectedException \GuzzleHttp\Promise\AggregateException
|
|---|
| 176 | * @expectedExceptionMessage Not enough promises to fulfill count
|
|---|
| 177 | */
|
|---|
| 178 | public function testThrowsIfResolvedWithoutCountTotalResults()
|
|---|
| 179 | {
|
|---|
| 180 | $a = new Promise();
|
|---|
| 181 | $b = new Promise();
|
|---|
| 182 | $d = \GuzzleHttp\Promise\some(3, [$a, $b]);
|
|---|
| 183 | $a->resolve('a');
|
|---|
| 184 | $b->resolve('b');
|
|---|
| 185 | $d->wait();
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | public function testAnyReturnsFirstMatch()
|
|---|
| 189 | {
|
|---|
| 190 | $a = new Promise();
|
|---|
| 191 | $b = new Promise();
|
|---|
| 192 | $c = \GuzzleHttp\Promise\any([$a, $b]);
|
|---|
| 193 | $b->resolve('b');
|
|---|
| 194 | $a->resolve('a');
|
|---|
| 195 | //P\queue()->run();
|
|---|
| 196 | //$this->assertEquals('fulfilled', $c->getState());
|
|---|
| 197 | $c->then(function ($value) use (&$result) { $result = $value; });
|
|---|
| 198 | P\queue()->run();
|
|---|
| 199 | $this->assertEquals('b', $result);
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | public function testSettleFulfillsWithFulfilledAndRejected()
|
|---|
| 203 | {
|
|---|
| 204 | $a = new Promise();
|
|---|
| 205 | $b = new Promise();
|
|---|
| 206 | $c = new Promise();
|
|---|
| 207 | $d = \GuzzleHttp\Promise\settle([$a, $b, $c]);
|
|---|
| 208 | $b->resolve('b');
|
|---|
| 209 | $c->resolve('c');
|
|---|
| 210 | $a->reject('a');
|
|---|
| 211 | P\queue()->run();
|
|---|
| 212 | $this->assertEquals('fulfilled', $d->getState());
|
|---|
| 213 | $d->then(function ($value) use (&$result) { $result = $value; });
|
|---|
| 214 | P\queue()->run();
|
|---|
| 215 | $this->assertEquals([
|
|---|
| 216 | ['state' => 'rejected', 'reason' => 'a'],
|
|---|
| 217 | ['state' => 'fulfilled', 'value' => 'b'],
|
|---|
| 218 | ['state' => 'fulfilled', 'value' => 'c']
|
|---|
| 219 | ], $result);
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | public function testCanInspectFulfilledPromise()
|
|---|
| 223 | {
|
|---|
| 224 | $p = new FulfilledPromise('foo');
|
|---|
| 225 | $this->assertEquals([
|
|---|
| 226 | 'state' => 'fulfilled',
|
|---|
| 227 | 'value' => 'foo'
|
|---|
| 228 | ], \GuzzleHttp\Promise\inspect($p));
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | public function testCanInspectRejectedPromise()
|
|---|
| 232 | {
|
|---|
| 233 | $p = new RejectedPromise('foo');
|
|---|
| 234 | $this->assertEquals([
|
|---|
| 235 | 'state' => 'rejected',
|
|---|
| 236 | 'reason' => 'foo'
|
|---|
| 237 | ], \GuzzleHttp\Promise\inspect($p));
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | public function testCanInspectRejectedPromiseWithNormalException()
|
|---|
| 241 | {
|
|---|
| 242 | $e = new \Exception('foo');
|
|---|
| 243 | $p = new RejectedPromise($e);
|
|---|
| 244 | $this->assertEquals([
|
|---|
| 245 | 'state' => 'rejected',
|
|---|
| 246 | 'reason' => $e
|
|---|
| 247 | ], \GuzzleHttp\Promise\inspect($p));
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | public function testCallsEachLimit()
|
|---|
| 251 | {
|
|---|
| 252 | $p = new Promise();
|
|---|
| 253 | $aggregate = \GuzzleHttp\Promise\each_limit($p, 2);
|
|---|
| 254 | $p->resolve('a');
|
|---|
| 255 | P\queue()->run();
|
|---|
| 256 | $this->assertEquals($p::FULFILLED, $aggregate->getState());
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | public function testEachLimitAllRejectsOnFailure()
|
|---|
| 260 | {
|
|---|
| 261 | $p = [new FulfilledPromise('a'), new RejectedPromise('b')];
|
|---|
| 262 | $aggregate = \GuzzleHttp\Promise\each_limit_all($p, 2);
|
|---|
| 263 | P\queue()->run();
|
|---|
| 264 | $this->assertEquals(P\PromiseInterface::REJECTED, $aggregate->getState());
|
|---|
| 265 | $result = \GuzzleHttp\Promise\inspect($aggregate);
|
|---|
| 266 | $this->assertEquals('b', $result['reason']);
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | public function testIterForReturnsIterator()
|
|---|
| 270 | {
|
|---|
| 271 | $iter = new \ArrayIterator();
|
|---|
| 272 | $this->assertSame($iter, \GuzzleHttp\Promise\iter_for($iter));
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | public function testKnowsIfFulfilled()
|
|---|
| 276 | {
|
|---|
| 277 | $p = new FulfilledPromise(null);
|
|---|
| 278 | $this->assertTrue(P\is_fulfilled($p));
|
|---|
| 279 | $this->assertFalse(P\is_rejected($p));
|
|---|
| 280 | }
|
|---|
| 281 |
|
|---|
| 282 | public function testKnowsIfRejected()
|
|---|
| 283 | {
|
|---|
| 284 | $p = new RejectedPromise(null);
|
|---|
| 285 | $this->assertTrue(P\is_rejected($p));
|
|---|
| 286 | $this->assertFalse(P\is_fulfilled($p));
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | public function testKnowsIfSettled()
|
|---|
| 290 | {
|
|---|
| 291 | $p = new RejectedPromise(null);
|
|---|
| 292 | $this->assertTrue(P\is_settled($p));
|
|---|
| 293 | $p = new Promise();
|
|---|
| 294 | $this->assertFalse(P\is_settled($p));
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | public function testReturnsTrampoline()
|
|---|
| 298 | {
|
|---|
| 299 | $this->assertInstanceOf('GuzzleHttp\Promise\TaskQueue', P\queue());
|
|---|
| 300 | $this->assertSame(P\queue(), P\queue());
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | public function testCanScheduleThunk()
|
|---|
| 304 | {
|
|---|
| 305 | $tramp = P\queue();
|
|---|
| 306 | $promise = P\task(function () { return 'Hi!'; });
|
|---|
| 307 | $c = null;
|
|---|
| 308 | $promise->then(function ($v) use (&$c) { $c = $v; });
|
|---|
| 309 | $this->assertNull($c);
|
|---|
| 310 | $tramp->run();
|
|---|
| 311 | $this->assertEquals('Hi!', $c);
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | public function testCanScheduleThunkWithRejection()
|
|---|
| 315 | {
|
|---|
| 316 | $tramp = P\queue();
|
|---|
| 317 | $promise = P\task(function () { throw new \Exception('Hi!'); });
|
|---|
| 318 | $c = null;
|
|---|
| 319 | $promise->otherwise(function ($v) use (&$c) { $c = $v; });
|
|---|
| 320 | $this->assertNull($c);
|
|---|
| 321 | $tramp->run();
|
|---|
| 322 | $this->assertEquals('Hi!', $c->getMessage());
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | public function testCanScheduleThunkWithWait()
|
|---|
| 326 | {
|
|---|
| 327 | $tramp = P\queue();
|
|---|
| 328 | $promise = P\task(function () { return 'a'; });
|
|---|
| 329 | $this->assertEquals('a', $promise->wait());
|
|---|
| 330 | $tramp->run();
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | public function testYieldsFromCoroutine()
|
|---|
| 334 | {
|
|---|
| 335 | $promise = P\coroutine(function () {
|
|---|
| 336 | $value = (yield new P\FulfilledPromise('a'));
|
|---|
| 337 | yield $value . 'b';
|
|---|
| 338 | });
|
|---|
| 339 | $promise->then(function ($value) use (&$result) { $result = $value; });
|
|---|
| 340 | P\queue()->run();
|
|---|
| 341 | $this->assertEquals('ab', $result);
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 | public function testCanCatchExceptionsInCoroutine()
|
|---|
| 345 | {
|
|---|
| 346 | $promise = P\coroutine(function () {
|
|---|
| 347 | try {
|
|---|
| 348 | yield new P\RejectedPromise('a');
|
|---|
| 349 | $this->fail('Should have thrown into the coroutine!');
|
|---|
| 350 | } catch (P\RejectionException $e) {
|
|---|
| 351 | $value = (yield new P\FulfilledPromise($e->getReason()));
|
|---|
| 352 | yield $value . 'b';
|
|---|
| 353 | }
|
|---|
| 354 | });
|
|---|
| 355 | $promise->then(function ($value) use (&$result) { $result = $value; });
|
|---|
| 356 | P\queue()->run();
|
|---|
| 357 | $this->assertEquals(P\PromiseInterface::FULFILLED, $promise->getState());
|
|---|
| 358 | $this->assertEquals('ab', $result);
|
|---|
| 359 | }
|
|---|
| 360 |
|
|---|
| 361 | public function testRejectsParentExceptionWhenException()
|
|---|
| 362 | {
|
|---|
| 363 | $promise = P\coroutine(function () {
|
|---|
| 364 | yield new P\FulfilledPromise(0);
|
|---|
| 365 | throw new \Exception('a');
|
|---|
| 366 | });
|
|---|
| 367 | $promise->then(
|
|---|
| 368 | function () { $this->fail(); },
|
|---|
| 369 | function ($reason) use (&$result) { $result = $reason; }
|
|---|
| 370 | );
|
|---|
| 371 | P\queue()->run();
|
|---|
| 372 | $this->assertInstanceOf('Exception', $result);
|
|---|
| 373 | $this->assertEquals('a', $result->getMessage());
|
|---|
| 374 | }
|
|---|
| 375 |
|
|---|
| 376 | public function testCanRejectFromRejectionCallback()
|
|---|
| 377 | {
|
|---|
| 378 | $promise = P\coroutine(function () {
|
|---|
| 379 | yield new P\FulfilledPromise(0);
|
|---|
| 380 | yield new P\RejectedPromise('no!');
|
|---|
| 381 | });
|
|---|
| 382 | $promise->then(
|
|---|
| 383 | function () { $this->fail(); },
|
|---|
| 384 | function ($reason) use (&$result) { $result = $reason; }
|
|---|
| 385 | );
|
|---|
| 386 | P\queue()->run();
|
|---|
| 387 | $this->assertInstanceOf('GuzzleHttp\Promise\RejectionException', $result);
|
|---|
| 388 | $this->assertEquals('no!', $result->getReason());
|
|---|
| 389 | }
|
|---|
| 390 |
|
|---|
| 391 | public function testCanAsyncReject()
|
|---|
| 392 | {
|
|---|
| 393 | $rej = new P\Promise();
|
|---|
| 394 | $promise = P\coroutine(function () use ($rej) {
|
|---|
| 395 | yield new P\FulfilledPromise(0);
|
|---|
| 396 | yield $rej;
|
|---|
| 397 | });
|
|---|
| 398 | $promise->then(
|
|---|
| 399 | function () { $this->fail(); },
|
|---|
| 400 | function ($reason) use (&$result) { $result = $reason; }
|
|---|
| 401 | );
|
|---|
| 402 | $rej->reject('no!');
|
|---|
| 403 | P\queue()->run();
|
|---|
| 404 | $this->assertInstanceOf('GuzzleHttp\Promise\RejectionException', $result);
|
|---|
| 405 | $this->assertEquals('no!', $result->getReason());
|
|---|
| 406 | }
|
|---|
| 407 |
|
|---|
| 408 | public function testCanCatchAndThrowOtherException()
|
|---|
| 409 | {
|
|---|
| 410 | $promise = P\coroutine(function () {
|
|---|
| 411 | try {
|
|---|
| 412 | yield new P\RejectedPromise('a');
|
|---|
| 413 | $this->fail('Should have thrown into the coroutine!');
|
|---|
| 414 | } catch (P\RejectionException $e) {
|
|---|
| 415 | throw new \Exception('foo');
|
|---|
| 416 | }
|
|---|
| 417 | });
|
|---|
| 418 | $promise->otherwise(function ($value) use (&$result) { $result = $value; });
|
|---|
| 419 | P\queue()->run();
|
|---|
| 420 | $this->assertEquals(P\PromiseInterface::REJECTED, $promise->getState());
|
|---|
| 421 | $this->assertContains('foo', $result->getMessage());
|
|---|
| 422 | }
|
|---|
| 423 |
|
|---|
| 424 | public function testCanCatchAndYieldOtherException()
|
|---|
| 425 | {
|
|---|
| 426 | $promise = P\coroutine(function () {
|
|---|
| 427 | try {
|
|---|
| 428 | yield new P\RejectedPromise('a');
|
|---|
| 429 | $this->fail('Should have thrown into the coroutine!');
|
|---|
| 430 | } catch (P\RejectionException $e) {
|
|---|
| 431 | yield new P\RejectedPromise('foo');
|
|---|
| 432 | }
|
|---|
| 433 | });
|
|---|
| 434 | $promise->otherwise(function ($value) use (&$result) { $result = $value; });
|
|---|
| 435 | P\queue()->run();
|
|---|
| 436 | $this->assertEquals(P\PromiseInterface::REJECTED, $promise->getState());
|
|---|
| 437 | $this->assertContains('foo', $result->getMessage());
|
|---|
| 438 | }
|
|---|
| 439 |
|
|---|
| 440 | public function createLotsOfSynchronousPromise()
|
|---|
| 441 | {
|
|---|
| 442 | return P\coroutine(function () {
|
|---|
| 443 | $value = 0;
|
|---|
| 444 | for ($i = 0; $i < 1000; $i++) {
|
|---|
| 445 | $value = (yield new P\FulfilledPromise($i));
|
|---|
| 446 | }
|
|---|
| 447 | yield $value;
|
|---|
| 448 | });
|
|---|
| 449 | }
|
|---|
| 450 |
|
|---|
| 451 | public function testLotsOfSynchronousDoesNotBlowStack()
|
|---|
| 452 | {
|
|---|
| 453 | $promise = $this->createLotsOfSynchronousPromise();
|
|---|
| 454 | $promise->then(function ($v) use (&$r) { $r = $v; });
|
|---|
| 455 | P\queue()->run();
|
|---|
| 456 | $this->assertEquals(999, $r);
|
|---|
| 457 | }
|
|---|
| 458 |
|
|---|
| 459 | public function testLotsOfSynchronousWaitDoesNotBlowStack()
|
|---|
| 460 | {
|
|---|
| 461 | $promise = $this->createLotsOfSynchronousPromise();
|
|---|
| 462 | $promise->then(function ($v) use (&$r) { $r = $v; });
|
|---|
| 463 | $this->assertEquals(999, $promise->wait());
|
|---|
| 464 | $this->assertEquals(999, $r);
|
|---|
| 465 | }
|
|---|
| 466 |
|
|---|
| 467 | private function createLotsOfFlappingPromise()
|
|---|
| 468 | {
|
|---|
| 469 | return P\coroutine(function () {
|
|---|
| 470 | $value = 0;
|
|---|
| 471 | for ($i = 0; $i < 1000; $i++) {
|
|---|
| 472 | try {
|
|---|
| 473 | if ($i % 2) {
|
|---|
| 474 | $value = (yield new P\FulfilledPromise($i));
|
|---|
| 475 | } else {
|
|---|
| 476 | $value = (yield new P\RejectedPromise($i));
|
|---|
| 477 | }
|
|---|
| 478 | } catch (\Exception $e) {
|
|---|
| 479 | $value = (yield new P\FulfilledPromise($i));
|
|---|
| 480 | }
|
|---|
| 481 | }
|
|---|
| 482 | yield $value;
|
|---|
| 483 | });
|
|---|
| 484 | }
|
|---|
| 485 |
|
|---|
| 486 | public function testLotsOfTryCatchingDoesNotBlowStack()
|
|---|
| 487 | {
|
|---|
| 488 | $promise = $this->createLotsOfFlappingPromise();
|
|---|
| 489 | $promise->then(function ($v) use (&$r) { $r = $v; });
|
|---|
| 490 | P\queue()->run();
|
|---|
| 491 | $this->assertEquals(999, $r);
|
|---|
| 492 | }
|
|---|
| 493 |
|
|---|
| 494 | public function testLotsOfTryCatchingWaitingDoesNotBlowStack()
|
|---|
| 495 | {
|
|---|
| 496 | $promise = $this->createLotsOfFlappingPromise();
|
|---|
| 497 | $promise->then(function ($v) use (&$r) { $r = $v; });
|
|---|
| 498 | $this->assertEquals(999, $promise->wait());
|
|---|
| 499 | $this->assertEquals(999, $r);
|
|---|
| 500 | }
|
|---|
| 501 |
|
|---|
| 502 | public function testAsyncPromisesWithCorrectlyYieldedValues()
|
|---|
| 503 | {
|
|---|
| 504 | $promises = [
|
|---|
| 505 | new P\Promise(),
|
|---|
| 506 | new P\Promise(),
|
|---|
| 507 | new P\Promise()
|
|---|
| 508 | ];
|
|---|
| 509 |
|
|---|
| 510 | $promise = P\coroutine(function () use ($promises) {
|
|---|
| 511 | $value = null;
|
|---|
| 512 | $this->assertEquals('skip', (yield new P\FulfilledPromise('skip')));
|
|---|
| 513 | foreach ($promises as $idx => $p) {
|
|---|
| 514 | $value = (yield $p);
|
|---|
| 515 | $this->assertEquals($value, $idx);
|
|---|
| 516 | $this->assertEquals('skip', (yield new P\FulfilledPromise('skip')));
|
|---|
| 517 | }
|
|---|
| 518 | $this->assertEquals('skip', (yield new P\FulfilledPromise('skip')));
|
|---|
| 519 | yield $value;
|
|---|
| 520 | });
|
|---|
| 521 |
|
|---|
| 522 | $promises[0]->resolve(0);
|
|---|
| 523 | $promises[1]->resolve(1);
|
|---|
| 524 | $promises[2]->resolve(2);
|
|---|
| 525 |
|
|---|
| 526 | $promise->then(function ($v) use (&$r) { $r = $v; });
|
|---|
| 527 | P\queue()->run();
|
|---|
| 528 | $this->assertEquals(2, $r);
|
|---|
| 529 | }
|
|---|
| 530 |
|
|---|
| 531 | public function testYieldFinalWaitablePromise()
|
|---|
| 532 | {
|
|---|
| 533 | $p1 = new P\Promise(function () use (&$p1) {
|
|---|
| 534 | $p1->resolve('skip me');
|
|---|
| 535 | });
|
|---|
| 536 | $p2 = new P\Promise(function () use (&$p2) {
|
|---|
| 537 | $p2->resolve('hello!');
|
|---|
| 538 | });
|
|---|
| 539 | $co = P\coroutine(function() use ($p1, $p2) {
|
|---|
| 540 | yield $p1;
|
|---|
| 541 | yield $p2;
|
|---|
| 542 | });
|
|---|
| 543 | P\queue()->run();
|
|---|
| 544 | $this->assertEquals('hello!', $co->wait());
|
|---|
| 545 | }
|
|---|
| 546 |
|
|---|
| 547 | public function testCanYieldFinalPendingPromise()
|
|---|
| 548 | {
|
|---|
| 549 | $p1 = new P\Promise();
|
|---|
| 550 | $p2 = new P\Promise();
|
|---|
| 551 | $co = P\coroutine(function() use ($p1, $p2) {
|
|---|
| 552 | yield $p1;
|
|---|
| 553 | yield $p2;
|
|---|
| 554 | });
|
|---|
| 555 | $p1->resolve('a');
|
|---|
| 556 | $p2->resolve('b');
|
|---|
| 557 | $co->then(function ($value) use (&$result) { $result = $value; });
|
|---|
| 558 | P\queue()->run();
|
|---|
| 559 | $this->assertEquals('b', $result);
|
|---|
| 560 | }
|
|---|
| 561 |
|
|---|
| 562 | public function testCanNestYieldsAndFailures()
|
|---|
| 563 | {
|
|---|
| 564 | $p1 = new P\Promise();
|
|---|
| 565 | $p2 = new P\Promise();
|
|---|
| 566 | $p3 = new P\Promise();
|
|---|
| 567 | $p4 = new P\Promise();
|
|---|
| 568 | $p5 = new P\Promise();
|
|---|
| 569 | $co = P\coroutine(function() use ($p1, $p2, $p3, $p4, $p5) {
|
|---|
| 570 | try {
|
|---|
| 571 | yield $p1;
|
|---|
| 572 | } catch (\Exception $e) {
|
|---|
| 573 | yield $p2;
|
|---|
| 574 | try {
|
|---|
| 575 | yield $p3;
|
|---|
| 576 | yield $p4;
|
|---|
| 577 | } catch (\Exception $e) {
|
|---|
| 578 | yield $p5;
|
|---|
| 579 | }
|
|---|
| 580 | }
|
|---|
| 581 | });
|
|---|
| 582 | $p1->reject('a');
|
|---|
| 583 | $p2->resolve('b');
|
|---|
| 584 | $p3->resolve('c');
|
|---|
| 585 | $p4->reject('d');
|
|---|
| 586 | $p5->resolve('e');
|
|---|
| 587 | $co->then(function ($value) use (&$result) { $result = $value; });
|
|---|
| 588 | P\queue()->run();
|
|---|
| 589 | $this->assertEquals('e', $result);
|
|---|
| 590 | }
|
|---|
| 591 |
|
|---|
| 592 | public function testCanYieldErrorsAndSuccessesWithoutRecursion()
|
|---|
| 593 | {
|
|---|
| 594 | $promises = [];
|
|---|
| 595 | for ($i = 0; $i < 20; $i++) {
|
|---|
| 596 | $promises[] = new P\Promise();
|
|---|
| 597 | }
|
|---|
| 598 |
|
|---|
| 599 | $co = P\coroutine(function() use ($promises) {
|
|---|
| 600 | for ($i = 0; $i < 20; $i += 4) {
|
|---|
| 601 | try {
|
|---|
| 602 | yield $promises[$i];
|
|---|
| 603 | yield $promises[$i + 1];
|
|---|
| 604 | } catch (\Exception $e) {
|
|---|
| 605 | yield $promises[$i + 2];
|
|---|
| 606 | yield $promises[$i + 3];
|
|---|
| 607 | }
|
|---|
| 608 | }
|
|---|
| 609 | });
|
|---|
| 610 |
|
|---|
| 611 | for ($i = 0; $i < 20; $i += 4) {
|
|---|
| 612 | $promises[$i]->resolve($i);
|
|---|
| 613 | $promises[$i + 1]->reject($i + 1);
|
|---|
| 614 | $promises[$i + 2]->resolve($i + 2);
|
|---|
| 615 | $promises[$i + 3]->resolve($i + 3);
|
|---|
| 616 | }
|
|---|
| 617 |
|
|---|
| 618 | $co->then(function ($value) use (&$result) { $result = $value; });
|
|---|
| 619 | P\queue()->run();
|
|---|
| 620 | $this->assertEquals('19', $result);
|
|---|
| 621 | }
|
|---|
| 622 |
|
|---|
| 623 | public function testCanWaitOnPromiseAfterFulfilled()
|
|---|
| 624 | {
|
|---|
| 625 | $f = function () {
|
|---|
| 626 | static $i = 0;
|
|---|
| 627 | $i++;
|
|---|
| 628 | return $p = new P\Promise(function () use (&$p, $i) {
|
|---|
| 629 | $p->resolve($i . '-bar');
|
|---|
| 630 | });
|
|---|
| 631 | };
|
|---|
| 632 |
|
|---|
| 633 | $promises = [];
|
|---|
| 634 | for ($i = 0; $i < 20; $i++) {
|
|---|
| 635 | $promises[] = $f();
|
|---|
| 636 | }
|
|---|
| 637 |
|
|---|
| 638 | $p = P\coroutine(function () use ($promises) {
|
|---|
| 639 | yield new P\FulfilledPromise('foo!');
|
|---|
| 640 | foreach ($promises as $promise) {
|
|---|
| 641 | yield $promise;
|
|---|
| 642 | }
|
|---|
| 643 | });
|
|---|
| 644 |
|
|---|
| 645 | $this->assertEquals('20-bar', $p->wait());
|
|---|
| 646 | }
|
|---|
| 647 |
|
|---|
| 648 | public function testCanWaitOnErroredPromises()
|
|---|
| 649 | {
|
|---|
| 650 | $p1 = new P\Promise(function () use (&$p1) { $p1->reject('a'); });
|
|---|
| 651 | $p2 = new P\Promise(function () use (&$p2) { $p2->resolve('b'); });
|
|---|
| 652 | $p3 = new P\Promise(function () use (&$p3) { $p3->resolve('c'); });
|
|---|
| 653 | $p4 = new P\Promise(function () use (&$p4) { $p4->reject('d'); });
|
|---|
| 654 | $p5 = new P\Promise(function () use (&$p5) { $p5->resolve('e'); });
|
|---|
| 655 | $p6 = new P\Promise(function () use (&$p6) { $p6->reject('f'); });
|
|---|
| 656 |
|
|---|
| 657 | $co = P\coroutine(function() use ($p1, $p2, $p3, $p4, $p5, $p6) {
|
|---|
| 658 | try {
|
|---|
| 659 | yield $p1;
|
|---|
| 660 | } catch (\Exception $e) {
|
|---|
| 661 | yield $p2;
|
|---|
| 662 | try {
|
|---|
| 663 | yield $p3;
|
|---|
| 664 | yield $p4;
|
|---|
| 665 | } catch (\Exception $e) {
|
|---|
| 666 | yield $p5;
|
|---|
| 667 | yield $p6;
|
|---|
| 668 | }
|
|---|
| 669 | }
|
|---|
| 670 | });
|
|---|
| 671 |
|
|---|
| 672 | $res = P\inspect($co);
|
|---|
| 673 | $this->assertEquals('f', $res['reason']);
|
|---|
| 674 | }
|
|---|
| 675 |
|
|---|
| 676 | public function testCoroutineOtherwiseIntegrationTest()
|
|---|
| 677 | {
|
|---|
| 678 | $a = new P\Promise();
|
|---|
| 679 | $b = new P\Promise();
|
|---|
| 680 | $promise = P\coroutine(function () use ($a, $b) {
|
|---|
| 681 | // Execute the pool of commands concurrently, and process errors.
|
|---|
| 682 | yield $a;
|
|---|
| 683 | yield $b;
|
|---|
| 684 | })->otherwise(function (\Exception $e) {
|
|---|
| 685 | // Throw errors from the operations as a specific Multipart error.
|
|---|
| 686 | throw new \OutOfBoundsException('a', 0, $e);
|
|---|
| 687 | });
|
|---|
| 688 | $a->resolve('a');
|
|---|
| 689 | $b->reject('b');
|
|---|
| 690 | $reason = P\inspect($promise)['reason'];
|
|---|
| 691 | $this->assertInstanceOf('OutOfBoundsException', $reason);
|
|---|
| 692 | $this->assertInstanceOf('GuzzleHttp\Promise\RejectionException', $reason->getPrevious());
|
|---|
| 693 | }
|
|---|
| 694 | }
|
|---|