1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482: 483: 484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495: 496: 497: 498: 499: 500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 534: 535: 536: 537: 538: 539: 540: 541: 542: 543: 544: 545: 546: 547: 548: 549: 550: 551: 552: 553: 554: 555: 556: 557: 558: 559: 560: 561: 562: 563: 564: 565: 566: 567: 568: 569: 570: 571: 572: 573: 574: 575: 576: 577: 578: 579: 580: 581: 582: 583: 584: 585: 586: 587: 588: 589: 590: 591: 592:
<?php
namespace GuzzleHttp\Promise\Tests;
use GuzzleHttp\Promise\CancellationException;
use GuzzleHttp\Promise as P;
use GuzzleHttp\Promise\Promise;
use GuzzleHttp\Promise\RejectedPromise;
use GuzzleHttp\Promise\RejectionException;
class PromiseTest extends \PHPUnit_Framework_TestCase
{
public function testCannotResolveNonPendingPromise()
{
$p = new Promise();
$p->resolve('foo');
$p->resolve('bar');
$this->assertEquals('foo', $p->wait());
}
public function testCanResolveWithSameValue()
{
$p = new Promise();
$p->resolve('foo');
$p->resolve('foo');
}
public function testCannotRejectNonPendingPromise()
{
$p = new Promise();
$p->resolve('foo');
$p->reject('bar');
$this->assertEquals('foo', $p->wait());
}
public function testCanRejectWithSameValue()
{
$p = new Promise();
$p->reject('foo');
$p->reject('foo');
}
public function testCannotRejectResolveWithSameValue()
{
$p = new Promise();
$p->resolve('foo');
$p->reject('foo');
}
public function testInvokesWaitFunction()
{
$p = new Promise(function () use (&$p) { $p->resolve('10'); });
$this->assertEquals('10', $p->wait());
}
public function testRejectsAndThrowsWhenWaitFailsToResolve()
{
$p = new Promise(function () {});
$p->wait();
}
public function testThrowsWhenUnwrapIsRejectedWithNonException()
{
$p = new Promise(function () use (&$p) { $p->reject('foo'); });
$p->wait();
}
public function testThrowsWhenUnwrapIsRejectedWithException()
{
$e = new \UnexpectedValueException('foo');
$p = new Promise(function () use (&$p, $e) { $p->reject($e); });
$p->wait();
}
public function testDoesNotUnwrapExceptionsWhenDisabled()
{
$p = new Promise(function () use (&$p) { $p->reject('foo'); });
$this->assertEquals('pending', $p->getState());
$p->wait(false);
$this->assertEquals('rejected', $p->getState());
}
public function testRejectsSelfWhenWaitThrows()
{
$e = new \UnexpectedValueException('foo');
$p = new Promise(function () use ($e) { throw $e; });
try {
$p->wait();
$this->fail();
} catch (\UnexpectedValueException $e) {
$this->assertEquals('rejected', $p->getState());
}
}
public function testWaitsOnNestedPromises()
{
$p = new Promise(function () use (&$p) { $p->resolve('_'); });
$p2 = new Promise(function () use (&$p2) { $p2->resolve('foo'); });
$p3 = $p->then(function () use ($p2) { return $p2; });
$this->assertSame('foo', $p3->wait());
}
public function testThrowsWhenWaitingOnPromiseWithNoWaitFunction()
{
$p = new Promise();
$p->wait();
}
public function testThrowsWaitExceptionAfterPromiseIsResolved()
{
$p = new Promise(function () use (&$p) {
$p->reject('Foo!');
throw new \Exception('Bar?');
});
try {
$p->wait();
$this->fail();
} catch (\Exception $e) {
$this->assertEquals('Bar?', $e->getMessage());
}
}
public function testGetsActualWaitValueFromThen()
{
$p = new Promise(function () use (&$p) { $p->reject('Foo!'); });
$p2 = $p->then(null, function ($reason) {
return new RejectedPromise([$reason]);
});
try {
$p2->wait();
$this->fail('Should have thrown');
} catch (RejectionException $e) {
$this->assertEquals(['Foo!'], $e->getReason());
}
}
public function testWaitBehaviorIsBasedOnLastPromiseInChain()
{
$p3 = new Promise(function () use (&$p3) { $p3->resolve('Whoop'); });
$p2 = new Promise(function () use (&$p2, $p3) { $p2->reject($p3); });
$p = new Promise(function () use (&$p, $p2) { $p->reject($p2); });
$this->assertEquals('Whoop', $p->wait());
}
public function testWaitsOnAPromiseChainEvenWhenNotUnwrapped()
{
$p2 = new Promise(function () use (&$p2) {
$p2->reject('Fail');
});
$p = new Promise(function () use ($p2, &$p) {
$p->resolve($p2);
});
$p->wait(false);
$this->assertSame(Promise::REJECTED, $p2->getState());
}
public function testCannotCancelNonPending()
{
$p = new Promise();
$p->resolve('foo');
$p->cancel();
$this->assertEquals('fulfilled', $p->getState());
}
public function testCancelsPromiseWhenNoCancelFunction()
{
$p = new Promise();
$p->cancel();
$this->assertEquals('rejected', $p->getState());
$p->wait();
}
public function testCancelsPromiseWithCancelFunction()
{
$called = false;
$p = new Promise(null, function () use (&$called) { $called = true; });
$p->cancel();
$this->assertEquals('rejected', $p->getState());
$this->assertTrue($called);
}
public function testCancelsUppermostPendingPromise()
{
$called = false;
$p1 = new Promise(null, function () use (&$called) { $called = true; });
$p2 = $p1->then(function () {});
$p3 = $p2->then(function () {});
$p4 = $p3->then(function () {});
$p3->cancel();
$this->assertEquals('rejected', $p1->getState());
$this->assertEquals('rejected', $p2->getState());
$this->assertEquals('rejected', $p3->getState());
$this->assertEquals('pending', $p4->getState());
$this->assertTrue($called);
try {
$p3->wait();
$this->fail();
} catch (CancellationException $e) {
$this->assertContains('cancelled', $e->getMessage());
}
try {
$p4->wait();
$this->fail();
} catch (CancellationException $e) {
$this->assertContains('cancelled', $e->getMessage());
}
$this->assertEquals('rejected', $p4->getState());
}
public function testCancelsChildPromises()
{
$called1 = $called2 = $called3 = false;
$p1 = new Promise(null, function () use (&$called1) { $called1 = true; });
$p2 = new Promise(null, function () use (&$called2) { $called2 = true; });
$p3 = new Promise(null, function () use (&$called3) { $called3 = true; });
$p4 = $p2->then(function () use ($p3) { return $p3; });
$p5 = $p4->then(function () { $this->fail(); });
$p4->cancel();
$this->assertEquals('pending', $p1->getState());
$this->assertEquals('rejected', $p2->getState());
$this->assertEquals('rejected', $p4->getState());
$this->assertEquals('pending', $p5->getState());
$this->assertFalse($called1);
$this->assertTrue($called2);
$this->assertFalse($called3);
}
public function testRejectsPromiseWhenCancelFails()
{
$called = false;
$p = new Promise(null, function () use (&$called) {
$called = true;
throw new \Exception('e');
});
$p->cancel();
$this->assertEquals('rejected', $p->getState());
$this->assertTrue($called);
try {
$p->wait();
$this->fail();
} catch (\Exception $e) {
$this->assertEquals('e', $e->getMessage());
}
}
public function testCreatesPromiseWhenFulfilledAfterThen()
{
$p = new Promise();
$carry = null;
$p2 = $p->then(function ($v) use (&$carry) { $carry = $v; });
$this->assertNotSame($p, $p2);
$p->resolve('foo');
P\queue()->run();
$this->assertEquals('foo', $carry);
}
public function testCreatesPromiseWhenFulfilledBeforeThen()
{
$p = new Promise();
$p->resolve('foo');
$carry = null;
$p2 = $p->then(function ($v) use (&$carry) { $carry = $v; });
$this->assertNotSame($p, $p2);
$this->assertNull($carry);
\GuzzleHttp\Promise\queue()->run();
$this->assertEquals('foo', $carry);
}
public function testCreatesPromiseWhenFulfilledWithNoCallback()
{
$p = new Promise();
$p->resolve('foo');
$p2 = $p->then();
$this->assertNotSame($p, $p2);
$this->assertInstanceOf('GuzzleHttp\Promise\FulfilledPromise', $p2);
}
public function testCreatesPromiseWhenRejectedAfterThen()
{
$p = new Promise();
$carry = null;
$p2 = $p->then(null, function ($v) use (&$carry) { $carry = $v; });
$this->assertNotSame($p, $p2);
$p->reject('foo');
P\queue()->run();
$this->assertEquals('foo', $carry);
}
public function testCreatesPromiseWhenRejectedBeforeThen()
{
$p = new Promise();
$p->reject('foo');
$carry = null;
$p2 = $p->then(null, function ($v) use (&$carry) { $carry = $v; });
$this->assertNotSame($p, $p2);
$this->assertNull($carry);
P\queue()->run();
$this->assertEquals('foo', $carry);
}
public function testCreatesPromiseWhenRejectedWithNoCallback()
{
$p = new Promise();
$p->reject('foo');
$p2 = $p->then();
$this->assertNotSame($p, $p2);
$this->assertInstanceOf('GuzzleHttp\Promise\RejectedPromise', $p2);
}
public function testInvokesWaitFnsForThens()
{
$p = new Promise(function () use (&$p) { $p->resolve('a'); });
$p2 = $p
->then(function ($v) { return $v . '-1-'; })
->then(function ($v) { return $v . '2'; });
$this->assertEquals('a-1-2', $p2->wait());
}
public function testStacksThenWaitFunctions()
{
$p1 = new Promise(function () use (&$p1) { $p1->resolve('a'); });
$p2 = new Promise(function () use (&$p2) { $p2->resolve('b'); });
$p3 = new Promise(function () use (&$p3) { $p3->resolve('c'); });
$p4 = $p1
->then(function () use ($p2) { return $p2; })
->then(function () use ($p3) { return $p3; });
$this->assertEquals('c', $p4->wait());
}
public function testForwardsFulfilledDownChainBetweenGaps()
{
$p = new Promise();
$r = $r2 = null;
$p->then(null, null)
->then(function ($v) use (&$r) { $r = $v; return $v . '2'; })
->then(function ($v) use (&$r2) { $r2 = $v; });
$p->resolve('foo');
P\queue()->run();
$this->assertEquals('foo', $r);
$this->assertEquals('foo2', $r2);
}
public function testForwardsRejectedPromisesDownChainBetweenGaps()
{
$p = new Promise();
$r = $r2 = null;
$p->then(null, null)
->then(null, function ($v) use (&$r) { $r = $v; return $v . '2'; })
->then(function ($v) use (&$r2) { $r2 = $v; });
$p->reject('foo');
P\queue()->run();
$this->assertEquals('foo', $r);
$this->assertEquals('foo2', $r2);
}
public function testForwardsThrownPromisesDownChainBetweenGaps()
{
$e = new \Exception();
$p = new Promise();
$r = $r2 = null;
$p->then(null, null)
->then(null, function ($v) use (&$r, $e) {
$r = $v;
throw $e;
})
->then(
null,
function ($v) use (&$r2) { $r2 = $v; }
);
$p->reject('foo');
P\queue()->run();
$this->assertEquals('foo', $r);
$this->assertSame($e, $r2);
}
public function testForwardsReturnedRejectedPromisesDownChainBetweenGaps()
{
$p = new Promise();
$rejected = new RejectedPromise('bar');
$r = $r2 = null;
$p->then(null, null)
->then(null, function ($v) use (&$r, $rejected) {
$r = $v;
return $rejected;
})
->then(
null,
function ($v) use (&$r2) { $r2 = $v; }
);
$p->reject('foo');
P\queue()->run();
$this->assertEquals('foo', $r);
$this->assertEquals('bar', $r2);
try {
$p->wait();
} catch (RejectionException $e) {
$this->assertEquals('foo', $e->getReason());
}
}
public function testForwardsHandlersToNextPromise()
{
$p = new Promise();
$p2 = new Promise();
$resolved = null;
$p
->then(function ($v) use ($p2) { return $p2; })
->then(function ($value) use (&$resolved) { $resolved = $value; });
$p->resolve('a');
$p2->resolve('b');
P\queue()->run();
$this->assertEquals('b', $resolved);
}
public function testRemovesReferenceFromChildWhenParentWaitedUpon()
{
$r = null;
$p = new Promise(function () use (&$p) { $p->resolve('a'); });
$p2 = new Promise(function () use (&$p2) { $p2->resolve('b'); });
$pb = $p->then(
function ($v) use ($p2, &$r) {
$r = $v;
return $p2;
})
->then(function ($v) { return $v . '.'; });
$this->assertEquals('a', $p->wait());
$this->assertEquals('b', $p2->wait());
$this->assertEquals('b.', $pb->wait());
$this->assertEquals('a', $r);
}
public function testForwardsHandlersWhenFulfilledPromiseIsReturned()
{
$res = [];
$p = new Promise();
$p2 = new Promise();
$p2->resolve('foo');
$p2->then(function ($v) use (&$res) { $res[] = 'A:' . $v; });
$p
->then(function () use ($p2, &$res) { $res[] = 'B'; return $p2; })
->then(function ($v) use (&$res) { $res[] = 'C:' . $v; });
$p->resolve('a');
$p->then(function ($v) use (&$res) { $res[] = 'D:' . $v; });
P\queue()->run();
$this->assertEquals(['A:foo', 'B', 'D:a', 'C:foo'], $res);
}
public function testForwardsHandlersWhenRejectedPromiseIsReturned()
{
$res = [];
$p = new Promise();
$p2 = new Promise();
$p2->reject('foo');
$p2->then(null, function ($v) use (&$res) { $res[] = 'A:' . $v; });
$p->then(null, function () use ($p2, &$res) { $res[] = 'B'; return $p2; })
->then(null, function ($v) use (&$res) { $res[] = 'C:' . $v; });
$p->reject('a');
$p->then(null, function ($v) use (&$res) { $res[] = 'D:' . $v; });
P\queue()->run();
$this->assertEquals(['A:foo', 'B', 'D:a', 'C:foo'], $res);
}
public function testDoesNotForwardRejectedPromise()
{
$res = [];
$p = new Promise();
$p2 = new Promise();
$p2->cancel();
$p2->then(function ($v) use (&$res) { $res[] = "B:$v"; return $v; });
$p->then(function ($v) use ($p2, &$res) { $res[] = "B:$v"; return $p2; })
->then(function ($v) use (&$res) { $res[] = 'C:' . $v; });
$p->resolve('a');
$p->then(function ($v) use (&$res) { $res[] = 'D:' . $v; });
P\queue()->run();
$this->assertEquals(['B:a', 'D:a'], $res);
}
public function testRecursivelyForwardsWhenOnlyThennable()
{
$res = [];
$p = new Promise();
$p2 = new Thennable();
$p2->resolve('foo');
$p2->then(function ($v) use (&$res) { $res[] = 'A:' . $v; });
$p->then(function () use ($p2, &$res) { $res[] = 'B'; return $p2; })
->then(function ($v) use (&$res) { $res[] = 'C:' . $v; });
$p->resolve('a');
$p->then(function ($v) use (&$res) { $res[] = 'D:' . $v; });
P\queue()->run();
$this->assertEquals(['A:foo', 'B', 'D:a', 'C:foo'], $res);
}
public function testRecursivelyForwardsWhenNotInstanceOfPromise()
{
$res = [];
$p = new Promise();
$p2 = new NotPromiseInstance();
$p2->then(function ($v) use (&$res) { $res[] = 'A:' . $v; });
$p->then(function () use ($p2, &$res) { $res[] = 'B'; return $p2; })
->then(function ($v) use (&$res) { $res[] = 'C:' . $v; });
$p->resolve('a');
$p->then(function ($v) use (&$res) { $res[] = 'D:' . $v; });
P\queue()->run();
$this->assertEquals(['B', 'D:a'], $res);
$p2->resolve('foo');
P\queue()->run();
$this->assertEquals(['B', 'D:a', 'A:foo', 'C:foo'], $res);
}
public function testCannotResolveWithSelf()
{
$p = new Promise();
$p->resolve($p);
}
public function testCannotRejectWithSelf()
{
$p = new Promise();
$p->reject($p);
}
public function testDoesNotBlowStackWhenWaitingOnNestedThens()
{
$inner = new Promise(function () use (&$inner) { $inner->resolve(0); });
$prev = $inner;
for ($i = 1; $i < 100; $i++) {
$prev = $prev->then(function ($i) { return $i + 1; });
}
$parent = new Promise(function () use (&$parent, $prev) {
$parent->resolve($prev);
});
$this->assertEquals(99, $parent->wait());
}
public function testOtherwiseIsSugarForRejections()
{
$p = new Promise();
$p->reject('foo');
$p->otherwise(function ($v) use (&$c) { $c = $v; });
P\queue()->run();
$this->assertEquals($c, 'foo');
}
}