$serviceBuilder
$serviceBuilder :
Base testcase class for all Guzzle testcases.
getServer() : \Guzzle\Tests\Http\Server
Get the global server object used throughout the unit tests of Guzzle
setServiceBuilder(\Guzzle\Service\Builder\ServiceBuilderInterface $builder)
Set the service builder to use for tests
\Guzzle\Service\Builder\ServiceBuilderInterface | $builder | Service builder |
getServiceBuilder() : \Guzzle\Service\Builder\ServiceBuilder
Get a service builder object that can be used throughout the service tests
getWildcardObserver(\Guzzle\Common\HasDispatcherInterface $hasDispatcher) : \Guzzle\Tests\Mock\MockObserver
Get a wildcard observer for an event dispatcher
\Guzzle\Common\HasDispatcherInterface | $hasDispatcher |
setMockBasePath(string $path) : \Guzzle\Tests\GuzzleTestCase
Set the mock response base path
string | $path | Path to mock response folder |
addMockedRequest(\Guzzle\Http\Message\RequestInterface $request) : self
Mark a request as being mocked
\Guzzle\Http\Message\RequestInterface | $request |
getMockResponse(string $path) : \Guzzle\Http\Message\Response
Get a mock response for a client by mock file name
string | $path | Relative path to the mock response file |
setMockResponse(\Guzzle\Http\Client $client, string $paths) : \Guzzle\Plugin\Mock\MockPlugin
Set a mock response from a mock file on the next client request.
This method assumes that mock response files are located under the Command/Mock/ directory of the Service being tested (e.g. Unfuddle/Command/Mock/). A mock response is added to the next request sent by the client.
\Guzzle\Http\Client | $client | Client object to modify |
string | $paths | Path to files within the Mock folder of the service |
returns the created mock plugin
compareHeaders(array $filteredHeaders, array $actualHeaders) : array|boolean
Compare HTTP headers and use special markup to filter values A header prefixed with '!' means it must not exist A header prefixed with '_' means it must be ignored A header value of '*' means anything after the * will be ignored
array | $filteredHeaders | Array of special headers |
array | $actualHeaders | Array of headers to check against |
Returns an array of the differences or FALSE if none
hasSubscriber(\Guzzle\Common\HasDispatcherInterface $dispatcher, \Symfony\Component\EventDispatcher\EventSubscriberInterface $subscriber) : boolean
Check if an event dispatcher has a subscriber
\Guzzle\Common\HasDispatcherInterface | $dispatcher | |
\Symfony\Component\EventDispatcher\EventSubscriberInterface | $subscriber |
<?php
namespace Guzzle\Tests\Plugin\ErrorResponse;
use Guzzle\Service\Client;
use Guzzle\Http\Message\Response;
use Guzzle\Plugin\ErrorResponse\ErrorResponsePlugin;
use Guzzle\Service\Description\ServiceDescription;
use Guzzle\Tests\Mock\ErrorResponseMock;
/**
* @covers \Guzzle\Plugin\ErrorResponse\ErrorResponsePlugin
*/
class ErrorResponsePluginTest extends \Guzzle\Tests\GuzzleTestCase
{
protected $client;
public static function tearDownAfterClass()
{
self::getServer()->flush();
}
public function setUp()
{
$mockError = 'Guzzle\Tests\Mock\ErrorResponseMock';
$description = ServiceDescription::factory(array(
'operations' => array(
'works' => array(
'httpMethod' => 'GET',
'errorResponses' => array(
array('code' => 500, 'class' => $mockError),
array('code' => 503, 'reason' => 'foo', 'class' => $mockError),
array('code' => 200, 'reason' => 'Error!', 'class' => $mockError)
)
),
'bad_class' => array(
'httpMethod' => 'GET',
'errorResponses' => array(
array('code' => 500, 'class' => 'Does\\Not\\Exist')
)
),
'does_not_implement' => array(
'httpMethod' => 'GET',
'errorResponses' => array(
array('code' => 500, 'class' => __CLASS__)
)
),
'no_errors' => array('httpMethod' => 'GET'),
'no_class' => array(
'httpMethod' => 'GET',
'errorResponses' => array(
array('code' => 500)
)
),
)
));
$this->client = new Client($this->getServer()->getUrl());
$this->client->setDescription($description);
}
/**
* @expectedException \Guzzle\Http\Exception\ServerErrorResponseException
*/
public function testSkipsWhenErrorResponsesIsNotSet()
{
$this->getServer()->enqueue("HTTP/1.1 500 Foo\r\nContent-Length: 0\r\n\r\n");
$this->client->addSubscriber(new ErrorResponsePlugin());
$this->client->getCommand('no_errors')->execute();
}
public function testSkipsWhenErrorResponsesIsNotSetAndAllowsSuccess()
{
$this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
$this->client->addSubscriber(new ErrorResponsePlugin());
$this->client->getCommand('no_errors')->execute();
}
/**
* @expectedException \Guzzle\Plugin\ErrorResponse\Exception\ErrorResponseException
* @expectedExceptionMessage Does\Not\Exist does not exist
*/
public function testEnsuresErrorResponseExists()
{
$this->getServer()->enqueue("HTTP/1.1 500 Foo\r\nContent-Length: 0\r\n\r\n");
$this->client->addSubscriber(new ErrorResponsePlugin());
$this->client->getCommand('bad_class')->execute();
}
/**
* @expectedException \Guzzle\Plugin\ErrorResponse\Exception\ErrorResponseException
* @expectedExceptionMessage must implement Guzzle\Plugin\ErrorResponse\ErrorResponseExceptionInterface
*/
public function testEnsuresErrorResponseImplementsInterface()
{
$this->getServer()->enqueue("HTTP/1.1 500 Foo\r\nContent-Length: 0\r\n\r\n");
$this->client->addSubscriber(new ErrorResponsePlugin());
$this->client->getCommand('does_not_implement')->execute();
}
public function testThrowsSpecificErrorResponseOnMatch()
{
try {
$this->getServer()->enqueue("HTTP/1.1 500 Foo\r\nContent-Length: 0\r\n\r\n");
$this->client->addSubscriber(new ErrorResponsePlugin());
$command = $this->client->getCommand('works');
$command->execute();
$this->fail('Exception not thrown');
} catch (ErrorResponseMock $e) {
$this->assertSame($command, $e->command);
$this->assertEquals(500, $e->response->getStatusCode());
}
}
/**
* @expectedException \Guzzle\Tests\Mock\ErrorResponseMock
*/
public function testThrowsWhenCodeAndPhraseMatch()
{
$this->getServer()->enqueue("HTTP/1.1 200 Error!\r\nContent-Length: 0\r\n\r\n");
$this->client->addSubscriber(new ErrorResponsePlugin());
$this->client->getCommand('works')->execute();
}
public function testSkipsWhenReasonDoesNotMatch()
{
$this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
$this->client->addSubscriber(new ErrorResponsePlugin());
$this->client->getCommand('works')->execute();
}
public function testSkipsWhenNoClassIsSet()
{
$this->getServer()->enqueue("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
$this->client->addSubscriber(new ErrorResponsePlugin());
$this->client->getCommand('no_class')->execute();
}
}