diff --git a/src/SoapHeaderEventSubscriber.php b/src/SoapHeaderEventSubscriber.php index fb6f890..173800f 100644 --- a/src/SoapHeaderEventSubscriber.php +++ b/src/SoapHeaderEventSubscriber.php @@ -68,9 +68,15 @@ public function addSoapHeader(ObjectEvent $event) $document->firstChild->firstChild ) ); + + $tagName = $metadata->xmlRootName; + if ($metadata->xmlRootPrefix !== null) { + $tagName = $metadata->xmlRootPrefix . ':' . $metadata->xmlRootName; + } + $visitor->setCurrentNode( $header->appendChild( - $document->createElementNS($metadata->xmlRootNamespace, $metadata->xmlRootName) + $document->createElementNS($metadata->xmlRootNamespace, $tagName) ) ); diff --git a/src/SoapMessageEventSubscriber.php b/src/SoapMessageEventSubscriber.php index 9216942..8d6c9d7 100644 --- a/src/SoapMessageEventSubscriber.php +++ b/src/SoapMessageEventSubscriber.php @@ -56,8 +56,13 @@ public function addMessage(PreSerializeEvent $event) throw new RuntimeException('Missing XmlRootName or XmlRootNamespace for ' . $event->getType()['name']); } + $tagName = $metadata->xmlRootName; + if ($metadata->xmlRootPrefix !== null) { + $tagName = $metadata->xmlRootPrefix . ':' . $metadata->xmlRootName; + } + $document = $visitor->getDocument(); - $message = $document->createElementNS($metadata->xmlRootNamespace, $metadata->xmlRootName); + $message = $document->createElementNS($metadata->xmlRootNamespace, $tagName); $visitor->getCurrentNode()->appendChild($message); $visitor->setCurrentNode($message); diff --git a/tests/Fixtures/HeaderLoginWithPrefix.php b/tests/Fixtures/HeaderLoginWithPrefix.php new file mode 100644 index 0000000..2080707 --- /dev/null +++ b/tests/Fixtures/HeaderLoginWithPrefix.php @@ -0,0 +1,18 @@ +username)); static::assertSame('secret123!', strval($header->password)); } + + /** + * Test the SOAP Header is added, when provided. + */ + public function testAddingSoapHeaderWithPrefix() + { + AnnotationRegistry::registerUniqueLoader('class_exists'); + + $serializer = SerializerBuilder::create() + ->setSerializationVisitor('soap', new SoapSerializationVisitorFactory()) + ->setPropertyNamingStrategy( + new SerializedNameAnnotationStrategy( + new IdenticalPropertyNamingStrategy() + ) + ) + ->configureListeners( + function (EventDispatcher $dispatcher) { + $dispatcher->addSubscriber( + new SoapMessageEventSubscriber() + ); + $dispatcher->addSubscriber( + new SoapHeaderEventSubscriber( + new HeaderLoginWithPrefix('dummy', 'secret123!') + ) + ); + } + ) + ->configureHandlers( + function (HandlerRegistry $registry) { + $registry->registerSubscribingHandler(new SoapDateHandler()); + } + ) + ->build(); + + $languages = new ListLanguages(); + $languages->setLanguages([ + new Language('Python', 33, new DateTime('1994-01-25')), + new Language('Perl', 40, new DateTime('1987-12-18')) + ]); + $languages->setCount(new Count(2)); + + $serialized = $serializer->serialize($languages, 'soap'); + + $doc = new \DOMDocument(); + $doc->loadXML($serialized); + + $xpath = new \DOMXPath($doc); + $node = $xpath->query('//*[local-name()="HeaderAuthenticate" and namespace-uri()="http://xmpl-namespace.nl"]')[0]; + $username = $xpath->query('//*[local-name()="username" and namespace-uri()="http://xmpl-namespace.nl"]')[0]; + $password = $xpath->query('//*[local-name()="password" and namespace-uri()="http://xmpl-namespace.nl"]')[0]; + + static::assertSame('ns', $node->prefix); + static::assertSame('ns', $username->prefix); + static::assertSame('ns', $password->prefix); + } } diff --git a/tests/SoapSerializationTest.php b/tests/SoapSerializationTest.php index d81e4be..82212e5 100644 --- a/tests/SoapSerializationTest.php +++ b/tests/SoapSerializationTest.php @@ -5,6 +5,7 @@ use DateTime; use DMT\Soap\Serializer\SoapNamespaceInterface; use DMT\Test\Soap\Serializer\Fixtures\Language; +use DMT\Test\Soap\Serializer\Fixtures\LanguageWithPrefix; use PHPUnit\Framework\TestCase; /** @@ -38,6 +39,32 @@ public function testSerialization(string $name, int $complexity, DateTime $date) static::assertSame($date->format('Y-m-d'), strval($message->since)); } + /** + * @dataProvider provideLanguage + * + * @param string $name + * @param int $complexity + * @param DateTime $date + */ + public function testSerializationWithPrefix(string $name, int $complexity, DateTime $date) + { + $serialized = $this->serializer->serialize(new LanguageWithPrefix($name, $complexity, $date), 'soap'); + + $doc = new \DOMDocument(); + $doc->loadXML($serialized); + + $xpath = new \DOMXPath($doc); + $node = $xpath->query('//*[local-name()="Language" and namespace-uri()="http://xmpl-namespace.nl"]')[0]; + $name = $xpath->query('//*[local-name()="name" and namespace-uri()="http://xmpl-namespace.nl"]')[0]; + $complexity = $xpath->query('//*[local-name()="complexity" and namespace-uri()="http://xmpl-namespace.nl"]')[0]; + $since = $xpath->query('//*[local-name()="since" and namespace-uri()="http://xmpl-namespace.nl"]')[0]; + + static::assertSame('ns', $node->prefix); + static::assertSame('ns', $name->prefix); + static::assertSame('ns', $complexity->prefix); + static::assertSame('ns', $since->prefix); + } + public function provideLanguage(): array { return [