257 lines
8.9 KiB
PHP
257 lines
8.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Ticket;
|
|
|
|
use App\Domains\Attachable\Enums\AttachmentType;
|
|
use App\Domains\Attachable\Models\Attachment;
|
|
use App\Domains\Auth\Models\User;
|
|
use App\Domains\Catalog\Enums\CatalogItemType;
|
|
use App\Domains\Catalog\Models\CatalogItem;
|
|
use App\Domains\Purchase\Models\Purchase;
|
|
use App\Domains\Tenant\Models\Tenant;
|
|
use App\Domains\Ticket\Exceptions\TicketGenerationException;
|
|
use App\Domains\Ticket\Services\TicketGeneratorService;
|
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Str;
|
|
use Tests\TestCase;
|
|
|
|
class TicketGeneratorServiceTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private TicketGeneratorService $service;
|
|
|
|
private Tenant $tenant;
|
|
|
|
private User $user;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
Carbon::setTestNow('2026-07-21 10:00:00');
|
|
$this->service = app(TicketGeneratorService::class);
|
|
$this->tenant = $this->createTenant();
|
|
$this->user = User::factory()->create();
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
Carbon::setTestNow();
|
|
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function test_it_generates_tickets_from_a_standard_catalog_item(): void
|
|
{
|
|
$item = $this->createTicketableItem(
|
|
'single-day',
|
|
now()->subHour(),
|
|
now()->addDay(),
|
|
);
|
|
|
|
$tickets = $this->service->generate($item, $this->user, 2);
|
|
|
|
$this->assertCount(2, $tickets);
|
|
foreach ($tickets as $ticket) {
|
|
$this->assertTrue(Str::isUuid($ticket->ticket));
|
|
$this->assertSame($this->tenant->codigo, $ticket->tenant_code);
|
|
$this->assertSame($this->user->id, $ticket->user_id);
|
|
$this->assertSame($item->nombre, $ticket->name);
|
|
$this->assertSame($item->descripcion, $ticket->description);
|
|
$this->assertTrue($ticket->starts_at->equalTo($item->minimum_use_date));
|
|
$this->assertTrue($ticket->expires_at->equalTo($item->maximum_use_date));
|
|
}
|
|
}
|
|
|
|
public function test_it_rejects_an_item_without_tickets_enabled(): void
|
|
{
|
|
$item = $this->createTicketableItem('disabled');
|
|
$item->update(['has_tickets' => false]);
|
|
|
|
$this->expectException(TicketGenerationException::class);
|
|
$this->expectExceptionMessage('no tiene tickets habilitados');
|
|
|
|
$this->service->generate($item->fresh(), $this->user);
|
|
}
|
|
|
|
public function test_it_rejects_an_item_when_its_maximum_use_date_was_reached(): void
|
|
{
|
|
$item = $this->createTicketableItem('expired', maximumUseDate: now());
|
|
|
|
$this->expectException(TicketGenerationException::class);
|
|
$this->expectExceptionMessage('alcanzó su fecha máxima de uso');
|
|
|
|
$this->service->generate($item, $this->user);
|
|
}
|
|
|
|
public function test_it_generates_tickets_for_every_bundle_component_and_quantity(): void
|
|
{
|
|
$first = $this->createTicketableItem('first', maximumUseDate: now()->addDay());
|
|
$second = $this->createTicketableItem('second', maximumUseDate: now()->addDays(2));
|
|
$bundle = $this->createBundle('bundle');
|
|
$bundle->bundleComponents()->createMany([
|
|
['component_catalog_item_id' => $first->id, 'quantity' => 2],
|
|
['component_catalog_item_id' => $second->id, 'quantity' => 1],
|
|
]);
|
|
|
|
$tickets = $this->service->generate($bundle, $this->user, 2);
|
|
|
|
$this->assertCount(6, $tickets);
|
|
$this->assertCount(4, $tickets->where('name', $first->nombre));
|
|
$this->assertCount(2, $tickets->where('name', $second->nombre));
|
|
}
|
|
|
|
public function test_bundle_generation_is_rolled_back_when_a_component_is_invalid(): void
|
|
{
|
|
$valid = $this->createTicketableItem('valid');
|
|
$invalid = $this->createTicketableItem('invalid');
|
|
$invalid->update(['has_tickets' => false]);
|
|
$bundle = $this->createBundle('invalid-bundle');
|
|
$bundle->bundleComponents()->createMany([
|
|
['component_catalog_item_id' => $valid->id, 'quantity' => 1],
|
|
['component_catalog_item_id' => $invalid->id, 'quantity' => 1],
|
|
]);
|
|
|
|
try {
|
|
$this->service->generate($bundle, $this->user);
|
|
$this->fail('La generación debería haber fallado.');
|
|
} catch (TicketGenerationException) {
|
|
$this->assertDatabaseCount('tickets', 0);
|
|
}
|
|
}
|
|
|
|
public function test_marking_a_purchase_as_paid_generates_its_tickets_once(): void
|
|
{
|
|
$item = $this->createTicketableItem('paid-ticket');
|
|
$purchase = $this->createPurchase($item, 2);
|
|
$purchase->setRelation('items', new EloquentCollection);
|
|
|
|
$purchase->markAsPaid();
|
|
|
|
$this->assertSame(Purchase::STATUS_PAID, $purchase->status);
|
|
$this->assertDatabaseCount('tickets', 2);
|
|
|
|
$purchase->markAsPaid();
|
|
|
|
$this->assertDatabaseCount('tickets', 2);
|
|
}
|
|
|
|
public function test_marking_a_purchase_as_paid_ignores_items_without_tickets(): void
|
|
{
|
|
$item = $this->createTicketableItem('regular-product');
|
|
$item->update(['has_tickets' => false]);
|
|
$purchase = $this->createPurchase($item->fresh(), 1);
|
|
|
|
$purchase->markAsPaid();
|
|
|
|
$this->assertSame(Purchase::STATUS_PAID, $purchase->status);
|
|
$this->assertDatabaseCount('tickets', 0);
|
|
}
|
|
|
|
public function test_paid_status_is_rolled_back_when_ticket_generation_fails(): void
|
|
{
|
|
$item = $this->createTicketableItem('expired-paid-ticket', maximumUseDate: now());
|
|
$purchase = $this->createPurchase($item, 1);
|
|
|
|
try {
|
|
$purchase->markAsPaid();
|
|
$this->fail('La generación debería haber fallado.');
|
|
} catch (TicketGenerationException) {
|
|
$this->assertSame(Purchase::STATUS_PENDING_PAYMENT, $purchase->fresh()->status);
|
|
$this->assertDatabaseCount('tickets', 0);
|
|
}
|
|
}
|
|
|
|
private function createTicketableItem(
|
|
string $slug,
|
|
mixed $minimumUseDate = null,
|
|
mixed $maximumUseDate = null,
|
|
): CatalogItem {
|
|
return CatalogItem::query()->create([
|
|
'tenant_code' => $this->tenant->codigo,
|
|
'slug' => $slug,
|
|
'nombre' => ucfirst($slug),
|
|
'descripcion' => "Descripción de {$slug}",
|
|
'precio' => 10,
|
|
'has_tickets' => true,
|
|
'minimum_use_date' => $minimumUseDate,
|
|
'maximum_use_date' => $maximumUseDate,
|
|
]);
|
|
}
|
|
|
|
private function createBundle(string $slug): CatalogItem
|
|
{
|
|
return CatalogItem::query()->create([
|
|
'tenant_code' => $this->tenant->codigo,
|
|
'type' => CatalogItemType::Bundle,
|
|
'inventory_policy' => null,
|
|
'slug' => $slug,
|
|
'nombre' => ucfirst($slug),
|
|
'descripcion' => "Descripción de {$slug}",
|
|
'precio' => 20,
|
|
]);
|
|
}
|
|
|
|
private function createPurchase(CatalogItem $catalogItem, int $quantity): Purchase
|
|
{
|
|
$purchase = Purchase::query()->create([
|
|
'tenant_codigo' => $this->tenant->codigo,
|
|
'user_id' => $this->user->id,
|
|
'status' => Purchase::STATUS_PENDING_PAYMENT,
|
|
'payment_method' => 'transfer',
|
|
'total' => 10 * $quantity,
|
|
]);
|
|
|
|
$purchase->items()->create([
|
|
'source_catalog_item_id' => $catalogItem->id,
|
|
'source_variant_id' => null,
|
|
'image_attachment_id' => null,
|
|
'nombre' => $catalogItem->nombre,
|
|
'descripcion' => $catalogItem->descripcion,
|
|
'slug' => $catalogItem->slug,
|
|
'item_nombre' => $catalogItem->nombre,
|
|
'variant_attributes' => [],
|
|
'cantidad' => $quantity,
|
|
'precio_unitario' => 10,
|
|
'discount_total' => null,
|
|
'tax_total' => null,
|
|
'total' => 10 * $quantity,
|
|
]);
|
|
|
|
return $purchase;
|
|
}
|
|
|
|
private function createTenant(): Tenant
|
|
{
|
|
$header = Attachment::query()->create([
|
|
'path' => 'test/ticket-header.png',
|
|
'filename' => 'header.png',
|
|
'type' => AttachmentType::Image,
|
|
'mime_type' => 'image/png',
|
|
]);
|
|
$footer = Attachment::query()->create([
|
|
'path' => 'test/ticket-footer.png',
|
|
'filename' => 'footer.png',
|
|
'type' => AttachmentType::Image,
|
|
'mime_type' => 'image/png',
|
|
]);
|
|
|
|
return Tenant::query()->create([
|
|
'codigo' => 'ticket-tenant',
|
|
'nombre' => 'Ticket Tenant',
|
|
'dominio' => 'ticket.local',
|
|
'primary_color' => '#000000',
|
|
'secondary_color' => '#000000',
|
|
'danger_color' => '#000000',
|
|
'success_color' => '#000000',
|
|
'header_bg_color' => '#000000',
|
|
'footer_bg_color' => '#000000',
|
|
'header_logo_id' => $header->id,
|
|
'footer_logo_id' => $footer->id,
|
|
]);
|
|
}
|
|
}
|