FunctionAnyTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace React\Promise;
  3. use Exception;
  4. use React\Promise\Exception\CompositeException;
  5. use React\Promise\Exception\LengthException;
  6. class FunctionAnyTest extends TestCase
  7. {
  8. /** @test */
  9. public function shouldRejectWithLengthExceptionWithEmptyInputArray(): void
  10. {
  11. $mock = $this->createCallableMock();
  12. $mock
  13. ->expects(self::once())
  14. ->method('__invoke')
  15. ->with(
  16. self::callback(function ($exception) {
  17. return $exception instanceof LengthException &&
  18. 'Must contain at least 1 item but contains only 0 items.' === $exception->getMessage();
  19. })
  20. );
  21. any([])
  22. ->then($this->expectCallableNever(), $mock);
  23. }
  24. /** @test */
  25. public function shouldRejectWithLengthExceptionWithEmptyInputGenerator(): void
  26. {
  27. $mock = $this->createCallableMock();
  28. $mock
  29. ->expects(self::once())
  30. ->method('__invoke')
  31. ->with(new LengthException('Must contain at least 1 item but contains only 0 items.'));
  32. $gen = (function () {
  33. if (false) { // @phpstan-ignore-line
  34. yield;
  35. }
  36. })();
  37. any($gen)->then($this->expectCallableNever(), $mock);
  38. }
  39. /** @test */
  40. public function shouldResolveWithAnInputValue(): void
  41. {
  42. $mock = $this->createCallableMock();
  43. $mock
  44. ->expects(self::once())
  45. ->method('__invoke')
  46. ->with(self::identicalTo(1));
  47. any([1, 2, 3])
  48. ->then($mock);
  49. }
  50. /** @test */
  51. public function shouldResolveWithAPromisedInputValue(): void
  52. {
  53. $mock = $this->createCallableMock();
  54. $mock
  55. ->expects(self::once())
  56. ->method('__invoke')
  57. ->with(self::identicalTo(1));
  58. any([resolve(1), resolve(2), resolve(3)])
  59. ->then($mock);
  60. }
  61. /** @test */
  62. public function shouldResolveWithAnInputValueFromDeferred(): void
  63. {
  64. $mock = $this->createCallableMock();
  65. $mock
  66. ->expects(self::once())
  67. ->method('__invoke')
  68. ->with(self::identicalTo(1));
  69. $deferred = new Deferred();
  70. any([$deferred->promise()])->then($mock);
  71. $deferred->resolve(1);
  72. }
  73. /** @test */
  74. public function shouldResolveValuesGenerator(): void
  75. {
  76. $mock = $this->createCallableMock();
  77. $mock
  78. ->expects(self::once())
  79. ->method('__invoke')
  80. ->with(self::identicalTo(1));
  81. $gen = (function () {
  82. for ($i = 1; $i <= 3; ++$i) {
  83. yield $i;
  84. }
  85. })();
  86. any($gen)->then($mock);
  87. }
  88. /** @test */
  89. public function shouldResolveValuesInfiniteGenerator(): void
  90. {
  91. $mock = $this->createCallableMock();
  92. $mock
  93. ->expects(self::once())
  94. ->method('__invoke')
  95. ->with(self::identicalTo(1));
  96. $gen = (function () {
  97. for ($i = 1; ; ++$i) {
  98. yield $i;
  99. }
  100. })();
  101. any($gen)->then($mock);
  102. }
  103. /** @test */
  104. public function shouldRejectWithAllRejectedInputValuesIfAllInputsAreRejected(): void
  105. {
  106. $exception1 = new Exception();
  107. $exception2 = new Exception();
  108. $exception3 = new Exception();
  109. $compositeException = new CompositeException(
  110. [0 => $exception1, 1 => $exception2, 2 => $exception3],
  111. 'All promises rejected.'
  112. );
  113. $mock = $this->createCallableMock();
  114. $mock
  115. ->expects(self::once())
  116. ->method('__invoke')
  117. ->with($compositeException);
  118. any([reject($exception1), reject($exception2), reject($exception3)])
  119. ->then($this->expectCallableNever(), $mock);
  120. }
  121. /** @test */
  122. public function shouldRejectWithAllRejectedInputValuesIfInputIsRejectedFromDeferred(): void
  123. {
  124. $exception = new Exception();
  125. $compositeException = new CompositeException(
  126. [2 => $exception],
  127. 'All promises rejected.'
  128. );
  129. $mock = $this->createCallableMock();
  130. $mock
  131. ->expects(self::once())
  132. ->method('__invoke')
  133. ->with($compositeException);
  134. $deferred = new Deferred();
  135. any([2 => $deferred->promise()])->then($this->expectCallableNever(), $mock);
  136. $deferred->reject($exception);
  137. }
  138. /** @test */
  139. public function shouldResolveWhenFirstInputPromiseResolves(): void
  140. {
  141. $exception2 = new Exception();
  142. $exception3 = new Exception();
  143. $mock = $this->createCallableMock();
  144. $mock
  145. ->expects(self::once())
  146. ->method('__invoke')
  147. ->with(self::identicalTo(1));
  148. any([resolve(1), reject($exception2), reject($exception3)])
  149. ->then($mock);
  150. }
  151. /** @test */
  152. public function shouldNotRelyOnArryIndexesWhenUnwrappingToASingleResolutionValue(): void
  153. {
  154. $mock = $this->createCallableMock();
  155. $mock
  156. ->expects(self::once())
  157. ->method('__invoke')
  158. ->with(self::identicalTo(2));
  159. $d1 = new Deferred();
  160. $d2 = new Deferred();
  161. any(['abc' => $d1->promise(), 1 => $d2->promise()])
  162. ->then($mock);
  163. $d2->resolve(2);
  164. $d1->resolve(1);
  165. }
  166. /** @test */
  167. public function shouldCancelInputArrayPromises(): void
  168. {
  169. $promise1 = new Promise(function () {}, $this->expectCallableOnce());
  170. $promise2 = new Promise(function () {}, $this->expectCallableOnce());
  171. any([$promise1, $promise2])->cancel();
  172. }
  173. /** @test */
  174. public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseFulfills(): void
  175. {
  176. $deferred = new Deferred($this->expectCallableNever());
  177. $deferred->resolve(null);
  178. $promise2 = new Promise(function () {}, $this->expectCallableNever());
  179. any([$deferred->promise(), $promise2])->cancel();
  180. }
  181. }