| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?php
- namespace Tests\Unit;
- use App\Services\ThirdGameBalanceService;
- use InvalidArgumentException;
- use Illuminate\Config\Repository;
- use Illuminate\Foundation\Application;
- use Illuminate\Support\Facades\Facade;
- use PHPUnit\Framework\TestCase;
- class ThirdGamePlayerIdTest extends TestCase
- {
- private Application $app;
- protected function setUp(): void
- {
- Facade::clearResolvedInstances();
- $this->app = new Application(dirname(__DIR__, 2));
- $this->app->instance('config', new Repository(['third_game' => ['player_prefix' => 'u']]));
- Facade::setFacadeApplication($this->app);
- }
- protected function tearDown(): void
- {
- Facade::clearResolvedInstances(); Facade::setFacadeApplication(null); parent::tearDown();
- }
- /** @dataProvider playerIds */
- public function test_member_player_ids_meet_provider_length_without_changing_valid_accounts(int $id, string $expected): void
- {
- $actual = (new ThirdGameBalanceService())->playerIdFromUserId($id);
- $this->assertSame($expected, $actual);
- $this->assertMatchesRegularExpression('/^u[0-9]{4,10}$/D', $actual);
- }
- public static function playerIds(): array
- {
- return [[1, 'u0001'], [106, 'u0106'], [483, 'u0483'], [999, 'u0999'],
- [1000, 'u1000'], [751434, 'u751434'], [9999999999, 'u9999999999']];
- }
- /** @dataProvider invalidIds */
- public function test_invalid_user_ids_fail_locally_instead_of_producing_an_invalid_provider_account(int $id): void
- {
- $this->expectException(InvalidArgumentException::class);
- (new ThirdGameBalanceService())->playerIdFromUserId($id);
- }
- public static function invalidIds(): array { return [[0], [-1], [10000000000]]; }
- public function test_test_namespace_never_reuses_production_member_identity_or_balance_cache(): void
- {
- $service = new ThirdGameBalanceService();
- $cache = new \ReflectionMethod($service, 'cacheKey');
- $detail = new \ReflectionMethod($service, 'detailCacheKey');
- $this->assertSame('u0106', $service->playerIdFromUserId(106));
- $this->assertSame('third_game_total_balance:m106', $cache->invoke($service, 'm106'));
- $this->assertSame('third_game_balance_detail:m106', $detail->invoke($service, 'm106'));
- $this->app['config']->set('third_game.player_prefix', 'tu');
- $this->assertSame('tu0106', $service->playerIdFromUserId(106));
- $this->assertSame('tu0001', $service->playerIdFromUserId(1));
- $this->assertSame('tu999999999', $service->playerIdFromUserId(999999999));
- $this->assertSame('third_game_total_balance:tu:m106', $cache->invoke($service, 'm106'));
- $this->assertSame('third_game_balance_detail:tu:m106', $detail->invoke($service, 'm106'));
- $this->expectException(InvalidArgumentException::class);
- $service->playerIdFromUserId(1000000000);
- }
- /** @dataProvider invalidPrefixes */
- public function test_invalid_prefixes_do_not_fall_back_to_production(string $prefix): void
- {
- $this->app['config']->set('third_game.player_prefix', $prefix);
- $this->expectException(InvalidArgumentException::class);
- (new ThirdGameBalanceService())->playerIdFromUserId(106);
- }
- public static function invalidPrefixes(): array { return [[''], ['U'], ['test'], ['a'], ['t_u']]; }
- }
|