When testing an application that uses Cashier, you may mock the actual HTTP requests to the Stripe API; however, this requires you to partially re-implement Cashier's own behavior. Therefore, we recommend allowing your tests to hit the actual Stripe API. While this is slower, it provides more confidence that your application is working as expected and any slow tests may be placed within their own Pest / PHPUnit testing group.
Prefer creating real Stripe test accounts and making actual API calls over mocking. This approach:
- Tests integration: Verifies your code works with the actual Stripe API
- Catches API changes: Stripe API changes will be caught by tests
- More confidence: You're testing the actual behavior, not a mock
Use the Stripe test mode to create test accounts. Always clean up test accounts after each test:
beforeEach(function () {
$stripe = new \Stripe\StripeClient(config('cashier.secret'));
try {
$this->stripeAccount = $stripe->accounts->create([
'type' => 'express',
'country' => 'US',
]);
$this->stripeAccountId = $this->stripeAccount->id;
} catch (\Exception $e) {
$this->markTestSkipped('Could not create Stripe test account: '.$e->getMessage());
}
});
afterEach(function () {
if (isset($this->stripeAccountId)) {
$stripe = new \Stripe\StripeClient(config('cashier.secret'));
try {
$stripe->accounts->delete($this->stripeAccountId);
} catch (\Exception $e) {
// Ignore deletion errors
}
}
});Place Stripe-related tests in dedicated directories:
tests/Feature/Controllers/Stripe/- Controller teststests/Feature/Stripe/- Service and integration tests
See the Laravel Cashier documentation for more information.