Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for local elements when elementFormDefault is set #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions tests/Client/LocalGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php


namespace GoetasWebservices\SoapServices\SoapClient\Tests\Client;


use GoetasWebservices\WsdlToPhp\Generation\JmsSoapConverter;
use GoetasWebservices\WsdlToPhp\Tests\Generator;
use GoetasWebservices\Xsd\XsdToPhp\Jms\YamlConverter;

class LocalGenerator extends Generator
{
protected function generateJMSFiles(array $schemas, array $services)
{
$converter = new YamlConverter($this->namingStrategy);
$soapConverter = new JmsSoapConverter($converter);

$this->setNamespaces($converter);
$items = $converter->convert($schemas);
$items = array_merge($items, $soapConverter->visitServices($services));
return $items;
}
}
200 changes: 200 additions & 0 deletions tests/Client/LocalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<?php


namespace GoetasWebservices\SoapServices\SoapClient\Tests\Client;

use GoetasWebservices\SoapServices\SoapClient\Arguments\Headers\Handler\HeaderHandler;
use GoetasWebservices\SoapServices\SoapClient\ClientFactory;
use GoetasWebservices\SoapServices\SoapClient\Envelope\Handler\FaultHandler;
use GoetasWebservices\SoapServices\SoapClient\Envelope\SoapEnvelope\Parts\Fault;
use GoetasWebservices\SoapServices\SoapClient\Metadata\Generator\MetadataGenerator;
use GoetasWebservices\SoapServices\SoapClient\Metadata\Loader\DevMetadataLoader;
use GoetasWebservices\WsdlToPhp\Tests\Generator;
use GoetasWebservices\XML\SOAPReader\SoapReader;
use GoetasWebservices\XML\WSDLReader\DefinitionsReader;
use GoetasWebservices\Xsd\XsdToPhp\Naming\ShortNamingStrategy;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response;
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
use JMS\Serializer\Handler\HandlerRegistryInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;

