FutureValueTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace GuzzleHttp\Tests\Ring\Future;
  3. use GuzzleHttp\Ring\Exception\CancelledFutureAccessException;
  4. use GuzzleHttp\Ring\Future\FutureValue;
  5. use React\Promise\Deferred;
  6. class FutureValueTest extends \PHPUnit_Framework_TestCase
  7. {
  8. public function testDerefReturnsValue()
  9. {
  10. $called = 0;
  11. $deferred = new Deferred();
  12. $f = new FutureValue(
  13. $deferred->promise(),
  14. function () use ($deferred, &$called) {
  15. $called++;
  16. $deferred->resolve('foo');
  17. }
  18. );
  19. $this->assertEquals('foo', $f->wait());
  20. $this->assertEquals(1, $called);
  21. $this->assertEquals('foo', $f->wait());
  22. $this->assertEquals(1, $called);
  23. $f->cancel();
  24. $this->assertTrue($this->readAttribute($f, 'isRealized'));
  25. }
  26. /**
  27. * @expectedException \GuzzleHttp\Ring\Exception\CancelledFutureAccessException
  28. */
  29. public function testThrowsWhenAccessingCancelled()
  30. {
  31. $f = new FutureValue(
  32. (new Deferred())->promise(),
  33. function () {},
  34. function () { return true; }
  35. );
  36. $f->cancel();
  37. $f->wait();
  38. }
  39. /**
  40. * @expectedException \OutOfBoundsException
  41. */
  42. public function testThrowsWhenDerefFailure()
  43. {
  44. $called = false;
  45. $deferred = new Deferred();
  46. $f = new FutureValue(
  47. $deferred->promise(),
  48. function () use(&$called) {
  49. $called = true;
  50. }
  51. );
  52. $deferred->reject(new \OutOfBoundsException());
  53. $f->wait();
  54. $this->assertFalse($called);
  55. }
  56. /**
  57. * @expectedException \GuzzleHttp\Ring\Exception\RingException
  58. * @expectedExceptionMessage Waiting did not resolve future
  59. */
  60. public function testThrowsWhenDerefDoesNotResolve()
  61. {
  62. $deferred = new Deferred();
  63. $f = new FutureValue(
  64. $deferred->promise(),
  65. function () use(&$called) {
  66. $called = true;
  67. }
  68. );
  69. $f->wait();
  70. }
  71. public function testThrowingCancelledFutureAccessExceptionCancels()
  72. {
  73. $deferred = new Deferred();
  74. $f = new FutureValue(
  75. $deferred->promise(),
  76. function () use ($deferred) {
  77. throw new CancelledFutureAccessException();
  78. }
  79. );
  80. try {
  81. $f->wait();
  82. $this->fail('did not throw');
  83. } catch (CancelledFutureAccessException $e) {}
  84. }
  85. /**
  86. * @expectedException \Exception
  87. * @expectedExceptionMessage foo
  88. */
  89. public function testThrowingExceptionInDerefMarksAsFailed()
  90. {
  91. $deferred = new Deferred();
  92. $f = new FutureValue(
  93. $deferred->promise(),
  94. function () {
  95. throw new \Exception('foo');
  96. }
  97. );
  98. $f->wait();
  99. }
  100. }