feat(social-media): add SocialMedia model, migration, and tests for tenant associations

This commit is contained in:
2026-07-23 14:54:42 -03:00
parent 12d496544d
commit c66d2019d6
4 changed files with 202 additions and 0 deletions

View File

@@ -0,0 +1,108 @@
<?php
namespace Tests\Feature\Tenant;
use App\Domains\Attachable\Enums\AttachmentType;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Tenant\Models\SocialMedia;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;
class TenantSocialMediaTest extends TestCase
{
use RefreshDatabase;
public function test_social_media_tables_have_the_expected_columns(): void
{
$this->assertEqualsCanonicalizing([
'id',
'code',
'icon',
'name',
'created_at',
'updated_at',
], Schema::getColumnListing('social_media'));
$this->assertEqualsCanonicalizing([
'id',
'tenant_code',
'social_media_code',
'url',
'created_at',
'updated_at',
], Schema::getColumnListing('tenant_social_media'));
}
public function test_a_tenant_can_have_social_media_with_its_own_url(): void
{
$tenant = $this->createTenant();
$instagram = SocialMedia::query()->create([
'code' => 'instagram',
'icon' => 'instagram',
'name' => 'Instagram',
]);
$tenant->socialMedia()->attach($instagram->code, [
'url' => 'https://instagram.com/acme',
]);
$this->assertTrue($tenant->socialMedia()->firstOrFail()->is($instagram));
$this->assertSame(
'https://instagram.com/acme',
$tenant->socialMedia()->firstOrFail()->pivot->url
);
$this->assertTrue($instagram->tenants()->firstOrFail()->is($tenant));
}
public function test_deleting_a_social_media_deletes_its_tenant_associations(): void
{
$tenant = $this->createTenant();
$instagram = SocialMedia::query()->create([
'code' => 'instagram',
'icon' => 'instagram',
'name' => 'Instagram',
]);
$tenant->socialMedia()->attach($instagram->code, [
'url' => 'https://instagram.com/acme',
]);
$instagram->delete();
$this->assertDatabaseMissing('tenant_social_media', [
'tenant_code' => $tenant->codigo,
'social_media_code' => $instagram->code,
]);
}
private function createTenant(): Tenant
{
$headerLogo = $this->createAttachment('header.png');
$footerLogo = $this->createAttachment('footer.png');
return Tenant::query()->create([
'codigo' => 'acme',
'nombre' => 'Acme',
'dominio' => 'acme.com',
'primary_color' => '#111111',
'secondary_color' => '#222222',
'danger_color' => '#333333',
'success_color' => '#444444',
'header_bg_color' => '#ffffff',
'footer_bg_color' => '#ffffff',
'header_logo_id' => $headerLogo->id,
'footer_logo_id' => $footerLogo->id,
]);
}
private function createAttachment(string $filename): Attachment
{
return Attachment::query()->create([
'path' => "test/{$filename}",
'filename' => $filename,
'type' => AttachmentType::Image,
'mime_type' => 'image/png',
]);
}
}