EgameEsportsMigrationTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Tests\Integration;
  3. use Illuminate\Config\Repository;
  4. use Illuminate\Database\Capsule\Manager;
  5. use Illuminate\Database\Schema\Blueprint;
  6. use Illuminate\Foundation\Application;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Facade;
  9. use PHPUnit\Framework\TestCase;
  10. class EgameEsportsMigrationTest extends TestCase
  11. {
  12. protected function setUp(): void
  13. {
  14. Facade::clearResolvedInstances();
  15. $app = new Application(dirname(__DIR__, 2));
  16. $app->instance('config', new Repository());
  17. Facade::setFacadeApplication($app);
  18. $db = new Manager($app);
  19. $db->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => 'bot_']);
  20. $db->setAsGlobal();
  21. $app->instance('db', $db->getDatabaseManager());
  22. $app->bind('db.schema', fn () => $db->schema());
  23. $db->schema()->create('egame_items', function (Blueprint $table) {
  24. $table->id();
  25. $table->string('item_type');
  26. $table->string('plat_type')->default('');
  27. $table->unsignedTinyInteger('game_type');
  28. $table->string('game_code')->default('');
  29. $table->string('name')->default('');
  30. $table->string('logo')->default('');
  31. $table->text('logo_langs')->nullable();
  32. $table->string('ingress')->default('3');
  33. $table->integer('status')->default(1);
  34. $table->integer('sort')->default(0);
  35. $table->timestamps();
  36. $table->unique(['item_type', 'plat_type', 'game_type', 'game_code']);
  37. });
  38. (require dirname(__DIR__, 2) . '/database/migrations/2026_07_21_120000_add_catalog_management_to_egame_items_table.php')->up();
  39. }
  40. protected function tearDown(): void
  41. {
  42. DB::disconnect();
  43. Facade::clearResolvedInstances();
  44. parent::tearDown();
  45. }
  46. private function migration()
  47. {
  48. return require dirname(__DIR__, 2) . '/database/migrations/2026_10_28_180000_restore_esports_platform_relations.php';
  49. }
  50. public function test_upgrades_the_legacy_catalog_without_changing_other_rows_or_display_fields(): void
  51. {
  52. $targets = DB::table('egame_items')->where('item_type', 'platform')->where('game_type', 5)->get();
  53. $this->assertCount(3, $targets);
  54. foreach ($targets as $row) {
  55. $this->assertSame(0, (int) $row->status, 'Reproduce the legacy whitelist omission');
  56. }
  57. DB::table('egame_items')->where('plat_type', 'db5')->where('game_type', 5)->update([
  58. 'name' => '自定义电竞名称', 'logo' => '/custom.png', 'sort' => 99, 'lobby_enabled' => 0,
  59. ]);
  60. // Explicit category/global closures remain effective after the repair.
  61. DB::table('egame_items')->where('item_type', 'category')->where('game_type', 5)->update(['status' => 0]);
  62. DB::table('egame_items')->where('plat_type', 'tf')->where('game_type', 0)->update(['status' => 0]);
  63. DB::table('egame_items')->insert([
  64. ['item_type' => 'game', 'plat_type' => 'db5', 'game_type' => 5, 'game_code' => 'closed-game', 'status' => 0],
  65. ['item_type' => 'platform', 'plat_type' => 'custom', 'game_type' => 5, 'game_code' => '', 'status' => 0],
  66. ]);
  67. $before = DB::table('egame_items')->orderBy('id')->get();
  68. $this->migration()->up();
  69. $this->assertSame($before->count(), DB::table('egame_items')->count());
  70. foreach ($before as $row) {
  71. $after = DB::table('egame_items')->where('id', $row->id)->first();
  72. if ($row->item_type === 'platform' && (int) $row->game_type === 5
  73. && in_array($row->plat_type, ['db5', 'esb', 'tf'], true) && $row->game_code === '') {
  74. $this->assertSame(1, (int) $after->status);
  75. $this->assertSame(1, (int) $after->lobby_enabled);
  76. foreach (['status', 'lobby_enabled', 'updated_at'] as $field) {
  77. unset($row->{$field}, $after->{$field});
  78. }
  79. }
  80. $this->assertEquals($row, $after);
  81. }
  82. }
  83. public function test_repeated_up_is_unchanged_and_rollback_preserves_later_admin_changes(): void
  84. {
  85. $migration = $this->migration();
  86. $migration->up();
  87. DB::table('egame_items')->update(['updated_at' => '2026-01-01 00:00:00']);
  88. $before = DB::table('egame_items')->orderBy('id')->get();
  89. $migration->up();
  90. $this->assertEquals($before, DB::table('egame_items')->orderBy('id')->get());
  91. DB::table('egame_items')->where('plat_type', 'esb')->where('game_type', 5)->update(['status' => 0]);
  92. $before = DB::table('egame_items')->orderBy('id')->get();
  93. $migration->down();
  94. $this->assertEquals($before, DB::table('egame_items')->orderBy('id')->get());
  95. }
  96. }