~ballot/wordpress/openstack-objectstorage-bis

« back to all changes in this revision

Viewing changes to vendor/guzzlehttp/guzzle/tests/FunctionsTest.php

  • Committer: Thomas Cuthbert
  • Date: 2020-04-23 05:22:45 UTC
  • Revision ID: thomas.cuthbert@canonical.com-20200423052245-1jxao3mw31w435js
[,r=trivial] bionic composer vendor update

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
 
3
 
namespace GuzzleHttp\Tests;
4
 
 
5
 
use GuzzleHttp\Client;
6
 
use GuzzleHttp\Event\BeforeEvent;
7
 
use GuzzleHttp\Event\CompleteEvent;
8
 
use GuzzleHttp\Event\ErrorEvent;
9
 
use GuzzleHttp\Message\Response;
10
 
use GuzzleHttp\Subscriber\Mock;
11
 
 
12
 
class FunctionsTest extends \PHPUnit_Framework_TestCase
13
 
{
14
 
    public function testExpandsTemplate()
15
 
    {
16
 
        $this->assertEquals('foo/123', \GuzzleHttp\uri_template('foo/{bar}', ['bar' => '123']));
17
 
    }
18
 
 
19
 
    public function noBodyProvider()
20
 
    {
21
 
        return [['get'], ['head'], ['delete']];
22
 
    }
23
 
 
24
 
    /**
25
 
     * @dataProvider noBodyProvider
26
 
     */
27
 
    public function testSendsNoBody($method)
28
 
    {
29
 
        Server::flush();
30
 
        Server::enqueue([new Response(200)]);
31
 
        call_user_func("GuzzleHttp\\{$method}", Server::$url, [
32
 
            'headers' => ['foo' => 'bar'],
33
 
            'query' => ['a' => '1']
34
 
        ]);
35
 
        $sent = Server::received(true)[0];
36
 
        $this->assertEquals(strtoupper($method), $sent->getMethod());
37
 
        $this->assertEquals('/?a=1', $sent->getResource());
38
 
        $this->assertEquals('bar', $sent->getHeader('foo'));
39
 
    }
40
 
 
41
 
    public function testSendsOptionsRequest()
42
 
    {
43
 
        Server::flush();
44
 
        Server::enqueue([new Response(200)]);
45
 
        \GuzzleHttp\options(Server::$url, ['headers' => ['foo' => 'bar']]);
46
 
        $sent = Server::received(true)[0];
47
 
        $this->assertEquals('OPTIONS', $sent->getMethod());
48
 
        $this->assertEquals('/', $sent->getResource());
49
 
        $this->assertEquals('bar', $sent->getHeader('foo'));
50
 
    }
51
 
 
52
 
    public function hasBodyProvider()
53
 
    {
54
 
        return [['put'], ['post'], ['patch']];
55
 
    }
56
 
 
57
 
    /**
58
 
     * @dataProvider hasBodyProvider
59
 
     */
60
 
    public function testSendsWithBody($method)
61
 
    {
62
 
        Server::flush();
63
 
        Server::enqueue([new Response(200)]);
64
 
        call_user_func("GuzzleHttp\\{$method}", Server::$url, [
65
 
            'headers' => ['foo' => 'bar'],
66
 
            'body'    => 'test',
67
 
            'query'   => ['a' => '1']
68
 
        ]);
69
 
        $sent = Server::received(true)[0];
70
 
        $this->assertEquals(strtoupper($method), $sent->getMethod());
71
 
        $this->assertEquals('/?a=1', $sent->getResource());
72
 
        $this->assertEquals('bar', $sent->getHeader('foo'));
73
 
        $this->assertEquals('test', $sent->getBody());
74
 
    }
75
 
 
76
 
    /**
77
 
     * @expectedException \PHPUnit_Framework_Error_Deprecated
78
 
     * @expectedExceptionMessage GuzzleHttp\Tests\HasDeprecations::baz() is deprecated and will be removed in a future version. Update your code to use the equivalent GuzzleHttp\Tests\HasDeprecations::foo() method instead to avoid breaking changes when this shim is removed.
79
 
     */
80
 
    public function testManagesDeprecatedMethods()
81
 
    {
82
 
        $d = new HasDeprecations();
83
 
        $d->baz();
84
 
    }
85
 
 
86
 
    /**
87
 
     * @expectedException \BadMethodCallException
88
 
     */
89
 
    public function testManagesDeprecatedMethodsAndHandlesMissingMethods()
90
 
    {
91
 
        $d = new HasDeprecations();
92
 
        $d->doesNotExist();
93
 
    }
94
 
 
95
 
    public function testBatchesRequests()
96
 
    {
97
 
        $client = new Client();
98
 
        $responses = [
99
 
            new Response(301, ['Location' => 'http://foo.com/bar']),
100
 
            new Response(200),
101
 
            new Response(200),
102
 
            new Response(404)
103
 
        ];
104
 
        $client->getEmitter()->attach(new Mock($responses));
105
 
        $requests = [
106
 
            $client->createRequest('GET', 'http://foo.com/baz'),
107
 
            $client->createRequest('HEAD', 'http://httpbin.org/get'),
108
 
            $client->createRequest('PUT', 'http://httpbin.org/put'),
109
 
        ];
110
 
 
111
 
        $a = $b = $c = 0;
112
 
        $result = \GuzzleHttp\batch($client, $requests, [
113
 
            'before'   => function (BeforeEvent $e) use (&$a) { $a++; },
114
 
            'complete' => function (CompleteEvent $e) use (&$b) { $b++; },
115
 
            'error'    => function (ErrorEvent $e) use (&$c) { $c++; },
116
 
        ]);
117
 
 
118
 
        $this->assertEquals(4, $a);
119
 
        $this->assertEquals(2, $b);
120
 
        $this->assertEquals(1, $c);
121
 
        $this->assertCount(3, $result);
122
 
 
123
 
        foreach ($result as $i => $request) {
124
 
            $this->assertSame($requests[$i], $request);
125
 
        }
126
 
 
127
 
        // The first result is actually the second (redirect) response.
128
 
        $this->assertSame($responses[1], $result[$requests[0]]);
129
 
        // The second result is a 1:1 request:response map
130
 
        $this->assertSame($responses[2], $result[$requests[1]]);
131
 
        // The third entry is the 404 RequestException
132
 
        $this->assertSame($responses[3], $result[$requests[2]]->getResponse());
133
 
    }
134
 
 
135
 
    /**
136
 
     * @expectedException \InvalidArgumentException
137
 
     * @expectedExceptionMessage Invalid event format
138
 
     */
139
 
    public function testBatchValidatesTheEventFormat()
140
 
    {
141
 
        $client = new Client();
142
 
        $requests = [$client->createRequest('GET', 'http://foo.com/baz')];
143
 
        \GuzzleHttp\batch($client, $requests, ['complete' => 'foo']);
144
 
    }
145
 
 
146
 
    public function testJsonDecodes()
147
 
    {
148
 
        $data = \GuzzleHttp\json_decode('true');
149
 
        $this->assertTrue($data);
150
 
    }
151
 
 
152
 
    /**
153
 
     * @expectedException \InvalidArgumentException
154
 
     * @expectedExceptionMessage Unable to parse JSON data: JSON_ERROR_SYNTAX - Syntax error, malformed JSON
155
 
     */
156
 
    public function testJsonDecodesWithErrorMessages()
157
 
    {
158
 
        \GuzzleHttp\json_decode('!narf!');
159
 
    }
160
 
}
161
 
 
162
 
class HasDeprecations
163
 
{
164
 
    function foo()
165
 
    {
166
 
        return 'abc';
167
 
    }
168
 
    function __call($name, $arguments)
169
 
    {
170
 
        return \GuzzleHttp\deprecation_proxy($this, $name, $arguments, [
171
 
            'baz' => 'foo'
172
 
        ]);
173
 
    }
174
 
}