$serviceBuilder
$serviceBuilder :
Base testcase class for all Guzzle testcases.
$request : \Guzzle\Http\Message\EntityEnclosingRequest
$command : \Guzzle\Service\Command\AbstractCommand
$client : \Guzzle\Service\Client
$serializer : \Guzzle\Service\Command\DefaultRequestSerializer
$operation : \Guzzle\Service\Description\Operation
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\Service\Command;
use Guzzle\Service\Command\DefaultRequestSerializer;
use Guzzle\Http\Message\EntityEnclosingRequest;
use Guzzle\Service\Client;
use Guzzle\Service\Description\ServiceDescription;
use Guzzle\Service\Description\Operation;
use Guzzle\Service\Description\Parameter;
use Guzzle\Service\Command\LocationVisitor\Request\HeaderVisitor;
use Guzzle\Service\Command\LocationVisitor\VisitorFlyweight;
/**
* @covers Guzzle\Service\Command\DefaultRequestSerializer
*/
class DefaultRequestSerializerTest extends \Guzzle\Tests\GuzzleTestCase
{
/** @var EntityEnclosingRequest */
protected $request;
/** @var \Guzzle\Service\Command\AbstractCommand */
protected $command;
/** @var Client */
protected $client;
/** @var DefaultRequestSerializer */
protected $serializer;
/** @var Operation */
protected $operation;
public function setUp()
{
$this->serializer = DefaultRequestSerializer::getInstance();
$this->client = new Client('http://foo.com/baz');
$this->operation = new Operation(array('httpMethod' => 'POST'));
$this->command = $this->getMockBuilder('Guzzle\Service\Command\AbstractCommand')
->setConstructorArgs(array(array(), $this->operation))
->getMockForAbstractClass();
$this->command->setClient($this->client);
}
public function testAllowsCustomVisitor()
{
$this->serializer->addVisitor('custom', new HeaderVisitor());
$this->command['test'] = '123';
$this->operation->addParam(new Parameter(array('name' => 'test', 'location' => 'custom')));
$request = $this->serializer->prepare($this->command);
$this->assertEquals('123', (string) $request->getHeader('test'));
}
public function testUsesRelativePath()
{
$this->operation->setUri('bar');
$request = $this->serializer->prepare($this->command);
$this->assertEquals('http://foo.com/baz/bar', (string) $request->getUrl());
}
public function testUsesRelativePathWithUriLocations()
{
$this->command['test'] = '123';
$this->operation->setUri('bar/{test}');
$this->operation->addParam(new Parameter(array('name' => 'test', 'location' => 'uri')));
$request = $this->serializer->prepare($this->command);
$this->assertEquals('http://foo.com/baz/bar/123', (string) $request->getUrl());
}
public function testAllowsCustomFactory()
{
$f = new VisitorFlyweight();
$serializer = new DefaultRequestSerializer($f);
$this->assertSame($f, $this->readAttribute($serializer, 'factory'));
}
public function testMixedParams()
{
$this->operation->setUri('bar{?limit,fields}');
$this->operation->addParam(new Parameter(array(
'name' => 'limit',
'location' => 'uri',
'required' => false,
)));
$this->operation->addParam(new Parameter(array(
'name' => 'fields',
'location' => 'uri',
'required' => true,
)));
$this->command['fields'] = array('id', 'name');
$request = $this->serializer->prepare($this->command);
$this->assertEquals('http://foo.com/baz/bar?fields='.urlencode('id,name'), (string) $request->getUrl());
}
public function testValidatesAdditionalParameters()
{
$description = ServiceDescription::factory(array(
'operations' => array(
'foo' => array(
'httpMethod' => 'PUT',
'parameters' => array(
'bar' => array('location' => 'header')
),
'additionalParameters' => array(
'location' => 'json'
)
)
)
));
$client = new Client();
$client->setDescription($description);
$command = $client->getCommand('foo');
$command['bar'] = 'test';
$command['hello'] = 'abc';
$request = $command->prepare();
$this->assertEquals('test', (string) $request->getHeader('bar'));
$this->assertEquals('{"hello":"abc"}', (string) $request->getBody());
}
}