LazyPromiseTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace React\Promise;
  3. use React\Promise\PromiseAdapter\CallbackPromiseAdapter;
  4. class LazyPromiseTest extends TestCase
  5. {
  6. use PromiseTest\FullTestTrait;
  7. public function getPromiseTestAdapter(callable $canceller = null)
  8. {
  9. $d = new Deferred($canceller);
  10. $factory = function () use ($d) {
  11. return $d->promise();
  12. };
  13. return new CallbackPromiseAdapter([
  14. 'promise' => function () use ($factory) {
  15. return new LazyPromise($factory);
  16. },
  17. 'resolve' => [$d, 'resolve'],
  18. 'reject' => [$d, 'reject'],
  19. 'notify' => [$d, 'progress'],
  20. 'settle' => [$d, 'resolve'],
  21. ]);
  22. }
  23. /** @test */
  24. public function shouldNotCallFactoryIfThenIsNotInvoked()
  25. {
  26. $factory = $this->createCallableMock();
  27. $factory
  28. ->expects($this->never())
  29. ->method('__invoke');
  30. new LazyPromise($factory);
  31. }
  32. /** @test */
  33. public function shouldCallFactoryIfThenIsInvoked()
  34. {
  35. $factory = $this->createCallableMock();
  36. $factory
  37. ->expects($this->once())
  38. ->method('__invoke');
  39. $p = new LazyPromise($factory);
  40. $p->then();
  41. }
  42. /** @test */
  43. public function shouldReturnPromiseFromFactory()
  44. {
  45. $factory = $this->createCallableMock();
  46. $factory
  47. ->expects($this->once())
  48. ->method('__invoke')
  49. ->will($this->returnValue(new FulfilledPromise(1)));
  50. $onFulfilled = $this->createCallableMock();
  51. $onFulfilled
  52. ->expects($this->once())
  53. ->method('__invoke')
  54. ->with($this->identicalTo(1));
  55. $p = new LazyPromise($factory);
  56. $p->then($onFulfilled);
  57. }
  58. /** @test */
  59. public function shouldReturnPromiseIfFactoryReturnsNull()
  60. {
  61. $factory = $this->createCallableMock();
  62. $factory
  63. ->expects($this->once())
  64. ->method('__invoke')
  65. ->will($this->returnValue(null));
  66. $p = new LazyPromise($factory);
  67. $this->assertInstanceOf('React\\Promise\\PromiseInterface', $p->then());
  68. }
  69. /** @test */
  70. public function shouldReturnRejectedPromiseIfFactoryThrowsException()
  71. {
  72. $exception = new \Exception();
  73. $factory = $this->createCallableMock();
  74. $factory
  75. ->expects($this->once())
  76. ->method('__invoke')
  77. ->will($this->throwException($exception));
  78. $onRejected = $this->createCallableMock();
  79. $onRejected
  80. ->expects($this->once())
  81. ->method('__invoke')
  82. ->with($this->identicalTo($exception));
  83. $p = new LazyPromise($factory);
  84. $p->then($this->expectCallableNever(), $onRejected);
  85. }
  86. }