feat(attachments): refactor attachment management to use attachable pivot table and update related models and tests
This commit is contained in:
@@ -31,11 +31,10 @@ class AttachmentTest extends TestCase
|
||||
$file = UploadedFile::fake()->image('logo.png');
|
||||
|
||||
$attachment = app(AttachmentService::class)->store(
|
||||
$tenant,
|
||||
$file,
|
||||
'attachments/acme/logo.png',
|
||||
AttachmentType::Image,
|
||||
);
|
||||
$tenant->attachments()->attach($attachment->getKey());
|
||||
|
||||
$this->assertSame(AttachmentType::Image, $attachment->type);
|
||||
$this->assertTrue(Str::isUuid($attachment->key));
|
||||
@@ -44,18 +43,21 @@ class AttachmentTest extends TestCase
|
||||
Storage::disk('s3')->assertExists('attachments/acme/'.$attachment->key);
|
||||
$this->assertDatabaseHas('attachments', [
|
||||
'id' => $attachment->id,
|
||||
'attachable_type' => $tenant->getMorphClass(),
|
||||
'attachable_id' => $tenant->getKey(),
|
||||
'key' => $attachment->key,
|
||||
'path' => 'attachments/acme/'.$attachment->key,
|
||||
'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($freshAttachment->attachable->is($tenant));
|
||||
$this->assertTrue($tenant->attachments->contains($freshAttachment));
|
||||
}
|
||||
|
||||
public function test_it_does_not_persist_the_attachment_when_the_s3_upload_fails(): void
|
||||
@@ -78,10 +80,8 @@ class AttachmentTest extends TestCase
|
||||
|
||||
try {
|
||||
app(AttachmentService::class)->store(
|
||||
$tenant,
|
||||
UploadedFile::fake()->create('manual.pdf', 10, 'application/pdf'),
|
||||
'attachments/globex/manual.pdf',
|
||||
AttachmentType::Pdf,
|
||||
);
|
||||
|
||||
$this->fail('Expected an AttachmentStorageException to be thrown.');
|
||||
@@ -102,14 +102,16 @@ class AttachmentTest extends TestCase
|
||||
|
||||
Storage::disk('s3')->put('attachments/initech/spec.pdf', 'spec');
|
||||
|
||||
$attachment = $tenant->attachments()->create([
|
||||
$attachment = Attachment::query()->create([
|
||||
'path' => 'attachments/initech/spec.pdf',
|
||||
'key' => (string) Str::uuid(),
|
||||
'filename' => 'spec.pdf',
|
||||
'type' => AttachmentType::Pdf,
|
||||
'mime_type' => 'application/pdf',
|
||||
'extension' => 'pdf',
|
||||
'size' => 512,
|
||||
]);
|
||||
$tenant->attachments()->attach($attachment->getKey());
|
||||
|
||||
app(AttachmentService::class)->delete($attachment);
|
||||
|
||||
@@ -117,6 +119,9 @@ 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
|
||||
@@ -127,14 +132,16 @@ class AttachmentTest extends TestCase
|
||||
'dominio' => 'umbrella.com',
|
||||
]);
|
||||
|
||||
$attachment = $tenant->attachments()->create([
|
||||
$attachment = Attachment::query()->create([
|
||||
'path' => 'attachments/umbrella/audio.mp3',
|
||||
'key' => (string) Str::uuid(),
|
||||
'filename' => 'audio.mp3',
|
||||
'type' => AttachmentType::Audio,
|
||||
'mime_type' => 'audio/mpeg',
|
||||
'extension' => 'mp3',
|
||||
'size' => 1024,
|
||||
]);
|
||||
$tenant->attachments()->attach($attachment->getKey());
|
||||
|
||||
$disk = Mockery::mock();
|
||||
Storage::shouldReceive('disk')
|
||||
@@ -154,6 +161,11 @@ 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(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user