Файл: vkolhoze.com/vendor/guzzle/guzzle/tests/Guzzle/Tests/Service/Command/OperationCommandTest.php
Строк: 135
<?php
namespace GuzzleTestsServiceCommand;
use GuzzleHttpMessageEntityEnclosingRequest;
use GuzzleHttpMessageResponse;
use GuzzleServiceClient;
use GuzzleServiceCommandOperationCommand;
use GuzzleServiceDescriptionOperation;
use GuzzleServiceDescriptionServiceDescription;
use GuzzleServiceCommandDefaultRequestSerializer;
use GuzzleServiceResourceModel;
use GuzzleServiceCommandLocationVisitorVisitorFlyweight;
/**
* @covers GuzzleServiceCommandOperationCommand
*/
class OperationCommandTest extends GuzzleTestsGuzzleTestCase
{
public function testHasRequestSerializer()
{
$operation = new OperationCommand();
$a = $operation->getRequestSerializer();
$b = new DefaultRequestSerializer(VisitorFlyweight::getInstance());
$operation->setRequestSerializer($b);
$this->assertNotSame($a, $operation->getRequestSerializer());
}
public function testPreparesRequestUsingSerializer()
{
$op = new OperationCommand(array(), new Operation());
$op->setClient(new Client());
$s = $this->getMockBuilder('GuzzleServiceCommandRequestSerializerInterface')
->setMethods(array('prepare'))
->getMockForAbstractClass();
$s->expects($this->once())
->method('prepare')
->will($this->returnValue(new EntityEnclosingRequest('POST', 'http://foo.com')));
$op->setRequestSerializer($s);
$op->prepare();
}
public function testParsesResponsesWithResponseParser()
{
$op = new OperationCommand(array(), new Operation());
$p = $this->getMockBuilder('GuzzleServiceCommandResponseParserInterface')
->setMethods(array('parse'))
->getMockForAbstractClass();
$p->expects($this->once())
->method('parse')
->will($this->returnValue(array('foo' => 'bar')));
$op->setResponseParser($p);
$op->setClient(new Client());
$request = $op->prepare();
$request->setResponse(new Response(200), true);
$this->assertEquals(array('foo' => 'bar'), $op->execute());
}
public function testParsesResponsesUsingModelParserWhenMatchingModelIsFound()
{
$description = ServiceDescription::factory(array(
'operations' => array(
'foo' => array('responseClass' => 'bar', 'responseType' => 'model')
),
'models' => array(
'bar' => array(
'type' => 'object',
'properties' => array(
'Baz' => array('type' => 'string', 'location' => 'xml')
)
)
)
));
$op = new OperationCommand(array(), $description->getOperation('foo'));
$op->setClient(new Client());
$request = $op->prepare();
$request->setResponse(new Response(200, array(
'Content-Type' => 'application/xml'
), '<Foo><Baz>Bar</Baz></Foo>'), true);
$result = $op->execute();
$this->assertEquals(new Model(array('Baz' => 'Bar')), $result);
}
public function testAllowsRawResponses()
{
$description = new ServiceDescription(array(
'operations' => array('foo' => array('responseClass' => 'bar', 'responseType' => 'model')),
'models' => array('bar' => array())
));
$op = new OperationCommand(array(
OperationCommand::RESPONSE_PROCESSING => OperationCommand::TYPE_RAW
), $description->getOperation('foo'));
$op->setClient(new Client());
$request = $op->prepare();
$response = new Response(200, array(
'Content-Type' => 'application/xml'
), '<Foo><Baz>Bar</Baz></Foo>');
$request->setResponse($response, true);
$this->assertSame($response, $op->execute());
}
}