To test the subscription lifecycle in Laravel Cashier Stripe, you can use Laravel's built-in testing framework and Stripe's testing environment.
First, set up a Stripe test account and configure Laravel Cashier to use it by adding the test API keys to your .env file.
Next, create a test that subscribes a user to a plan and checks that they receive an email. You can use Laravel's built-in NotificationFake to mock the email sending.
Then, simulate a failed payment by creating a test card in Stripe's testing environment that will always fail. You can do this by using the card number 4000000000000341.
Finally, run the test and check that the user's subscription is cancelled and they receive an email notification.
Here's an example test:
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
class SubscriptionTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_cancels_subscription_on_failed_payment()
{
// Create a user
$user = factory(User::class)->create();
// Subscribe the user to a plan
$user->newSubscription('default', 'plan_id')->create('stripe_token');
// Mock the email notification
Notification::fake();
// Simulate a failed payment
$user->updateCard('4000000000000341');
// Check that the subscription is cancelled and the user receives an email
$this->assertFalse($user->subscribed('default'));
Notification::assertSentTo($user, SubscriptionCancelled::class);
}
}