FunctionMapTest.php 4.6 KB

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