FulfilledPromiseTest.php 2.8 KB

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