Adding a `fake` method to Cashier
kylemilloy opened this issue · comments
I'm building an app that leverages Spark and per-seat billing and whenever I add a seat it makes a call to the (in my case) Paddle API and because I'm faking my subscriptions for the tests this returns an exception. What I have done to circumvent this is created a trait that mocks the Http class for the vendor's URL specifically before running tests which touch this API and I'm wondering if this is something the rest of community might find valuable for a PR. There may be a lot of specific stuff that Cashier does that I'm unaware of so looking for feedback on how to do this without tightly coupling the endpoints and their intended responses and yet not mocking so much that it might introduce false positives. I have it listed in the example below of just faking every path but I'm worried this is too broad so looking for feedback on that too.
I was initially thinking that it just fakes the routes with success data and the user can then overwrite a specific URL for a specific response if they wanted but maybe there's more appetite for doing an assertion of payment succeeded or a quick method to make it so a specific subscription or payment fails...dunno...open for discussion...below is the very generalized version of this that I've used in my existing project.
Open to thoughts and feedback and willing to help out on a PR to get this started too if there's interest.
class Cashier
{
/**
* @param array $responses allow a user to overwrite the response from the mocked URL by matching the endpoint with the desired response
*/
static public function fake(array $responses = [])
{
$url = Cashier::vendorsUrl();
// not sure if we should just carpet bomb all paths or not...
Http::fake(array_merge([
"$url/*" => Http::response(['fake-data' => 'goes here'], 200)
], $responses));
}
}
Hey @kylemilloy, thank you for considering to give back! I think this is a good idea. There's an exercise you can do with this by replacing the existing Http::fake
calls in our test suite with the new Cashier::fake
call.
Feel free to attempt a PR with that and put it in draft so I can review it before Taylor does.