132 lines
4.3 KiB
PHP
132 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Catalog;
|
|
|
|
use App\Domains\Tenant\Models\Tenant;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class AttributeControllerTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_it_creates_a_select_attribute_with_options(): void
|
|
{
|
|
$this->createTenant('acme', 'Acme', 'acme.com');
|
|
|
|
$response = $this->postJson('/api/tenants/acme/attributes', [
|
|
'codigo' => 'color',
|
|
'nombre' => 'Color',
|
|
'is_required' => true,
|
|
'type' => 'select',
|
|
'metadata_schema' => [
|
|
'swatch' => ['type' => 'hex'],
|
|
],
|
|
'options' => [
|
|
[
|
|
'value' => 'red',
|
|
'label' => 'Red',
|
|
'sort_order' => 1,
|
|
'metadata' => ['hex' => '#ff0000'],
|
|
],
|
|
[
|
|
'value' => 'blue',
|
|
'label' => 'Blue',
|
|
'sort_order' => 2,
|
|
'metadata' => ['hex' => '#0000ff'],
|
|
],
|
|
],
|
|
]);
|
|
|
|
$response
|
|
->assertCreated()
|
|
->assertJsonPath('data.codigo', 'color')
|
|
->assertJsonPath('data.type', 'select')
|
|
->assertJsonPath('data.options.0.value', 'red')
|
|
->assertJsonPath('data.options.0.label', 'Red')
|
|
->assertJsonPath('data.options.1.metadata.hex', '#0000ff');
|
|
|
|
$this->assertDatabaseHas('attribute', [
|
|
'tenant_codigo' => 'acme',
|
|
'codigo' => 'color',
|
|
'type' => 'select',
|
|
]);
|
|
|
|
$this->assertDatabaseHas('attribute_options', [
|
|
'value' => 'red',
|
|
'label' => 'Red',
|
|
'sort_order' => 1,
|
|
]);
|
|
}
|
|
|
|
public function test_it_rejects_options_for_string_attributes(): void
|
|
{
|
|
$this->createTenant('acme', 'Acme', 'acme.com');
|
|
|
|
$response = $this->postJson('/api/tenants/acme/attributes', [
|
|
'codigo' => 'material',
|
|
'nombre' => 'Material',
|
|
'type' => 'string',
|
|
'options' => [
|
|
['label' => 'Cotton'],
|
|
],
|
|
]);
|
|
|
|
$response
|
|
->assertUnprocessable()
|
|
->assertJsonValidationErrors(['options']);
|
|
}
|
|
|
|
public function test_it_throws_exception_when_creating_non_select_attribute_with_options_directly_on_model(): void
|
|
{
|
|
$tenant = $this->createTenant('acme2', 'Acme 2', 'acme2.com');
|
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
$this->expectExceptionMessage('Options are only allowed for select and multiselect attributes.');
|
|
|
|
\App\Domains\Catalog\Models\Product::createAttribute($tenant, [
|
|
'codigo' => 'material2',
|
|
'nombre' => 'Material 2',
|
|
'type' => 'string',
|
|
'options' => [
|
|
['label' => 'Cotton', 'value' => 'cotton'],
|
|
],
|
|
]);
|
|
}
|
|
|
|
protected function createTenant(string $codigo, string $nombre, string $dominio): Tenant
|
|
{
|
|
$hdrKey = (string) \Illuminate\Support\Str::uuid();
|
|
$ftrKey = (string) \Illuminate\Support\Str::uuid();
|
|
|
|
$headerAttachment = \App\Domains\Attachable\Models\Attachment::create([
|
|
'key' => $hdrKey,
|
|
'path' => 'tenants/' . $hdrKey . '.png',
|
|
'filename' => 'logo_header.png',
|
|
'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
|
|
'mime_type' => 'image/png',
|
|
]);
|
|
$footerAttachment = \App\Domains\Attachable\Models\Attachment::create([
|
|
'key' => $ftrKey,
|
|
'path' => 'tenants/' . $ftrKey . '.png',
|
|
'filename' => 'logo_footer.png',
|
|
'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
|
|
'mime_type' => 'image/png',
|
|
]);
|
|
|
|
return Tenant::create([
|
|
'codigo' => $codigo,
|
|
'nombre' => $nombre,
|
|
'dominio' => $dominio,
|
|
'primary_color' => '#111111',
|
|
'secondary_color' => '#222222',
|
|
'danger_color' => '#333333',
|
|
'success_color' => '#28a745',
|
|
'header_bg_color' => '#444444',
|
|
'footer_bg_color' => '#444444',
|
|
'header_logo_id' => $headerAttachment->id,
|
|
'footer_logo_id' => $footerAttachment->id,
|
|
]);
|
|
}
|
|
}
|