PromiseTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. namespace React\Promise;
  3. use Exception;
  4. use React\Promise\PromiseAdapter\CallbackPromiseAdapter;
  5. /**
  6. * @template T
  7. */
  8. class PromiseTest extends TestCase
  9. {
  10. use PromiseTest\FullTestTrait;
  11. /**
  12. * @return CallbackPromiseAdapter<T>
  13. */
  14. public function getPromiseTestAdapter(callable $canceller = null): CallbackPromiseAdapter
  15. {
  16. $resolveCallback = $rejectCallback = null;
  17. $promise = new Promise(function (callable $resolve, callable $reject) use (&$resolveCallback, &$rejectCallback): void {
  18. $resolveCallback = $resolve;
  19. $rejectCallback = $reject;
  20. }, $canceller);
  21. assert(is_callable($resolveCallback));
  22. assert(is_callable($rejectCallback));
  23. return new CallbackPromiseAdapter([
  24. 'promise' => function () use ($promise) {
  25. return $promise;
  26. },
  27. 'resolve' => $resolveCallback,
  28. 'reject' => $rejectCallback,
  29. 'settle' => $resolveCallback,
  30. ]);
  31. }
  32. /** @test */
  33. public function shouldRejectIfResolverThrowsException(): void
  34. {
  35. $exception = new Exception('foo');
  36. $promise = new Promise(function () use ($exception) {
  37. throw $exception;
  38. });
  39. $mock = $this->createCallableMock();
  40. $mock
  41. ->expects(self::once())
  42. ->method('__invoke')
  43. ->with(self::identicalTo($exception));
  44. $promise
  45. ->then($this->expectCallableNever(), $mock);
  46. }
  47. /** @test */
  48. public function shouldResolveWithoutCreatingGarbageCyclesIfResolverResolvesWithException(): void
  49. {
  50. gc_collect_cycles();
  51. $promise = new Promise(function ($resolve) {
  52. $resolve(new \Exception('foo'));
  53. });
  54. unset($promise);
  55. $this->assertSame(0, gc_collect_cycles());
  56. }
  57. /** @test */
  58. public function shouldRejectWithoutCreatingGarbageCyclesIfResolverThrowsExceptionWithoutResolver(): void
  59. {
  60. gc_collect_cycles();
  61. $promise = new Promise(function () {
  62. throw new \Exception('foo');
  63. });
  64. $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
  65. unset($promise);
  66. $this->assertSame(0, gc_collect_cycles());
  67. }
  68. /** @test */
  69. public function shouldRejectWithoutCreatingGarbageCyclesIfResolverRejectsWithException(): void
  70. {
  71. gc_collect_cycles();
  72. $promise = new Promise(function ($resolve, $reject) {
  73. $reject(new \Exception('foo'));
  74. });
  75. $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
  76. unset($promise);
  77. $this->assertSame(0, gc_collect_cycles());
  78. }
  79. /** @test */
  80. public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerRejectsWithException(): void
  81. {
  82. gc_collect_cycles();
  83. $promise = new Promise(function ($resolve, $reject) { }, function ($resolve, $reject) {
  84. $reject(new \Exception('foo'));
  85. });
  86. $promise->cancel();
  87. unset($promise);
  88. $this->assertSame(0, gc_collect_cycles());
  89. }
  90. /** @test */
  91. public function shouldRejectWithoutCreatingGarbageCyclesIfParentCancellerRejectsWithException(): void
  92. {
  93. gc_collect_cycles();
  94. $promise = new Promise(function ($resolve, $reject) { }, function ($resolve, $reject) {
  95. $reject(new \Exception('foo'));
  96. });
  97. $promise->then()->then()->then()->cancel();
  98. unset($promise);
  99. $this->assertSame(0, gc_collect_cycles());
  100. }
  101. /** @test */
  102. public function shouldRejectWithoutCreatingGarbageCyclesIfResolverThrowsException(): void
  103. {
  104. gc_collect_cycles();
  105. $promise = new Promise(function ($resolve, $reject) {
  106. throw new \Exception('foo');
  107. });
  108. $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
  109. unset($promise);
  110. $this->assertSame(0, gc_collect_cycles());
  111. }
  112. /**
  113. * Test that checks number of garbage cycles after throwing from a canceller
  114. * that explicitly uses a reference to the promise. This is rather synthetic,
  115. * actual use cases often have implicit (hidden) references which ought not
  116. * to be stored in the stack trace.
  117. *
  118. * Reassigned arguments only show up in the stack trace in PHP 7, so we can't
  119. * avoid this on legacy PHP. As an alternative, consider explicitly unsetting
  120. * any references before throwing.
  121. *
  122. * @test
  123. * @requires PHP 7
  124. */
  125. public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerWithReferenceThrowsException(): void
  126. {
  127. gc_collect_cycles();
  128. /** @var Promise<never> $promise */
  129. $promise = new Promise(function () {}, function () use (&$promise) {
  130. assert($promise instanceof Promise);
  131. throw new \Exception('foo');
  132. });
  133. $promise->cancel();
  134. unset($promise);
  135. $this->assertSame(0, gc_collect_cycles());
  136. }
  137. /**
  138. * @test
  139. * @requires PHP 7
  140. * @see self::shouldRejectWithoutCreatingGarbageCyclesIfCancellerWithReferenceThrowsException
  141. */
  142. public function shouldRejectWithoutCreatingGarbageCyclesIfResolverWithReferenceThrowsException(): void
  143. {
  144. gc_collect_cycles();
  145. /** @var Promise<never> $promise */
  146. $promise = new Promise(function () use (&$promise) {
  147. assert($promise instanceof Promise);
  148. throw new \Exception('foo');
  149. });
  150. $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
  151. unset($promise);
  152. $this->assertSame(0, gc_collect_cycles());
  153. }
  154. /**
  155. * @test
  156. * @requires PHP 7
  157. * @see self::shouldRejectWithoutCreatingGarbageCyclesIfCancellerWithReferenceThrowsException
  158. */
  159. public function shouldRejectWithoutCreatingGarbageCyclesIfCancellerHoldsReferenceAndResolverThrowsException(): void
  160. {
  161. gc_collect_cycles();
  162. /** @var Promise<never> $promise */
  163. $promise = new Promise(function () {
  164. throw new \Exception('foo');
  165. }, function () use (&$promise) {
  166. assert($promise instanceof Promise);
  167. });
  168. $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
  169. unset($promise);
  170. $this->assertSame(0, gc_collect_cycles());
  171. }
  172. /** @test */
  173. public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromise(): void
  174. {
  175. gc_collect_cycles();
  176. $promise = new Promise(function () { });
  177. unset($promise);
  178. $this->assertSame(0, gc_collect_cycles());
  179. }
  180. /** @test */
  181. public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromiseWithThenFollowers(): void
  182. {
  183. gc_collect_cycles();
  184. $promise = new Promise(function () { });
  185. $promise->then()->then()->then();
  186. unset($promise);
  187. $this->assertSame(0, gc_collect_cycles());
  188. }
  189. /** @test */
  190. public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromiseWithCatchFollowers(): void
  191. {
  192. gc_collect_cycles();
  193. $promise = new Promise(function () { });
  194. $promise->catch(function () { });
  195. unset($promise);
  196. $this->assertSame(0, gc_collect_cycles());
  197. }
  198. /** @test */
  199. public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromiseWithFinallyFollowers(): void
  200. {
  201. gc_collect_cycles();
  202. $promise = new Promise(function () { });
  203. $promise->finally(function () { });
  204. unset($promise);
  205. $this->assertSame(0, gc_collect_cycles());
  206. }
  207. /**
  208. * @test
  209. * @deprecated
  210. */
  211. public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromiseWithOtherwiseFollowers(): void
  212. {
  213. gc_collect_cycles();
  214. $promise = new Promise(function () { });
  215. $promise->otherwise(function () { });
  216. unset($promise);
  217. $this->assertSame(0, gc_collect_cycles());
  218. }
  219. /**
  220. * @test
  221. * @deprecated
  222. */
  223. public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToPendingPromiseWithAlwaysFollowers(): void
  224. {
  225. gc_collect_cycles();
  226. $promise = new Promise(function () { });
  227. $promise->always(function () { });
  228. unset($promise);
  229. $this->assertSame(0, gc_collect_cycles());
  230. }
  231. /** @test */
  232. public function shouldFulfillIfFullfilledWithSimplePromise(): void
  233. {
  234. gc_collect_cycles();
  235. $promise = new Promise(function () {
  236. throw new Exception('foo');
  237. });
  238. $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
  239. unset($promise);
  240. self::assertSame(0, gc_collect_cycles());
  241. }
  242. }