CTTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Tokenizer;
  13. use PhpCsFixer\Tests\TestCase;
  14. use PhpCsFixer\Tokenizer\CT;
  15. /**
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Tokenizer\CT
  21. */
  22. final class CTTest extends TestCase
  23. {
  24. public function testUniqueValues(): void
  25. {
  26. $constants = self::getConstants();
  27. self::assertSame($constants, array_flip(array_flip($constants)), 'Values of CT::T_* constants must be unique.');
  28. }
  29. /**
  30. * @dataProvider provideConstantsCases
  31. */
  32. public function testHas(string $name, int $value): void
  33. {
  34. self::assertTrue(CT::has($value));
  35. }
  36. public function testHasNotExists(): void
  37. {
  38. self::assertFalse(CT::has(123));
  39. }
  40. /**
  41. * @dataProvider provideConstantsCases
  42. */
  43. public function testGetName(string $name, int $value): void
  44. {
  45. self::assertSame('CT::'.$name, CT::getName($value));
  46. }
  47. public function testGetNameNotExists(): void
  48. {
  49. $this->expectException(\InvalidArgumentException::class);
  50. $this->expectExceptionMessage('No custom token was found for "123".');
  51. CT::getName(123);
  52. }
  53. /**
  54. * @dataProvider provideConstantsCases
  55. */
  56. public function testConstants(string $name, int $value): void
  57. {
  58. self::assertGreaterThan(10_000, $value);
  59. self::assertFalse(\defined($name), 'The CT name must not use native T_* name.');
  60. }
  61. /**
  62. * @return iterable<array{string, int}>
  63. */
  64. public static function provideConstantsCases(): iterable
  65. {
  66. foreach (self::getConstants() as $name => $value) {
  67. yield [$name, $value];
  68. }
  69. }
  70. /**
  71. * @return array<string, int>
  72. */
  73. private static function getConstants(): array
  74. {
  75. static $constants;
  76. if (null === $constants) {
  77. $reflection = new \ReflectionClass(CT::class);
  78. $constants = $reflection->getConstants();
  79. }
  80. return $constants;
  81. }
  82. }