FunctionSomeTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace React\Promise;
  3. use React\Promise\Exception\LengthException;
  4. class FunctionSomeTest 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. some(
  20. [],
  21. 1
  22. )->then($this->expectCallableNever(), $mock);
  23. }
  24. /** @test */
  25. public function shouldRejectWithLengthExceptionWithInputArrayContainingNotEnoughItems()
  26. {
  27. $mock = $this->createCallableMock();
  28. $mock
  29. ->expects($this->once())
  30. ->method('__invoke')
  31. ->with(
  32. $this->callback(function($exception){
  33. return $exception instanceof LengthException &&
  34. 'Input array must contain at least 4 items but contains only 3 items.' === $exception->getMessage();
  35. })
  36. );
  37. some(
  38. [1, 2, 3],
  39. 4
  40. )->then($this->expectCallableNever(), $mock);
  41. }
  42. /** @test */
  43. public function shouldResolveToEmptyArrayWithNonArrayInput()
  44. {
  45. $mock = $this->createCallableMock();
  46. $mock
  47. ->expects($this->once())
  48. ->method('__invoke')
  49. ->with($this->identicalTo([]));
  50. some(
  51. null,
  52. 1
  53. )->then($mock);
  54. }
  55. /** @test */
  56. public function shouldResolveValuesArray()
  57. {
  58. $mock = $this->createCallableMock();
  59. $mock
  60. ->expects($this->once())
  61. ->method('__invoke')
  62. ->with($this->identicalTo([1, 2]));
  63. some(
  64. [1, 2, 3],
  65. 2
  66. )->then($mock);
  67. }
  68. /** @test */
  69. public function shouldResolvePromisesArray()
  70. {
  71. $mock = $this->createCallableMock();
  72. $mock
  73. ->expects($this->once())
  74. ->method('__invoke')
  75. ->with($this->identicalTo([1, 2]));
  76. some(
  77. [resolve(1), resolve(2), resolve(3)],
  78. 2
  79. )->then($mock);
  80. }
  81. /** @test */
  82. public function shouldResolveSparseArrayInput()
  83. {
  84. $mock = $this->createCallableMock();
  85. $mock
  86. ->expects($this->once())
  87. ->method('__invoke')
  88. ->with($this->identicalTo([null, 1]));
  89. some(
  90. [null, 1, null, 2, 3],
  91. 2
  92. )->then($mock);
  93. }
  94. /** @test */
  95. public function shouldRejectIfAnyInputPromiseRejectsBeforeDesiredNumberOfInputsAreResolved()
  96. {
  97. $mock = $this->createCallableMock();
  98. $mock
  99. ->expects($this->once())
  100. ->method('__invoke')
  101. ->with($this->identicalTo([1 => 2, 2 => 3]));
  102. some(
  103. [resolve(1), reject(2), reject(3)],
  104. 2
  105. )->then($this->expectCallableNever(), $mock);
  106. }
  107. /** @test */
  108. public function shouldAcceptAPromiseForAnArray()
  109. {
  110. $mock = $this->createCallableMock();
  111. $mock
  112. ->expects($this->once())
  113. ->method('__invoke')
  114. ->with($this->identicalTo([1, 2]));
  115. some(
  116. resolve([1, 2, 3]),
  117. 2
  118. )->then($mock);
  119. }
  120. /** @test */
  121. public function shouldResolveWithEmptyArrayIfHowManyIsLessThanOne()
  122. {
  123. $mock = $this->createCallableMock();
  124. $mock
  125. ->expects($this->once())
  126. ->method('__invoke')
  127. ->with($this->identicalTo([]));
  128. some(
  129. [1],
  130. 0
  131. )->then($mock);
  132. }
  133. /** @test */
  134. public function shouldResolveToEmptyArrayWhenInputPromiseDoesNotResolveToArray()
  135. {
  136. $mock = $this->createCallableMock();
  137. $mock
  138. ->expects($this->once())
  139. ->method('__invoke')
  140. ->with($this->identicalTo([]));
  141. some(
  142. resolve(1),
  143. 1
  144. )->then($mock);
  145. }
  146. /** @test */
  147. public function shouldRejectWhenInputPromiseRejects()
  148. {
  149. $mock = $this->createCallableMock();
  150. $mock
  151. ->expects($this->once())
  152. ->method('__invoke')
  153. ->with($this->identicalTo(null));
  154. some(
  155. reject(),
  156. 1
  157. )->then($this->expectCallableNever(), $mock);
  158. }
  159. /** @test */
  160. public function shouldCancelInputPromise()
  161. {
  162. $mock = $this
  163. ->getMockBuilder('React\Promise\CancellablePromiseInterface')
  164. ->getMock();
  165. $mock
  166. ->expects($this->once())
  167. ->method('cancel');
  168. some($mock, 1)->cancel();
  169. }
  170. /** @test */
  171. public function shouldCancelInputArrayPromises()
  172. {
  173. $mock1 = $this
  174. ->getMockBuilder('React\Promise\CancellablePromiseInterface')
  175. ->getMock();
  176. $mock1
  177. ->expects($this->once())
  178. ->method('cancel');
  179. $mock2 = $this
  180. ->getMockBuilder('React\Promise\CancellablePromiseInterface')
  181. ->getMock();
  182. $mock2
  183. ->expects($this->once())
  184. ->method('cancel');
  185. some([$mock1, $mock2], 1)->cancel();
  186. }
  187. /** @test */
  188. public function shouldNotCancelOtherPendingInputArrayPromisesIfEnoughPromisesFulfill()
  189. {
  190. $mock = $this->createCallableMock();
  191. $mock
  192. ->expects($this->never())
  193. ->method('__invoke');
  194. $deferred = New Deferred($mock);
  195. $deferred->resolve();
  196. $mock2 = $this
  197. ->getMockBuilder('React\Promise\CancellablePromiseInterface')
  198. ->getMock();
  199. $mock2
  200. ->expects($this->never())
  201. ->method('cancel');
  202. some([$deferred->promise(), $mock2], 1);
  203. }
  204. /** @test */
  205. public function shouldNotCancelOtherPendingInputArrayPromisesIfEnoughPromisesReject()
  206. {
  207. $mock = $this->createCallableMock();
  208. $mock
  209. ->expects($this->never())
  210. ->method('__invoke');
  211. $deferred = New Deferred($mock);
  212. $deferred->reject();
  213. $mock2 = $this
  214. ->getMockBuilder('React\Promise\CancellablePromiseInterface')
  215. ->getMock();
  216. $mock2
  217. ->expects($this->never())
  218. ->method('cancel');
  219. some([$deferred->promise(), $mock2], 2);
  220. }
  221. }