ThirdGamePlayerIdTest.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Services\ThirdGameBalanceService;
  4. use InvalidArgumentException;
  5. use Illuminate\Config\Repository;
  6. use Illuminate\Foundation\Application;
  7. use Illuminate\Support\Facades\Facade;
  8. use PHPUnit\Framework\Attributes\DataProvider;
  9. use PHPUnit\Framework\TestCase;
  10. class ThirdGamePlayerIdTest extends TestCase
  11. {
  12. private Application $app;
  13. protected function setUp(): void
  14. {
  15. Facade::clearResolvedInstances();
  16. $this->app = new Application(dirname(__DIR__, 2));
  17. $this->app->instance('config', new Repository(['third_game' => ['player_prefix' => 'u']]));
  18. Facade::setFacadeApplication($this->app);
  19. }
  20. protected function tearDown(): void
  21. {
  22. Facade::clearResolvedInstances(); Facade::setFacadeApplication(null); parent::tearDown();
  23. }
  24. #[DataProvider('playerIds')]
  25. public function test_member_player_ids_meet_provider_length_without_changing_valid_accounts(int $id, string $expected): void
  26. {
  27. $actual = (new ThirdGameBalanceService())->playerIdFromUserId($id);
  28. $this->assertSame($expected, $actual);
  29. $this->assertMatchesRegularExpression('/^u[0-9]{4,10}$/D', $actual);
  30. }
  31. public static function playerIds(): array
  32. {
  33. return [[1, 'u0001'], [106, 'u0106'], [483, 'u0483'], [999, 'u0999'],
  34. [1000, 'u1000'], [751434, 'u751434'], [9999999999, 'u9999999999']];
  35. }
  36. #[DataProvider('invalidIds')]
  37. public function test_invalid_user_ids_fail_locally_instead_of_producing_an_invalid_provider_account(int $id): void
  38. {
  39. $this->expectException(InvalidArgumentException::class);
  40. (new ThirdGameBalanceService())->playerIdFromUserId($id);
  41. }
  42. public static function invalidIds(): array { return [[0], [-1], [10000000000]]; }
  43. public function test_test_namespace_never_reuses_production_member_identity_or_balance_cache(): void
  44. {
  45. $service = new ThirdGameBalanceService();
  46. $cache = new \ReflectionMethod($service, 'cacheKey');
  47. $detail = new \ReflectionMethod($service, 'detailCacheKey');
  48. $this->assertSame('u0106', $service->playerIdFromUserId(106));
  49. $this->assertSame('third_game_total_balance:m106', $cache->invoke($service, 'm106'));
  50. $this->assertSame('third_game_balance_detail:m106', $detail->invoke($service, 'm106'));
  51. $this->app['config']->set('third_game.player_prefix', 'tu');
  52. $this->assertSame('tu0106', $service->playerIdFromUserId(106));
  53. $this->assertSame('tu0001', $service->playerIdFromUserId(1));
  54. $this->assertSame('tu999999999', $service->playerIdFromUserId(999999999));
  55. $this->assertSame('third_game_total_balance:tu:m106', $cache->invoke($service, 'm106'));
  56. $this->assertSame('third_game_balance_detail:tu:m106', $detail->invoke($service, 'm106'));
  57. $this->expectException(InvalidArgumentException::class);
  58. $service->playerIdFromUserId(1000000000);
  59. }
  60. #[DataProvider('invalidPrefixes')]
  61. public function test_invalid_prefixes_do_not_fall_back_to_production(string $prefix): void
  62. {
  63. $this->app['config']->set('third_game.player_prefix', $prefix);
  64. $this->expectException(InvalidArgumentException::class);
  65. (new ThirdGameBalanceService())->playerIdFromUserId(106);
  66. }
  67. public static function invalidPrefixes(): array { return [[''], ['U'], ['test'], ['a'], ['t_u']]; }
  68. }