Files
fotospiel-app/tests/Feature/Checkout/CheckoutSessionStatusTest.php
Codex Agent 10c99de1e2
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
tests / ui (push) Has been cancelled
Migrate billing from Paddle to Lemon Squeezy
2026-02-03 10:59:54 +01:00

186 lines
5.8 KiB
PHP

<?php
namespace Tests\Feature\Checkout;
use App\Models\CheckoutSession;
use App\Models\Package;
use App\Models\Tenant;
use App\Models\User;
use App\Services\Checkout\CheckoutSessionService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
class CheckoutSessionStatusTest extends TestCase
{
use RefreshDatabase;
public function test_user_can_fetch_checkout_session_status(): void
{
$tenant = Tenant::factory()->create();
$user = User::factory()->for($tenant)->create();
$package = Package::factory()->create();
/** @var CheckoutSessionService $sessions */
$sessions = app(CheckoutSessionService::class);
$session = $sessions->createOrResume($user, $package, [
'tenant' => $tenant,
]);
$sessions->markCompleted($session, now());
$this->actingAs($user);
$response = $this->getJson(route('checkout.session.status', $session));
$response->assertOk()
->assertJsonPath('status', CheckoutSession::STATUS_COMPLETED);
}
public function test_user_cannot_fetch_other_users_checkout_session_status(): void
{
$tenant = Tenant::factory()->create();
$owner = User::factory()->for($tenant)->create();
$otherUser = User::factory()->create();
$package = Package::factory()->create();
/** @var CheckoutSessionService $sessions */
$sessions = app(CheckoutSessionService::class);
$session = $sessions->createOrResume($owner, $package, [
'tenant' => $tenant,
]);
$this->actingAs($otherUser);
$response = $this->getJson(route('checkout.session.status', $session));
$response->assertForbidden();
}
public function test_session_status_recovers_completed_lemonsqueezy_order(): void
{
$tenant = Tenant::factory()->create();
$user = User::factory()->for($tenant)->create([
'pending_purchase' => true,
]);
$package = Package::factory()->create([
'type' => 'endcustomer',
'price' => 99,
]);
/** @var CheckoutSessionService $sessions */
$sessions = app(CheckoutSessionService::class);
$session = $sessions->createOrResume($user, $package, [
'tenant' => $tenant,
]);
$sessions->selectProvider($session, CheckoutSession::PROVIDER_LEMONSQUEEZY);
$session->forceFill([
'provider_metadata' => [
'lemonsqueezy_checkout_id' => 'chk_123',
],
])->save();
config()->set([
'lemonsqueezy.api_key' => 'test-key',
'lemonsqueezy.base_url' => 'https://lemonsqueezy.test',
]);
Http::fake([
'https://lemonsqueezy.test/checkouts/chk_123' => Http::response([
'data' => [
'id' => 'chk_123',
'attributes' => [
'order_id' => 'ord_123',
],
],
], 200),
'https://lemonsqueezy.test/orders/ord_123' => Http::response([
'data' => [
'id' => 'ord_123',
'attributes' => [
'status' => 'paid',
'currency' => 'EUR',
'total' => 9900,
],
],
], 200),
]);
Mail::fake();
Notification::fake();
$this->actingAs($user);
$response = $this->getJson(route('checkout.session.status', $session));
$response->assertOk()
->assertJsonPath('status', CheckoutSession::STATUS_COMPLETED);
$this->assertDatabaseHas('checkout_sessions', [
'id' => $session->id,
'status' => CheckoutSession::STATUS_COMPLETED,
]);
$this->assertDatabaseHas('package_purchases', [
'tenant_id' => $tenant->id,
'package_id' => $package->id,
]);
}
public function test_session_confirm_recovers_completed_lemonsqueezy_order(): void
{
$tenant = Tenant::factory()->create();
$user = User::factory()->for($tenant)->create([
'pending_purchase' => true,
]);
$package = Package::factory()->create([
'type' => 'endcustomer',
'price' => 79,
]);
/** @var CheckoutSessionService $sessions */
$sessions = app(CheckoutSessionService::class);
$session = $sessions->createOrResume($user, $package, [
'tenant' => $tenant,
]);
$sessions->selectProvider($session, CheckoutSession::PROVIDER_LEMONSQUEEZY);
config()->set([
'lemonsqueezy.api_key' => 'test-key',
'lemonsqueezy.base_url' => 'https://lemonsqueezy.test',
]);
Http::fake([
'https://lemonsqueezy.test/orders/ord_987' => Http::response([
'data' => [
'id' => 'ord_987',
'attributes' => [
'status' => 'paid',
'currency' => 'EUR',
'total' => 7900,
],
],
], 200),
]);
Mail::fake();
Notification::fake();
$this->actingAs($user);
$response = $this->postJson(route('checkout.session.confirm', $session), [
'order_id' => 'ord_987',
'checkout_id' => 'chk_987',
]);
$response->assertOk()
->assertJsonPath('status', CheckoutSession::STATUS_COMPLETED);
$this->assertDatabaseHas('checkout_sessions', [
'id' => $session->id,
'status' => CheckoutSession::STATUS_COMPLETED,
]);
}
}