Skip to content

Argument check in unit Tests #3

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

Merged
merged 2 commits into from
Apr 22, 2020
Merged
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
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,17 +274,18 @@ When faking responses, you may occasionally wish to inspect the requests the cli

The `assertSent` method accepts a callback which will be given an `CodeDredd\Soap\Client\Request` instance and should return a boolean value indicating if the request matches your expectations. In order for the test to pass, at least one request must have been issued matching the given expectations:

Right now you can only check the action

Soap::fake();

Soap::withHeaders([
'X-First' => 'foo',
])->baseWsdl('http://test.com/v1?wsdl')
->call('Get_Users');
->call('Get_Users', [
'name' => 'CodeDredd'
]);

Soap::assertSent(function ($request) {
return $request->action() === 'Get_Users';
return $request->action() === 'Get_Users' &&
$request->arguments() === ['name' => 'CodeDredd'];
});
//Or shortcut
Soap::assertActionSent('Get_Users')
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"php": ">=7.2.0",
"ext-soap": "*",
"ext-json": "*",
"ext-dom": "*",
"ext-simplexml": "*",
"illuminate/support": "^5.6 || ^6.0 || ^7.0",
"phpro/soap-client": "^1.1",
"php-http/guzzle6-adapter": "^2.0",
Expand Down
16 changes: 9 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 34 additions & 4 deletions src/Client/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

namespace CodeDredd\Soap\Client;

use Phpro\SoapClient\Type\MultiArgumentRequest;
use CodeDredd\Soap\Xml\SoapXml;
use CodeDredd\Soap\Xml\XMLSerializer;
use Illuminate\Support\Arr;

class Request extends MultiArgumentRequest
/**
* Class Request
* @package CodeDredd\Soap\Client
*/
class Request
{
/**
* The underlying PSR request.
Expand All @@ -22,8 +28,6 @@ class Request extends MultiArgumentRequest
public function __construct($request)
{
$this->request = $request;
//@todo still need to get the arguments some how
parent::__construct([]);
}

/**
Expand All @@ -33,4 +37,30 @@ public function action(): string
{
return $this->request->getHeaderLine('SOAPAction');
}

/**
* @return \Psr\Http\Message\RequestInterface
*/
public function getRequest() {
return $this->request;
}

/**
* Return complete xml request body
*
* @return string
*/
public function xmlContent() {
return $this->request->getBody()->getContents();
}

/**
* Return request arguments
*
* @return array
*/
public function arguments(): array {
$xml = SoapXml::fromString($this->xmlContent());
return Arr::first(XMLSerializer::domNodeToArray($xml->getBody()));
}
}
2 changes: 1 addition & 1 deletion src/Client/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use LogicException;
use ArrayAccess;
use CodeDredd\Soap\Exceptions\RequestException;
use CodeDredd\Soap\XML\SoapXml;
use CodeDredd\Soap\Xml\SoapXml;
use GuzzleHttp\Psr7\Response as Psr7Response;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
Expand Down
33 changes: 26 additions & 7 deletions src/Faker/EngineFaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace CodeDredd\Soap\Faker;

use CodeDredd\Soap\XML\XMLSerializer;
use CodeDredd\Soap\Xml\XMLSerializer;
use Phpro\SoapClient\Soap\Engine\DriverInterface;
use Phpro\SoapClient\Soap\Engine\EngineInterface;
use Phpro\SoapClient\Soap\Engine\Metadata\MetadataInterface;
Expand All @@ -13,6 +13,10 @@
use Phpro\SoapClient\Soap\HttpBinding\SoapRequest;
use Phpro\SoapClient\Xml\SoapXml;

/**
* Class EngineFaker
* @package CodeDredd\Soap\Faker
*/
class EngineFaker implements EngineInterface
{
/**
Expand All @@ -25,8 +29,17 @@ class EngineFaker implements EngineInterface
*/
private $handler;

/**
* @var string
*/
private $wsdl;

/**
* EngineFaker constructor.
* @param DriverInterface $driver
* @param HandlerInterface $handler
* @param string $wsdl
*/
public function __construct(
DriverInterface $driver,
HandlerInterface $handler,
Expand All @@ -37,24 +50,30 @@ public function __construct(
$this->wsdl = $wsdl;
}

/**
* @return MetadataInterface
*/
public function getMetadata(): MetadataInterface
{
return $this->driver->getMetadata();
}

/**
* @param string $method
* @param array $arguments
* @return mixed
*/
public function request(string $method, array $arguments)
{
$arguments = [
'SOAP-ENV:Body' => $arguments
];
$xml = new \SimpleXMLElement('<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"/>');
XMLSerializer::arrayToXml($arguments, $xml);
$request = new SoapRequest($xml->asXML(), $this->wsdl, $method, 1);
$request = new SoapRequest(XMLSerializer::arrayToSoapXml($arguments), $this->wsdl, $method, 1);
$response = $this->handler->request($request);

return json_decode($response->getResponse());
}

/**
* @return LastRequestInfo
*/
public function collectLastRequestInfo(): LastRequestInfo
{
return $this->handler->collectLastRequestInfo();
Expand Down
2 changes: 1 addition & 1 deletion src/SoapFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Closure;
use CodeDredd\Soap\Client\Request;
use CodeDredd\Soap\Client\ResponseSequence;
use CodeDredd\Soap\XML\XMLSerializer;
use CodeDredd\Soap\Xml\XMLSerializer;
use GuzzleHttp\Psr7\Response as Psr7Response;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
Expand Down
124 changes: 0 additions & 124 deletions src/XML/XMLSerializer.php

This file was deleted.

6 changes: 4 additions & 2 deletions src/XML/SoapXml.php → src/Xml/SoapXml.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

namespace CodeDredd\Soap\XML;
namespace CodeDredd\Soap\Xml;

class SoapXml extends \Phpro\SoapClient\Xml\SoapXml
{
/**
* Get the error message from a SoapFault instance
*
* @return string
*/
public function getFaultMessage(): string
Expand All @@ -13,4 +15,4 @@ public function getFaultMessage(): string

return $list->length ? $list->item(0)->firstChild->nodeValue : 'No Fault Message found';
}
}
}
Loading