Skip to content

feature: Import some features from laravel HTTP #131

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 1 commit into from
Apr 16, 2021
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
6 changes: 6 additions & 0 deletions docs/client/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,9 @@ Like Wss/Wsse it uses the same package:
$response = Soap::baseWsdl(...)
->withWsa()
->call(...)

### DHL Cis Authentication

DHL uses his own authentication header

$client = Soap::withCisDHLAuth('user', 'signature')
2 changes: 2 additions & 0 deletions docs/client/response.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ The `call` method returns an instance of `CodeDredd\Soap\Client\Response`, which
$response->successful() : bool;
$response->serverError() : bool;
$response->clientError() : bool;
$response->onError(callable $callback): \CodeDredd\Soap\Client\Response;
$response->collect(): \Illuminate\Support\Collection;

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

Expand Down
8 changes: 8 additions & 0 deletions docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,11 @@ When faking responses, you may occasionally wish to inspect the requests the cli

Soap::assertSentCount(2);
```

### :fontawesome-solid-jedi: ***assertSentInOrder***

> Assert that the given request was sent in the given order.

!!! info ""
- **`Method`** : `#!php-inline function assertSentInOrder($callbacks)`
- **`Return`** : `#!php-inline void`
47 changes: 35 additions & 12 deletions src/Client/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use CodeDredd\Soap\Exceptions\RequestException;
use CodeDredd\Soap\Xml\SoapXml;
use GuzzleHttp\Psr7\Response as Psr7Response;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use LogicException;
Expand Down Expand Up @@ -64,17 +65,6 @@ public static function fromSoapFault(\SoapFault $soapFault)
return new self(new Psr7Response(400, [], $soapFault->getMessage()));
}

/**
* Get the full SOAP enveloppe response.
*
* @deprecated removed in v2 and replaced by toPsrResponse
* @return string
*/
public function getResponse(): string
{
return $this->response;
}

/**
* Get the underlying PSR response for the response.
*
Expand All @@ -95,6 +85,17 @@ public function object()
return json_decode($this->body(), false);
}

/**
* Get the JSON decoded body of the response as a collection.
*
* @param string|null $key
* @return \Illuminate\Support\Collection
*/
public function collect($key = null)
{
return Collection::make($this->json($key));
}

/**
* Get the body of the response.
*
Expand Down Expand Up @@ -167,14 +168,21 @@ public function redirect()
/**
* Throw an exception if a server or client error occurred.
*
* @param \Closure|null $callback
* @return $this
*
* @throws \CodeDredd\Soap\Exceptions\RequestException
*/
public function throw()
{
$callback = func_get_args()[0] ?? null;

if ($this->failed()) {
throw new RequestException($this);
throw tap(new RequestException($this), function ($exception) use ($callback) {
if ($callback && is_callable($callback)) {
$callback($this, $exception);
}
});
}

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

/**
* Execute the given callback if there was a server or client error.
*
* @param \Closure|callable $callback
* @return \CodeDredd\Soap\Client\Response
*/
public function onError(callable $callback)
{
if ($this->failed()) {
$callback($this);
}

return $this;
}

/**
* Determine if the given offset exists.
*
Expand Down
22 changes: 22 additions & 0 deletions src/SoapTesting.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,26 @@ public function assertSentCount($count)
{
PHPUnit::assertCount($count, $this->factory->getRecorded());
}

/**
* Assert that the given request was sent in the given order.
*
* @param array $callbacks
* @return void
*/
public function assertSentInOrder($callbacks)
{
$this->assertSentCount(count($callbacks));

foreach ($callbacks as $index => $url) {
$callback = is_callable($url) ? $url : function ($request) use ($url) {
return $request->url() == $url;
};

PHPUnit::assertTrue($callback(
$this->factory->getRecorded()[$index][0],
$this->factory->getRecorded()[$index][1]
), 'An expected request (#'.($index + 1).') was not recorded.');
}
}
}