RejectedPromiseTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace GuzzleHttp\Promise\Tests;
  3. use GuzzleHttp\Promise\Promise;
  4. use GuzzleHttp\Promise\RejectedPromise;
  5. use PHPUnit\Framework\TestCase;
  6. /**
  7. * @covers GuzzleHttp\Promise\RejectedPromise
  8. */
  9. class RejectedPromiseTest extends TestCase
  10. {
  11. public function testThrowsReasonWhenWaitedUpon()
  12. {
  13. $p = new RejectedPromise('foo');
  14. $this->assertEquals('rejected', $p->getState());
  15. try {
  16. $p->wait(true);
  17. $this->fail();
  18. } catch (\Exception $e) {
  19. $this->assertEquals('rejected', $p->getState());
  20. $this->assertContains('foo', $e->getMessage());
  21. }
  22. }
  23. public function testCannotCancel()
  24. {
  25. $p = new RejectedPromise('foo');
  26. $p->cancel();
  27. $this->assertEquals('rejected', $p->getState());
  28. }
  29. /**
  30. * @expectedException \LogicException
  31. * @exepctedExceptionMessage Cannot resolve a rejected promise
  32. */
  33. public function testCannotResolve()
  34. {
  35. $p = new RejectedPromise('foo');
  36. $p->resolve('bar');
  37. }
  38. /**
  39. * @expectedException \LogicException
  40. * @exepctedExceptionMessage Cannot reject a rejected promise
  41. */
  42. public function testCannotReject()
  43. {
  44. $p = new RejectedPromise('foo');
  45. $p->reject('bar');
  46. }
  47. public function testCanRejectWithSameValue()
  48. {
  49. $p = new RejectedPromise('foo');
  50. $p->reject('foo');
  51. }
  52. public function testThrowsSpecificException()
  53. {
  54. $e = new \Exception();
  55. $p = new RejectedPromise($e);
  56. try {
  57. $p->wait(true);
  58. $this->fail();
  59. } catch (\Exception $e2) {
  60. $this->assertSame($e, $e2);
  61. }
  62. }
  63. /**
  64. * @expectedException \InvalidArgumentException
  65. */
  66. public function testCannotResolveWithPromise()
  67. {
  68. new RejectedPromise(new Promise());
  69. }
  70. public function testReturnsSelfWhenNoOnReject()
  71. {
  72. $p = new RejectedPromise('a');
  73. $this->assertSame($p, $p->then());
  74. }
  75. public function testInvokesOnRejectedAsynchronously()
  76. {
  77. $p = new RejectedPromise('a');
  78. $r = null;
  79. $f = function ($reason) use (&$r) { $r = $reason; };
  80. $p->then(null, $f);
  81. $this->assertNull($r);
  82. \GuzzleHttp\Promise\queue()->run();
  83. $this->assertEquals('a', $r);
  84. }
  85. public function testReturnsNewRejectedWhenOnRejectedFails()
  86. {
  87. $p = new RejectedPromise('a');
  88. $f = function () { throw new \Exception('b'); };
  89. $p2 = $p->then(null, $f);
  90. $this->assertNotSame($p, $p2);
  91. try {
  92. $p2->wait();
  93. $this->fail();
  94. } catch (\Exception $e) {
  95. $this->assertEquals('b', $e->getMessage());
  96. }
  97. }
  98. public function testWaitingIsNoOp()
  99. {
  100. $p = new RejectedPromise('a');
  101. $p->wait(false);
  102. }
  103. public function testOtherwiseIsSugarForRejections()
  104. {
  105. $p = new RejectedPromise('foo');
  106. $p->otherwise(function ($v) use (&$c) { $c = $v; });
  107. \GuzzleHttp\Promise\queue()->run();
  108. $this->assertSame('foo', $c);
  109. }
  110. public function testCanResolveThenWithSuccess()
  111. {
  112. $actual = null;
  113. $p = new RejectedPromise('foo');
  114. $p->otherwise(function ($v) {
  115. return $v . ' bar';
  116. })->then(function ($v) use (&$actual) {
  117. $actual = $v;
  118. });
  119. \GuzzleHttp\Promise\queue()->run();
  120. $this->assertEquals('foo bar', $actual);
  121. }
  122. public function testDoesNotTryToRejectTwiceDuringTrampoline()
  123. {
  124. $fp = new RejectedPromise('a');
  125. $t1 = $fp->then(null, function ($v) { return $v . ' b'; });
  126. $t1->resolve('why!');
  127. $this->assertEquals('why!', $t1->wait());
  128. }
  129. }