Skip to content

Instantly share code, notes, and snippets.

@ronssij
Created February 19, 2026 09:57
Show Gist options
  • Select an option

  • Save ronssij/10e5e04278e2c8489d281ab0a4252336 to your computer and use it in GitHub Desktop.

Select an option

Save ronssij/10e5e04278e2c8489d281ab0a4252336 to your computer and use it in GitHub Desktop.
Laravel Boost Cashier tests guidelines

Stripe / Cashier Testing

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.

Real Stripe API Testing

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

Creating Stripe Test Accounts

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
        }
    }
});

Test Organization

Place Stripe-related tests in dedicated directories:

  • tests/Feature/Controllers/Stripe/ - Controller tests
  • tests/Feature/Stripe/ - Service and integration tests

Reference

See the Laravel Cashier documentation for more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment