Files
fotospiel-app/tests/Feature/Tenant/TenantBillingPortalTest.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

75 lines
2.5 KiB
PHP

<?php
namespace Tests\Feature\Tenant;
use App\Models\Package;
use App\Models\TenantPackage;
use App\Services\LemonSqueezy\Exceptions\LemonSqueezyException;
use App\Services\LemonSqueezy\LemonSqueezySubscriptionService;
use Illuminate\Support\Facades\Log;
use Mockery;
class TenantBillingPortalTest extends TenantTestCase
{
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
public function test_portal_logs_lemonsqueezy_error_context(): void
{
$logged = [];
Log::listen(function ($event) use (&$logged): void {
$logged[] = [
'level' => $event->level,
'message' => $event->message,
'context' => $event->context,
];
});
$package = Package::factory()->reseller()->create();
TenantPackage::factory()->create([
'tenant_id' => $this->tenant->id,
'package_id' => $package->id,
'lemonsqueezy_subscription_id' => 'sub_123',
'active' => true,
]);
$subscriptions = Mockery::mock(LemonSqueezySubscriptionService::class);
$subscriptions->shouldReceive('retrieve')
->once()
->with('sub_123')
->andThrow(new LemonSqueezyException('Not found', 404, [
'errors' => [
[
'code' => 'entity_not_found',
'detail' => 'Not found',
],
],
'meta' => [
'request_id' => 'req_123',
],
]));
$this->instance(LemonSqueezySubscriptionService::class, $subscriptions);
$response = $this->authenticatedRequest('POST', '/api/v1/tenant/billing/portal');
$response->assertStatus(502)
->assertJson([
'message' => 'Failed to fetch Lemon Squeezy subscription portal URL.',
]);
$matched = collect($logged)->contains(function (array $entry): bool {
return $entry['level'] === 'warning'
&& $entry['message'] === 'Failed to fetch Lemon Squeezy subscription portal URL'
&& ($entry['context']['tenant_id'] ?? null) === $this->tenant->id
&& ($entry['context']['lemonsqueezy_status'] ?? null) === 404
&& (($entry['context']['lemonsqueezy_error']['code'] ?? null) === 'entity_not_found');
});
$this->assertTrue($matched);
}
}