Files
fotospiel-app/tests/Unit/Services/PackageLimitEvaluatorTest.php
2025-11-01 13:19:07 +01:00

129 lines
3.9 KiB
PHP

<?php
namespace Tests\Unit\Services;
use App\Models\Event;
use App\Models\EventPackage;
use App\Models\Package;
use App\Models\Tenant;
use App\Models\TenantPackage;
use App\Services\Packages\PackageLimitEvaluator;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class PackageLimitEvaluatorTest extends TestCase
{
use RefreshDatabase;
private PackageLimitEvaluator $evaluator;
protected function setUp(): void
{
parent::setUp();
$this->evaluator = $this->app->make(PackageLimitEvaluator::class);
}
public function test_assess_event_creation_returns_null_when_allowance_available(): void
{
$tenant = Tenant::factory()->create(['event_credits_balance' => 2]);
$violation = $this->evaluator->assessEventCreation($tenant);
$this->assertNull($violation);
}
public function test_assess_event_creation_returns_package_violation_when_quota_reached(): void
{
$package = Package::factory()->reseller()->create([
'max_events_per_year' => 1,
]);
$tenant = Tenant::factory()->create(['event_credits_balance' => 0]);
TenantPackage::factory()->create([
'tenant_id' => $tenant->id,
'package_id' => $package->id,
'used_events' => 1,
'expires_at' => now()->addMonth(),
'active' => true,
]);
$tenant->refresh();
$violation = $this->evaluator->assessEventCreation($tenant);
$this->assertNotNull($violation);
$this->assertSame('event_limit_exceeded', $violation['code']);
$this->assertSame('events', $violation['meta']['scope']);
$this->assertSame(0, $violation['meta']['remaining']);
}
public function test_assess_event_creation_returns_credit_violation_when_no_credits(): void
{
$tenant = Tenant::factory()->create(['event_credits_balance' => 0]);
$violation = $this->evaluator->assessEventCreation($tenant);
$this->assertNotNull($violation);
$this->assertSame('event_credits_exhausted', $violation['code']);
$this->assertSame('credits', $violation['meta']['scope']);
}
public function test_assess_photo_upload_returns_violation_when_photo_limit_reached(): void
{
$package = Package::factory()->endcustomer()->create([
'max_photos' => 5,
]);
$tenant = Tenant::factory()->create();
$event = Event::factory()
->for($tenant)
->create();
EventPackage::create([
'event_id' => $event->id,
'package_id' => $package->id,
'purchased_price' => $package->price,
'purchased_at' => now(),
'used_photos' => 5,
'used_guests' => 0,
'gallery_expires_at' => now()->addDays(14),
]);
$violation = $this->evaluator->assessPhotoUpload($tenant->fresh(), $event->id);
$this->assertNotNull($violation);
$this->assertSame('photo_limit_exceeded', $violation['code']);
$this->assertSame('photos', $violation['meta']['scope']);
$this->assertSame(0, $violation['meta']['remaining']);
}
public function test_assess_photo_upload_returns_null_when_below_limit(): void
{
$package = Package::factory()->endcustomer()->create([
'max_photos' => 10,
]);
$tenant = Tenant::factory()->create();
$event = Event::factory()
->for($tenant)
->create();
EventPackage::create([
'event_id' => $event->id,
'package_id' => $package->id,
'purchased_price' => $package->price,
'purchased_at' => now(),
'used_photos' => 4,
'used_guests' => 0,
'gallery_expires_at' => now()->addDays(14),
]);
$violation = $this->evaluator->assessPhotoUpload($tenant->fresh(), $event->id);
$this->assertNull($violation);
}
}