CancelTestTrait.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace React\Promise\PromiseTest;
  3. use Exception;
  4. use React\Promise;
  5. use React\Promise\PromiseAdapter\PromiseAdapterInterface;
  6. trait CancelTestTrait
  7. {
  8. abstract public function getPromiseTestAdapter(callable $canceller = null): PromiseAdapterInterface;
  9. /** @test */
  10. public function cancelShouldCallCancellerWithResolverArguments(): void
  11. {
  12. $args = [];
  13. $adapter = $this->getPromiseTestAdapter(function ($resolve, $reject) use (&$args) {
  14. $args = func_get_args();
  15. });
  16. $adapter->promise()->cancel();
  17. self::assertCount(2, $args);
  18. self::assertTrue(is_callable($args[0]));
  19. self::assertTrue(is_callable($args[1]));
  20. }
  21. /** @test */
  22. public function cancelShouldCallCancellerWithoutArgumentsIfNotAccessed(): void
  23. {
  24. $args = null;
  25. $adapter = $this->getPromiseTestAdapter(function () use (&$args) {
  26. $args = func_num_args();
  27. });
  28. $adapter->promise()->cancel();
  29. self::assertSame(0, $args);
  30. }
  31. /** @test */
  32. public function cancelShouldFulfillPromiseIfCancellerFulfills(): void
  33. {
  34. $adapter = $this->getPromiseTestAdapter(function ($resolve) {
  35. $resolve(1);
  36. });
  37. $mock = $this->createCallableMock();
  38. $mock
  39. ->expects($this->once())
  40. ->method('__invoke')
  41. ->with($this->identicalTo(1));
  42. $adapter->promise()
  43. ->then($mock, $this->expectCallableNever());
  44. $adapter->promise()->cancel();
  45. }
  46. /** @test */
  47. public function cancelShouldRejectPromiseIfCancellerRejects(): void
  48. {
  49. $exception = new Exception();
  50. $adapter = $this->getPromiseTestAdapter(function ($resolve, $reject) use ($exception) {
  51. $reject($exception);
  52. });
  53. $mock = $this->createCallableMock();
  54. $mock
  55. ->expects($this->once())
  56. ->method('__invoke')
  57. ->with($this->identicalTo($exception));
  58. $adapter->promise()
  59. ->then($this->expectCallableNever(), $mock);
  60. $adapter->promise()->cancel();
  61. }
  62. /** @test */
  63. public function cancelShouldRejectPromiseWithExceptionIfCancellerThrows(): void
  64. {
  65. $e = new Exception();
  66. $adapter = $this->getPromiseTestAdapter(function () use ($e) {
  67. throw $e;
  68. });
  69. $mock = $this->createCallableMock();
  70. $mock
  71. ->expects($this->once())
  72. ->method('__invoke')
  73. ->with($this->identicalTo($e));
  74. $adapter->promise()
  75. ->then($this->expectCallableNever(), $mock);
  76. $adapter->promise()->cancel();
  77. }
  78. /** @test */
  79. public function cancelShouldCallCancellerOnlyOnceIfCancellerResolves(): void
  80. {
  81. $mock = $this->createCallableMock();
  82. $mock
  83. ->expects($this->once())
  84. ->method('__invoke')
  85. ->will($this->returnCallback(function ($resolve) {
  86. $resolve(null);
  87. }));
  88. $adapter = $this->getPromiseTestAdapter($mock);
  89. $adapter->promise()->cancel();
  90. $adapter->promise()->cancel();
  91. }
  92. /** @test */
  93. public function cancelShouldHaveNoEffectIfCancellerDoesNothing(): void
  94. {
  95. $adapter = $this->getPromiseTestAdapter(function () {});
  96. $adapter->promise()
  97. ->then($this->expectCallableNever(), $this->expectCallableNever());
  98. $adapter->promise()->cancel();
  99. $adapter->promise()->cancel();
  100. }
  101. /** @test */
  102. public function cancelShouldCallCancellerFromDeepNestedPromiseChain(): void
  103. {
  104. $adapter = $this->getPromiseTestAdapter($this->expectCallableOnce());
  105. $promise = $adapter->promise()
  106. ->then(function () {
  107. return new Promise\Promise(function () {});
  108. })
  109. ->then(function () {
  110. $d = new Promise\Deferred();
  111. return $d->promise();
  112. })
  113. ->then(function () {
  114. return new Promise\Promise(function () {});
  115. });
  116. $promise->cancel();
  117. }
  118. /** @test */
  119. public function cancelCalledOnChildrenSouldOnlyCancelWhenAllChildrenCancelled(): void
  120. {
  121. $adapter = $this->getPromiseTestAdapter($this->expectCallableNever());
  122. $child1 = $adapter->promise()
  123. ->then()
  124. ->then();
  125. $adapter->promise()
  126. ->then();
  127. $child1->cancel();
  128. }
  129. /** @test */
  130. public function cancelShouldTriggerCancellerWhenAllChildrenCancel(): void
  131. {
  132. $adapter = $this->getPromiseTestAdapter($this->expectCallableOnce());
  133. $child1 = $adapter->promise()
  134. ->then()
  135. ->then();
  136. $child2 = $adapter->promise()
  137. ->then();
  138. $child1->cancel();
  139. $child2->cancel();
  140. }
  141. /** @test */
  142. public function cancelShouldNotTriggerCancellerWhenCancellingOneChildrenMultipleTimes(): void
  143. {
  144. $adapter = $this->getPromiseTestAdapter($this->expectCallableNever());
  145. $child1 = $adapter->promise()
  146. ->then()
  147. ->then();
  148. $child2 = $adapter->promise()
  149. ->then();
  150. $child1->cancel();
  151. $child1->cancel();
  152. }
  153. /** @test */
  154. public function cancelShouldTriggerCancellerOnlyOnceWhenCancellingMultipleTimes(): void
  155. {
  156. $adapter = $this->getPromiseTestAdapter($this->expectCallableOnce());
  157. $adapter->promise()->cancel();
  158. $adapter->promise()->cancel();
  159. }
  160. /** @test */
  161. public function cancelShouldAlwaysTriggerCancellerWhenCalledOnRootPromise(): void
  162. {
  163. $adapter = $this->getPromiseTestAdapter($this->expectCallableOnce());
  164. $adapter->promise()
  165. ->then()
  166. ->then();
  167. $adapter->promise()
  168. ->then();
  169. $adapter->promise()->cancel();
  170. }
  171. /** @test */
  172. public function cancelShouldTriggerCancellerWhenFollowerCancels(): void
  173. {
  174. $adapter1 = $this->getPromiseTestAdapter($this->expectCallableOnce());
  175. $root = $adapter1->promise();
  176. $adapter2 = $this->getPromiseTestAdapter($this->expectCallableOnce());
  177. $follower = $adapter2->promise();
  178. $adapter2->resolve($root);
  179. $follower->cancel();
  180. }
  181. /** @test */
  182. public function cancelShouldNotTriggerCancellerWhenCancellingOnlyOneFollower(): void
  183. {
  184. $adapter1 = $this->getPromiseTestAdapter($this->expectCallableNever());
  185. $root = $adapter1->promise();
  186. $adapter2 = $this->getPromiseTestAdapter($this->expectCallableOnce());
  187. $follower1 = $adapter2->promise();
  188. $adapter2->resolve($root);
  189. $adapter3 = $this->getPromiseTestAdapter($this->expectCallableNever());
  190. $adapter3->resolve($root);
  191. $follower1->cancel();
  192. }
  193. /** @test */
  194. public function cancelCalledOnFollowerShouldOnlyCancelWhenAllChildrenAndFollowerCancelled(): void
  195. {
  196. $adapter1 = $this->getPromiseTestAdapter($this->expectCallableOnce());
  197. $root = $adapter1->promise();
  198. $child = $root->then();
  199. $adapter2 = $this->getPromiseTestAdapter($this->expectCallableOnce());
  200. $follower = $adapter2->promise();
  201. $adapter2->resolve($root);
  202. $follower->cancel();
  203. $child->cancel();
  204. }
  205. /** @test */
  206. public function cancelShouldNotTriggerCancellerWhenCancellingFollowerButNotChildren(): void
  207. {
  208. $adapter1 = $this->getPromiseTestAdapter($this->expectCallableNever());
  209. $root = $adapter1->promise();
  210. $root->then();
  211. $adapter2 = $this->getPromiseTestAdapter($this->expectCallableOnce());
  212. $follower = $adapter2->promise();
  213. $adapter2->resolve($root);
  214. $follower->cancel();
  215. }
  216. }