Files
fotospiel-app/tests/Feature/Api/Tenant/BillingPortalTest.php
2025-12-23 15:00:52 +01:00

94 lines
3.0 KiB
PHP

<?php
namespace Tests\Feature\Api\Tenant;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Tests\Feature\Tenant\TenantTestCase;
class BillingPortalTest extends TenantTestCase
{
public function test_tenant_can_create_paddle_portal_session(): void
{
Http::fake([
'*paddle.com/customers' => Http::response([
'data' => ['id' => 'cus_123'],
], 200),
'*paddle.com/customer-portal-sessions' => Http::response([
'data' => [
'urls' => [
'general' => [
'overview' => 'https://portal.example/overview',
],
],
],
], 200),
]);
$this->tenant->forceFill(['paddle_customer_id' => null])->save();
$response = $this->authenticatedRequest('POST', '/api/v1/tenant/billing/portal');
$response->assertOk();
$response->assertJsonPath('url', 'https://portal.example/overview');
Http::assertSent(fn (Request $request) => $request->hasHeader('Paddle-Version', '1'));
$this->assertDatabaseHas('tenants', [
'id' => $this->tenant->id,
'paddle_customer_id' => 'cus_123',
]);
}
public function test_tenant_can_reuse_existing_paddle_customer_when_customer_already_exists(): void
{
Http::fake(function (Request $request) {
$path = parse_url($request->url(), PHP_URL_PATH);
if ($path === '/customers' && $request->method() === 'POST') {
return Http::response([
'error' => [
'type' => 'request_error',
'code' => 'customer_already_exists',
'message' => 'Customer already exists.',
],
], 409);
}
if ($path === '/customers' && $request->method() === 'GET') {
return Http::response([
'data' => [
['id' => 'cus_existing'],
],
], 200);
}
if ($path === '/customer-portal-sessions' && $request->method() === 'POST') {
return Http::response([
'data' => [
'urls' => [
'general' => [
'overview' => 'https://portal.example/overview',
],
],
],
], 200);
}
return Http::response([], 404);
});
$this->tenant->forceFill(['paddle_customer_id' => null])->save();
$response = $this->authenticatedRequest('POST', '/api/v1/tenant/billing/portal');
$response->assertOk();
$response->assertJsonPath('url', 'https://portal.example/overview');
$this->assertDatabaseHas('tenants', [
'id' => $this->tenant->id,
'paddle_customer_id' => 'cus_existing',
]);
}
}