diff --git a/.travis.yml b/.travis.yml index d1da0f2..d274738 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: php php: - - 5.3.3 - 5.3 - 5.4 - 5.5 @@ -11,18 +10,14 @@ php: matrix: allow_failures: - php: hhvm - -env: - - SYMFONY_VERSION="2.2.0" GUZZLE_VERSION="3.0.0" SENSIO_FRAMEWORK_EXTRA_VERSION="~2.2" - - SYMFONY_VERSION="2.3.x" GUZZLE_VERSION="~3.0" SENSIO_FRAMEWORK_EXTRA_VERSION="~2.2" - - SYMFONY_VERSION="~2.4" GUZZLE_VERSION="~3.0" SENSIO_FRAMEWORK_EXTRA_VERSION="~3.0" + include: + - php: 5.3.3 + env: dependencies=lowest before_install: - - composer require symfony/framework-bundle:${SYMFONY_VERSION} --no-interaction --no-update - - composer require guzzle/guzzle:${GUZZLE_VERSION} --no-interaction --no-update - - composer require sensio/framework-extra-bundle:${SENSIO_FRAMEWORK_EXTRA_VERSION} --no-interaction --dev --no-update + - composer self-update install: - - composer install --no-interaction --dev --prefer-source + - if [ "$dependencies" = "lowest" ]; then composer update --dev --prefer-lowest --prefer-stable --prefer-source --no-interaction; else composer install --dev --prefer-source --no-interaction; fi; script: ./vendor/bin/phpunit diff --git a/Resources/doc/serialization.md b/Resources/doc/serialization.md index acd8efd..7d1cd2b 100644 --- a/Resources/doc/serialization.md +++ b/Resources/doc/serialization.md @@ -32,7 +32,7 @@ Executing the `GetPerson` command will now return an instance of `Vendor\MyBundl $command = $client->getCommand('GetPerson', array('id' => $id)); $person = $client->execute($command); -If you wish to customize the deserialization context of the serializer, you can do so by usage of the `data` property of the Operation. Groups, version, and max depth checks are configurable for deserialization: +If you wish to customize the deserialization context of the serializer (JMSSerializer v0.12+), you can do so by usage of the `data` property of the Operation. Groups, version, and max depth checks (JMSSerializer v0.13+) are configurable for deserialization: For example: diff --git a/Service/Command/JMSSerializerResponseParser.php b/Service/Command/JMSSerializerResponseParser.php index 8dbdd5f..8b6ec42 100644 --- a/Service/Command/JMSSerializerResponseParser.php +++ b/Service/Command/JMSSerializerResponseParser.php @@ -105,24 +105,36 @@ protected function deserialize(CommandInterface $command, Response $response, $c if (null !== $serializerContentType && OperationInterface::TYPE_CLASS === $command->getOperation()->getResponseType() ) { - $context = DeserializationContext::create(); - $operation = $command->getOperation(); + if (true === class_exists('JMS\Serializer\DeserializationContext')) { + $context = DeserializationContext::create(); + $operation = $command->getOperation(); - if (null !== $groups = $operation->getData('jms_serializer.groups')) { - $context->setGroups($groups); - } - if (null !== $version = $operation->getData('jms_serializer.version')) { - $context->setVersion($version); - } - if (true === $operation->getData('jms_serializer.max_depth_checks')) { - $context->enableMaxDepthChecks(); + if (null !== $groups = $operation->getData('jms_serializer.groups')) { + $context->setGroups($groups); + } + if (null !== $version = $operation->getData('jms_serializer.version')) { + $context->setVersion($version); + } + if ( + true === $operation->getData('jms_serializer.max_depth_checks') + && + true === method_exists('JMS\Serializer\DeserializationContext', 'enableMaxDepthChecks') + ) { + $context->enableMaxDepthChecks(); + } + + return $this->serializer->deserialize( + $response->getBody(), + $command->getOperation()->getResponseClass(), + $serializerContentType, + $context + ); } return $this->serializer->deserialize( $response->getBody(), $command->getOperation()->getResponseClass(), - $serializerContentType, - $context + $serializerContentType ); } } diff --git a/Service/Command/LocationVisitor/Request/JMSSerializerBodyVisitor.php b/Service/Command/LocationVisitor/Request/JMSSerializerBodyVisitor.php index 125a24b..8e46fd8 100644 --- a/Service/Command/LocationVisitor/Request/JMSSerializerBodyVisitor.php +++ b/Service/Command/LocationVisitor/Request/JMSSerializerBodyVisitor.php @@ -65,22 +65,31 @@ public function visit(CommandInterface $command, RequestInterface $request, Para $contentType = 'xml'; break; } - $context = SerializationContext::create(); - if (null !== $groups = $param->getData('jms_serializer.groups')) { - $context->setGroups($groups); - } - if (null !== $version = $param->getData('jms_serializer.version')) { - $context->setVersion($version); - } - if (null !== $nulls = $param->getData('jms_serializer.serialize_nulls')) { - $context->setSerializeNull($nulls); - } - if (true === $param->getData('jms_serializer.max_depth_checks')) { - $context->enableMaxDepthChecks(); - } + if (true === class_exists('JMS\Serializer\SerializationContext')) { + $context = SerializationContext::create(); + + if (null !== $groups = $param->getData('jms_serializer.groups')) { + $context->setGroups($groups); + } + if (null !== $version = $param->getData('jms_serializer.version')) { + $context->setVersion($version); + } + if (null !== $nulls = $param->getData('jms_serializer.serialize_nulls')) { + $context->setSerializeNull($nulls); + } + if ( + true === $param->getData('jms_serializer.max_depth_checks') + && + true === method_exists('JMS\Serializer\SerializationContext', 'enableMaxDepthChecks') + ) { + $context->enableMaxDepthChecks(); + } - $value = $this->serializer->serialize($filteredValue, $contentType, $context); + $value = $this->serializer->serialize($filteredValue, $contentType, $context); + } else { + $value = $this->serializer->serialize($filteredValue, $contentType); + } } parent::visit($command, $request, $param, $value); diff --git a/Tests/Service/Command/JMSSerializerResponseParserTest.php b/Tests/Service/Command/JMSSerializerResponseParserTest.php index 891cdc2..6fa7164 100644 --- a/Tests/Service/Command/JMSSerializerResponseParserTest.php +++ b/Tests/Service/Command/JMSSerializerResponseParserTest.php @@ -20,10 +20,16 @@ class JMSSerializerResponseParserTest extends \PHPUnit_Framework_TestCase { public function testDeserializeContextConfiguration() { - $expectedContext = DeserializationContext::create(); - $expectedContext->setGroups('group'); - $expectedContext->setVersion(1); - $expectedContext->enableMaxDepthChecks(); + if (true === class_exists('JMS\Serializer\DeserializationContext')) { + $expectedContext = DeserializationContext::create(); + $expectedContext->setGroups('group'); + $expectedContext->setVersion(1); + if(true === method_exists('JMS\Serializer\DeserializationContext', 'enableMaxDepthChecks')) { + $expectedContext->enableMaxDepthChecks(); + } + } else { + $expectedContext = null; + } $operation = $this->getMock('Guzzle\Service\Description\OperationInterface'); $operation->expects($this->any())->method('getResponseType')->will($this->returnValue(OperationInterface::TYPE_CLASS)); @@ -46,8 +52,13 @@ public function testDeserializeContextConfiguration() $response->setBody('body'); $serializer = $this->getMock('JMS\Serializer\SerializerInterface'); - $serializer->expects($this->once())->method('deserialize') - ->with('body', 'ResponseClass', 'json', $this->equalTo($expectedContext)); + if(null === $expectedContext) { + $serializer->expects($this->once())->method('deserialize') + ->with('body', 'ResponseClass', 'json'); + } else { + $serializer->expects($this->once())->method('deserialize') + ->with('body', 'ResponseClass', 'json', $this->equalTo($expectedContext)); + } $parser = new JMSSerializerResponseParser($serializer, $this->getMock('Guzzle\Service\Command\ResponseParserInterface')); diff --git a/Tests/Service/Command/LocationVisitor/Request/JMSSerializerBodyVisitorTest.php b/Tests/Service/Command/LocationVisitor/Request/JMSSerializerBodyVisitorTest.php index d98258b..14f181d 100644 --- a/Tests/Service/Command/LocationVisitor/Request/JMSSerializerBodyVisitorTest.php +++ b/Tests/Service/Command/LocationVisitor/Request/JMSSerializerBodyVisitorTest.php @@ -18,11 +18,17 @@ class JMSSerializerBodyVisitorTest extends \PHPUnit_Framework_TestCase { public function testSerializeContextConfiguration() { - $expectedContext = SerializationContext::create(); - $expectedContext->setGroups('group'); - $expectedContext->setVersion(1); - $expectedContext->setSerializeNull(true); - $expectedContext->enableMaxDepthChecks(); + if (true === class_exists('JMS\Serializer\SerializationContext')) { + $expectedContext = SerializationContext::create(); + $expectedContext->setGroups('group'); + $expectedContext->setVersion(1); + $expectedContext->setSerializeNull(true); + if(true === method_exists('JMS\Serializer\SerializationContext', 'enableMaxDepthChecks')) { + $expectedContext->enableMaxDepthChecks(); + } + } else { + $expectedContext = null; + } $parameter = $this->getMock('Guzzle\Service\Description\Parameter'); $parameter->expects($this->once())->method('getSentAs')->will($this->returnValue('json')); @@ -45,9 +51,15 @@ public function testSerializeContextConfiguration() ->getMock(); $serializer = $this->getMock('JMS\Serializer\SerializerInterface'); - $serializer->expects($this->once())->method('serialize') - ->with(array(), 'json', $this->equalTo($expectedContext)) - ->will($this->returnValue('serialized')); + if(null === $expectedContext) { + $serializer->expects($this->once())->method('serialize') + ->with(array(), 'json') + ->will($this->returnValue('serialized')); + } else { + $serializer->expects($this->once())->method('serialize') + ->with(array(), 'json', $this->equalTo($expectedContext)) + ->will($this->returnValue('serialized')); + } $parser = new JMSSerializerBodyVisitor($serializer, $this->getMock('Guzzle\Service\Command\ResponseParserInterface'));