presseportale/tests/Feature/Billing/CheckoutFlowTest.php
Kevin Adametz c8dc99c3c8 Phase 9E (Abschluss): Checkout-Flows und Plan-Kontingent statt Quota-Stub
- Checkout-Backend: me.checkout.subscription (Tarif-Abo monatlich/jährlich)
  und me.checkout.single-pm (Einzel-PM 19 € netto, pending-Kauf mit
  Webhook-Erfüllung); StripeCheckoutService als mockbarer Stripe-Wrapper;
  Stripe Tax via Cashier::calculateTaxes() (Netto-Preise, USt-ID-Abfrage)
- Slot-Logik: Kontingent aus dem Tarif (plans.press_release_quota) plus
  bezahlte Einmalkäufe; Verbrauch bei Veröffentlichung zuerst aus dem
  Plan-Zähler, danach Einlösung des ältesten Einmalkaufs (consumed +
  PM-Verknüpfung); Grandfathered = unbegrenzt (Entscheidung 12.06.2026,
  Bestandsschutz); Stub-Spalte users.press_release_quota entfernt
- billing:sync-stripe-plans legt zusätzlich das Einzel-PM-Produkt an
  (STRIPE_PRICE_SINGLE_PM); Test-Mode-Sync gelaufen
- Buchungs-Seite: Rückmeldung nach Checkout (erfolg/abbruch/Guard-Hinweis)
- Tests: PressReleaseQuotaTest auf Plan-Semantik neu geschrieben,
  CheckoutFlowTest (8 Tests), Modal-/API-Tests angepasst; Suite 510 passed
- Doku: Billing-und-Rechnungskreise (Kontingent-Tabelle, Checkout-Routen,
  Webhook-Events, Stripe-CLI-Hinweis), PHASE-9-Plan 9E , Checkliste,
  STATUS-ABGLEICH, PROGRESS

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 12:10:32 +00:00

132 lines
4.6 KiB
PHP

<?php
use App\Enums\SinglePurchaseStatus;
use App\Enums\SinglePurchaseType;
use App\Models\Plan;
use App\Models\SinglePurchase;
use App\Models\User;
use App\Services\Billing\StripeCheckoutService;
use Database\Seeders\RolesAndPermissionsSeeder;
use Illuminate\Http\RedirectResponse;
use Laravel\Cashier\Checkout;
use Tests\TestCase;
beforeEach(function (): void {
/** @var TestCase $this */
$this->seed(RolesAndPermissionsSeeder::class);
});
function checkoutTestCustomer(): User
{
$user = User::factory()->create();
$user->assignRole('customer');
return $user;
}
/**
* Stripe-Checkout-Stub: Der Controller gibt das Checkout-Objekt zurück,
* der Router ruft toResponse() — wir leiten auf eine Fake-Stripe-URL um.
*/
function fakeStripeCheckout(): Checkout
{
$checkout = Mockery::mock(Checkout::class);
$checkout->shouldReceive('toResponse')
->andReturn(new RedirectResponse('https://checkout.stripe.com/c/pay/test'));
return $checkout;
}
test('guests are redirected to the login', function () {
/** @var TestCase $this */
$plan = Plan::factory()->create();
$this->get(route('me.checkout.subscription', ['planSlug' => $plan->slug, 'interval' => 'monthly']))
->assertRedirect();
});
test('an unknown plan or invalid interval responds 404', function () {
/** @var TestCase $this */
$this->actingAs(checkoutTestCustomer());
$this->get('/admin/me/checkout/abo/gibts-nicht/monthly')->assertNotFound();
$plan = Plan::factory()->create();
$this->get("/admin/me/checkout/abo/{$plan->slug}/weekly")->assertNotFound();
});
test('a plan without a synced stripe price redirects back with a notice', function () {
/** @var TestCase $this */
$plan = Plan::factory()->create(['stripe_price_id_monthly' => null]);
$this->actingAs(checkoutTestCustomer())
->get(route('me.checkout.subscription', ['planSlug' => $plan->slug, 'interval' => 'monthly']))
->assertRedirect(route('me.bookings.index'))
->assertSessionHas('checkout-notice');
});
test('an already subscribed user is redirected instead of double-booking', function () {
/** @var TestCase $this */
$user = checkoutTestCustomer();
$plan = Plan::factory()->create(['stripe_price_id_monthly' => 'price_test_m_1']);
subscribeUserToPlan($user, $plan);
$this->actingAs($user)
->get(route('me.checkout.subscription', ['planSlug' => $plan->slug, 'interval' => 'monthly']))
->assertRedirect(route('me.bookings.index'))
->assertSessionHas('checkout-notice');
});
test('the subscription checkout hands plan and interval to stripe', function () {
/** @var TestCase $this */
$user = checkoutTestCustomer();
$plan = Plan::factory()->create(['stripe_price_id_yearly' => 'price_test_y_1']);
$this->mock(StripeCheckoutService::class, function ($mock) use ($plan) {
$mock->shouldReceive('forSubscription')
->once()
->withArgs(fn (User $u, Plan $p, string $interval) => $p->is($plan) && $interval === 'yearly')
->andReturn(fakeStripeCheckout());
});
$this->actingAs($user)
->get(route('me.checkout.subscription', ['planSlug' => $plan->slug, 'interval' => 'yearly']))
->assertRedirect('https://checkout.stripe.com/c/pay/test');
});
test('the single pm checkout is disabled without a configured stripe price', function () {
/** @var TestCase $this */
config()->set('billing.single_pm_stripe_price_id', null);
$this->actingAs(checkoutTestCustomer())
->get(route('me.checkout.single-pm'))
->assertRedirect(route('me.bookings.index'))
->assertSessionHas('checkout-notice');
expect(SinglePurchase::count())->toBe(0);
});
test('the single pm checkout creates a pending purchase and hands it to stripe', function () {
/** @var TestCase $this */
config()->set('billing.single_pm_stripe_price_id', 'price_test_single_pm');
config()->set('billing.single_pm_price_cents', 1900);
$user = checkoutTestCustomer();
$this->mock(StripeCheckoutService::class, function ($mock) use ($user) {
$mock->shouldReceive('forSinglePurchase')
->once()
->withArgs(fn (User $u, SinglePurchase $p) => $u->is($user) && $p->exists)
->andReturn(fakeStripeCheckout());
});
$this->actingAs($user)
->get(route('me.checkout.single-pm'))
->assertRedirect('https://checkout.stripe.com/c/pay/test');
$purchase = SinglePurchase::sole();
expect($purchase->user_id)->toBe($user->id);
expect($purchase->type)->toBe(SinglePurchaseType::SinglePm);
expect($purchase->status)->toBe(SinglePurchaseStatus::Pending);
expect($purchase->price_cents)->toBe(1900);
});