FunctionAllTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace React\Promise;
  3. class FunctionAllTest 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([]));
  13. all([])
  14. ->then($mock);
  15. }
  16. /** @test */
  17. public function shouldResolveValuesArray()
  18. {
  19. $mock = $this->createCallableMock();
  20. $mock
  21. ->expects($this->once())
  22. ->method('__invoke')
  23. ->with($this->identicalTo([1, 2, 3]));
  24. all([1, 2, 3])
  25. ->then($mock);
  26. }
  27. /** @test */
  28. public function shouldResolvePromisesArray()
  29. {
  30. $mock = $this->createCallableMock();
  31. $mock
  32. ->expects($this->once())
  33. ->method('__invoke')
  34. ->with($this->identicalTo([1, 2, 3]));
  35. all([resolve(1), resolve(2), resolve(3)])
  36. ->then($mock);
  37. }
  38. /** @test */
  39. public function shouldResolveSparseArrayInput()
  40. {
  41. $mock = $this->createCallableMock();
  42. $mock
  43. ->expects($this->once())
  44. ->method('__invoke')
  45. ->with($this->identicalTo([null, 1, null, 1, 1]));
  46. all([null, 1, null, 1, 1])
  47. ->then($mock);
  48. }
  49. /** @test */
  50. public function shouldRejectIfAnyInputPromiseRejects()
  51. {
  52. $mock = $this->createCallableMock();
  53. $mock
  54. ->expects($this->once())
  55. ->method('__invoke')
  56. ->with($this->identicalTo(2));
  57. all([resolve(1), reject(2), resolve(3)])
  58. ->then($this->expectCallableNever(), $mock);
  59. }
  60. /** @test */
  61. public function shouldAcceptAPromiseForAnArray()
  62. {
  63. $mock = $this->createCallableMock();
  64. $mock
  65. ->expects($this->once())
  66. ->method('__invoke')
  67. ->with($this->identicalTo([1, 2, 3]));
  68. all(resolve([1, 2, 3]))
  69. ->then($mock);
  70. }
  71. /** @test */
  72. public function shouldResolveToEmptyArrayWhenInputPromiseDoesNotResolveToArray()
  73. {
  74. $mock = $this->createCallableMock();
  75. $mock
  76. ->expects($this->once())
  77. ->method('__invoke')
  78. ->with($this->identicalTo([]));
  79. all(resolve(1))
  80. ->then($mock);
  81. }
  82. /** @test */
  83. public function shouldPreserveTheOrderOfArrayWhenResolvingAsyncPromises()
  84. {
  85. $mock = $this->createCallableMock();
  86. $mock
  87. ->expects($this->once())
  88. ->method('__invoke')
  89. ->with($this->identicalTo([1, 2, 3]));
  90. $deferred = new Deferred();
  91. all([resolve(1), $deferred->promise(), resolve(3)])
  92. ->then($mock);
  93. $deferred->resolve(2);
  94. }
  95. }