diff --git a/app/Domains/Attachable/Enums/AttachmentType.php b/app/Domains/Attachable/Enums/AttachmentType.php new file mode 100644 index 0000000..3b7237d --- /dev/null +++ b/app/Domains/Attachable/Enums/AttachmentType.php @@ -0,0 +1,13 @@ + 'integer', + 'type' => AttachmentType::class, 'size' => 'integer', ]; } diff --git a/database/migrations/2026_06_25_000500_create_attachments_table.php b/database/migrations/2026_06_25_000500_create_attachments_table.php index 30543d2..8f32728 100644 --- a/database/migrations/2026_06_25_000500_create_attachments_table.php +++ b/database/migrations/2026_06_25_000500_create_attachments_table.php @@ -18,7 +18,7 @@ return new class extends Migration $table->uuid('key')->unique(); $table->string('path'); $table->string('filename'); - $table->string('type')->nullable(); + $table->string('type'); $table->string('mime_type'); $table->string('extension', 20)->nullable(); $table->unsignedBigInteger('size')->default(0); diff --git a/tests/Feature/Attachable/AttachmentTest.php b/tests/Feature/Attachable/AttachmentTest.php new file mode 100644 index 0000000..cff0ba7 --- /dev/null +++ b/tests/Feature/Attachable/AttachmentTest.php @@ -0,0 +1,65 @@ +create([ + 'codigo' => 'acme', + 'nombre' => 'Acme', + 'dominio' => 'acme.com', + ]); + + $attachment = Attachment::query()->create([ + 'attachable_type' => $tenant->getMorphClass(), + 'attachable_id' => $tenant->getKey(), + 'path' => 'attachments/acme/logo.png', + 'filename' => 'logo.png', + 'type' => AttachmentType::Image, + 'mime_type' => 'image/png', + 'extension' => 'png', + 'size' => 1234, + ]); + + $this->assertSame(AttachmentType::Image, $attachment->type); + $this->assertDatabaseHas('attachments', [ + 'id' => $attachment->id, + 'type' => AttachmentType::Image->value, + ]); + + $freshAttachment = Attachment::query()->findOrFail($attachment->id); + + $this->assertSame(AttachmentType::Image, $freshAttachment->type); + } + + public function test_it_requires_attachment_type(): void + { + $tenant = Tenant::query()->create([ + 'codigo' => 'globex', + 'nombre' => 'Globex', + 'dominio' => 'globex.com', + ]); + + $this->expectException(\Illuminate\Database\QueryException::class); + + Attachment::query()->create([ + 'attachable_type' => $tenant->getMorphClass(), + 'attachable_id' => $tenant->getKey(), + 'path' => 'attachments/globex/manual.pdf', + 'filename' => 'manual.pdf', + 'mime_type' => 'application/pdf', + 'extension' => 'pdf', + 'size' => 9876, + ]); + } +}