From 9dfeb8198f986c201c21a5fe7b4049a1baa93187 Mon Sep 17 00:00:00 2001 From: Robbert van den Bogerd Date: Fri, 8 Jan 2021 17:37:35 +0100 Subject: [PATCH 1/4] Pass soapEndpoint and soapAction attributes on the serializer context --- src/Client.php | 3 ++ tests/Client/Client12RequestResponsesTest.php | 36 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/Client.php b/src/Client.php index 945c2a0..6b9166f 100644 --- a/src/Client.php +++ b/src/Client.php @@ -131,6 +131,9 @@ public function __call(string $functionName, array $args) $bag = new HeadersOutgoing($headers); $context = SerializationContext::create()->setAttribute('headers_outgoing', $bag); + $context->setAttribute('soapAction', $soapOperation['action']); + $context->setAttribute('soapEndpoint', $this->serviceDefinition['endpoint']); + $xmlMessage = $this->serializer->serialize($message, 'xml', $context); $requestMessage = $this->createRequestMessage($xmlMessage, $soapOperation); diff --git a/tests/Client/Client12RequestResponsesTest.php b/tests/Client/Client12RequestResponsesTest.php index 0aa4f94..375d746 100644 --- a/tests/Client/Client12RequestResponsesTest.php +++ b/tests/Client/Client12RequestResponsesTest.php @@ -14,10 +14,14 @@ use GoetasWebservices\SoapServices\Metadata\Loader\DevMetadataLoader; use GoetasWebservices\SoapServices\SoapClient\Client; use GoetasWebservices\SoapServices\SoapClient\Exception\Fault12Exception; +use GoetasWebservices\WsdlToPhp\Tests\Generator; use GoetasWebservices\XML\SOAPReader\SoapReader; use GoetasWebservices\XML\WSDLReader\DefinitionsReader; use GoetasWebservices\Xsd\XsdToPhp\Naming\ShortNamingStrategy; use GuzzleHttp\Psr7\Response; +use JMS\Serializer\Context; +use JMS\Serializer\GraphNavigator; +use JMS\Serializer\Handler\HandlerRegistryInterface; use Symfony\Component\EventDispatcher\EventDispatcher; class Client12RequestResponsesTest extends RequestResponsesTest @@ -398,4 +402,36 @@ public function testApplicationError(int $code): void '), $detail->asXML()); } } + + public function testSerializerContextParametersAreAdded() + { + // Override serializer with a custom handler for asserting the context parameters + $handlers = function (HandlerRegistryInterface $h) { + $h->registerHandler(GraphNavigator::DIRECTION_SERIALIZATION, \Ex\SoapEnvelope12\Messages\GetSimpleInput::class, 'xml', + function($visitor, \Ex\SoapEnvelope12\Messages\GetSimpleInput $obj, array $type, Context $context) { + + self::assertTrue($context->hasAttribute('soapEndpoint'), 'The "soapEndpoint" attribute was not found on the context object'); + self::assertTrue($context->hasAttribute('soapAction'), 'The "soapAction" attribute was not found on the context object'); + self::assertEquals('http://www.example.org/12', $context->getAttribute('soapEndpoint')); + self::assertEquals('http://www.example.org/test/getSimple', $context->getAttribute('soapAction')); + + throw new SerializerHandlerAssertionsWereExecuted('Stop serialization, test has finished'); + }); + }; + $ref = new \ReflectionClass(Fault::class); + $serializer = self::$generator->buildSerializer($handlers, [ + 'GoetasWebservices\SoapServices\Metadata\Envelope\SoapEnvelope12' => dirname($ref->getFileName()) . '/../../../Resources/metadata/jms12', + 'GoetasWebservices\SoapServices\Metadata\Envelope\SoapEnvelope' => dirname($ref->getFileName()) . '/../../../Resources/metadata/jms', + ]); + + $this->factory->setSerializer($serializer); + $client = $this->getClient(); + + // Assert that subscribing handler with assertions was executed + self::expectException(SerializerHandlerAssertionsWereExecuted::class); + + $client->getSimple('foo'); + } } + +class SerializerHandlerAssertionsWereExecuted extends \Exception {}; \ No newline at end of file From 9243a4f5c48823bf7f1a3ecacf5b86ad53588e87 Mon Sep 17 00:00:00 2001 From: Robbert van den Bogerd Date: Sun, 10 Jan 2021 22:41:08 +0100 Subject: [PATCH 2/4] expose handlerRegistry on test instance --- tests/Client/Client12RequestResponsesTest.php | 33 +++++++------------ tests/Client/RequestResponsesTest.php | 8 ++++- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/tests/Client/Client12RequestResponsesTest.php b/tests/Client/Client12RequestResponsesTest.php index 375d746..8e410f1 100644 --- a/tests/Client/Client12RequestResponsesTest.php +++ b/tests/Client/Client12RequestResponsesTest.php @@ -14,7 +14,6 @@ use GoetasWebservices\SoapServices\Metadata\Loader\DevMetadataLoader; use GoetasWebservices\SoapServices\SoapClient\Client; use GoetasWebservices\SoapServices\SoapClient\Exception\Fault12Exception; -use GoetasWebservices\WsdlToPhp\Tests\Generator; use GoetasWebservices\XML\SOAPReader\SoapReader; use GoetasWebservices\XML\WSDLReader\DefinitionsReader; use GoetasWebservices\Xsd\XsdToPhp\Naming\ShortNamingStrategy; @@ -405,26 +404,18 @@ public function testApplicationError(int $code): void public function testSerializerContextParametersAreAdded() { - // Override serializer with a custom handler for asserting the context parameters - $handlers = function (HandlerRegistryInterface $h) { - $h->registerHandler(GraphNavigator::DIRECTION_SERIALIZATION, \Ex\SoapEnvelope12\Messages\GetSimpleInput::class, 'xml', - function($visitor, \Ex\SoapEnvelope12\Messages\GetSimpleInput $obj, array $type, Context $context) { - - self::assertTrue($context->hasAttribute('soapEndpoint'), 'The "soapEndpoint" attribute was not found on the context object'); - self::assertTrue($context->hasAttribute('soapAction'), 'The "soapAction" attribute was not found on the context object'); - self::assertEquals('http://www.example.org/12', $context->getAttribute('soapEndpoint')); - self::assertEquals('http://www.example.org/test/getSimple', $context->getAttribute('soapAction')); - - throw new SerializerHandlerAssertionsWereExecuted('Stop serialization, test has finished'); - }); - }; - $ref = new \ReflectionClass(Fault::class); - $serializer = self::$generator->buildSerializer($handlers, [ - 'GoetasWebservices\SoapServices\Metadata\Envelope\SoapEnvelope12' => dirname($ref->getFileName()) . '/../../../Resources/metadata/jms12', - 'GoetasWebservices\SoapServices\Metadata\Envelope\SoapEnvelope' => dirname($ref->getFileName()) . '/../../../Resources/metadata/jms', - ]); - - $this->factory->setSerializer($serializer); + $this->handlerRegistry->registerHandler(GraphNavigator::DIRECTION_SERIALIZATION, \Ex\SoapEnvelope12\Messages\GetSimpleInput::class, 'xml', + function($visitor, \Ex\SoapEnvelope12\Messages\GetSimpleInput $obj, array $type, Context $context) { + + self::assertTrue($context->hasAttribute('soapEndpoint'), 'The "soapEndpoint" attribute was not found on the context object'); + self::assertTrue($context->hasAttribute('soapAction'), 'The "soapAction" attribute was not found on the context object'); + self::assertEquals('http://www.example.org/12', $context->getAttribute('soapEndpoint')); + self::assertEquals('http://www.example.org/test/getSimple', $context->getAttribute('soapAction')); + + throw new SerializerHandlerAssertionsWereExecuted('Stop serialization, test has finished'); + } + ); + $client = $this->getClient(); // Assert that subscribing handler with assertions was executed diff --git a/tests/Client/RequestResponsesTest.php b/tests/Client/RequestResponsesTest.php index a68a79d..73af59a 100644 --- a/tests/Client/RequestResponsesTest.php +++ b/tests/Client/RequestResponsesTest.php @@ -58,6 +58,11 @@ abstract class RequestResponsesTest extends TestCase */ protected $factory; + /** + * @var HandlerRegistryInterface + */ + protected $handlerRegistry; + public static function setUpBeforeClass(): void { self::$generator = new Generator(self::$namespaces, [], __DIR__ . '/tmp'); @@ -81,8 +86,9 @@ public function setUp(): void $d->addSubscriber($headerHandler); }; - $handlers = static function (HandlerRegistryInterface $h) use ($headerHandler): void { + $handlers = function (HandlerRegistryInterface $h) use ($headerHandler): void { $h->registerSubscribingHandler($headerHandler); + $this->handlerRegistry = $h; }; $serializer = self::$generator->buildSerializer($handlers, [ From aee0cfc009a84e601f43be291abff3cf81614475 Mon Sep 17 00:00:00 2001 From: Robbert van den Bogerd Date: Sun, 10 Jan 2021 23:32:33 +0100 Subject: [PATCH 3/4] Expose soapOperation array as context attribute on serializer --- src/Client.php | 2 +- tests/Client/Client12RequestResponsesTest.php | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Client.php b/src/Client.php index 6b9166f..200e072 100644 --- a/src/Client.php +++ b/src/Client.php @@ -131,7 +131,7 @@ public function __call(string $functionName, array $args) $bag = new HeadersOutgoing($headers); $context = SerializationContext::create()->setAttribute('headers_outgoing', $bag); - $context->setAttribute('soapAction', $soapOperation['action']); + $context->setAttribute('soapOperation', $soapOperation); $context->setAttribute('soapEndpoint', $this->serviceDefinition['endpoint']); $xmlMessage = $this->serializer->serialize($message, 'xml', $context); diff --git a/tests/Client/Client12RequestResponsesTest.php b/tests/Client/Client12RequestResponsesTest.php index 8e410f1..c83f471 100644 --- a/tests/Client/Client12RequestResponsesTest.php +++ b/tests/Client/Client12RequestResponsesTest.php @@ -20,7 +20,6 @@ use GuzzleHttp\Psr7\Response; use JMS\Serializer\Context; use JMS\Serializer\GraphNavigator; -use JMS\Serializer\Handler\HandlerRegistryInterface; use Symfony\Component\EventDispatcher\EventDispatcher; class Client12RequestResponsesTest extends RequestResponsesTest @@ -408,9 +407,11 @@ public function testSerializerContextParametersAreAdded() function($visitor, \Ex\SoapEnvelope12\Messages\GetSimpleInput $obj, array $type, Context $context) { self::assertTrue($context->hasAttribute('soapEndpoint'), 'The "soapEndpoint" attribute was not found on the context object'); - self::assertTrue($context->hasAttribute('soapAction'), 'The "soapAction" attribute was not found on the context object'); self::assertEquals('http://www.example.org/12', $context->getAttribute('soapEndpoint')); - self::assertEquals('http://www.example.org/test/getSimple', $context->getAttribute('soapAction')); + + self::assertTrue($context->hasAttribute('soapOperation'), 'The "soapOperation" attribute was not found on the context object'); + self::assertIsArray($context->getAttribute('soapOperation')); + self::assertEquals('http://www.example.org/test/getSimple', $context->getAttribute('soapOperation')['action']); throw new SerializerHandlerAssertionsWereExecuted('Stop serialization, test has finished'); } From dab63a9e404be1b2332eccbf5a7a0b9bdc823e3c Mon Sep 17 00:00:00 2001 From: Robbert van den Bogerd Date: Mon, 11 Jan 2021 09:32:16 +0100 Subject: [PATCH 4/4] Make tests phpunit 7 compatible --- tests/Client/Client12RequestResponsesTest.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tests/Client/Client12RequestResponsesTest.php b/tests/Client/Client12RequestResponsesTest.php index c83f471..c210ef9 100644 --- a/tests/Client/Client12RequestResponsesTest.php +++ b/tests/Client/Client12RequestResponsesTest.php @@ -406,12 +406,15 @@ public function testSerializerContextParametersAreAdded() $this->handlerRegistry->registerHandler(GraphNavigator::DIRECTION_SERIALIZATION, \Ex\SoapEnvelope12\Messages\GetSimpleInput::class, 'xml', function($visitor, \Ex\SoapEnvelope12\Messages\GetSimpleInput $obj, array $type, Context $context) { - self::assertTrue($context->hasAttribute('soapEndpoint'), 'The "soapEndpoint" attribute was not found on the context object'); - self::assertEquals('http://www.example.org/12', $context->getAttribute('soapEndpoint')); + $this->assertTrue($context->hasAttribute('soapEndpoint'), 'The "soapEndpoint" attribute was not found on the context object'); + $this->assertEquals('http://www.example.org/12', $context->getAttribute('soapEndpoint')); - self::assertTrue($context->hasAttribute('soapOperation'), 'The "soapOperation" attribute was not found on the context object'); - self::assertIsArray($context->getAttribute('soapOperation')); - self::assertEquals('http://www.example.org/test/getSimple', $context->getAttribute('soapOperation')['action']); + $this->assertTrue($context->hasAttribute('soapOperation'), 'The "soapOperation" attribute was not found on the context object'); + $this->assertTrue( + is_array($context->getAttribute('soapOperation')), + 'The "soapOperation" attribute is not of type array, but '.gettype($context->getAttribute('soapOperation')) + ); + $this->assertEquals('http://www.example.org/test/getSimple', $context->getAttribute('soapOperation')['action']); throw new SerializerHandlerAssertionsWereExecuted('Stop serialization, test has finished'); } @@ -420,7 +423,7 @@ function($visitor, \Ex\SoapEnvelope12\Messages\GetSimpleInput $obj, array $type, $client = $this->getClient(); // Assert that subscribing handler with assertions was executed - self::expectException(SerializerHandlerAssertionsWereExecuted::class); + $this->expectException(SerializerHandlerAssertionsWereExecuted::class); $client->getSimple('foo'); }