feat(attachments): add AttachmentType enum and update Attachment model to use it
This commit is contained in:
13
app/Domains/Attachable/Enums/AttachmentType.php
Normal file
13
app/Domains/Attachable/Enums/AttachmentType.php
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Attachable\Enums;
|
||||||
|
|
||||||
|
enum AttachmentType: string
|
||||||
|
{
|
||||||
|
case Image = 'image';
|
||||||
|
case Video = 'video';
|
||||||
|
case Pdf = 'pdf';
|
||||||
|
case Document = 'document';
|
||||||
|
case Audio = 'audio';
|
||||||
|
case Other = 'other';
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Domains\Attachable\Models;
|
namespace App\Domains\Attachable\Models;
|
||||||
|
|
||||||
|
use App\Domains\Attachable\Enums\AttachmentType;
|
||||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
@@ -35,6 +36,7 @@ class Attachment extends Model
|
|||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'attachable_id' => 'integer',
|
'attachable_id' => 'integer',
|
||||||
|
'type' => AttachmentType::class,
|
||||||
'size' => 'integer',
|
'size' => 'integer',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ return new class extends Migration
|
|||||||
$table->uuid('key')->unique();
|
$table->uuid('key')->unique();
|
||||||
$table->string('path');
|
$table->string('path');
|
||||||
$table->string('filename');
|
$table->string('filename');
|
||||||
$table->string('type')->nullable();
|
$table->string('type');
|
||||||
$table->string('mime_type');
|
$table->string('mime_type');
|
||||||
$table->string('extension', 20)->nullable();
|
$table->string('extension', 20)->nullable();
|
||||||
$table->unsignedBigInteger('size')->default(0);
|
$table->unsignedBigInteger('size')->default(0);
|
||||||
|
|||||||
65
tests/Feature/Attachable/AttachmentTest.php
Normal file
65
tests/Feature/Attachable/AttachmentTest.php
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature\Attachable;
|
||||||
|
|
||||||
|
use App\Domains\Attachable\Enums\AttachmentType;
|
||||||
|
use App\Domains\Attachable\Models\Attachment;
|
||||||
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class AttachmentTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
public function test_it_persists_and_casts_attachment_type(): void
|
||||||
|
{
|
||||||
|
$tenant = Tenant::query()->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,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user