How to get Saloon to work with Laravel Zero
I have been using the amazing Saloon to handle my API integrations in a Laravel Zero application recently, as I wanted to introduce some consistency with them and generally keep my codebase nice and tidy.
Along with using Saloon, I'm also using the Saloon Laravel Plugin package, which adds a handful of nice additions that we all love when it comes to working with Laravel applications.
The package adds a Saloon
facade and one of the things that can do, is allow you to specify fake responses in your tests. Really similar to Http::fake()
, if you've ever used the Laravel HTTP Client.
At first, I couldn't get this to work at all. I was getting all manner of weirdness going on, I even opened up an issue on the GitHub repo as I was at a total loss as to what to do.
After going back and forth with Craig Potter for a couple of days, I randomly came up with the solution whilst putting my shoes on to go to work, of all times!
It turns out, Laravel Zero doesn't auto register service providers. This was news to me as Laravel itself does. So the solution was pretty simple:
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->register(SaloonServiceProvider::class);
}
After I'd manually registered the provider, guess what? Everything worked as expected!
This was a new discovery for a bunch of us, so in light of this, the Saloon documentation has been updated to mention this.
ðŸ¤