| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- namespace Tests\Integration;
- use App\Models\User;
- use Illuminate\Config\Repository;
- use Illuminate\Database\Capsule\Manager;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Events\Dispatcher;
- use Illuminate\Foundation\Application;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Facade;
- use PHPUnit\Framework\TestCase;
- /** The database deliberately retains legacy DEFAULT 0 to verify application-level inserts. */
- class UserRechargeChannelGroupDefaultTest extends TestCase
- {
- protected function setUp(): void
- {
- Facade::clearResolvedInstances();
- $app = new Application(dirname(__DIR__, 2));
- $app->instance('config', new Repository());
- Facade::setFacadeApplication($app);
- $db = new Manager($app);
- $db->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => 'bot_']);
- $db->setEventDispatcher(new Dispatcher($app));
- $db->setAsGlobal(); $db->bootEloquent();
- $app->instance('db', $db->getDatabaseManager());
- User::clearBootedModels();
- $db->schema()->create('users', function (Blueprint $table) {
- $table->id(); $table->string('member_id')->unique(); $table->string('first_name')->nullable();
- $table->string('language')->default('zh');
- $table->integer('recharge_channel_group_id')->nullable()->default(0); $table->timestamps();
- });
- }
- protected function tearDown(): void
- {
- DB::disconnect(); User::clearBootedModels(); Facade::clearResolvedInstances(); parent::tearDown();
- }
- public function test_new_users_default_to_one_even_if_database_default_is_zero(): void
- {
- foreach ([[], ['recharge_channel_group_id' => 0], ['recharge_channel_group_id' => '0'],
- ['recharge_channel_group_id' => null], ['recharge_channel_group_id' => '']] as $index => $extra) {
- $user = User::create(['member_id' => 'new-' . $index] + $extra);
- $this->assertSame(1, (int)$user->recharge_channel_group_id);
- $this->assertSame(1, (int)DB::table('users')->where('id', $user->id)->value('recharge_channel_group_id'));
- }
- }
- public function test_explicit_positive_groups_and_existing_group_assignments_are_preserved(): void
- {
- $assigned = User::create(['member_id' => 'assigned', 'recharge_channel_group_id' => 7]);
- $this->assertSame(7, (int)$assigned->fresh()->recharge_channel_group_id);
- $legacyId = DB::table('users')->insertGetId(['member_id' => 'legacy', 'recharge_channel_group_id' => 0]);
- foreach ([$legacyId => 0, $assigned->id => 7] as $id => $expected) {
- $user = User::findOrFail($id);
- $this->assertSame($expected, (int)$user->recharge_channel_group_id);
- $user->first_name = 'updated profile';
- $user->save();
- $this->assertSame($expected, (int)$user->fresh()->recharge_channel_group_id);
- }
- $assigned->recharge_channel_group_id = 2;
- $assigned->save();
- $this->assertSame(2, (int)$assigned->fresh()->recharge_channel_group_id);
- }
- public function test_migration_only_changes_the_prefixed_column_default_without_rewriting_existing_users(): void
- {
- app('config')->set('database.connections.mysql_preview', [
- 'driver' => 'mysql', 'host' => '127.0.0.1', 'database' => 'not_connected',
- 'username' => 'not_connected', 'password' => '', 'prefix' => 'bot_',
- ]);
- $original = DB::getDefaultConnection();
- DB::setDefaultConnection('mysql_preview');
- try {
- $connection = DB::connection();
- $connection->setPdo(static function () { throw new \RuntimeException('SQL preview must not connect to a real database.'); });
- $migration = require dirname(__DIR__, 2) . '/database/migrations/2026_10_25_130000_default_user_recharge_channel_group_to_one.php';
- $queries = $connection->pretend(function () use ($migration) { $migration->up(); $migration->down(); });
- $this->assertSame([
- 'ALTER TABLE `bot_users` ALTER COLUMN `recharge_channel_group_id` SET DEFAULT 1',
- 'ALTER TABLE `bot_users` ALTER COLUMN `recharge_channel_group_id` SET DEFAULT 0',
- ], array_column($queries, 'query'));
- } finally {
- DB::disconnect('mysql_preview'); DB::setDefaultConnection($original);
- }
- }
- }
|