Files
mytodos/tests/Feature/Billing/CheckoutTest.php
T

158 lines
4.8 KiB
PHP
Raw Normal View History

2026-10-04 20:34:19 -07:00
<?php
use App\Models\Organization;
use App\Models\Setting;
use App\Models\User;
use Laravel\Cashier\Subscription;
use Livewire\Livewire;
test('guests can see the pricing page', function () {
$this->get(route('pricing'))->assertOk();
});
test('a guest is asked to create an account rather than subscribe', function () {
Livewire::test('pages::pricing')
->assertSee(route('register'))
->assertDontSee('subscribe-', false);
});
test('a configured plan is offered with its trial', function () {
$this->actingAs(User::factory()->unsubscribed()->create());
Livewire::test('pages::pricing')
->assertSee('Pro')
->assertOk();
});
test('a plan card shows the advertised price', function () {
Setting::put(Setting::planAmountKey('pro'), '1250');
$this->actingAs(User::factory()->unsubscribed()->create());
Livewire::test('pages::pricing')
->assertOk()
->assertSee('data-test="plan-pro-amount"', escape: false)
->assertSee('$12.50');
});
test('a plan without a price is hidden', function () {
config()->set('plans.plans', [
'pro' => ['name' => 'Pro', 'price' => null, 'interval' => 'month'],
]);
expect(Organization::availablePlans())->toBe([]);
Livewire::test('pages::pricing')
->assertSee('No plans are available');
});
test('available plans fall back to a usable name and interval', function () {
config()->set('plans.plans', [
'mystery' => ['price' => 'price_123'],
]);
expect(Organization::availablePlans())->toBe([
'mystery' => ['name' => 'mystery', 'price' => 'price_123', 'interval' => 'month', 'amount' => 1000],
]);
});
test('an unknown plan is not available', function () {
expect(Organization::isAvailablePlan('pro'))->toBeTrue()
->and(Organization::isAvailablePlan('enterprise'))->toBeFalse();
});
test('an already subscribed user is sent to manage their subscription instead', function () {
$this->actingAs(User::factory()->create());
Livewire::test('pages::pricing')
->assertSee('Manage your subscription')
->assertDontSee('Start 14-day free trial');
});
test('a guest cannot start a subscription', function () {
Livewire::test('pages::pricing')
->call('subscribe', 'pro')
->assertForbidden();
});
test('a plan that is not configured cannot be subscribed to', function () {
$this->actingAs(User::factory()->unsubscribed()->create());
Livewire::test('pages::pricing')
->call('subscribe', 'enterprise')
->assertHasErrors('plan');
});
test('a plan removed from configuration cannot be subscribed to', function () {
$this->actingAs(User::factory()->unsubscribed()->create());
Livewire::test('pages::pricing')->assertOk();
config()->set('plans.plans', []);
Livewire::test('pages::pricing')
->call('subscribe', 'pro')
->assertHasErrors('plan');
});
test('a trial shorter than Stripe allows is treated as no trial', function () {
expect(Organization::trialDays())->toBe(14);
config()->set('plans.trial_days', 1);
expect(Organization::trialDays())->toBe(0);
config()->set('plans.trial_days', 0);
expect(Organization::trialDays())->toBe(0);
});
test('checkout options are shared by every entry point', function () {
expect(Organization::checkoutOptions())
->toHaveKey('success_url', route('subscription.confirmed', ['session_id' => '{CHECKOUT_SESSION_ID}']))
->toHaveKey('cancel_url', route('pricing'))
->toHaveKey('allow_promotion_codes', true);
});
test('the trial length comes from configuration', function () {
config()->set('plans.trial_days', 0);
$this->actingAs(User::factory()->unsubscribed()->create());
Livewire::test('pages::pricing')
->assertSee('Subscribe')
->assertDontSee('free trial');
});
test('the current plan is resolved from the subscription price', function () {
$user = User::factory()->unsubscribed()->create();
$organization = $user->currentOrganization()->first();
Subscription::factory()
->active()
->withPrice('price_test_placeholder')
->create(['organization_id' => $organization->id]);
expect($organization->refresh()->currentPlan())->toBe([
'name' => 'Pro',
'price' => 'price_test_placeholder',
'interval' => 'month',
'amount' => 1000,
]);
});
test('a subscription on an unknown price has no resolvable plan', function () {
$organization = Organization::factory()->create();
Subscription::factory()
->active()
->withPrice('price_legacy')
->create(['organization_id' => $organization->id]);
expect($organization->refresh()->currentPlan())->toBeNull();
});
test('an organization with no subscription has no plan', function () {
expect(Organization::factory()->create()->currentPlan())->toBeNull();
});