feat: implement product creation workflow with variants, attribute definitions, and validation logic
This commit is contained in:
113
tests/Feature/Catalog/AttributeControllerTest.php
Normal file
113
tests/Feature/Catalog/AttributeControllerTest.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?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.tenant_codigo', 'acme')
|
||||
->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']);
|
||||
}
|
||||
|
||||
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',
|
||||
'header_footer_bg_color' => '#444444',
|
||||
'header_logo_id' => $headerAttachment->id,
|
||||
'footer_logo_id' => $footerAttachment->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user