UserRechargeChannelGroupDefaultTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace Tests\Integration;
  3. use App\Models\User;
  4. use Illuminate\Config\Repository;
  5. use Illuminate\Database\Capsule\Manager;
  6. use Illuminate\Database\Schema\Blueprint;
  7. use Illuminate\Events\Dispatcher;
  8. use Illuminate\Foundation\Application;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Facade;
  11. use PHPUnit\Framework\TestCase;
  12. /** The database deliberately retains legacy DEFAULT 0 to verify application-level inserts. */
  13. class UserRechargeChannelGroupDefaultTest extends TestCase
  14. {
  15. protected function setUp(): void
  16. {
  17. Facade::clearResolvedInstances();
  18. $app = new Application(dirname(__DIR__, 2));
  19. $app->instance('config', new Repository());
  20. Facade::setFacadeApplication($app);
  21. $db = new Manager($app);
  22. $db->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => 'bot_']);
  23. $db->setEventDispatcher(new Dispatcher($app));
  24. $db->setAsGlobal(); $db->bootEloquent();
  25. $app->instance('db', $db->getDatabaseManager());
  26. User::clearBootedModels();
  27. $db->schema()->create('users', function (Blueprint $table) {
  28. $table->id(); $table->string('member_id')->unique(); $table->string('first_name')->nullable();
  29. $table->string('language')->default('zh');
  30. $table->integer('recharge_channel_group_id')->nullable()->default(0); $table->timestamps();
  31. });
  32. }
  33. protected function tearDown(): void
  34. {
  35. DB::disconnect(); User::clearBootedModels(); Facade::clearResolvedInstances(); parent::tearDown();
  36. }
  37. public function test_new_users_default_to_one_even_if_database_default_is_zero(): void
  38. {
  39. foreach ([[], ['recharge_channel_group_id' => 0], ['recharge_channel_group_id' => '0'],
  40. ['recharge_channel_group_id' => null], ['recharge_channel_group_id' => '']] as $index => $extra) {
  41. $user = User::create(['member_id' => 'new-' . $index] + $extra);
  42. $this->assertSame(1, (int)$user->recharge_channel_group_id);
  43. $this->assertSame(1, (int)DB::table('users')->where('id', $user->id)->value('recharge_channel_group_id'));
  44. }
  45. }
  46. public function test_explicit_positive_groups_and_existing_group_assignments_are_preserved(): void
  47. {
  48. $assigned = User::create(['member_id' => 'assigned', 'recharge_channel_group_id' => 7]);
  49. $this->assertSame(7, (int)$assigned->fresh()->recharge_channel_group_id);
  50. $legacyId = DB::table('users')->insertGetId(['member_id' => 'legacy', 'recharge_channel_group_id' => 0]);
  51. foreach ([$legacyId => 0, $assigned->id => 7] as $id => $expected) {
  52. $user = User::findOrFail($id);
  53. $this->assertSame($expected, (int)$user->recharge_channel_group_id);
  54. $user->first_name = 'updated profile';
  55. $user->save();
  56. $this->assertSame($expected, (int)$user->fresh()->recharge_channel_group_id);
  57. }
  58. $assigned->recharge_channel_group_id = 2;
  59. $assigned->save();
  60. $this->assertSame(2, (int)$assigned->fresh()->recharge_channel_group_id);
  61. }
  62. public function test_migration_only_changes_the_prefixed_column_default_without_rewriting_existing_users(): void
  63. {
  64. app('config')->set('database.connections.mysql_preview', [
  65. 'driver' => 'mysql', 'host' => '127.0.0.1', 'database' => 'not_connected',
  66. 'username' => 'not_connected', 'password' => '', 'prefix' => 'bot_',
  67. ]);
  68. $original = DB::getDefaultConnection();
  69. DB::setDefaultConnection('mysql_preview');
  70. try {
  71. $connection = DB::connection();
  72. $connection->setPdo(static function () { throw new \RuntimeException('SQL preview must not connect to a real database.'); });
  73. $migration = require dirname(__DIR__, 2) . '/database/migrations/2026_10_25_130000_default_user_recharge_channel_group_to_one.php';
  74. $queries = $connection->pretend(function () use ($migration) { $migration->up(); $migration->down(); });
  75. $this->assertSame([
  76. 'ALTER TABLE `bot_users` ALTER COLUMN `recharge_channel_group_id` SET DEFAULT 1',
  77. 'ALTER TABLE `bot_users` ALTER COLUMN `recharge_channel_group_id` SET DEFAULT 0',
  78. ], array_column($queries, 'query'));
  79. } finally {
  80. DB::disconnect('mysql_preview'); DB::setDefaultConnection($original);
  81. }
  82. }
  83. }