aboutsummaryrefslogtreecommitdiffstats
path: root/public/system/storage/vendor/guzzlehttp/ringphp/tests/Client/MockHandlerTest.php
blob: 26bcd6cdc7ea86445f81f5389e963f4f9ab88f2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace GuzzleHttp\Tests\Ring\Client;

use GuzzleHttp\Ring\Client\MockHandler;
use GuzzleHttp\Ring\Future\FutureArray;
use React\Promise\Deferred;

class MockHandlerTest extends \PHPUnit_Framework_TestCase
{
    public function testReturnsArray()
    {
        $mock = new MockHandler(['status' => 200]);
        $response = $mock([]);
        $this->assertEquals(200, $response['status']);
        $this->assertEquals([], $response['headers']);
        $this->assertNull($response['body']);
        $this->assertNull($response['reason']);
        $this->assertNull($response['effective_url']);
    }

    public function testReturnsFutures()
    {
        $deferred = new Deferred();
        $future = new FutureArray(
            $deferred->promise(),
            function () use ($deferred) {
                $deferred->resolve(['status' => 200]);
            }
        );
        $mock = new MockHandler($future);
        $response = $mock([]);
        $this->assertInstanceOf('GuzzleHttp\Ring\Future\FutureArray', $response);
        $this->assertEquals(200, $response['status']);
    }

    public function testReturnsFuturesWithThenCall()
    {
        $deferred = new Deferred();
        $future = new FutureArray(
            $deferred->promise(),
            function () use ($deferred) {
                $deferred->resolve(['status' => 200]);
            }
        );
        $mock = new MockHandler($future);
        $response = $mock([]);
        $this->assertInstanceOf('GuzzleHttp\Ring\Future\FutureArray', $response);
        $this->assertEquals(200, $response['status']);
        $req = null;
        $promise = $response->then(function ($value) use (&$req) {
            $req = $value;
            $this->assertEquals(200, $req['status']);
        });
        $this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
        $this->assertEquals(200, $req['status']);
    }

    public function testReturnsFuturesAndProxiesCancel()
    {
        $c = null;
        $deferred = new Deferred();
        $future = new FutureArray(
            $deferred->promise(),
            function () {},
            function () use (&$c) {
                $c = true;
                return true;
            }
        );
        $mock = new MockHandler($future);
        $response = $mock([]);
        $this->assertInstanceOf('GuzzleHttp\Ring\Future\FutureArray', $response);
        $response->cancel();
        $this->assertTrue($c);
    }

    /**
     * @expectedException \InvalidArgumentException
     * @expectedExceptionMessage Response must be an array or FutureArrayInterface. Found
     */
    public function testEnsuresMockIsValid()
    {
        $mock = new MockHandler('foo');
        $mock([]);
    }
}