Courses

Testing in Laravel 11: Advanced Level

Testing Third-Parties with Sandbox Mode

Summary of this lesson:
- Use real sandbox/testing environment credentials
- Test external services with actual API calls
- Verify functionality with test accounts
- Simulate production-like testing
- Ensure accurate external service integration

Now, let's cover the case where you DO want to test the external service and make the request to that service without faking it.

For this, we will check the example from the open-source package Laravel Cashier.


In the main FeatureTestCase.php file, we have the stripe() method, which creates the Cashier Stripe object with a real API key of Stripe.

use Laravel\Cashier\Cashier;
use Laravel\Cashier\Tests\TestCase;
use Stripe\StripeClient;
 
abstract class FeatureTestCase extends TestCase
{
// ...
 
protected static function stripe(array $options = []): StripeClient
{
return Cashier::stripe(array_merge(['api_key' => getenv('STRIPE_SECRET')], $options));
}
 
// ...
}

Important point: Your Stripe API Key should be from your testing Stripe environment. In your provider's external API, it may be called a Sandbox or a Developer Account, or you could even create a totally separate account just for testing purposes.

Then, you set those...

The full lesson is only for Premium Members.
Want to access all 31 lessons of this course? (74 min read)

You also get:

  • 69 courses (majority in latest Laravel 11)
  • Premium tutorials
  • Access to repositories
  • Private Discord