class LocalTest extends TestCase
{
protected static $namespaces = [
'urn:example:local' => 'Example\Local'
];
/**
* @var Generator
*/
protected static $generator;

/**
* @var \GuzzleHttp\Handler\MockHandler
*/
protected $responseMock;

protected $requestResponseStack = [];

/**
* @var ClientFactory
*/
private $factory;

public static function setUpBeforeClass(): void
{
self::$generator = new LocalGenerator(self::$namespaces);
self::$generator->generate([__DIR__ . '/../Fixtures/Local/local.wsdl']);
self::$generator->registerAutoloader();
}

public static function tearDownAfterClass(): void
{
self::$generator->unRegisterAutoloader();
//self::$generator->cleanDirectories();
}

public function setUp(): void
{
$ref = new \ReflectionClass(Fault::class);
$headerHandler = new HeaderHandler();
$serializer = self::$generator->buildSerializer(function (HandlerRegistryInterface $h) use ($headerHandler) {
$h->registerSubscribingHandler($headerHandler);
$h->registerSubscribingHandler(new FaultHandler());
}, [
'GoetasWebservices\SoapServices\SoapClient\Envelope\SoapEnvelope12' => dirname($ref->getFileName()) . '/../../../Resources/metadata/jms12',
'GoetasWebservices\SoapServices\SoapClient\Envelope\SoapEnvelope' => dirname($ref->getFileName()) . '/../../../Resources/metadata/jms',
]);

$this->responseMock = new MockHandler();
$history = Middleware::history($this->requestResponseStack);

$handler = HandlerStack::create($this->responseMock);
$handler->push($history);

$guzzle = new Client(['handler' => $handler]);


$naming = new ShortNamingStrategy();
$dispatcher = new EventDispatcher();
$wsdlReader = new DefinitionsReader(null, $dispatcher);
$soapReader = new SoapReader();
$dispatcher->addSubscriber($soapReader);

$metadataGenerator = new MetadataGenerator($naming, self::$namespaces);
$metadataLoader = new DevMetadataLoader($metadataGenerator, $soapReader, $wsdlReader);


$this->factory = new ClientFactory($metadataLoader, $serializer);
$this->factory->setHttpClient(new GuzzleAdapter($guzzle));
$this->factory->setHeaderHandler($headerHandler);
}

public function testLocalCheckOperation()
{
$httpResponse = new Response(200, ['Content-Type' => 'application/soap+xml'], '
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:urn="urn:example:local">
<soap:Header/>
<soap:Body>
<urn:element2>
<data>
<Id>2</Id>
</data>
</urn:element2>
</soap:Body>
</soap:Envelope>');

$this->responseMock->append($httpResponse);

$client = $this->factory->getClient(__DIR__ . '/../Fixtures/Local/local.wsdl');
$this->assertNull($client->__getLastRequestMessage());

$element1 = "Example\Local\Element1";
$element2 = "Example\Local\Element2";
$type2Type = "Example\Local\Type2Type";

$input = new $element1();
$e2 = new $element2();

$data = new $type2Type();
$data->setId(1);
$e2->setData($data);
$input->setElement2($e2);

$response = $client->localCheckOperation($input);

$this->assertXmlStringEqualsXmlString(
'
<SOAP:Envelope xmlns:SOAP="http://www.w3.org/2003/05/soap-envelope">
<SOAP:Body>
<ns-9c385b9e:element1 xmlns:ns-9c385b9e="urn:example:local">
<ns-9c385b9e:element2>
<data>
<Id>1</Id>
</data>
</ns-9c385b9e:element2>
</ns-9c385b9e:element1>
</SOAP:Body>
</SOAP:Envelope>',
(string)$client->__getLastRequestMessage()->getBody());

$this->assertNotNull($response);
$this->assertNotNull($response->getData());
$this->assertEquals(2, $response->getData()->getId());
}

public function testLocalCheckOperationReversed()
{
$httpResponse = new Response(200, ['Content-Type' => 'application/soap+xml'], '
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:urn="urn:example:local">
<soap:Header/>
<soap:Body>
<urn:element1>
<urn:element2>
<data>
<Id>4</Id>
</data>
</urn:element2>
</urn:element1>
</soap:Body>
</soap:Envelope>');

$this->responseMock->append($httpResponse);

$client = $this->factory->getClient(__DIR__ . '/../Fixtures/Local/local.wsdl');
$this->assertNull($client->__getLastRequestMessage());

$element2 = "Example\Local\Element2";
$type2Type = "Example\Local\Type2Type";

$input = new $element2();
$data = new $type2Type();
$data->setId(3);
$input->setData($data);

$response = $client->localCheckOperationReversed($input);

$this->assertXmlStringEqualsXmlString(
'
<SOAP:Envelope xmlns:SOAP="http://www.w3.org/2003/05/soap-envelope">
<SOAP:Body>
<ns-9c385b9e:element2 xmlns:ns-9c385b9e="urn:example:local">
<data>
<Id>3</Id>
</data>
</ns-9c385b9e:element2>
</SOAP:Body>
</SOAP:Envelope>
',
(string)$client->__getLastRequestMessage()->getBody());

$this->assertNotNull($response);
$this->assertNotNull($response->getElement2());
$this->assertNotNull($response->getElement2()->getData());
$this->assertEquals(4, $response->getElement2()->getData()->getId());
}
}
56 changes: 56 additions & 0 deletions tests/Fixtures/Local/local.wsdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version='1.0' encoding='UTF-8'?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
xmlns:tns="urn:example:local"
targetNamespace="urn:example:local"
name="LocalService">
<wsdl:types>
<schema xmlns:tns="urn:example:local"
xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:example:local">
<import namespace="urn:example:local" schemaLocation="local.xsd"/>
</schema>
</wsdl:types>
<wsdl:message name="InputMessage">
<wsdl:part element="tns:element1" name="e1"/>
</wsdl:message>
<wsdl:message name="OutputMessage">
<wsdl:part element="tns:element2" name="e2"/>
</wsdl:message>
<wsdl:portType name="LocalCheckPortType">
<wsdl:operation name="LocalCheckOperation">
<wsdl:input message="tns:InputMessage" name="input"/>
<wsdl:output message="tns:OutputMessage" name="output"/>
</wsdl:operation>
<wsdl:operation name="LocalCheckOperationReversed">
<wsdl:input message="tns:OutputMessage" name="output"/>
<wsdl:output message="tns:InputMessage" name="input"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="LocalCheckBinding" type="tns:LocalCheckPortType">
<soap12:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="LocalCheckOperation">
<soap12:operation soapAction="LocalOperationAction" style="document"/>
<wsdl:input name="input">
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output name="output">
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="LocalCheckOperationReversed">
<soap12:operation soapAction="LocalOperationAction" style="document"/>
<wsdl:input name="output">
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output name="input">
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="LocalCheckService">
<wsdl:port name="LocalCheckBindingPort" binding="tns:LocalCheckBinding">
<soap12:address location="http://example.tld/LocalService/LocalCheckBindingPort"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
23 changes: 23 additions & 0 deletions tests/Fixtures/Local/local.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version='1.0' encoding='UTF-8'?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="urn:example:local"
attributeFormDefault="unqualified" elementFormDefault="unqualified"
targetNamespace="urn:example:local">
<xs:element name="element1" type="tns:type1"/>
<xs:element name="element2">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="data" type="tns:type2"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="type1">
<xs:sequence>
<xs:element minOccurs="0" ref="tns:element2"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="type2">
<xs:sequence>
<xs:element name="Id" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:schema>