| 1 | <?php
|
|---|
| 2 | namespace GuzzleHttp\Tests\Promise;
|
|---|
| 3 |
|
|---|
| 4 | use GuzzleHttp\Promise\Promise;
|
|---|
| 5 | use GuzzleHttp\Promise\FulfilledPromise;
|
|---|
| 6 |
|
|---|
| 7 | /**
|
|---|
| 8 | * @covers GuzzleHttp\Promise\FulfilledPromise
|
|---|
| 9 | */
|
|---|
| 10 | class FulfilledPromiseTest extends \PHPUnit_Framework_TestCase
|
|---|
| 11 | {
|
|---|
| 12 | public function testReturnsValueWhenWaitedUpon()
|
|---|
| 13 | {
|
|---|
| 14 | $p = new FulfilledPromise('foo');
|
|---|
| 15 | $this->assertEquals('fulfilled', $p->getState());
|
|---|
| 16 | $this->assertEquals('foo', $p->wait(true));
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | public function testCannotCancel()
|
|---|
| 20 | {
|
|---|
| 21 | $p = new FulfilledPromise('foo');
|
|---|
| 22 | $this->assertEquals('fulfilled', $p->getState());
|
|---|
| 23 | $p->cancel();
|
|---|
| 24 | $this->assertEquals('foo', $p->wait());
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * @expectedException \LogicException
|
|---|
| 29 | * @exepctedExceptionMessage Cannot resolve a fulfilled promise
|
|---|
| 30 | */
|
|---|
| 31 | public function testCannotResolve()
|
|---|
| 32 | {
|
|---|
| 33 | $p = new FulfilledPromise('foo');
|
|---|
| 34 | $p->resolve('bar');
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | /**
|
|---|
| 38 | * @expectedException \LogicException
|
|---|
| 39 | * @exepctedExceptionMessage Cannot reject a fulfilled promise
|
|---|
| 40 | */
|
|---|
| 41 | public function testCannotReject()
|
|---|
| 42 | {
|
|---|
| 43 | $p = new FulfilledPromise('foo');
|
|---|
| 44 | $p->reject('bar');
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | public function testCanResolveWithSameValue()
|
|---|
| 48 | {
|
|---|
| 49 | $p = new FulfilledPromise('foo');
|
|---|
| 50 | $p->resolve('foo');
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | /**
|
|---|
| 54 | * @expectedException \InvalidArgumentException
|
|---|
| 55 | */
|
|---|
| 56 | public function testCannotResolveWithPromise()
|
|---|
| 57 | {
|
|---|
| 58 | new FulfilledPromise(new Promise());
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | public function testReturnsSelfWhenNoOnFulfilled()
|
|---|
| 62 | {
|
|---|
| 63 | $p = new FulfilledPromise('a');
|
|---|
| 64 | $this->assertSame($p, $p->then());
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | public function testAsynchronouslyInvokesOnFulfilled()
|
|---|
| 68 | {
|
|---|
| 69 | $p = new FulfilledPromise('a');
|
|---|
| 70 | $r = null;
|
|---|
| 71 | $f = function ($d) use (&$r) { $r = $d; };
|
|---|
| 72 | $p2 = $p->then($f);
|
|---|
| 73 | $this->assertNotSame($p, $p2);
|
|---|
| 74 | $this->assertNull($r);
|
|---|
| 75 | \GuzzleHttp\Promise\queue()->run();
|
|---|
| 76 | $this->assertEquals('a', $r);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | public function testReturnsNewRejectedWhenOnFulfilledFails()
|
|---|
| 80 | {
|
|---|
| 81 | $p = new FulfilledPromise('a');
|
|---|
| 82 | $f = function () { throw new \Exception('b'); };
|
|---|
| 83 | $p2 = $p->then($f);
|
|---|
| 84 | $this->assertNotSame($p, $p2);
|
|---|
| 85 | try {
|
|---|
| 86 | $p2->wait();
|
|---|
| 87 | $this->fail();
|
|---|
| 88 | } catch (\Exception $e) {
|
|---|
| 89 | $this->assertEquals('b', $e->getMessage());
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | public function testOtherwiseIsSugarForRejections()
|
|---|
| 94 | {
|
|---|
| 95 | $c = null;
|
|---|
| 96 | $p = new FulfilledPromise('foo');
|
|---|
| 97 | $p->otherwise(function ($v) use (&$c) { $c = $v; });
|
|---|
| 98 | $this->assertNull($c);
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | public function testDoesNotTryToFulfillTwiceDuringTrampoline()
|
|---|
| 102 | {
|
|---|
| 103 | $fp = new FulfilledPromise('a');
|
|---|
| 104 | $t1 = $fp->then(function ($v) { return $v . ' b'; });
|
|---|
| 105 | $t1->resolve('why!');
|
|---|
| 106 | $this->assertEquals('why!', $t1->wait());
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|