FunctionResolveTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace React\Promise;
  3. class FunctionResolveTest extends TestCase
  4. {
  5. /** @test */
  6. public function shouldResolveAnImmediateValue()
  7. {
  8. $expected = 123;
  9. $mock = $this->createCallableMock();
  10. $mock
  11. ->expects($this->once())
  12. ->method('__invoke')
  13. ->with($this->identicalTo($expected));
  14. resolve($expected)
  15. ->then(
  16. $mock,
  17. $this->expectCallableNever()
  18. );
  19. }
  20. /** @test */
  21. public function shouldResolveAFulfilledPromise()
  22. {
  23. $expected = 123;
  24. $resolved = new FulfilledPromise($expected);
  25. $mock = $this->createCallableMock();
  26. $mock
  27. ->expects($this->once())
  28. ->method('__invoke')
  29. ->with($this->identicalTo($expected));
  30. resolve($resolved)
  31. ->then(
  32. $mock,
  33. $this->expectCallableNever()
  34. );
  35. }
  36. /** @test */
  37. public function shouldResolveAThenable()
  38. {
  39. $thenable = new SimpleFulfilledTestThenable();
  40. $mock = $this->createCallableMock();
  41. $mock
  42. ->expects($this->once())
  43. ->method('__invoke')
  44. ->with($this->identicalTo('foo'));
  45. resolve($thenable)
  46. ->then(
  47. $mock,
  48. $this->expectCallableNever()
  49. );
  50. }
  51. /** @test */
  52. public function shouldResolveACancellableThenable()
  53. {
  54. $thenable = new SimpleTestCancellableThenable();
  55. $promise = resolve($thenable);
  56. $promise->cancel();
  57. $this->assertTrue($thenable->cancelCalled);
  58. }
  59. /** @test */
  60. public function shouldRejectARejectedPromise()
  61. {
  62. $expected = 123;
  63. $resolved = new RejectedPromise($expected);
  64. $mock = $this->createCallableMock();
  65. $mock
  66. ->expects($this->once())
  67. ->method('__invoke')
  68. ->with($this->identicalTo($expected));
  69. resolve($resolved)
  70. ->then(
  71. $this->expectCallableNever(),
  72. $mock
  73. );
  74. }
  75. /** @test */
  76. public function shouldSupportDeepNestingInPromiseChains()
  77. {
  78. $d = new Deferred();
  79. $d->resolve(false);
  80. $result = resolve(resolve($d->promise()->then(function ($val) {
  81. $d = new Deferred();
  82. $d->resolve($val);
  83. $identity = function ($val) {
  84. return $val;
  85. };
  86. return resolve($d->promise()->then($identity))->then(
  87. function ($val) {
  88. return !$val;
  89. }
  90. );
  91. })));
  92. $mock = $this->createCallableMock();
  93. $mock
  94. ->expects($this->once())
  95. ->method('__invoke')
  96. ->with($this->identicalTo(true));
  97. $result->then($mock);
  98. }
  99. /** @test */
  100. public function shouldSupportVeryDeepNestedPromises()
  101. {
  102. $deferreds = [];
  103. // @TODO Increase count once global-queue is merged
  104. for ($i = 0; $i < 10; $i++) {
  105. $deferreds[] = $d = new Deferred();
  106. $p = $d->promise();
  107. $last = $p;
  108. for ($j = 0; $j < 10; $j++) {
  109. $last = $last->then(function($result) {
  110. return $result;
  111. });
  112. }
  113. }
  114. $p = null;
  115. foreach ($deferreds as $d) {
  116. if ($p) {
  117. $d->resolve($p);
  118. }
  119. $p = $d->promise();
  120. }
  121. $deferreds[0]->resolve(true);
  122. $mock = $this->createCallableMock();
  123. $mock
  124. ->expects($this->once())
  125. ->method('__invoke')
  126. ->with($this->identicalTo(true));
  127. $deferreds[0]->promise()->then($mock);
  128. }
  129. /** @test */
  130. public function returnsExtendePromiseForSimplePromise()
  131. {
  132. $promise = $this
  133. ->getMockBuilder('React\Promise\PromiseInterface')
  134. ->getMock();
  135. $this->assertInstanceOf('React\Promise\ExtendedPromiseInterface', resolve($promise));
  136. }
  137. }