RejectedPromiseTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace React\Promise;
  3. use React\Promise\PromiseAdapter\CallbackPromiseAdapter;
  4. class RejectedPromiseTest extends TestCase
  5. {
  6. use PromiseTest\PromiseSettledTestTrait,
  7. PromiseTest\PromiseRejectedTestTrait;
  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('RejectedPromise must be rejected before obtaining the promise');
  15. }
  16. return $promise;
  17. },
  18. 'resolve' => function () {
  19. throw new \LogicException('You cannot call resolve() for React\Promise\RejectedPromise');
  20. },
  21. 'reject' => function ($reason = null) use (&$promise) {
  22. if (!$promise) {
  23. $promise = new RejectedPromise($reason);
  24. }
  25. },
  26. 'notify' => function () {
  27. // no-op
  28. },
  29. 'settle' => function ($reason = null) use (&$promise) {
  30. if (!$promise) {
  31. $promise = new RejectedPromise($reason);
  32. }
  33. },
  34. ]);
  35. }
  36. /** @test */
  37. public function shouldThrowExceptionIfConstructedWithAPromise()
  38. {
  39. $this->setExpectedException('\InvalidArgumentException');
  40. return new RejectedPromise(new RejectedPromise());
  41. }
  42. /** @test */
  43. public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToRejectedPromiseWithAlwaysFollowers()
  44. {
  45. gc_collect_cycles();
  46. $promise = new RejectedPromise(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 shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToRejectedPromiseWithThenFollowers()
  55. {
  56. gc_collect_cycles();
  57. $promise = new RejectedPromise(1);
  58. $promise = $promise->then(null, function () {
  59. throw new \RuntimeException();
  60. });
  61. unset($promise);
  62. $this->assertSame(0, gc_collect_cycles());
  63. }
  64. }