ThirdGamePlayerIdTest.php 3.3 KB

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