Skip to content

Commit

Permalink
Add functional tests for debugging harder issues
Browse files Browse the repository at this point in the history
  • Loading branch information
veewee committed Aug 31, 2018
1 parent 89632ea commit 7cea335
Show file tree
Hide file tree
Showing 15 changed files with 533 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ You can customize the generated code based on the manual installation pages in t
- [Specify your data transfer handler.](docs/handlers.md)
- [SoapHandle](docs/handlers.md#soaphandle)
- [HTTPlugHandle](docs/handlers.md#httplughandle)
- [LocalSoapServerHandle](docs/handlers.md#localsoapserverhandle)
- [Configure one or multiple HTTP middlewares.](docs/middlewares.md)
- [BasicAuthMiddleware](docs/middlewares.md#basicauthmiddleware)
- [NtlmMiddleware](docs/middlewares.md#ntlmmiddleware)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"phpro/grumphp": "~0.11",
"phpspec/phpspec": "~3.2",
"phpspec/prophecy": "~1.7",
"phpunit/phpunit": "~6.0",
"phpunit/phpunit": "~7.3",
"psr/http-message": "^1.0",
"robrichards/wse-php": "^2.0.2",
"robrichards/xmlseclibs": "^3.0",
Expand Down
22 changes: 22 additions & 0 deletions docs/handlers.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Here is a list of built-in handlers:

- [SoapHandle](#soaphandle)
- [HttPlugHandle](#guzzlehandle)
- [LocalSoapServerHandle](#localsoapserverhandle)


## SoapHandle
Expand Down Expand Up @@ -63,3 +64,24 @@ $clientBuilder = new ClientBuilder($clientFactory, $wsdl, $soapOptions);
$clientBuilder->withHandler(HttPlugHandle::createForClient($httpClient));
$client = $clientBuilder->build();
```

## LocalSoapServerHandle

*Features: LastRequestInfoCollector*

The LocalSoapServerHandle can be used to link the soap-client to a local PHP SoapServer instance.
This handle can be used for testing purpose, it is not recommended to use it in production.

*NOTE: * Since SoapServer is sending headers, you want to run this handler in a separate process.
You can use `@runInSeparateProcess` in PHPunit.


**Configuration**
```php
$soapServer = new \SoapServer('some.wsdl', []);
$soapServer->setObject($someTestingImplementation);

$clientBuilder = new ClientBuilder($clientFactory, $wsdl, $soapOptions);
$clientBuilder->withHandler(new LocalSoapServerHandle($soapServer));
$client = $clientBuilder->build();
```
15 changes: 15 additions & 0 deletions docs/wsdl-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ $client = $clientBuilder->build();
Here is a list of built-in providers:

- [HttPlugWsdlProvider](#httplugwsdlprovider)
- [InMemoryWsdlProvider](#inmemorywsdlprovider)
- [LocalWsdlProvider](#localwsdlprovider)
- [MixedWsdlProvider](#mixedwsdlprovider)

Expand Down Expand Up @@ -78,6 +79,20 @@ soap.wsdl_cache_ttl: 86400
```


## InMemoryWsdlProvider

By using the in-memory WSDL provider, you can just use a complete XML version of the WSDL as source.
This one might come in handy during tests, but probably shouldn't be used in production.

**Configuration**
```php
$wsdl = '<definitions ..... />'
$clientBuilder = new ClientBuilder($clientFactory, $wsdl, $soapOptions);
$clientBuilder->withWsdlProvider(new InMemoryWsdlProvider());
$client = $clientBuilder->build();
```


## LocalWsdlProvider

The local WSDL provider can be used to load a local file.
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
<testsuite name="Integration">
<directory>./test/PhproTest/SoapClient/Integration</directory>
</testsuite>
<testsuite name="Functional">
<directory>./test/PhproTest/SoapClient/Functional</directory>
</testsuite>
</testsuites>
</phpunit>
25 changes: 25 additions & 0 deletions spec/Phpro/SoapClient/Wsdl/Provider/InMemoryWsdlProviderSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace spec\Phpro\SoapClient\Wsdl\Provider;

use Phpro\SoapClient\Wsdl\Provider\InMemoryWsdlProvider;
use Phpro\SoapClient\Wsdl\Provider\WsdlProviderInterface;
use PhpSpec\ObjectBehavior;

class InMemoryWsdlProviderSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType(InMemoryWsdlProvider::class);
}

function it_is_a_wsdl_provider()
{
$this->shouldImplement(WsdlProviderInterface::class);
}

function it_provides_an_in_memory_data_source()
{
$this->provide('source')->shouldBe('data://text/plain;base64,'.base64_encode('source'));
}
}
57 changes: 57 additions & 0 deletions src/Phpro/SoapClient/Soap/Handler/LocalSoapServerHandle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Phpro\SoapClient\Soap\Handler;

use Phpro\SoapClient\Soap\HttpBinding\LastRequestInfo;
use Phpro\SoapClient\Soap\HttpBinding\SoapRequest;
use Phpro\SoapClient\Soap\HttpBinding\SoapResponse;
use SoapServer;

class LocalSoapServerHandle implements HandlerInterface, LastRequestInfoCollectorInterface
{
/**
* @var SoapServer
*/
private $server;

/**
* @var LastRequestInfo
*/
private $lastRequestInfo;

public function __construct(SoapServer $server)
{
$this->server = $server;
$this->lastRequestInfo = LastRequestInfo::createEmpty();
}

/**
* @param SoapRequest $request
*
* @return SoapResponse
*/
public function request(SoapRequest $request): SoapResponse
{
ob_start();
$this->server->handle($request->getRequest());
$responseBody = ob_get_contents();
ob_end_clean();

$this->lastRequestInfo = new LastRequestInfo(
'',
$request->getRequest(),
'',
$responseBody
);

return new SoapResponse($responseBody);
}

/**
* @return LastRequestInfo
*/
public function collectLastRequestInfo(): LastRequestInfo
{
return $this->lastRequestInfo;
}
}
13 changes: 13 additions & 0 deletions src/Phpro/SoapClient/Wsdl/Provider/InMemoryWsdlProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare( strict_types=1 );

namespace Phpro\SoapClient\Wsdl\Provider;

class InMemoryWsdlProvider implements WsdlProviderInterface
{
public function provide(string $source): string
{
return 'data://text/plain;base64,'.base64_encode($source);
}
}
67 changes: 67 additions & 0 deletions test/PhproTest/SoapClient/Functional/AbstractSoapTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare( strict_types=1 );

namespace PhproTest\SoapClient\Functional;

use Phpro\SoapClient\Soap\Handler\LocalSoapServerHandle;
use Phpro\SoapClient\Soap\SoapClient as PhproSoapClient;
use Phpro\SoapClient\Wsdl\Provider\InMemoryWsdlProvider;
use PHPUnit\Framework\TestCase;
use SoapServer;

abstract class AbstractSoapTestCase extends TestCase
{
/**
* @var SoapServer
*/
protected $server;

/**
* @var PhproSoapClient
*/
protected $client;

/**
* @return SoapServer
*/
abstract protected function configureServer(SoapServer $server);

/**
* @return string|null
*/
abstract protected function getWsdl();
abstract protected function getSoapOptions(): array;

protected function setUp() {
$wsdl = $this->getWsdl();
$options = $this->getSoapOptions();

$this->server = new SoapServer($wsdl, $options);
$this->configureServer($this->server);

$this->client = new PhproSoapClient($wsdl, $options);
$this->client->setHandler(new LocalSoapServerHandle($this->server));
}

protected function generateInMemoryWsdl(string $wsdl): string
{
return (new InMemoryWsdlProvider())->provide($wsdl);
}

protected function provideBasicNonWsdlOptions(): array {
return [
'soap_version' => SOAP_1_2,
'uri' => 'http://localhost/dummysoap',
'location' => 'http://localhost/dummysoap',
'cache_wsdl' => WSDL_CACHE_NONE
];
}

protected function provideBasicWsdlOptions(): array {
return [
'soap_version' => SOAP_1_2,
'cache_wsdl' => WSDL_CACHE_NONE
];
}
}
66 changes: 66 additions & 0 deletions test/PhproTest/SoapClient/Functional/Encoding/Base64BinaryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare( strict_types=1 );

namespace PhproTest\SoapClient\Functional\Encoding;

use PhproTest\SoapClient\Functional\AbstractSoapTestCase;
use SoapServer;
use SoapVar;

class Base64BinaryTest extends AbstractSoapTestCase
{
protected function configureServer(SoapServer $server)
{
$server->setObject(new class() {
public function validate($input)
{
return [
'input' => $input,
'output' => 'output',
];
}
});
}

protected function getWsdl()
{
return FIXTURE_DIR . '/wsdl/functional/base64Binary.wsdl';
}

protected function getSoapOptions(): array {
return $this->provideBasicNonWsdlOptions();
}

/**
* @test
* @runInSeparateProcess
*/
function it_automatically_converts_base64_binary_fields()
{
$input = 'input';
$output = 'output';
$response = (array)$this->client->validate($input);

$this->assertEquals($output, $response['output']);
$this->assertEquals($input, $response['input']);
$this->assertContains(base64_encode($input), $this->client->__getLastRequest());
$this->assertContains(base64_encode($output), $this->client->__getLastResponse());
}

/**
* @test
* @runInSeparateProcess
*/
function it_automatically_converts_base64_binary_internal_types()
{
$input = 'input';
$output = 'output';
$response = (array)$this->client->validate(new SoapVar('input', XSD_BASE64BINARY));

$this->assertEquals($output, $response['output']);
$this->assertEquals($input, $response['input']);
$this->assertContains(base64_encode($input), $this->client->__getLastRequest());
$this->assertContains(base64_encode($output), $this->client->__getLastResponse());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare( strict_types=1 );

namespace PhproTest\SoapClient\Functional\Encoding;

use PhproTest\SoapClient\Functional\AbstractSoapTestCase;
use SoapServer;

class SimpleContentTest extends AbstractSoapTestCase
{
protected function configureServer(SoapServer $server)
{
$server->setObject(new class() {
public function validate($input)
{
return $input;
}
});
}

protected function getWsdl(): string
{
return FIXTURE_DIR . '/wsdl/functional/simpleContent.wsdl';
}

protected function getSoapOptions(): array {
return $this->provideBasicWsdlOptions();
}

/**
* @test
* @runInSeparateProcess
*/
function it_uses_underscores_internally_as_node_value_of_simple_content()
{
$input = $output = ['_' => 132, 'country' => 'BE'];
$response = (array) $this->client->validate($input);

$this->assertEquals($output, $response);
$this->assertContains('<input xsi:type="ns2:SimpleContent" country="BE">132</input>', $this->client->__getLastRequest());
$this->assertContains('<output xsi:type="ns2:SimpleContent" country="BE">132</output>', $this->client->__getLastResponse());
}
}
Loading

0 comments on commit 7cea335

Please sign in to comment.