Skip to content

Commit 74120b1

Browse files
authored
Argument check in unit Tests (#3)
* feat(v1): add argument request check for tests * refactor(v1): Code enhancement - Add some missing doc blocks - Reworked `XmlSerializer` - made argument-check in Request for testing possible
1 parent de6a5fb commit 74120b1

File tree

13 files changed

+346
-254
lines changed

13 files changed

+346
-254
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,17 +274,18 @@ When faking responses, you may occasionally wish to inspect the requests the cli
274274

275275
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:
276276

277-
Right now you can only check the action
278-
279277
Soap::fake();
280278

281279
Soap::withHeaders([
282280
'X-First' => 'foo',
283281
])->baseWsdl('http://test.com/v1?wsdl')
284-
->call('Get_Users');
282+
->call('Get_Users', [
283+
'name' => 'CodeDredd'
284+
]);
285285

286286
Soap::assertSent(function ($request) {
287-
return $request->action() === 'Get_Users';
287+
return $request->action() === 'Get_Users' &&
288+
$request->arguments() === ['name' => 'CodeDredd'];
288289
});
289290
//Or shortcut
290291
Soap::assertActionSent('Get_Users')

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"php": ">=7.2.0",
1414
"ext-soap": "*",
1515
"ext-json": "*",
16+
"ext-dom": "*",
17+
"ext-simplexml": "*",
1618
"illuminate/support": "^5.6 || ^6.0 || ^7.0",
1719
"phpro/soap-client": "^1.1",
1820
"php-http/guzzle6-adapter": "^2.0",

composer.lock

Lines changed: 9 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Client/Request.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
namespace CodeDredd\Soap\Client;
44

5-
use Phpro\SoapClient\Type\MultiArgumentRequest;
5+
use CodeDredd\Soap\Xml\SoapXml;
6+
use CodeDredd\Soap\Xml\XMLSerializer;
7+
use Illuminate\Support\Arr;
68

7-
class Request extends MultiArgumentRequest
9+
/**
10+
* Class Request
11+
* @package CodeDredd\Soap\Client
12+
*/
13+
class Request
814
{
915
/**
1016
* The underlying PSR request.
@@ -22,8 +28,6 @@ class Request extends MultiArgumentRequest
2228
public function __construct($request)
2329
{
2430
$this->request = $request;
25-
//@todo still need to get the arguments some how
26-
parent::__construct([]);
2731
}
2832

2933
/**
@@ -33,4 +37,30 @@ public function action(): string
3337
{
3438
return $this->request->getHeaderLine('SOAPAction');
3539
}
40+
41+
/**
42+
* @return \Psr\Http\Message\RequestInterface
43+
*/
44+
public function getRequest() {
45+
return $this->request;
46+
}
47+
48+
/**
49+
* Return complete xml request body
50+
*
51+
* @return string
52+
*/
53+
public function xmlContent() {
54+
return $this->request->getBody()->getContents();
55+
}
56+
57+
/**
58+
* Return request arguments
59+
*
60+
* @return array
61+
*/
62+
public function arguments(): array {
63+
$xml = SoapXml::fromString($this->xmlContent());
64+
return Arr::first(XMLSerializer::domNodeToArray($xml->getBody()));
65+
}
3666
}

src/Client/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use LogicException;
66
use ArrayAccess;
77
use CodeDredd\Soap\Exceptions\RequestException;
8-
use CodeDredd\Soap\XML\SoapXml;
8+
use CodeDredd\Soap\Xml\SoapXml;
99
use GuzzleHttp\Psr7\Response as Psr7Response;
1010
use Illuminate\Support\Str;
1111
use Illuminate\Support\Traits\Macroable;

src/Faker/EngineFaker.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace CodeDredd\Soap\Faker;
66

7-
use CodeDredd\Soap\XML\XMLSerializer;
7+
use CodeDredd\Soap\Xml\XMLSerializer;
88
use Phpro\SoapClient\Soap\Engine\DriverInterface;
99
use Phpro\SoapClient\Soap\Engine\EngineInterface;
1010
use Phpro\SoapClient\Soap\Engine\Metadata\MetadataInterface;
@@ -13,6 +13,10 @@
1313
use Phpro\SoapClient\Soap\HttpBinding\SoapRequest;
1414
use Phpro\SoapClient\Xml\SoapXml;
1515

16+
/**
17+
* Class EngineFaker
18+
* @package CodeDredd\Soap\Faker
19+
*/
1620
class EngineFaker implements EngineInterface
1721
{
1822
/**
@@ -25,8 +29,17 @@ class EngineFaker implements EngineInterface
2529
*/
2630
private $handler;
2731

32+
/**
33+
* @var string
34+
*/
2835
private $wsdl;
2936

37+
/**
38+
* EngineFaker constructor.
39+
* @param DriverInterface $driver
40+
* @param HandlerInterface $handler
41+
* @param string $wsdl
42+
*/
3043
public function __construct(
3144
DriverInterface $driver,
3245
HandlerInterface $handler,
@@ -37,24 +50,30 @@ public function __construct(
3750
$this->wsdl = $wsdl;
3851
}
3952

53+
/**
54+
* @return MetadataInterface
55+
*/
4056
public function getMetadata(): MetadataInterface
4157
{
4258
return $this->driver->getMetadata();
4359
}
4460

61+
/**
62+
* @param string $method
63+
* @param array $arguments
64+
* @return mixed
65+
*/
4566
public function request(string $method, array $arguments)
4667
{
47-
$arguments = [
48-
'SOAP-ENV:Body' => $arguments
49-
];
50-
$xml = new \SimpleXMLElement('<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"/>');
51-
XMLSerializer::arrayToXml($arguments, $xml);
52-
$request = new SoapRequest($xml->asXML(), $this->wsdl, $method, 1);
68+
$request = new SoapRequest(XMLSerializer::arrayToSoapXml($arguments), $this->wsdl, $method, 1);
5369
$response = $this->handler->request($request);
5470

5571
return json_decode($response->getResponse());
5672
}
5773

74+
/**
75+
* @return LastRequestInfo
76+
*/
5877
public function collectLastRequestInfo(): LastRequestInfo
5978
{
6079
return $this->handler->collectLastRequestInfo();

src/SoapFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Closure;
66
use CodeDredd\Soap\Client\Request;
77
use CodeDredd\Soap\Client\ResponseSequence;
8-
use CodeDredd\Soap\XML\XMLSerializer;
8+
use CodeDredd\Soap\Xml\XMLSerializer;
99
use GuzzleHttp\Psr7\Response as Psr7Response;
1010
use Illuminate\Support\Str;
1111
use Illuminate\Support\Traits\Macroable;

src/XML/XMLSerializer.php

Lines changed: 0 additions & 124 deletions
This file was deleted.
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
22

3-
namespace CodeDredd\Soap\XML;
3+
namespace CodeDredd\Soap\Xml;
44

55
class SoapXml extends \Phpro\SoapClient\Xml\SoapXml
66
{
77
/**
8+
* Get the error message from a SoapFault instance
9+
*
810
* @return string
911
*/
1012
public function getFaultMessage(): string
@@ -13,4 +15,4 @@ public function getFaultMessage(): string
1315

1416
return $list->length ? $list->item(0)->firstChild->nodeValue : 'No Fault Message found';
1517
}
16-
}
18+
}

0 commit comments

Comments
 (0)