When testing in Laravel, use the Saloon facade and call Saloon::fake() method before testing your application logic that makes a Saloon request. Saloon will automatically detect when an API request is about to be made and will respond with the fake response - stopping the real API request from being sent. This powerful feature allows you to test any feature in your application that uses Saloon requests.
The MockResponse class
The MockResponse class is used to create fake responses Saloon understands. It can accept data, a status, headers and config.
Basic sequence testing allows you to define a number of fake responses. When your application uses Saloon, it will pull out the next response in the sequence, removing it too.
useSammyjo20\SaloonLaravel\Facades\Saloon;useSammyjo20\Saloon\Http\MockResponse;Saloon::fake([MockResponse::make(['name'=>'Sam'],200),MockResponse::make(['name'=>'Alex'],200),MockResponse::make(['error'=>'Server Unavailable'],500),]);(newGetForgeServerRequest)->send()// Will return with `['name' => 'Sam']` and status `200`(newGetForgeServerRequest)->send()// Will return with `['name' => 'Alex']` and status `200`(newGetForgeServerRequest)->send()// Will return with `['error' => 'Server Unavailable']` and status `500`
Connector Mocking
You may also explicitly define mock responses for a particular connector that is used. Unlike sequence tests, these are kept even after the response has been sent.
useSammyjo20\SaloonLaravel\Facades\Saloon;useSammyjo20\Saloon\Http\MockResponse;Saloon::fake([ForgeConnector::class=>MockResponse::make(['name'=>'Sam'],200),OtherServiceConnector::class=>MockResponse::make(['name'=>'Alex'],200),]);(newGetForgeServerRequest)->send()// Will return with `['name' => 'Sam']` and status `200`(newGetForgeServerRequest)->send()// Will return with `['name' => 'Sam']` and status `200`(newOtherServiceRequest)->send()// Will return with `['name' => 'Alex']` and status `200`
Request Mocking
You may also explicitly define mock responses for a particular request that is used. Unlike sequence tests, these are kept even after the response has been sent.
useSammyjo20\SaloonLaravel\Facades\Saloon;useSammyjo20\Saloon\Http\MockResponse;Saloon::fake([GetForgeServerRequest::class=>MockResponse::make(['name'=>'Sam'],200),OtherServiceRequest::class=>MockResponse::make(['name'=>'Alex'],200),]);(newGetForgeServerRequest)->send()// Will return with `['name' => 'Sam']` and status `200`(newOtherServiceRequest)->send()// Will return with `['name' => 'Alex']` and status `200`
URL Mocking
You can also define fake responses for particular URL patterns. Whenever a request is made for a particular pattern, Saloon will respond to that request.
useSammyjo20\SaloonLaravel\Facades\Saloon;useSammyjo20\Saloon\Http\MockResponse;Saloon::fake(['forge.laravel.com/api/*'=>MockResponse::make(['name'=>'Sam'],200),'samcarre.dev/*'=>MockResponse::make(['name'=>'Alex'],200),'samcarre.dev/exact'=>MockResponse::make(['name'=>'Taylor'],200),// Exact requests'*'=>MockResponse::make(['name'=>'Wildcard'],200),// Any other requests]);(newGetForgeServerRequest)->send()// Will return with `['name' => 'Sam']` and status `200`(newOtherServiceRequest)->send()// Will return with `['name' => 'Alex']` and status `200`(newExactRequest)->send()// Will return with `['name' => 'Taylor']` and status `200`(newWildcardServiceRequest)->send()// Will return with `['name' => 'Wildcard']` and status `200`
Adding Expectations
When using faking responses, it's important to be able to check that a specific make request was sent and with the correct data, headers, and config. Saloon provides you with various ways to add expectations to your tests.
Available Expectations
AssertSent
AssertNotSent
AssertSentJson
AssertNothingSent
AssertSentCount
To use one of the expectations, you can simply call the static method on the Saloon Facade.
The AssertSent / AssertNotSent are the two most powerful expectation methods. They can accept a Saloon request, a URL pattern or even a closure where you define if a request/response is what you expect.
Your test may require you to mock exceptions like Guzzle's RequestException or even your own exceptions that might get thrown. To mock an exception, chain the throw method after you have defined your mock response.
If you would like to test one of the Guzzle exceptions like RequestException, it will expect you to pass in a PSR-7 request as one of the arguments. For these exceptions, provide a closure and Saloon will fulfil the closure with the PSR-7 request.
<?phpuseGuzzleHttp\Exception\ConnectException;Saloon::fake([MockResponse::make()->throw(fn ($guzzleRequest) => newConnectException('Unable to connect!', $guzzleRequest))]);