TransformerTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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\AutoReview;
  13. use PhpCsFixer\Tests\TestCase;
  14. use PhpCsFixer\Tokenizer\TransformerInterface;
  15. use PhpCsFixer\Tokenizer\Transformers;
  16. /**
  17. * @author Dave van der Brugge <dmvdbrugge@gmail.com>
  18. *
  19. * @internal
  20. *
  21. * @coversNothing
  22. *
  23. * @group auto-review
  24. * @group covers-nothing
  25. */
  26. final class TransformerTest extends TestCase
  27. {
  28. /**
  29. * @dataProvider provideTransformerPriorityCases
  30. */
  31. public function testTransformerPriority(TransformerInterface $first, TransformerInterface $second): void
  32. {
  33. self::assertLessThan(
  34. $first->getPriority(),
  35. $second->getPriority(),
  36. \sprintf('"%s" should have less priority than "%s"', \get_class($second), \get_class($first))
  37. );
  38. }
  39. /**
  40. * @dataProvider provideTransformerPriorityIsListedCases
  41. */
  42. public function testTransformerPriorityIsListed(TransformerInterface $transformer): void
  43. {
  44. $priority = $transformer->getPriority();
  45. if (0 === $priority) {
  46. $this->expectNotToPerformAssertions();
  47. return;
  48. }
  49. $name = $transformer->getName();
  50. foreach (self::provideTransformerPriorityCases() as $pair) {
  51. [$first, $second] = $pair;
  52. if ($name === $first->getName() || $name === $second->getName()) {
  53. $this->addToAssertionCount(1);
  54. return;
  55. }
  56. }
  57. self::fail(\sprintf('Transformer "%s" has priority %d but is not in priority test list.', $name, $priority));
  58. }
  59. /**
  60. * @return iterable<array{TransformerInterface, TransformerInterface}>
  61. */
  62. public static function provideTransformerPriorityCases(): iterable
  63. {
  64. $transformers = [];
  65. foreach (self::provideTransformerPriorityIsListedCases() as [$transformer]) {
  66. $transformers[$transformer->getName()] = $transformer;
  67. }
  68. yield [$transformers['attribute'], $transformers['brace']];
  69. yield [$transformers['attribute'], $transformers['square_brace']];
  70. yield [$transformers['brace'], $transformers['brace_class_instantiation']];
  71. yield [$transformers['brace'], $transformers['import']];
  72. yield [$transformers['brace'], $transformers['use']];
  73. yield [$transformers['name_qualified'], $transformers['namespace_operator']];
  74. yield [$transformers['return_ref'], $transformers['import']];
  75. yield [$transformers['return_ref'], $transformers['type_colon']];
  76. yield [$transformers['square_brace'], $transformers['brace_class_instantiation']];
  77. yield [$transformers['type_colon'], $transformers['named_argument']];
  78. yield [$transformers['type_colon'], $transformers['nullable_type']];
  79. yield [$transformers['array_typehint'], $transformers['type_alternation']];
  80. yield [$transformers['type_colon'], $transformers['type_alternation']];
  81. yield [$transformers['array_typehint'], $transformers['type_intersection']];
  82. yield [$transformers['type_colon'], $transformers['type_intersection']];
  83. yield [$transformers['type_alternation'], $transformers['disjunctive_normal_form_type_parenthesis']];
  84. yield [$transformers['use'], $transformers['type_colon']];
  85. }
  86. /**
  87. * @return iterable<array{TransformerInterface}>
  88. */
  89. public static function provideTransformerPriorityIsListedCases(): iterable
  90. {
  91. static $transformersArray = null;
  92. if (null === $transformersArray) {
  93. $transformers = Transformers::createSingleton();
  94. $reflection = new \ReflectionObject($transformers);
  95. $builtInTransformers = $reflection->getMethod('findBuiltInTransformers');
  96. $builtInTransformers->setAccessible(true);
  97. $transformersArray = [];
  98. foreach ($builtInTransformers->invoke($transformers) as $transformer) {
  99. $transformersArray[] = [$transformer];
  100. }
  101. }
  102. return $transformersArray;
  103. }
  104. }