1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-02-25 02:22:57 +01:00
guzzle/tests/UtilsTest.php
Michael Dowling 668209c895 Getting tests working again. Removing more fluent interfaces.
Removing more fluent interfaces to make it easier to decorate
classes.
2014-09-08 21:35:11 -07:00

92 lines
2.8 KiB
PHP

<?php
namespace GuzzleHttp\Tests;
use GuzzleHttp\Client;
use GuzzleHttp\Event\BeforeEvent;
use GuzzleHttp\Event\CompleteEvent;
use GuzzleHttp\Event\ErrorEvent;
use GuzzleHttp\Message\Response;
use GuzzleHttp\Subscriber\Mock;
use GuzzleHttp\Utils;
class UtilsTest extends \PHPUnit_Framework_TestCase
{
public function testExpandsTemplate()
{
$this->assertEquals(
'foo/123',
Utils::uriTemplate('foo/{bar}', ['bar' => '123'])
);
}
public function noBodyProvider()
{
return [['get'], ['head'], ['delete']];
}
public function testBatchesRequests()
{
$client = new Client();
$responses = [
new Response(301, ['Location' => 'http://foo.com/bar']),
new Response(200),
new Response(200),
new Response(404)
];
$client->getEmitter()->attach(new Mock($responses));
$requests = [
$client->createRequest('GET', 'http://foo.com/baz'),
$client->createRequest('HEAD', 'http://httpbin.org/get'),
$client->createRequest('PUT', 'http://httpbin.org/put'),
];
$a = $b = $c = 0;
$result = Utils::batch($client, $requests, [
'before' => function (BeforeEvent $e) use (&$a) { $a++; },
'complete' => function (CompleteEvent $e) use (&$b) { $b++; },
'error' => function (ErrorEvent $e) use (&$c) { $c++; },
]);
$this->assertEquals(4, $a);
$this->assertEquals(2, $b);
$this->assertEquals(1, $c);
$this->assertCount(3, $result);
foreach ($result as $i => $request) {
$this->assertSame($requests[$i], $request);
}
// The first result is actually the second (redirect) response.
$this->assertSame($responses[1], $result[$requests[0]]);
// The second result is a 1:1 request:response map
$this->assertSame($responses[2], $result[$requests[1]]);
// The third entry is the 404 RequestException
$this->assertSame($responses[3], $result[$requests[2]]->getResponse());
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid event format
*/
public function testBatchValidatesTheEventFormat()
{
$client = new Client();
$requests = [$client->createRequest('GET', 'http://foo.com/baz')];
Utils::batch($client, $requests, ['complete' => 'foo']);
}
public function testJsonDecodes()
{
$this->assertTrue(Utils::jsonDecode('true'));
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Unable to parse JSON data: JSON_ERROR_SYNTAX - Syntax error, malformed JSON
*/
public function testJsonDecodesWithErrorMessages()
{
Utils::jsonDecode('!narf!');
}
}