|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace React\Tests\Async; |
| 4 | + |
| 5 | +use React; |
| 6 | +use React\EventLoop\Loop; |
| 7 | +use React\Promise\Promise; |
| 8 | + |
| 9 | +class AwaitTest extends TestCase |
| 10 | +{ |
| 11 | + public function testAwaitThrowsExceptionWhenPromiseIsRejectedWithException() |
| 12 | + { |
| 13 | + $promise = new Promise(function () { |
| 14 | + throw new \Exception('test'); |
| 15 | + }); |
| 16 | + |
| 17 | + $this->setExpectedException('Exception', 'test'); |
| 18 | + React\Async\await($promise); |
| 19 | + } |
| 20 | + |
| 21 | + public function testAwaitThrowsUnexpectedValueExceptionWhenPromiseIsRejectedWithFalse() |
| 22 | + { |
| 23 | + if (!interface_exists('React\Promise\CancellablePromiseInterface')) { |
| 24 | + $this->markTestSkipped('Promises must be rejected with a \Throwable instance since Promise v3'); |
| 25 | + } |
| 26 | + |
| 27 | + $promise = new Promise(function ($_, $reject) { |
| 28 | + $reject(false); |
| 29 | + }); |
| 30 | + |
| 31 | + $this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type bool'); |
| 32 | + React\Async\await($promise); |
| 33 | + } |
| 34 | + |
| 35 | + public function testAwaitThrowsUnexpectedValueExceptionWhenPromiseIsRejectedWithNull() |
| 36 | + { |
| 37 | + if (!interface_exists('React\Promise\CancellablePromiseInterface')) { |
| 38 | + $this->markTestSkipped('Promises must be rejected with a \Throwable instance since Promise v3'); |
| 39 | + } |
| 40 | + |
| 41 | + $promise = new Promise(function ($_, $reject) { |
| 42 | + $reject(null); |
| 43 | + }); |
| 44 | + |
| 45 | + $this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type NULL'); |
| 46 | + React\Async\await($promise); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @requires PHP 7 |
| 51 | + */ |
| 52 | + public function testAwaitThrowsErrorWhenPromiseIsRejectedWithError() |
| 53 | + { |
| 54 | + $promise = new Promise(function ($_, $reject) { |
| 55 | + throw new \Error('Test', 42); |
| 56 | + }); |
| 57 | + |
| 58 | + $this->setExpectedException('Error', 'Test', 42); |
| 59 | + React\Async\await($promise); |
| 60 | + } |
| 61 | + |
| 62 | + public function testAwaitReturnsValueWhenPromiseIsFullfilled() |
| 63 | + { |
| 64 | + $promise = new Promise(function ($resolve) { |
| 65 | + $resolve(42); |
| 66 | + }); |
| 67 | + |
| 68 | + $this->assertEquals(42, React\Async\await($promise)); |
| 69 | + } |
| 70 | + |
| 71 | + public function testAwaitReturnsValueWhenPromiseIsFulfilledEvenWhenOtherTimerStopsLoop() |
| 72 | + { |
| 73 | + $promise = new Promise(function ($resolve) { |
| 74 | + Loop::addTimer(0.02, function () use ($resolve) { |
| 75 | + $resolve(2); |
| 76 | + }); |
| 77 | + }); |
| 78 | + Loop::addTimer(0.01, function () { |
| 79 | + Loop::stop(); |
| 80 | + }); |
| 81 | + |
| 82 | + $this->assertEquals(2, React\Async\await($promise)); |
| 83 | + } |
| 84 | + |
| 85 | + public function testAwaitShouldNotCreateAnyGarbageReferencesForResolvedPromise() |
| 86 | + { |
| 87 | + if (class_exists('React\Promise\When') && PHP_VERSION_ID >= 50400) { |
| 88 | + $this->markTestSkipped('Not supported on legacy Promise v1 API with PHP 5.4+'); |
| 89 | + } |
| 90 | + |
| 91 | + gc_collect_cycles(); |
| 92 | + |
| 93 | + $promise = new Promise(function ($resolve) { |
| 94 | + $resolve(42); |
| 95 | + }); |
| 96 | + React\Async\await($promise); |
| 97 | + unset($promise); |
| 98 | + |
| 99 | + $this->assertEquals(0, gc_collect_cycles()); |
| 100 | + } |
| 101 | + |
| 102 | + public function testAwaitShouldNotCreateAnyGarbageReferencesForRejectedPromise() |
| 103 | + { |
| 104 | + if (class_exists('React\Promise\When')) { |
| 105 | + $this->markTestSkipped('Not supported on legacy Promise v1 API'); |
| 106 | + } |
| 107 | + |
| 108 | + gc_collect_cycles(); |
| 109 | + |
| 110 | + $promise = new Promise(function () { |
| 111 | + throw new \RuntimeException(); |
| 112 | + }); |
| 113 | + try { |
| 114 | + React\Async\await($promise); |
| 115 | + } catch (\Exception $e) { |
| 116 | + // no-op |
| 117 | + } |
| 118 | + unset($promise, $e); |
| 119 | + |
| 120 | + $this->assertEquals(0, gc_collect_cycles()); |
| 121 | + } |
| 122 | + |
| 123 | + public function testAwaitShouldNotCreateAnyGarbageReferencesForPromiseRejectedWithNullValue() |
| 124 | + { |
| 125 | + if (!interface_exists('React\Promise\CancellablePromiseInterface')) { |
| 126 | + $this->markTestSkipped('Promises must be rejected with a \Throwable instance since Promise v3'); |
| 127 | + } |
| 128 | + |
| 129 | + if (class_exists('React\Promise\When') && PHP_VERSION_ID >= 50400) { |
| 130 | + $this->markTestSkipped('Not supported on legacy Promise v1 API with PHP 5.4+'); |
| 131 | + } |
| 132 | + |
| 133 | + gc_collect_cycles(); |
| 134 | + |
| 135 | + $promise = new Promise(function ($_, $reject) { |
| 136 | + $reject(null); |
| 137 | + }); |
| 138 | + try { |
| 139 | + React\Async\await($promise); |
| 140 | + } catch (\Exception $e) { |
| 141 | + // no-op |
| 142 | + } |
| 143 | + unset($promise, $e); |
| 144 | + |
| 145 | + $this->assertEquals(0, gc_collect_cycles()); |
| 146 | + } |
| 147 | + |
| 148 | + public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null) |
| 149 | + { |
| 150 | + if (method_exists($this, 'expectException')) { |
| 151 | + // PHPUnit 5+ |
| 152 | + $this->expectException($exception); |
| 153 | + if ($exceptionMessage !== '') { |
| 154 | + $this->expectExceptionMessage($exceptionMessage); |
| 155 | + } |
| 156 | + if ($exceptionCode !== null) { |
| 157 | + $this->expectExceptionCode($exceptionCode); |
| 158 | + } |
| 159 | + } else { |
| 160 | + // legacy PHPUnit 4 |
| 161 | + parent::setExpectedException($exception, $exceptionMessage, $exceptionCode); |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments