68 lines
2.1 KiB
PHP
68 lines
2.1 KiB
PHP
<?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 = $tenant->attachments()->create([
|
|
'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->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
|
|
{
|
|
$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,
|
|
]);
|
|
}
|
|
}
|