FutureArrayTest.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace GuzzleHttp\Tests\Ring\Future;
  3. use GuzzleHttp\Ring\Future\FutureArray;
  4. use React\Promise\Deferred;
  5. class FutureArrayTest extends \PHPUnit_Framework_TestCase
  6. {
  7. public function testLazilyCallsDeref()
  8. {
  9. $c = false;
  10. $deferred = new Deferred();
  11. $f = new FutureArray(
  12. $deferred->promise(),
  13. function () use (&$c, $deferred) {
  14. $c = true;
  15. $deferred->resolve(['status' => 200]);
  16. }
  17. );
  18. $this->assertFalse($c);
  19. $this->assertFalse($this->readAttribute($f, 'isRealized'));
  20. $this->assertEquals(200, $f['status']);
  21. $this->assertTrue($c);
  22. }
  23. public function testActsLikeArray()
  24. {
  25. $deferred = new Deferred();
  26. $f = new FutureArray(
  27. $deferred->promise(),
  28. function () use (&$c, $deferred) {
  29. $deferred->resolve(['status' => 200]);
  30. }
  31. );
  32. $this->assertTrue(isset($f['status']));
  33. $this->assertEquals(200, $f['status']);
  34. $this->assertEquals(['status' => 200], $f->wait());
  35. $this->assertEquals(1, count($f));
  36. $f['baz'] = 10;
  37. $this->assertEquals(10, $f['baz']);
  38. unset($f['baz']);
  39. $this->assertFalse(isset($f['baz']));
  40. $this->assertEquals(['status' => 200], iterator_to_array($f));
  41. }
  42. /**
  43. * @expectedException \RuntimeException
  44. */
  45. public function testThrowsWhenAccessingInvalidProperty()
  46. {
  47. $deferred = new Deferred();
  48. $f = new FutureArray($deferred->promise(), function () {});
  49. $f->foo;
  50. }
  51. }