feat(social-media): add SocialMedia model, migration, and tests for tenant associations
This commit is contained in:
34
app/Domains/Tenant/Models/SocialMedia.php
Normal file
34
app/Domains/Tenant/Models/SocialMedia.php
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Tenant\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
|
|
||||||
|
class SocialMedia extends Model
|
||||||
|
{
|
||||||
|
protected $table = 'social_media';
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'code',
|
||||||
|
'icon',
|
||||||
|
'name',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return BelongsToMany<Tenant, $this>
|
||||||
|
*/
|
||||||
|
public function tenants(): BelongsToMany
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(
|
||||||
|
Tenant::class,
|
||||||
|
'tenant_social_media',
|
||||||
|
'social_media_code',
|
||||||
|
'tenant_code',
|
||||||
|
'code',
|
||||||
|
'codigo'
|
||||||
|
)
|
||||||
|
->withPivot('url')
|
||||||
|
->withTimestamps();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -94,6 +94,23 @@ class Tenant extends Model
|
|||||||
return $this->hasMany(CatalogItem::class, 'tenant_code', 'codigo');
|
return $this->hasMany(CatalogItem::class, 'tenant_code', 'codigo');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return BelongsToMany<SocialMedia, $this>
|
||||||
|
*/
|
||||||
|
public function socialMedia(): BelongsToMany
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(
|
||||||
|
SocialMedia::class,
|
||||||
|
'tenant_social_media',
|
||||||
|
'tenant_code',
|
||||||
|
'social_media_code',
|
||||||
|
'codigo',
|
||||||
|
'code'
|
||||||
|
)
|
||||||
|
->withPivot('url')
|
||||||
|
->withTimestamps();
|
||||||
|
}
|
||||||
|
|
||||||
public function menues(): BelongsToMany
|
public function menues(): BelongsToMany
|
||||||
{
|
{
|
||||||
return $this->belongsToMany(
|
return $this->belongsToMany(
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('social_media', function (Blueprint $table): void {
|
||||||
|
$table->id();
|
||||||
|
$table->string('code')->unique();
|
||||||
|
$table->string('icon');
|
||||||
|
$table->string('name');
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::create('tenant_social_media', function (Blueprint $table): void {
|
||||||
|
$table->id();
|
||||||
|
$table->string('tenant_code');
|
||||||
|
$table->string('social_media_code');
|
||||||
|
$table->string('url');
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->foreign('tenant_code')
|
||||||
|
->references('codigo')
|
||||||
|
->on('tenants')
|
||||||
|
->cascadeOnDelete();
|
||||||
|
$table->foreign('social_media_code')
|
||||||
|
->references('code')
|
||||||
|
->on('social_media')
|
||||||
|
->cascadeOnDelete();
|
||||||
|
$table->unique(['tenant_code', 'social_media_code']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('tenant_social_media');
|
||||||
|
Schema::dropIfExists('social_media');
|
||||||
|
}
|
||||||
|
};
|
||||||
108
tests/Feature/Tenant/TenantSocialMediaTest.php
Normal file
108
tests/Feature/Tenant/TenantSocialMediaTest.php
Normal 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',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user