| [01a620f] | 1 | <?php
|
|---|
| 2 | namespace GuzzleHttp\Promise\Tests;
|
|---|
| 3 |
|
|---|
| 4 | use GuzzleHttp\Promise\Promise;
|
|---|
| 5 | use GuzzleHttp\Promise\RejectedPromise;
|
|---|
| 6 |
|
|---|
| 7 | /**
|
|---|
| 8 | * @covers GuzzleHttp\Promise\RejectedPromise
|
|---|
| 9 | */
|
|---|
| 10 | class RejectedPromiseTest extends \PHPUnit_Framework_TestCase
|
|---|
| 11 | {
|
|---|
| 12 | public function testThrowsReasonWhenWaitedUpon()
|
|---|
| 13 | {
|
|---|
| 14 | $p = new RejectedPromise('foo');
|
|---|
| 15 | $this->assertEquals('rejected', $p->getState());
|
|---|
| 16 | try {
|
|---|
| 17 | $p->wait(true);
|
|---|
| 18 | $this->fail();
|
|---|
| 19 | } catch (\Exception $e) {
|
|---|
| 20 | $this->assertEquals('rejected', $p->getState());
|
|---|
| 21 | $this->assertContains('foo', $e->getMessage());
|
|---|
| 22 | }
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | public function testCannotCancel()
|
|---|
| 26 | {
|
|---|
| 27 | $p = new RejectedPromise('foo');
|
|---|
| 28 | $p->cancel();
|
|---|
| 29 | $this->assertEquals('rejected', $p->getState());
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * @expectedException \LogicException
|
|---|
| 34 | * @exepctedExceptionMessage Cannot resolve a rejected promise
|
|---|
| 35 | */
|
|---|
| 36 | public function testCannotResolve()
|
|---|
| 37 | {
|
|---|
| 38 | $p = new RejectedPromise('foo');
|
|---|
| 39 | $p->resolve('bar');
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | /**
|
|---|
| 43 | * @expectedException \LogicException
|
|---|
| 44 | * @exepctedExceptionMessage Cannot reject a rejected promise
|
|---|
| 45 | */
|
|---|
| 46 | public function testCannotReject()
|
|---|
| 47 | {
|
|---|
| 48 | $p = new RejectedPromise('foo');
|
|---|
| 49 | $p->reject('bar');
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | public function testCanRejectWithSameValue()
|
|---|
| 53 | {
|
|---|
| 54 | $p = new RejectedPromise('foo');
|
|---|
| 55 | $p->reject('foo');
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | public function testThrowsSpecificException()
|
|---|
| 59 | {
|
|---|
| 60 | $e = new \Exception();
|
|---|
| 61 | $p = new RejectedPromise($e);
|
|---|
| 62 | try {
|
|---|
| 63 | $p->wait(true);
|
|---|
| 64 | $this->fail();
|
|---|
| 65 | } catch (\Exception $e2) {
|
|---|
| 66 | $this->assertSame($e, $e2);
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | /**
|
|---|
| 71 | * @expectedException \InvalidArgumentException
|
|---|
| 72 | */
|
|---|
| 73 | public function testCannotResolveWithPromise()
|
|---|
| 74 | {
|
|---|
| 75 | new RejectedPromise(new Promise());
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | public function testReturnsSelfWhenNoOnReject()
|
|---|
| 79 | {
|
|---|
| 80 | $p = new RejectedPromise('a');
|
|---|
| 81 | $this->assertSame($p, $p->then());
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | public function testInvokesOnRejectedAsynchronously()
|
|---|
| 85 | {
|
|---|
| 86 | $p = new RejectedPromise('a');
|
|---|
| 87 | $r = null;
|
|---|
| 88 | $f = function ($reason) use (&$r) { $r = $reason; };
|
|---|
| 89 | $p->then(null, $f);
|
|---|
| 90 | $this->assertNull($r);
|
|---|
| 91 | \GuzzleHttp\Promise\queue()->run();
|
|---|
| 92 | $this->assertEquals('a', $r);
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | public function testReturnsNewRejectedWhenOnRejectedFails()
|
|---|
| 96 | {
|
|---|
| 97 | $p = new RejectedPromise('a');
|
|---|
| 98 | $f = function () { throw new \Exception('b'); };
|
|---|
| 99 | $p2 = $p->then(null, $f);
|
|---|
| 100 | $this->assertNotSame($p, $p2);
|
|---|
| 101 | try {
|
|---|
| 102 | $p2->wait();
|
|---|
| 103 | $this->fail();
|
|---|
| 104 | } catch (\Exception $e) {
|
|---|
| 105 | $this->assertEquals('b', $e->getMessage());
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | public function testWaitingIsNoOp()
|
|---|
| 110 | {
|
|---|
| 111 | $p = new RejectedPromise('a');
|
|---|
| 112 | $p->wait(false);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | public function testOtherwiseIsSugarForRejections()
|
|---|
| 116 | {
|
|---|
| 117 | $p = new RejectedPromise('foo');
|
|---|
| 118 | $p->otherwise(function ($v) use (&$c) { $c = $v; });
|
|---|
| 119 | \GuzzleHttp\Promise\queue()->run();
|
|---|
| 120 | $this->assertSame('foo', $c);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | public function testCanResolveThenWithSuccess()
|
|---|
| 124 | {
|
|---|
| 125 | $actual = null;
|
|---|
| 126 | $p = new RejectedPromise('foo');
|
|---|
| 127 | $p->otherwise(function ($v) {
|
|---|
| 128 | return $v . ' bar';
|
|---|
| 129 | })->then(function ($v) use (&$actual) {
|
|---|
| 130 | $actual = $v;
|
|---|
| 131 | });
|
|---|
| 132 | \GuzzleHttp\Promise\queue()->run();
|
|---|
| 133 | $this->assertEquals('foo bar', $actual);
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | public function testDoesNotTryToRejectTwiceDuringTrampoline()
|
|---|
| 137 | {
|
|---|
| 138 | $fp = new RejectedPromise('a');
|
|---|
| 139 | $t1 = $fp->then(null, function ($v) { return $v . ' b'; });
|
|---|
| 140 | $t1->resolve('why!');
|
|---|
| 141 | $this->assertEquals('why!', $t1->wait());
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|