TransformerTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. public static function provideTransformerPriorityCases(): iterable
  60. {
  61. $transformers = [];
  62. foreach (self::provideTransformerPriorityIsListedCases() as [$transformer]) {
  63. $transformers[$transformer->getName()] = $transformer;
  64. }
  65. yield [$transformers['attribute'], $transformers['curly_brace']];
  66. yield [$transformers['attribute'], $transformers['square_brace']];
  67. yield [$transformers['curly_brace'], $transformers['brace_class_instantiation']];
  68. yield [$transformers['curly_brace'], $transformers['import']];
  69. yield [$transformers['curly_brace'], $transformers['use']];
  70. yield [$transformers['name_qualified'], $transformers['namespace_operator']];
  71. yield [$transformers['return_ref'], $transformers['import']];
  72. yield [$transformers['return_ref'], $transformers['type_colon']];
  73. yield [$transformers['square_brace'], $transformers['brace_class_instantiation']];
  74. yield [$transformers['type_colon'], $transformers['named_argument']];
  75. yield [$transformers['type_colon'], $transformers['nullable_type']];
  76. yield [$transformers['array_typehint'], $transformers['type_alternation']];
  77. yield [$transformers['type_colon'], $transformers['type_alternation']];
  78. yield [$transformers['array_typehint'], $transformers['type_intersection']];
  79. yield [$transformers['type_colon'], $transformers['type_intersection']];
  80. yield [$transformers['type_alternation'], $transformers['disjunctive_normal_form_type_parenthesis']];
  81. yield [$transformers['use'], $transformers['type_colon']];
  82. }
  83. /**
  84. * @return TransformerInterface[]
  85. */
  86. public static function provideTransformerPriorityIsListedCases(): iterable
  87. {
  88. static $transformersArray = null;
  89. if (null === $transformersArray) {
  90. $transformers = Transformers::createSingleton();
  91. $reflection = new \ReflectionObject($transformers);
  92. $builtInTransformers = $reflection->getMethod('findBuiltInTransformers');
  93. $builtInTransformers->setAccessible(true);
  94. $transformersArray = [];
  95. foreach ($builtInTransformers->invoke($transformers) as $transformer) {
  96. $transformersArray[] = [$transformer];
  97. }
  98. }
  99. return $transformersArray;
  100. }
  101. }