FulfilledPromiseTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace React\Promise;
  3. use React\Promise\PromiseAdapter\CallbackPromiseAdapter;
  4. class FulfilledPromiseTest extends TestCase
  5. {
  6. use PromiseTest\PromiseSettledTestTrait,
  7. PromiseTest\PromiseFulfilledTestTrait;
  8. public function getPromiseTestAdapter(callable $canceller = null)
  9. {
  10. $promise = null;
  11. return new CallbackPromiseAdapter([
  12. 'promise' => function () use (&$promise) {
  13. if (!$promise) {
  14. throw new \LogicException('FulfilledPromise must be resolved before obtaining the promise');
  15. }
  16. return $promise;
  17. },
  18. 'resolve' => function ($value = null) use (&$promise) {
  19. if (!$promise) {
  20. $promise = new FulfilledPromise($value);
  21. }
  22. },
  23. 'reject' => function () {
  24. throw new \LogicException('You cannot call reject() for React\Promise\FulfilledPromise');
  25. },
  26. 'notify' => function () {
  27. // no-op
  28. },
  29. 'settle' => function ($value = null) use (&$promise) {
  30. if (!$promise) {
  31. $promise = new FulfilledPromise($value);
  32. }
  33. },
  34. ]);
  35. }
  36. /** @test */
  37. public function shouldThrowExceptionIfConstructedWithAPromise()
  38. {
  39. $this->setExpectedException('\InvalidArgumentException');
  40. return new FulfilledPromise(new FulfilledPromise());
  41. }
  42. /** @test */
  43. public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToFulfilledPromiseWithAlwaysFollowers()
  44. {
  45. gc_collect_cycles();
  46. $promise = new FulfilledPromise(1);
  47. $promise->always(function () {
  48. throw new \RuntimeException();
  49. });
  50. unset($promise);
  51. $this->assertSame(0, gc_collect_cycles());
  52. }
  53. /** @test */
  54. public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToFulfilledPromiseWithThenFollowers()
  55. {
  56. gc_collect_cycles();
  57. $promise = new FulfilledPromise(1);
  58. $promise = $promise->then(function () {
  59. throw new \RuntimeException();
  60. });
  61. unset($promise);
  62. $this->assertSame(0, gc_collect_cycles());
  63. }
  64. }