This repository has been archived by the owner on Nov 9, 2021. It is now read-only.
generated from renoki-co/laravel-package-skeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
SubscriptionTest.php
116 lines (83 loc) · 3.97 KB
/
SubscriptionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
namespace RenokiCo\BillingPortal\Test;
use RenokiCo\BillingPortal\Test\Models\User;
use RenokiCo\CashierRegister\Saas;
class SubscriptionTest extends TestCase
{
public function test_index_subscriptions()
{
$user = factory(User::class)->create();
$plan = Saas::getPlan(static::$stripeFreePlanId);
$subscription = $this->createStripeSubscription($user, $plan);
$this->actingAs($user)
->get(route('billing-portal.subscription.index'))
->assertInertia('BillingPortal/Subscription/Index', [
'currentPlan' => $subscription ? $subscription->getPlan() : null,
'hasDefaultPaymentMethod' => $user->hasDefaultPaymentMethod(),
'paymentMethods' => $user->paymentMethods(),
'plans' => Saas::getPlans(),
'recurring' => $subscription ? $subscription->recurring() : false,
'cancelled' => $subscription ? $subscription->cancelled() : false,
'onGracePeriod' => $subscription ? $subscription->onGracePeriod() : false,
'endingDate' => $subscription ? optional($subscription->ends_at)->format('d M Y \a\t H:i') : null,
]);
}
public function test_subscribe_to_free_plan()
{
$user = factory(User::class)->create();
$user->subscriptions()->delete();
$this->actingAs($user)
->post(route('billing-portal.subscription.plan-subscribe', ['plan' => static::$stripeFreePlanId]))
->assertOk();
$user->newSubscription('main', static::$stripeFreePlanId)->create('pm_card_us');
$this->assertCount(1, $user->subscriptions);
}
public function test_subscribe_to_paid_plan_without_payment_method()
{
$user = factory(User::class)->create();
$user->subscriptions()->delete();
$this->actingAs($user)
->post(route('billing-portal.subscription.plan-subscribe', ['plan' => static::$stripePlanId]))
->assertOk();
$this->assertCount(0, $user->subscriptions);
}
public function test_swap_to_free_plan()
{
$user = factory(User::class)->create();
$user->subscriptions()->delete();
$user->newSubscription('main', static::$stripePlanId)->create('pm_card_visa');
$this->actingAs($user)
->post(route('billing-portal.subscription.plan-swap', ['plan' => static::$stripeFreePlanId]))
->assertRedirect(route('billing-portal.subscription.index'));
}
public function test_swap_to_paid_plan_without_payment_method()
{
$user = factory(User::class)->create();
$user->subscriptions()->delete();
$this->actingAs($user)
->post(route('billing-portal.subscription.plan-subscribe', ['plan' => static::$stripeFreePlanId]))
->assertOk();
$user->newSubscription('main', static::$stripeFreePlanId)->create('pm_card_us');
$user->deletePaymentMethods();
$this->actingAs($user)
->post(route('billing-portal.subscription.plan-swap', ['plan' => static::$stripePlanId]))
->assertOk();
}
public function test_cancel_and_resume_plan()
{
$user = factory(User::class)->create();
$user->subscriptions()->delete();
$this->actingAs($user)
->post(route('billing-portal.subscription.plan-subscribe', ['plan' => static::$stripeFreePlanId]))
->assertOk();
$user->newSubscription('main', static::$stripeFreePlanId)->create('pm_card_us');
$this->actingAs($user)
->post(route('billing-portal.subscription.cancel'))
->assertRedirect(route('billing-portal.subscription.index'));
$this->assertTrue($user->subscription('main')->cancelled());
$this->actingAs($user)
->post(route('billing-portal.subscription.resume'))
->assertRedirect(route('billing-portal.subscription.index'));
$this->assertFalse($user->subscription('main')->cancelled());
}
}