FulfilledPromiseTest.php 3.0 KB

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