FunctionAnyTest.php 5.2 KB

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