feat: implement Attachment domain for file storage and integrate into Tenant service for logo management

This commit is contained in:
2026-06-29 09:43:46 -03:00
parent 67bc9e6cb4
commit b0508d79b3
9 changed files with 2 additions and 179 deletions

View File

@@ -6,7 +6,6 @@ use App\Domains\Attachable\Enums\AttachmentType;
use App\Domains\Attachable\Exceptions\AttachmentStorageException;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Attachable\Services\AttachmentService;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
@@ -22,19 +21,12 @@ class AttachmentTest extends TestCase
{
Storage::fake('s3');
$tenant = Tenant::query()->create([
'codigo' => 'acme',
'nombre' => 'Acme',
'dominio' => 'acme.com',
]);
$file = UploadedFile::fake()->image('logo.png');
$attachment = app(AttachmentService::class)->store(
$file,
'attachments/acme/logo.png',
);
$tenant->attachments()->attach($attachment->getKey());
$this->assertSame(AttachmentType::Image, $attachment->type);
$this->assertTrue(Str::isUuid($attachment->key));
@@ -48,26 +40,14 @@ class AttachmentTest extends TestCase
'filename' => 'logo.png',
'type' => AttachmentType::Image->value,
]);
$this->assertDatabaseHas('attachable_attachments', [
'attachable_type' => $tenant->getMorphClass(),
'attachable_id' => $tenant->getKey(),
'attachment_id' => $attachment->id,
]);
$freshAttachment = Attachment::query()->findOrFail($attachment->id);
$this->assertSame(AttachmentType::Image, $freshAttachment->type);
$this->assertTrue($tenant->attachments->contains($freshAttachment));
}
public function test_it_does_not_persist_the_attachment_when_the_s3_upload_fails(): void
{
$tenant = Tenant::query()->create([
'codigo' => 'globex',
'nombre' => 'Globex',
'dominio' => 'globex.com',
]);
$disk = Mockery::mock();
Storage::shouldReceive('disk')
->once()
@@ -94,12 +74,6 @@ class AttachmentTest extends TestCase
{
Storage::fake('s3');
$tenant = Tenant::query()->create([
'codigo' => 'initech',
'nombre' => 'Initech',
'dominio' => 'initech.com',
]);
Storage::disk('s3')->put('attachments/initech/spec.pdf', 'spec');
$attachment = Attachment::query()->create([
@@ -111,7 +85,6 @@ class AttachmentTest extends TestCase
'extension' => 'pdf',
'size' => 512,
]);
$tenant->attachments()->attach($attachment->getKey());
app(AttachmentService::class)->delete($attachment);
@@ -119,19 +92,10 @@ class AttachmentTest extends TestCase
$this->assertDatabaseMissing('attachments', [
'id' => $attachment->id,
]);
$this->assertDatabaseMissing('attachable_attachments', [
'attachment_id' => $attachment->id,
]);
}
public function test_it_keeps_the_database_record_when_the_s3_delete_fails(): void
{
$tenant = Tenant::query()->create([
'codigo' => 'umbrella',
'nombre' => 'Umbrella',
'dominio' => 'umbrella.com',
]);
$attachment = Attachment::query()->create([
'path' => 'attachments/umbrella/audio.mp3',
'key' => (string) Str::uuid(),
@@ -141,7 +105,6 @@ class AttachmentTest extends TestCase
'extension' => 'mp3',
'size' => 1024,
]);
$tenant->attachments()->attach($attachment->getKey());
$disk = Mockery::mock();
Storage::shouldReceive('disk')
@@ -161,11 +124,6 @@ class AttachmentTest extends TestCase
$this->assertDatabaseHas('attachments', [
'id' => $attachment->id,
]);
$this->assertDatabaseHas('attachable_attachments', [
'attachment_id' => $attachment->id,
'attachable_type' => $tenant->getMorphClass(),
'attachable_id' => $tenant->getKey(),
]);
}
}
}