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