Skip to content

Commit 94bcdb2

Browse files
authored
Merge pull request #137 from Sammyjo20/feature/v2-mock-exceptions
Feature | V2 - Added back throwing mock exceptions
2 parents 3d3385b + ff70a96 commit 94bcdb2

File tree

6 files changed

+42
-47
lines changed

6 files changed

+42
-47
lines changed

src/Http/Faking/SimulatedResponsePayload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public function throwsException(): bool
158158
*/
159159
public function getException(PendingRequest $pendingRequest): ?Throwable
160160
{
161-
if (is_null($this->responseException)) {
161+
if (! $this->throwsException()) {
162162
return null;
163163
}
164164

src/Http/Senders/SimulatedSender.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ class SimulatedSender implements Sender
2121
/**
2222
* Send the request.
2323
*
24-
* @param PendingRequest $pendingRequest
24+
* @param \Saloon\Contracts\PendingRequest $pendingRequest
2525
* @param bool $asynchronous
26-
* @return Response|PromiseInterface
26+
* @return \Saloon\Contracts\Response|\GuzzleHttp\Promise\PromiseInterface
2727
* @throws \Saloon\Exceptions\SenderException
28+
* @throws \Throwable
2829
*/
2930
public function sendRequest(PendingRequest $pendingRequest, bool $asynchronous = false): ResponseContract|PromiseInterface
3031
{
@@ -34,10 +35,17 @@ public function sendRequest(PendingRequest $pendingRequest, bool $asynchronous =
3435
throw new SenderException('Simulated response payload must be present on the pending request instance');
3536
}
3637

37-
// Let's create our response!
38+
// Check if the SimulatedResponsePayload throws an exception. If the request is
39+
// asynchronous, then we should allow the promise handler to deal with the exception.
3840

3941
$exception = $simulatedResponsePayload->getException($pendingRequest);
4042

43+
if ($exception instanceof Throwable && $asynchronous === false) {
44+
throw $exception;
45+
}
46+
47+
// Let's create our response!
48+
4149
/** @var class-string<\Saloon\Contracts\Response> $responseClass */
4250
$responseClass = $pendingRequest->getResponseClass();
4351

tests/Feature/GuzzleMiddlewareTest.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

tests/Feature/GuzzleSenderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
declare(strict_types=1);
44

5+
use GuzzleHttp\Utils;
56
use GuzzleHttp\Client;
67
use GuzzleHttp\Psr7\Uri;
78
use GuzzleHttp\HandlerStack;
@@ -96,3 +97,14 @@
9697

9798
expect($freshClient->getConfig())->toEqual($client->getConfig());
9899
});
100+
101+
test('you can set a custom handler stack on the guzzle sender', function () {
102+
$connector = new TestConnector;
103+
$sender = $connector->sender();
104+
105+
$handlerStack = new HandlerStack(Utils::chooseHandler());
106+
107+
$sender->setHandlerStack($handlerStack);
108+
109+
expect($sender->getHandlerStack())->toBe($handlerStack);
110+
});

tests/Unit/MockClientTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@
247247

248248
$response = connector()->send(new UserRequest, $mockClient);
249249
$response->throw();
250-
})->skip();
250+
});
251251

252252
test('you can mock normal exceptions', function () {
253253
$mockClient = new MockClient([
@@ -259,4 +259,4 @@
259259

260260
$response = connector()->send(new UserRequest, $mockClient);
261261
$response->throw();
262-
})->skip();
262+
});

tests/Unit/SaloonResponseTest.php

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

55
use GuzzleHttp\Psr7\Response;
66
use Saloon\Http\PendingRequest;
7+
use Saloon\Contracts\ArrayStore;
78
use Illuminate\Support\Collection;
89
use Saloon\Http\Faking\MockClient;
910
use Saloon\Http\Faking\MockResponse;
@@ -203,17 +204,27 @@
203204

204205
test('the headers method returns an array store', function () {
205206
$mockClient = new MockClient([
206-
MockResponse::make(['name' => 'Sam', 'work' => 'Codepotato'], ['X-Greeting' => 'Howdy']),
207+
MockResponse::make(['name' => 'Sam', 'work' => 'Codepotato'], 200, ['X-Greeting' => 'Howdy']),
207208
]);
208209

209210
$response = connector()->send(new UserRequest, $mockClient);
210211

211-
dd($response->headers());
212-
})->skip('SAM TODO');
212+
expect($response->headers())->toBeInstanceOf(ArrayStore::class);
213+
});
213214

214215
test('headers with a single value will have just the string value but headers with multiple values will be an array', function () {
215-
//
216-
})->skip('SAM TODO');
216+
$mockClient = new MockClient([
217+
MockResponse::make(['name' => 'Sam', 'work' => 'Codepotato'], 200, ['X-Greeting' => 'Howdy', 'X-Farewell' => ['Goodbye', 'Sam']]),
218+
]);
219+
220+
$response = connector()->send(new UserRequest, $mockClient);
221+
222+
expect($response->headers()->get('X-Greeting'))->toEqual('Howdy');
223+
expect($response->headers()->get('X-Farewell'))->toEqual(['Goodbye', 'Sam']);
224+
225+
expect($response->header('X-Greeting'))->toEqual('Howdy');
226+
expect($response->header('X-Farewell'))->toEqual(['Goodbye', 'Sam']);
227+
});
217228

218229
test('the dom method will return a crawler instance', function () {
219230
$dom = '<p>Howdy <i>Partner</i></p>';

0 commit comments

Comments
 (0)