fix(purchase): unify expired checkout errors

This commit is contained in:
2026-08-20 17:01:06 -03:00
parent ae6149df84
commit 9fd34f900f
11 changed files with 174 additions and 18 deletions

View File

@@ -0,0 +1,24 @@
<?php
namespace Tests\Feature\Purchase;
use App\Domains\Purchase\Exceptions\PurchaseExpiredException;
use Illuminate\Support\Facades\Route;
use Tests\TestCase;
class PurchaseExpiredExceptionResponseTest extends TestCase
{
public function test_it_returns_a_stable_api_error_for_an_expired_purchase(): void
{
Route::get('/api/test/purchase-expired', function (): never {
throw new PurchaseExpiredException;
});
$this->getJson('/api/test/purchase-expired')
->assertUnprocessable()
->assertExactJson([
'code' => 'purchase.expired',
'message' => __('api.purchase.expired'),
]);
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace Tests\Unit\Purchase;
use App\Domains\Purchase\Exceptions\PurchaseExpiredException;
use App\Domains\Purchase\Models\Purchase;
use App\Domains\Purchase\Services\PurchaseStateGuard;
use Tests\TestCase;
class PurchaseStateGuardTest extends TestCase
{
private PurchaseStateGuard $guard;
protected function setUp(): void
{
parent::setUp();
$this->guard = new PurchaseStateGuard;
}
public function test_it_rejects_a_purchase_with_expired_status(): void
{
$purchase = (new Purchase)->forceFill([
'status' => Purchase::STATUS_EXPIRED,
'expires_at' => null,
]);
$this->expectException(PurchaseExpiredException::class);
$this->guard->assertNotExpired($purchase);
}
public function test_it_rejects_an_active_purchase_when_its_deadline_has_passed(): void
{
$purchase = (new Purchase)->forceFill([
'status' => Purchase::STATUS_PENDING_PAYMENT,
'expires_at' => now()->subMinute(),
]);
$this->expectException(PurchaseExpiredException::class);
$this->guard->assertNotExpired($purchase);
}
public function test_it_does_not_treat_a_paid_purchase_as_expired_by_time(): void
{
$purchase = (new Purchase)->forceFill([
'status' => Purchase::STATUS_PAID,
'expires_at' => now()->subMinute(),
]);
$this->guard->assertNotExpired($purchase);
$this->assertTrue(true);
}
}