Skip to content

Commit 4b88e8a

Browse files
authored
feature: Import some features from laravel HTTP (#131)
- Removed `getResponse` inCodeDredd\Soap\Client\Response - Added `collect` & 'onError' in CodeDredd\Soap\Client\Response - Updated `throw` in CodeDredd\Soap\Client\Response - Updated Docs - Added `assertSentInOrder` to Soap factory
1 parent 550f841 commit 4b88e8a

File tree

5 files changed

+73
-12
lines changed

5 files changed

+73
-12
lines changed

docs/client/authentication.md

+6
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,9 @@ Like Wss/Wsse it uses the same package:
4141
$response = Soap::baseWsdl(...)
4242
->withWsa()
4343
->call(...)
44+
45+
### DHL Cis Authentication
46+
47+
DHL uses his own authentication header
48+
49+
$client = Soap::withCisDHLAuth('user', 'signature')

docs/client/response.md

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ The `call` method returns an instance of `CodeDredd\Soap\Client\Response`, which
1111
$response->successful() : bool;
1212
$response->serverError() : bool;
1313
$response->clientError() : bool;
14+
$response->onError(callable $callback): \CodeDredd\Soap\Client\Response;
15+
$response->collect(): \Illuminate\Support\Collection;
1416

1517
The `CodeDredd\Soap\Client\Response` object also implements the PHP `ArrayAccess` interface, allowing you to access your response data directly on the response:
1618

docs/testing.md

+8
Original file line numberDiff line numberDiff line change
@@ -347,3 +347,11 @@ When faking responses, you may occasionally wish to inspect the requests the cli
347347

348348
Soap::assertSentCount(2);
349349
```
350+
351+
### :fontawesome-solid-jedi: ***assertSentInOrder***
352+
353+
> Assert that the given request was sent in the given order.
354+
355+
!!! info ""
356+
- **`Method`** : `#!php-inline function assertSentInOrder($callbacks)`
357+
- **`Return`** : `#!php-inline void`

src/Client/Response.php

+35-12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use CodeDredd\Soap\Exceptions\RequestException;
77
use CodeDredd\Soap\Xml\SoapXml;
88
use GuzzleHttp\Psr7\Response as Psr7Response;
9+
use Illuminate\Support\Collection;
910
use Illuminate\Support\Str;
1011
use Illuminate\Support\Traits\Macroable;
1112
use LogicException;
@@ -64,17 +65,6 @@ public static function fromSoapFault(\SoapFault $soapFault)
6465
return new self(new Psr7Response(400, [], $soapFault->getMessage()));
6566
}
6667

67-
/**
68-
* Get the full SOAP enveloppe response.
69-
*
70-
* @deprecated removed in v2 and replaced by toPsrResponse
71-
* @return string
72-
*/
73-
public function getResponse(): string
74-
{
75-
return $this->response;
76-
}
77-
7868
/**
7969
* Get the underlying PSR response for the response.
8070
*
@@ -95,6 +85,17 @@ public function object()
9585
return json_decode($this->body(), false);
9686
}
9787

88+
/**
89+
* Get the JSON decoded body of the response as a collection.
90+
*
91+
* @param string|null $key
92+
* @return \Illuminate\Support\Collection
93+
*/
94+
public function collect($key = null)
95+
{
96+
return Collection::make($this->json($key));
97+
}
98+
9899
/**
99100
* Get the body of the response.
100101
*
@@ -167,14 +168,21 @@ public function redirect()
167168
/**
168169
* Throw an exception if a server or client error occurred.
169170
*
171+
* @param \Closure|null $callback
170172
* @return $this
171173
*
172174
* @throws \CodeDredd\Soap\Exceptions\RequestException
173175
*/
174176
public function throw()
175177
{
178+
$callback = func_get_args()[0] ?? null;
179+
176180
if ($this->failed()) {
177-
throw new RequestException($this);
181+
throw tap(new RequestException($this), function ($exception) use ($callback) {
182+
if ($callback && is_callable($callback)) {
183+
$callback($this, $exception);
184+
}
185+
});
178186
}
179187

180188
return $this;
@@ -200,6 +208,21 @@ public function clientError()
200208
return $this->status() >= 400 && $this->status() < 500;
201209
}
202210

211+
/**
212+
* Execute the given callback if there was a server or client error.
213+
*
214+
* @param \Closure|callable $callback
215+
* @return \CodeDredd\Soap\Client\Response
216+
*/
217+
public function onError(callable $callback)
218+
{
219+
if ($this->failed()) {
220+
$callback($this);
221+
}
222+
223+
return $this;
224+
}
225+
203226
/**
204227
* Determine if the given offset exists.
205228
*

src/SoapTesting.php

+22
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,26 @@ public function assertSentCount($count)
102102
{
103103
PHPUnit::assertCount($count, $this->factory->getRecorded());
104104
}
105+
106+
/**
107+
* Assert that the given request was sent in the given order.
108+
*
109+
* @param array $callbacks
110+
* @return void
111+
*/
112+
public function assertSentInOrder($callbacks)
113+
{
114+
$this->assertSentCount(count($callbacks));
115+
116+
foreach ($callbacks as $index => $url) {
117+
$callback = is_callable($url) ? $url : function ($request) use ($url) {
118+
return $request->url() == $url;
119+
};
120+
121+
PHPUnit::assertTrue($callback(
122+
$this->factory->getRecorded()[$index][0],
123+
$this->factory->getRecorded()[$index][1]
124+
), 'An expected request (#'.($index + 1).') was not recorded.');
125+
}
126+
}
105127
}

0 commit comments

Comments
 (0)