Files
shopit-back/tests/Feature/Forms/AdminAppDesfileEntryReservationFormControllerTest.php

146 lines
5.5 KiB
PHP

<?php
namespace Tests\Feature\Forms;
use App\Domains\Commerce\Catalog\Models\Attribute;
use App\Domains\Commerce\Catalog\Models\CatalogItem;
use App\Domains\Commerce\Catalog\Models\Inventory;
use App\Domains\Core\Auth\Models\User;
use App\Domains\Core\Authorization\Enums\RoleCode;
use App\Domains\Core\Menu\Models\Menu;
use App\Domains\Core\Tenant\Models\Tenant;
use App\Shared\Enums\FieldType;
use Database\Seeders\AuthorizationSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class AdminAppDesfileEntryReservationFormControllerTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->seed(AuthorizationSeeder::class);
}
public function test_authentication_is_required(): void
{
$this->getJson('/api/v1/adminapp/forms/desfile/entry-reservation')
->assertUnauthorized();
}
public function test_the_tenant_must_have_the_reservations_menu(): void
{
$tenant = $this->createTenant('without_reservations');
Sanctum::actingAs($this->createAdminAppUser($tenant));
$this->getJson('/api/v1/adminapp/forms/desfile/entry-reservation')
->assertNotFound();
}
public function test_it_returns_payment_types_and_empty_variant_options_without_a_catalog(): void
{
$tenant = $this->createTenant('desfile_pura_tendencia');
$this->grantReservationsMenu($tenant);
Sanctum::actingAs($this->createAdminAppUser($tenant));
$this->getJson('/api/v1/adminapp/forms/desfile/entry-reservation')
->assertOk()
->assertExactJson([
'data' => [
'payment_types' => [
['value' => 'sin_cargo', 'label' => 'Sin cargo'],
['value' => 'otro_metodo', 'label' => 'Otro método'],
],
'fields' => [
['key' => 'tipo', 'label' => 'Tipo', 'options' => []],
['key' => 'sector', 'label' => 'Sector', 'options' => []],
['key' => 'fila', 'label' => 'Fila', 'options' => []],
['key' => 'asiento', 'label' => 'Asiento', 'options' => []],
],
'variants' => [],
],
]);
}
public function test_it_returns_only_available_combinations_without_reserving_stock(): void
{
$tenant = $this->createTenant('desfile_pura_tendencia');
$this->grantReservationsMenu($tenant);
Sanctum::actingAs($this->createAdminAppUser($tenant));
$entry = CatalogItem::query()->create([
'tenant_code' => $tenant->codigo, 'slug' => 'entrada', 'nombre' => 'Entrada', 'precio' => 100,
]);
$attributes = [];
foreach (['tipo', 'sector', 'fila', 'asiento'] as $code) {
$attribute = Attribute::query()->create([
'tenant_codigo' => $tenant->codigo, 'codigo' => $code,
'nombre' => $code, 'type' => FieldType::Select,
]);
$attributes[$code] = $entry->itemAttributes()->create(['attribute_id' => $attribute->id]);
}
$availableId = null;
foreach (['available', 'reserved', 'sold', 'disabled', 'administrative'] as $index => $state) {
$inventory = Inventory::query()->create([
'real_stock' => $state === 'sold' ? 0 : 1,
'reserved_stock' => $state === 'reserved' ? 1 : 0,
]);
$variant = $entry->variants()->create([
'inventory_id' => $inventory->id, 'precio' => 100,
'sales_disabled_at' => $state === 'disabled' ? now() : null,
]);
foreach ($attributes as $code => $attribute) {
$variant->definitions()->create([
'item_attribute_id' => $attribute->id,
'value' => $code === 'asiento' ? (string) ($index + 1) : '1',
]);
}
if ($state === 'administrative') {
$variant->desfileEntryReservations()->create([
'fecha_reserva' => now(), 'importe' => 0, 'tipo_pago' => 'sin_cargo',
]);
}
if ($state === 'available') {
$availableId = $variant->id;
}
}
$this->getJson('/api/v1/adminapp/forms/desfile/entry-reservation')
->assertOk()->assertJsonCount(1, 'data.variants')
->assertJsonPath('data.variants.0.id', $availableId)
->assertJsonPath('data.fields.3.options', [['value' => '1', 'label' => '1']]);
$this->assertDatabaseCount('desfile_entry_reservations', 1);
$this->assertSame(1, (int) Inventory::query()->sum('reserved_stock'));
}
private function createTenant(string $code): Tenant
{
return Tenant::query()->create([
'codigo' => $code,
'nombre' => str($code)->headline(),
'dominio' => "{$code}.test",
]);
}
private function createAdminAppUser(Tenant $tenant): User
{
return User::factory()->create([
'rol_codigo' => RoleCode::AdminApp->value,
'tenant_codigo' => $tenant->codigo,
]);
}
private function grantReservationsMenu(Tenant $tenant): void
{
$menu = Menu::query()->create([
'code' => 'adminapp.desfile.reservas',
'label' => 'Reserva de Tickets',
'route' => '/admin/desfile/reservas',
]);
$tenant->menues()->attach($menu->code);
}
}