3
namespace GuzzleHttp\Tests;
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;
12
class FunctionsTest extends \PHPUnit_Framework_TestCase
14
public function testExpandsTemplate()
16
$this->assertEquals('foo/123', \GuzzleHttp\uri_template('foo/{bar}', ['bar' => '123']));
19
public function noBodyProvider()
21
return [['get'], ['head'], ['delete']];
25
* @dataProvider noBodyProvider
27
public function testSendsNoBody($method)
30
Server::enqueue([new Response(200)]);
31
call_user_func("GuzzleHttp\\{$method}", Server::$url, [
32
'headers' => ['foo' => 'bar'],
33
'query' => ['a' => '1']
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'));
41
public function testSendsOptionsRequest()
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'));
52
public function hasBodyProvider()
54
return [['put'], ['post'], ['patch']];
58
* @dataProvider hasBodyProvider
60
public function testSendsWithBody($method)
63
Server::enqueue([new Response(200)]);
64
call_user_func("GuzzleHttp\\{$method}", Server::$url, [
65
'headers' => ['foo' => 'bar'],
67
'query' => ['a' => '1']
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());
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.
80
public function testManagesDeprecatedMethods()
82
$d = new HasDeprecations();
87
* @expectedException \BadMethodCallException
89
public function testManagesDeprecatedMethodsAndHandlesMissingMethods()
91
$d = new HasDeprecations();
95
public function testBatchesRequests()
97
$client = new Client();
99
new Response(301, ['Location' => 'http://foo.com/bar']),
104
$client->getEmitter()->attach(new Mock($responses));
106
$client->createRequest('GET', 'http://foo.com/baz'),
107
$client->createRequest('HEAD', 'http://httpbin.org/get'),
108
$client->createRequest('PUT', 'http://httpbin.org/put'),
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++; },
118
$this->assertEquals(4, $a);
119
$this->assertEquals(2, $b);
120
$this->assertEquals(1, $c);
121
$this->assertCount(3, $result);
123
foreach ($result as $i => $request) {
124
$this->assertSame($requests[$i], $request);
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());
136
* @expectedException \InvalidArgumentException
137
* @expectedExceptionMessage Invalid event format
139
public function testBatchValidatesTheEventFormat()
141
$client = new Client();
142
$requests = [$client->createRequest('GET', 'http://foo.com/baz')];
143
\GuzzleHttp\batch($client, $requests, ['complete' => 'foo']);
146
public function testJsonDecodes()
148
$data = \GuzzleHttp\json_decode('true');
149
$this->assertTrue($data);
153
* @expectedException \InvalidArgumentException
154
* @expectedExceptionMessage Unable to parse JSON data: JSON_ERROR_SYNTAX - Syntax error, malformed JSON
156
public function testJsonDecodesWithErrorMessages()
158
\GuzzleHttp\json_decode('!narf!');
162
class HasDeprecations
168
function __call($name, $arguments)
170
return \GuzzleHttp\deprecation_proxy($this, $name, $arguments, [