diff --git a/app/Domains/Attachable/Models/Concerns/HasAttachments.php b/app/Domains/Attachable/Models/Concerns/HasAttachments.php new file mode 100644 index 0000000..23d881a --- /dev/null +++ b/app/Domains/Attachable/Models/Concerns/HasAttachments.php @@ -0,0 +1,18 @@ + + */ + public function attachments(): MorphMany + { + return $this->morphMany(Attachment::class, 'attachable'); + } +} diff --git a/tests/Feature/Attachable/AttachmentTest.php b/tests/Feature/Attachable/AttachmentTest.php index cff0ba7..0282625 100644 --- a/tests/Feature/Attachable/AttachmentTest.php +++ b/tests/Feature/Attachable/AttachmentTest.php @@ -20,9 +20,7 @@ class AttachmentTest extends TestCase 'dominio' => 'acme.com', ]); - $attachment = Attachment::query()->create([ - 'attachable_type' => $tenant->getMorphClass(), - 'attachable_id' => $tenant->getKey(), + $attachment = $tenant->attachments()->create([ 'path' => 'attachments/acme/logo.png', 'filename' => 'logo.png', 'type' => AttachmentType::Image, @@ -32,14 +30,18 @@ class AttachmentTest extends TestCase ]); $this->assertSame(AttachmentType::Image, $attachment->type); + $this->assertTrue($tenant->attachments->contains($attachment)); $this->assertDatabaseHas('attachments', [ 'id' => $attachment->id, + 'attachable_type' => $tenant->getMorphClass(), + 'attachable_id' => $tenant->getKey(), 'type' => AttachmentType::Image->value, ]); $freshAttachment = Attachment::query()->findOrFail($attachment->id); $this->assertSame(AttachmentType::Image, $freshAttachment->type); + $this->assertTrue($freshAttachment->attachable->is($tenant)); } public function test_it_requires_attachment_type(): void