connecteev
5 months ago
807
7
Laravel

Seeing puzzling and inconsistent results with unit test

Posted 5 months ago by connecteev

Method 1: directly call Laravel Cashier's API to create a new subscription

$user->newSubscription(env('STRIPE_SUBSCRIPTION_PRODUCT_NAME'), env('STRIPE_ID_PLAN_1'))->create($paymentMethodId);

$isUserSubscribed = $user->subscribed(env('STRIPE_SUBSCRIPTION_PRODUCT_NAME'));

This returns $isUserSubscribed = TRUE.

Method 2: My unit test makes an API call to my API endpoint at /api/v1/me/payments/createOrUpdateSubscription:

        $response = $this->actingAs($user, 'api')->json('PUT', '/api/v1/me/payments/createOrUpdateSubscription', [
            'stripePlanId' => env('STRIPE_ID_PLAN_1'),
            'paymentMethodId' => $paymentMethodId
        ]);

The API endpoint does this (which is exactly the same as what method #1 does directly)

        $loggedinUser = auth()->user();
        $loggedinUser->newSubscription(env('STRIPE_SUBSCRIPTION_PRODUCT_NAME'), $stripePlanId)->create($paymentMethodId);

$isUserSubscribed = $user->subscribed(env('STRIPE_SUBSCRIPTION_PRODUCT_NAME'));

This returns $isUserSubscribed = FALSE.

Whyy??

Please sign in or create an account to participate in this conversation.