CluPilotCloud/tests/Feature/CheckoutWithoutStripeKeyTes...

63 lines
2.6 KiB
PHP

<?php
use App\Exceptions\StripeNotConfigured;
use App\Livewire\Billing;
use App\Models\Customer;
use App\Models\Order;
use App\Models\User;
use App\Services\Stripe\HttpStripeClient;
use App\Support\OperatingMode;
use Livewire\Livewire;
/**
* Fehlt der Schlüssel des aktiven Modus, entsteht kein Auftrag — und der Kunde
* liest einen Satz statt einer 500er-Seite.
*
* Die Ausnahme ist eine KONKRETE Klasse. `toThrow(Throwable::class)` würde hier
* jeden beliebigen Fehler durchgehen lassen, auch einen Tippfehler im Test.
*/
it('refuses to reach stripe without a key for the active mode', function () {
OperatingMode::set(OperatingMode::Test);
expect(fn () => app(HttpStripeClient::class)->createPrice('prod_x', 100, 'EUR', 'month'))
->toThrow(StripeNotConfigured::class);
});
it('does not create an order when the key is missing', function () {
OperatingMode::set(OperatingMode::Test);
$customer = Customer::factory()->create();
// A portal user is matched to its customer by email, not by a foreign
// key — see the existing Billing feature tests (e.g. StorageAllowanceTest).
$user = User::factory()->create(['email' => $customer->email]);
// 'team' is a real, sellable plan (seeded by the plan-catalogue migration)
// that ranks above the customer's implicit 'start' plan — a genuine
// upgrade the purchase() guards would otherwise accept. A made-up key
// would be refused by the existing plan-validity check regardless of
// whether the Stripe guard works at all, and would prove nothing.
//
// assertHasErrors(), not assertHasNoErrors(): the guard reports through
// addError('purchase', …) — the same page-blocking pattern order.blade.php
// already uses for "cannot proceed" — and Livewire's test harness folds
// any non-empty error bag into the call's result. Asserting no errors
// here would contradict the very behaviour this test exists to prove.
Livewire::actingAs($user)
->test(Billing::class)
->call('purchase', 'upgrade', 'team')
->assertHasErrors('purchase');
// Der Punkt: nach der Zahlung wäre es zu spät. Es darf gar nichts entstehen.
expect(Order::count())->toBe(0);
});
it('says why instead of failing silently', function () {
OperatingMode::set(OperatingMode::Test);
$customer = Customer::factory()->create();
$user = User::factory()->create(['email' => $customer->email]);
Livewire::actingAs($user)
->test(Billing::class)
->call('purchase', 'upgrade', 'team')
->assertSee(__('billing.stripe_not_configured'));
});