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