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...