DeferredTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace React\Promise;
  3. use React\Promise\PromiseAdapter\CallbackPromiseAdapter;
  4. class DeferredTest extends TestCase
  5. {
  6. use PromiseTest\FullTestTrait;
  7. public function getPromiseTestAdapter(callable $canceller = null)
  8. {
  9. $d = new Deferred($canceller);
  10. return new CallbackPromiseAdapter([
  11. 'promise' => [$d, 'promise'],
  12. 'resolve' => [$d, 'resolve'],
  13. 'reject' => [$d, 'reject'],
  14. 'notify' => [$d, 'progress'],
  15. 'settle' => [$d, 'resolve'],
  16. ]);
  17. }
  18. /** @test */
  19. public function progressIsAnAliasForNotify()
  20. {
  21. $deferred = new Deferred();
  22. $sentinel = new \stdClass();
  23. $mock = $this->createCallableMock();
  24. $mock
  25. ->expects($this->once())
  26. ->method('__invoke')
  27. ->with($sentinel);
  28. $deferred->promise()
  29. ->then($this->expectCallableNever(), $this->expectCallableNever(), $mock);
  30. $deferred->progress($sentinel);
  31. }
  32. /** @test */
  33. public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerRejectsWithException()
  34. {
  35. gc_collect_cycles();
  36. $deferred = new Deferred(function ($resolve, $reject) {
  37. $reject(new \Exception('foo'));
  38. });
  39. $deferred->promise()->cancel();
  40. unset($deferred);
  41. $this->assertSame(0, gc_collect_cycles());
  42. }
  43. /** @test */
  44. public function shouldRejectWithoutCreatingGarbageCyclesIfParentCancellerRejectsWithException()
  45. {
  46. gc_collect_cycles();
  47. $deferred = new Deferred(function ($resolve, $reject) {
  48. $reject(new \Exception('foo'));
  49. });
  50. $deferred->promise()->then()->cancel();
  51. unset($deferred);
  52. $this->assertSame(0, gc_collect_cycles());
  53. }
  54. /** @test */
  55. public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerHoldsReferenceAndExplicitlyRejectWithException()
  56. {
  57. gc_collect_cycles();
  58. $deferred = new Deferred(function () use (&$deferred) { });
  59. $deferred->reject(new \Exception('foo'));
  60. unset($deferred);
  61. $this->assertSame(0, gc_collect_cycles());
  62. }
  63. /** @test */
  64. public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingDeferred()
  65. {
  66. gc_collect_cycles();
  67. $deferred = new Deferred();
  68. $deferred->promise();
  69. unset($deferred);
  70. $this->assertSame(0, gc_collect_cycles());
  71. }
  72. /** @test */
  73. public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingDeferredWithUnusedCanceller()
  74. {
  75. gc_collect_cycles();
  76. $deferred = new Deferred(function () { });
  77. $deferred->promise();
  78. unset($deferred);
  79. $this->assertSame(0, gc_collect_cycles());
  80. }
  81. /** @test */
  82. public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingDeferredWithNoopCanceller()
  83. {
  84. gc_collect_cycles();
  85. $deferred = new Deferred(function () { });
  86. $deferred->promise()->cancel();
  87. unset($deferred);
  88. $this->assertSame(0, gc_collect_cycles());
  89. }
  90. }