CaiWuXiTong/tests/Unit/EcommerceFinance/SmallPaymentCompletionIndexMigrationTest.php

53 lines
2.3 KiB
PHP

<?php
namespace Tests\Unit\EcommerceFinance;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;
class SmallPaymentCompletionIndexMigrationTest extends TestCase
{
public function test_indexes_are_idempotent_and_reversible_without_changing_rows(): void
{
config([
'database.connections.small_payment_index_fixture' => [
'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '',
],
'upstream_mirrors.connection' => 'small_payment_index_fixture',
'system_database.mode' => 'shared',
]);
$schema = Schema::connection('small_payment_index_fixture');
$db = DB::connection('small_payment_index_fixture');
$schema->create('js_jst_work_order_info', static function (Blueprint $table): void {
$table->integer('Id');
$table->string('woTypeId');
$table->string('woNo');
$table->dateTime('completeTime')->nullable();
});
$schema->create('js_jst_small_payment_order_map', static function (Blueprint $table): void {
$table->string('woTypeId');
$table->string('woNo');
});
$db->table('js_jst_work_order_info')->insert([
'Id' => 1, 'woTypeId' => 'TYPE', 'woNo' => 'GD-001', 'completeTime' => '2026-09-11 10:00:00',
]);
$db->table('js_jst_small_payment_order_map')->insert(['woTypeId' => 'TYPE', 'woNo' => 'GD-001']);
$migration = require base_path('database/migrations/2026_09_11_110000_index_small_payment_completion_queries.php');
$migration->up();
$migration->up();
$this->assertTrue($schema->hasIndex('js_jst_work_order_info', 'idx_small_payment_completion'));
$this->assertTrue($schema->hasIndex('js_jst_small_payment_order_map', 'idx_small_payment_order_map'));
$this->assertSame(1, $db->table('js_jst_work_order_info')->count());
$this->assertSame(1, $db->table('js_jst_small_payment_order_map')->count());
$migration->down();
$this->assertFalse($schema->hasIndex('js_jst_work_order_info', 'idx_small_payment_completion'));
$this->assertFalse($schema->hasIndex('js_jst_small_payment_order_map', 'idx_small_payment_order_map'));
DB::purge('small_payment_index_fixture');
}
}