FunctionAllTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace React\Promise;
  3. use Exception;
  4. class FunctionAllTest extends TestCase
  5. {
  6. /** @test */
  7. public function shouldResolveEmptyInput(): void
  8. {
  9. $mock = $this->createCallableMock();
  10. $mock
  11. ->expects(self::once())
  12. ->method('__invoke')
  13. ->with(self::identicalTo([]));
  14. all([])
  15. ->then($mock);
  16. }
  17. /** @test */
  18. public function shouldResolveValuesArray(): void
  19. {
  20. $mock = $this->createCallableMock();
  21. $mock
  22. ->expects(self::once())
  23. ->method('__invoke')
  24. ->with(self::identicalTo([1, 2, 3]));
  25. all([1, 2, 3])
  26. ->then($mock);
  27. }
  28. /** @test */
  29. public function shouldResolvePromisesArray(): void
  30. {
  31. $mock = $this->createCallableMock();
  32. $mock
  33. ->expects(self::once())
  34. ->method('__invoke')
  35. ->with(self::identicalTo([1, 2, 3]));
  36. all([resolve(1), resolve(2), resolve(3)])
  37. ->then($mock);
  38. }
  39. /** @test */
  40. public function shouldResolveSparseArrayInput(): void
  41. {
  42. $mock = $this->createCallableMock();
  43. $mock
  44. ->expects(self::once())
  45. ->method('__invoke')
  46. ->with(self::identicalTo([null, 1, null, 1, 1]));
  47. all([null, 1, null, 1, 1])
  48. ->then($mock);
  49. }
  50. /** @test */
  51. public function shouldResolveValuesGenerator(): void
  52. {
  53. $mock = $this->createCallableMock();
  54. $mock
  55. ->expects(self::once())
  56. ->method('__invoke')
  57. ->with(self::identicalTo([1, 2, 3]));
  58. $gen = (function () {
  59. for ($i = 1; $i <= 3; ++$i) {
  60. yield $i;
  61. }
  62. })();
  63. all($gen)->then($mock);
  64. }
  65. /** @test */
  66. public function shouldResolveValuesGeneratorEmpty(): void
  67. {
  68. $mock = $this->createCallableMock();
  69. $mock
  70. ->expects(self::once())
  71. ->method('__invoke')
  72. ->with(self::identicalTo([]));
  73. $gen = (function () {
  74. if (false) { // @phpstan-ignore-line
  75. yield;
  76. }
  77. })();
  78. all($gen)->then($mock);
  79. }
  80. /** @test */
  81. public function shouldRejectIfAnyInputPromiseRejects(): void
  82. {
  83. $exception2 = new Exception();
  84. $exception3 = new Exception();
  85. $mock = $this->createCallableMock();
  86. $mock
  87. ->expects(self::once())
  88. ->method('__invoke')
  89. ->with(self::identicalTo($exception2));
  90. all([resolve(1), reject($exception2), reject($exception3)])
  91. ->then($this->expectCallableNever(), $mock);
  92. }
  93. /** @test */
  94. public function shouldRejectInfiteGeneratorOrRejectedPromises(): void
  95. {
  96. $mock = $this->createCallableMock();
  97. $mock
  98. ->expects(self::once())
  99. ->method('__invoke')
  100. ->with(new \RuntimeException('Iteration 1'));
  101. $gen = (function () {
  102. for ($i = 1; ; ++$i) {
  103. yield reject(new \RuntimeException('Iteration ' . $i));
  104. }
  105. })();
  106. all($gen)->then(null, $mock);
  107. }
  108. /** @test */
  109. public function shouldPreserveTheOrderOfArrayWhenResolvingAsyncPromises(): void
  110. {
  111. $mock = $this->createCallableMock();
  112. $mock
  113. ->expects(self::once())
  114. ->method('__invoke')
  115. ->with(self::identicalTo([1, 2, 3]));
  116. $deferred = new Deferred();
  117. all([resolve(1), $deferred->promise(), resolve(3)])
  118. ->then($mock);
  119. $deferred->resolve(2);
  120. }
  121. }