Skip to content
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

Mock Exceptions #12

Merged
merged 2 commits into from
Apr 13, 2022
Merged
Changes from 1 commit
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
Next Next commit
Mock exceptions
  • Loading branch information
Sammyjo20 committed Apr 13, 2022
commit 55b5b8749a927cb4c2e9fa210775f39e0e7c256d
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();
});