saloonphp / saloon

🤠 Build beautiful API integrations and SDKs with Saloon
https://docs.saloon.dev
MIT License
2.08k stars 107 forks source link

Saloon Testing/Mocking (Coming Soon!) #5

Closed Sammyjo20 closed 2 years ago

Sammyjo20 commented 2 years ago

Just wanted to open an issue here to let people know that I am currently working on a mocking system for Saloon. It's going to introduce two ways to mock in Saloon, one way would be the "Larvel" way - inspired by the wonderful testing that is provided by Illuminate/Http. The other way would require a bit more intervention but I hope would still be useful especially when writing SDKs.

Laravel Mocking

Laravel mocking will have slightly more features than the standard PHP mocking that can be used within SDKs. This is because Laravel has a super powerful service container. See the testing page for the HTTP Client in the Laravel docs. I will recreate how it works there.

Mocking for SDKs

SDK mocking is slightly different, but the idea is that you will be able to tell Saloon to send a fake response when a request has been created.

$mockClient = new SaloonMockClient();
$mockClient->addResponse(200, $data, $headers);
$mockClient->addResponse(500, $data, $headers);

$response = (new SaloonRequest)->send($mockClient); // 200
$response = (new SaloonRequest)->send($mockClient); // 500

Here's an example of how it could be implemented

$sdk = new ForgeSdk($token, $saloonMockClient);
$sdk->getServers(); // Internally sends $saloonRequest->send($this->mockClient);

Extra Features

lao9s commented 2 years ago

Awesome 👍 That's what I'm looking for.

Sammyjo20 commented 2 years ago

@lao9s It's coming very soon, just making my final polishes on it at the moment.