FunctionRaceTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace React\Promise;
  3. class FunctionRaceTest extends TestCase
  4. {
  5. /** @test */
  6. public function shouldResolveEmptyInput()
  7. {
  8. $mock = $this->createCallableMock();
  9. $mock
  10. ->expects($this->once())
  11. ->method('__invoke')
  12. ->with($this->identicalTo(null));
  13. race(
  14. []
  15. )->then($mock);
  16. }
  17. /** @test */
  18. public function shouldResolveValuesArray()
  19. {
  20. $mock = $this->createCallableMock();
  21. $mock
  22. ->expects($this->once())
  23. ->method('__invoke')
  24. ->with($this->identicalTo(1));
  25. race(
  26. [1, 2, 3]
  27. )->then($mock);
  28. }
  29. /** @test */
  30. public function shouldResolvePromisesArray()
  31. {
  32. $mock = $this->createCallableMock();
  33. $mock
  34. ->expects($this->once())
  35. ->method('__invoke')
  36. ->with($this->identicalTo(2));
  37. $d1 = new Deferred();
  38. $d2 = new Deferred();
  39. $d3 = new Deferred();
  40. race(
  41. [$d1->promise(), $d2->promise(), $d3->promise()]
  42. )->then($mock);
  43. $d2->resolve(2);
  44. $d1->resolve(1);
  45. $d3->resolve(3);
  46. }
  47. /** @test */
  48. public function shouldResolveSparseArrayInput()
  49. {
  50. $mock = $this->createCallableMock();
  51. $mock
  52. ->expects($this->once())
  53. ->method('__invoke')
  54. ->with($this->identicalTo(null));
  55. race(
  56. [null, 1, null, 2, 3]
  57. )->then($mock);
  58. }
  59. /** @test */
  60. public function shouldRejectIfFirstSettledPromiseRejects()
  61. {
  62. $mock = $this->createCallableMock();
  63. $mock
  64. ->expects($this->once())
  65. ->method('__invoke')
  66. ->with($this->identicalTo(2));
  67. $d1 = new Deferred();
  68. $d2 = new Deferred();
  69. $d3 = new Deferred();
  70. race(
  71. [$d1->promise(), $d2->promise(), $d3->promise()]
  72. )->then($this->expectCallableNever(), $mock);
  73. $d2->reject(2);
  74. $d1->resolve(1);
  75. $d3->resolve(3);
  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. race(
  86. resolve([1, 2, 3])
  87. )->then($mock);
  88. }
  89. /** @test */
  90. public function shouldResolveToNullWhenInputPromiseDoesNotResolveToArray()
  91. {
  92. $mock = $this->createCallableMock();
  93. $mock
  94. ->expects($this->once())
  95. ->method('__invoke')
  96. ->with($this->identicalTo(null));
  97. race(
  98. resolve(1)
  99. )->then($mock);
  100. }
  101. /** @test */
  102. public function shouldRejectWhenInputPromiseRejects()
  103. {
  104. $mock = $this->createCallableMock();
  105. $mock
  106. ->expects($this->once())
  107. ->method('__invoke')
  108. ->with($this->identicalTo(null));
  109. race(
  110. reject()
  111. )->then($this->expectCallableNever(), $mock);
  112. }
  113. /** @test */
  114. public function shouldCancelInputPromise()
  115. {
  116. $mock = $this
  117. ->getMockBuilder('React\Promise\CancellablePromiseInterface')
  118. ->getMock();
  119. $mock
  120. ->expects($this->once())
  121. ->method('cancel');
  122. race($mock)->cancel();
  123. }
  124. /** @test */
  125. public function shouldCancelInputArrayPromises()
  126. {
  127. $mock1 = $this
  128. ->getMockBuilder('React\Promise\CancellablePromiseInterface')
  129. ->getMock();
  130. $mock1
  131. ->expects($this->once())
  132. ->method('cancel');
  133. $mock2 = $this
  134. ->getMockBuilder('React\Promise\CancellablePromiseInterface')
  135. ->getMock();
  136. $mock2
  137. ->expects($this->once())
  138. ->method('cancel');
  139. race([$mock1, $mock2])->cancel();
  140. }
  141. /** @test */
  142. public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseFulfills()
  143. {
  144. $mock = $this->createCallableMock();
  145. $mock
  146. ->expects($this->never())
  147. ->method('__invoke');
  148. $deferred = New Deferred($mock);
  149. $deferred->resolve();
  150. $mock2 = $this
  151. ->getMockBuilder('React\Promise\CancellablePromiseInterface')
  152. ->getMock();
  153. $mock2
  154. ->expects($this->never())
  155. ->method('cancel');
  156. race([$deferred->promise(), $mock2])->cancel();
  157. }
  158. /** @test */
  159. public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseRejects()
  160. {
  161. $mock = $this->createCallableMock();
  162. $mock
  163. ->expects($this->never())
  164. ->method('__invoke');
  165. $deferred = New Deferred($mock);
  166. $deferred->reject();
  167. $mock2 = $this
  168. ->getMockBuilder('React\Promise\CancellablePromiseInterface')
  169. ->getMock();
  170. $mock2
  171. ->expects($this->never())
  172. ->method('cancel');
  173. race([$deferred->promise(), $mock2])->cancel();
  174. }
  175. }