PromiseTest.php 22 KB

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