Skip to content

Commit

Permalink
Merge pull request #12 from Sammyjo20/feature/mock-exceptions
Browse files Browse the repository at this point in the history
Mock Exceptions
  • Loading branch information
Sammyjo20 authored Apr 13, 2022
2 parents 0985f66 + 8858bed commit 3e8efe0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"php": "^8.0",
"illuminate/support": "^8.0|^9.0",
"illuminate/console": "^8.0|^9.0",
"sammyjo20/saloon": "^0.10.0"
"sammyjo20/saloon": "^0.11.0"
},
"extra": {
"laravel": {
Expand Down
28 changes: 28 additions & 0 deletions tests/Unit/MockClientAssertionsTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use GuzzleHttp\Exception\ConnectException;
use Sammyjo20\Saloon\Http\MockResponse;
use Sammyjo20\Saloon\Http\SaloonRequest;
use Sammyjo20\Saloon\Http\SaloonResponse;
Expand Down Expand Up @@ -131,3 +132,30 @@

Saloon::assertSentCount(3);
});

test('you can mock guzzle exceptions', function () {
Saloon::fake([
MockResponse::make(['name' => 'Sam']),
MockResponse::make(['name' => 'Patrick'])->throw(fn ($guzzleRequest) => new ConnectException('Unable to connect!', $guzzleRequest)),
]);

$okResponse = (new UserRequest)->send();

expect($okResponse->json())->toEqual(['name' => 'Sam']);

$this->expectException(ConnectException::class);
$this->expectExceptionMessage('Unable to connect!');

(new UserRequest())->send();
});

test('you can mock normal exceptions', function () {
Saloon::fake([
MockResponse::make(['name' => 'Michael'])->throw(new Exception('Custom Exception!')),
]);

$this->expectException(Exception::class);
$this->expectExceptionMessage('Custom Exception!');

(new UserRequest())->send();
});

0 comments on commit 3e8efe0

Please sign in to comment.