|
|
@@ -0,0 +1,111 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Tests\Integration;
|
|
|
+
|
|
|
+use App\Services\ThirdGameBalanceService;
|
|
|
+use App\Services\ThirdGameOrderService;
|
|
|
+use Illuminate\Config\Repository;
|
|
|
+use Illuminate\Database\Capsule\Manager;
|
|
|
+use Illuminate\Foundation\Application;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+use Illuminate\Support\Facades\Facade;
|
|
|
+use PHPUnit\Framework\TestCase;
|
|
|
+
|
|
|
+/** Provider order identities are resolved against an isolated SQLite database only. */
|
|
|
+class ThirdGamePlayerIdentityTest extends TestCase
|
|
|
+{
|
|
|
+ protected function setUp(): void
|
|
|
+ {
|
|
|
+ Facade::clearResolvedInstances();
|
|
|
+ $app = new Application(dirname(__DIR__, 2));
|
|
|
+ $app->instance('config', new Repository(['app' => ['timezone' => 'Asia/Shanghai'], 'third_game' => ['player_prefix' => 'u']]));
|
|
|
+ Facade::setFacadeApplication($app);
|
|
|
+ $db = new Manager($app);
|
|
|
+ $db->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => 'bot_']);
|
|
|
+ $db->setAsGlobal(); $db->bootEloquent(); $app->instance('db', $db->getDatabaseManager());
|
|
|
+ DB::statement('CREATE TABLE bot_users (id INTEGER PRIMARY KEY,member_id TEXT,username TEXT,first_name TEXT)');
|
|
|
+ DB::statement('CREATE TABLE bot_third_game_orders (id INTEGER PRIMARY KEY AUTOINCREMENT,order_key TEXT UNIQUE,
|
|
|
+ user_id INTEGER,member_id TEXT,username TEXT,first_name TEXT,player_id TEXT,platform TEXT,currency TEXT,
|
|
|
+ game_type INTEGER,game_name TEXT,round_no TEXT,table_no TEXT,seat TEXT,bet_amount TEXT,valid_amount TEXT,
|
|
|
+ settled_amount TEXT,bet_content TEXT,status INTEGER,game_order_id TEXT,bet_time TEXT,last_update_time TEXT,
|
|
|
+ created_at TEXT,updated_at TEXT)');
|
|
|
+ DB::table('users')->insert([
|
|
|
+ ['id' => 1, 'member_id' => 'm1', 'username' => 'one', 'first_name' => ''],
|
|
|
+ ['id' => 106, 'member_id' => 'm106', 'username' => 'member106', 'first_name' => ''],
|
|
|
+ ['id' => 1000, 'member_id' => 'm1000', 'username' => 'member1000', 'first_name' => ''],
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function tearDown(): void
|
|
|
+ {
|
|
|
+ DB::disconnect(); Facade::clearResolvedInstances(); parent::tearDown();
|
|
|
+ }
|
|
|
+
|
|
|
+ private function lookup(array $playerIds): array
|
|
|
+ {
|
|
|
+ $service = new ThirdGameOrderService(new ThirdGameBalanceService());
|
|
|
+ return (new \ReflectionMethod($service, 'usersByPlayerId'))->invoke($service, $playerIds);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function test_new_padded_and_legacy_identifiers_in_one_batch_resolve_to_the_correct_member(): void
|
|
|
+ {
|
|
|
+ $rows = $this->lookup(['u106', 'u0106', 'u0001', 'u1', 'u1000']);
|
|
|
+ $this->assertCount(5, $rows);
|
|
|
+ $this->assertSame(106, $rows['u0106']->id);
|
|
|
+ $this->assertSame(106, $rows['u106']->id);
|
|
|
+ $this->assertSame('m106', $rows['u0106']->member_id);
|
|
|
+ $this->assertSame(1, $rows['u0001']->id);
|
|
|
+ $this->assertSame(1, $rows['u1']->id);
|
|
|
+ $this->assertSame(1000, $rows['u1000']->id);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function test_noncanonical_zero_padding_and_unknown_accounts_do_not_get_bound_to_another_member(): void
|
|
|
+ {
|
|
|
+ $this->assertSame([], $this->lookup(['u000106', 'u01', 'u00001', 'u01000', 'u0000',
|
|
|
+ 'u0', 'u10000000000', 'u0999', 'a0106', 'U0106', 'p1234567890']));
|
|
|
+ $this->assertSame([], $this->lookup([]));
|
|
|
+ }
|
|
|
+ public function test_test_namespace_does_not_associate_production_players_with_test_members(): void
|
|
|
+ {
|
|
|
+ config(['third_game.player_prefix' => 'tu']);
|
|
|
+ $rows = $this->lookup(['u106', 'u0106', 'u1000', 'tu106', 'tu0106', 'tu1000']);
|
|
|
+ $this->assertSame(['tu106', 'tu0106', 'tu1000'], array_keys($rows));
|
|
|
+ $this->assertSame(106, $rows['tu0106']->id);
|
|
|
+ $this->assertSame([], $this->lookup(['u0106', 'tu00106', 'tu0000', 'tu1000000000']));
|
|
|
+ }
|
|
|
+
|
|
|
+ public function test_sync_and_listing_keep_namespaces_separate_even_when_an_old_foreign_snapshot_exists(): void
|
|
|
+ {
|
|
|
+ config(['third_game.player_prefix' => 'tu']);
|
|
|
+ $foreign = ['player_id' => 'u0106', 'platform' => 'pg', 'game_order_id' => 'production-order',
|
|
|
+ 'currency' => 'CNY', 'game_type' => 2, 'status' => 1, 'bet_amount' => '999',
|
|
|
+ 'bet_time' => '2026-10-27 10:00:00', 'last_update_time' => '2026-10-27 10:01:00'];
|
|
|
+ $foreignKey = hash('sha256', "pg\0u0106\0production-order");
|
|
|
+ DB::table('third_game_orders')->insert($foreign + ['order_key' => $foreignKey, 'user_id' => 106,
|
|
|
+ 'member_id' => 'm106', 'username' => 'member106', 'first_name' => '', 'created_at' => '2026-10-27 10:00:00']);
|
|
|
+ $before = (array) DB::table('third_game_orders')->where('order_key', $foreignKey)->first();
|
|
|
+ $local = array_replace($foreign, ['player_id' => 'tu0106', 'game_order_id' => 'test-order', 'bet_amount' => '10']);
|
|
|
+ $service = new ThirdGameOrderService(new ThirdGameBalanceService());
|
|
|
+ $persist = new \ReflectionMethod($service, 'persistProviderResult');
|
|
|
+ $result = $persist->invoke($service, ['ok' => true, 'list' => [$foreign, $local],
|
|
|
+ 'page_no' => 1, 'page_size' => 2, 'total' => 4]);
|
|
|
+ $this->assertSame(2, $result['received']);
|
|
|
+ $this->assertSame(1, $result['synced']);
|
|
|
+ $this->assertSame(1, $result['matched_users']);
|
|
|
+ $this->assertSame(4, $result['total']);
|
|
|
+ $this->assertTrue($result['has_more']);
|
|
|
+ $this->assertSame($before, (array) DB::table('third_game_orders')->where('order_key', $foreignKey)->first());
|
|
|
+ $this->assertSame(106, DB::table('third_game_orders')->where('player_id', 'tu0106')->value('user_id'));
|
|
|
+ $list = $service->paginate([]);
|
|
|
+ $this->assertSame(1, $list['total']);
|
|
|
+ $this->assertSame('tu0106', $list['list'][0]['player_id']);
|
|
|
+ config(['third_game.player_prefix' => 'u']);
|
|
|
+ $production = $service->paginate([]);
|
|
|
+ $this->assertSame(1, $production['total']);
|
|
|
+ $this->assertSame('u0106', $production['list'][0]['player_id']);
|
|
|
+ $rejected = $persist->invoke($service, ['ok' => true, 'list' => [$local], 'page_no' => 1, 'page_size' => 1, 'total' => 1]);
|
|
|
+ $this->assertSame(0, $rejected['synced']);
|
|
|
+ $this->assertSame(2, DB::table('third_game_orders')->count());
|
|
|
+ }
|
|
|
+
|
|
|
+}
|