Skip to content

Commit

Permalink
Merge pull request #27 from Seriyyy95/patch-1
Browse files Browse the repository at this point in the history
Update SoapMessageEventSubscriber.php
  • Loading branch information
proggeler authored Feb 2, 2022
2 parents 7c80306 + b8b5701 commit d1490b5
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/SoapHeaderEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
);

Expand Down
7 changes: 6 additions & 1 deletion src/SoapMessageEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions tests/Fixtures/HeaderLoginWithPrefix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace DMT\Test\Soap\Serializer\Fixtures;

use JMS\Serializer\Annotation as JMS;

/**
* Class HeaderLogin
*
* @JMS\AccessType("public_method")
* @JMS\XmlNamespace(uri="http://xmpl-namespace.nl", prefix="")
* @JMS\XmlRoot("HeaderAuthenticate", namespace="http://xmpl-namespace.nl", prefix="ns")
*/
class HeaderLoginWithPrefix extends HeaderLogin
{
}
17 changes: 17 additions & 0 deletions tests/Fixtures/LanguageWithPrefix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace DMT\Test\Soap\Serializer\Fixtures;

use JMS\Serializer\Annotation as JMS;

/**
* Class ListLanguagesWithPrefix
*
* @JMS\AccessType("public_method")
* @JMS\XmlRoot("Language", namespace="http://xmpl-namespace.nl", prefix="ns")
*/
class LanguageWithPrefix extends Language
{
}
56 changes: 56 additions & 0 deletions tests/SoapHeaderEventSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use DMT\Soap\Serializer\SoapSerializationVisitorFactory;
use DMT\Test\Soap\Serializer\Fixtures\Count;
use DMT\Test\Soap\Serializer\Fixtures\HeaderLogin;
use DMT\Test\Soap\Serializer\Fixtures\HeaderLoginWithPrefix;
use DMT\Test\Soap\Serializer\Fixtures\Language;
use DMT\Test\Soap\Serializer\Fixtures\ListLanguages;
use Doctrine\Common\Annotations\AnnotationRegistry;
Expand Down Expand Up @@ -82,4 +83,59 @@ function (HandlerRegistry $registry) {
static::assertSame('dummy', strval($header->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);
}
}
27 changes: 27 additions & 0 deletions tests/SoapSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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 [
Expand Down

0 comments on commit d1490b5

Please sign in to comment.