1<?php
2namespace GuzzleHttp\Tests\Ring\Client;
3
4use GuzzleHttp\Ring\Client\MockHandler;
5use GuzzleHttp\Ring\Future\FutureArray;
6use React\Promise\Deferred;
7
8class MockHandlerTest extends \PHPUnit_Framework_TestCase
9{
10    public function testReturnsArray()
11    {
12        $mock = new MockHandler(['status' => 200]);
13        $response = $mock([]);
14        $this->assertEquals(200, $response['status']);
15        $this->assertEquals([], $response['headers']);
16        $this->assertNull($response['body']);
17        $this->assertNull($response['reason']);
18        $this->assertNull($response['effective_url']);
19    }
20
21    public function testReturnsFutures()
22    {
23        $deferred = new Deferred();
24        $future = new FutureArray(
25            $deferred->promise(),
26            function () use ($deferred) {
27                $deferred->resolve(['status' => 200]);
28            }
29        );
30        $mock = new MockHandler($future);
31        $response = $mock([]);
32        $this->assertInstanceOf('GuzzleHttp\Ring\Future\FutureArray', $response);
33        $this->assertEquals(200, $response['status']);
34    }
35
36    public function testReturnsFuturesWithThenCall()
37    {
38        $deferred = new Deferred();
39        $future = new FutureArray(
40            $deferred->promise(),
41            function () use ($deferred) {
42                $deferred->resolve(['status' => 200]);
43            }
44        );
45        $mock = new MockHandler($future);
46        $response = $mock([]);
47        $this->assertInstanceOf('GuzzleHttp\Ring\Future\FutureArray', $response);
48        $this->assertEquals(200, $response['status']);
49        $req = null;
50        $promise = $response->then(function ($value) use (&$req) {
51            $req = $value;
52            $this->assertEquals(200, $req['status']);
53        });
54        $this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
55        $this->assertEquals(200, $req['status']);
56    }
57
58    public function testReturnsFuturesAndProxiesCancel()
59    {
60        $c = null;
61        $deferred = new Deferred();
62        $future = new FutureArray(
63            $deferred->promise(),
64            function () {},
65            function () use (&$c) {
66                $c = true;
67                return true;
68            }
69        );
70        $mock = new MockHandler($future);
71        $response = $mock([]);
72        $this->assertInstanceOf('GuzzleHttp\Ring\Future\FutureArray', $response);
73        $response->cancel();
74        $this->assertTrue($c);
75    }
76
77    /**
78     * @expectedException \InvalidArgumentException
79     * @expectedExceptionMessage Response must be an array or FutureArrayInterface. Found
80     */
81    public function testEnsuresMockIsValid()
82    {
83        $mock = new MockHandler('foo');
84        $mock([]);
85    }
86}
87