hamburghost
2 years ago
287
3
Testing

How to mock a user with a Cashier/Stripe subscription?

Posted 2 years ago by hamburghost

I'm trying to test things like checking if a user on a specific subscription plan has access to a specific feature and I can't find how to mock a subscription.

$user = factory(\App\User::class)->make([]);

works fine to create a user but then if I try to call $user->newSubscription() it fails because the method doesn't exist (although it's provided by the Billable trait).

One workaround would be to use persisting mocks so the user actually gets saved on the database at which point $user->newSubscription() would be available but then calling it would make a real call to Stripe which is not only slow, it adds a bunch of test data on the Stripe account. Ideally when mocking I'd be able to factory() a user model directly and say "this model is on the monthly plan".

Anyone has an idea? In the Cashier tests it seems to hit Stripe (https://github.com/laravel/cashier/blob/7.0/tests/CashierTest.php) which is probably useful for them but not ideal for an app using Cashier. I'm assuming Cashier is working so I don't want to test that.

Thanks

Please sign in or create an account to participate in this conversation.