Files
shopit-back/tests/Feature/Migrations/DynamicContactMenuTest.php

49 lines
2.3 KiB
PHP

<?php
namespace Tests\Feature\Migrations;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;
class DynamicContactMenuTest extends TestCase
{
public function test_it_converts_only_contact_and_preserves_legacy_data_for_rollback(): void
{
Schema::create('menues', function (Blueprint $table): void {
$table->string('code')->primary();
$table->string('content_type');
$table->json('static_content_schema')->nullable();
});
Schema::create('tenants_menues', function (Blueprint $table): void {
$table->string('tenant_code');
$table->string('menu_code');
$table->json('static_content')->nullable();
});
DB::table('menues')->insert([
['code' => 'help.contact', 'content_type' => 'static', 'static_content_schema' => '{}'],
['code' => 'help.faq', 'content_type' => 'static', 'static_content_schema' => '{}'],
]);
$legacy = json_encode(['whatsapp' => ['whatsapp_url' => 'https://wa.me/123']], JSON_THROW_ON_ERROR);
DB::table('tenants_menues')->insert([
'tenant_code' => 'test', 'menu_code' => 'help.contact', 'static_content' => $legacy,
]);
$migration = require database_path('migrations/2026_09_30_000600_make_contact_menu_dynamic.php');
$migration->up();
$migration->up();
$this->assertDatabaseHas('menues', [
'code' => 'help.contact', 'content_type' => 'dynamic', 'static_content_schema' => null,
]);
$this->assertDatabaseHas('menues', ['code' => 'help.faq', 'content_type' => 'static', 'static_content_schema' => '{}']);
$this->assertDatabaseHas('tenants_menues', ['menu_code' => 'help.contact', 'static_content' => $legacy]);
$migration->down();
$this->assertDatabaseHas('menues', ['code' => 'help.contact', 'content_type' => 'static']);
$schema = json_decode(DB::table('menues')->where('code', 'help.contact')->value('static_content_schema'), true, flags: JSON_THROW_ON_ERROR);
$this->assertSame('required|url', $schema['whatsapp.whatsapp_url']);
$this->assertDatabaseHas('tenants_menues', ['menu_code' => 'help.contact', 'static_content' => $legacy]);
}
}