FixerFactoryTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\AutoReview;
  12. use PhpCsFixer\Fixer\FixerInterface;
  13. use PhpCsFixer\FixerFactory;
  14. use PhpCsFixer\Tests\Test\IntegrationCaseFactory;
  15. use PhpCsFixer\Tests\TestCase;
  16. use Symfony\Component\Finder\SplFileInfo;
  17. /**
  18. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  19. *
  20. * @internal
  21. *
  22. * @coversNothing
  23. * @group auto-review
  24. * @group covers-nothing
  25. */
  26. final class FixerFactoryTest extends TestCase
  27. {
  28. public function testFixersPriorityEdgeFixers()
  29. {
  30. $factory = new FixerFactory();
  31. $factory->registerBuiltInFixers();
  32. $fixers = $factory->getFixers();
  33. static::assertSame('encoding', $fixers[0]->getName(), 'Expected "encoding" fixer to have the highest priority.');
  34. static::assertSame('full_opening_tag', $fixers[1]->getName(), 'Expected "full_opening_tag" fixer has second highest priority.');
  35. static::assertSame('single_blank_line_at_eof', $fixers[\count($fixers) - 1]->getName(), 'Expected "single_blank_line_at_eof" to have the lowest priority.');
  36. }
  37. /**
  38. * @dataProvider provideFixersPriorityCases
  39. * @dataProvider provideFixersPrioritySpecialPhpdocCases
  40. */
  41. public function testFixersPriority(FixerInterface $first, FixerInterface $second)
  42. {
  43. static::assertLessThan($first->getPriority(), $second->getPriority(), sprintf('"%s" should have less priority than "%s"', \get_class($second), \get_class($first)));
  44. }
  45. public function provideFixersPriorityCases()
  46. {
  47. $factory = new FixerFactory();
  48. $factory->registerBuiltInFixers();
  49. $fixers = [];
  50. foreach ($factory->getFixers() as $fixer) {
  51. $fixers[$fixer->getName()] = $fixer;
  52. }
  53. return [
  54. [$fixers['array_indentation'], $fixers['align_multiline_comment']],
  55. [$fixers['array_indentation'], $fixers['binary_operator_spaces']],
  56. [$fixers['array_syntax'], $fixers['binary_operator_spaces']],
  57. [$fixers['array_syntax'], $fixers['ternary_operator_spaces']],
  58. [$fixers['backtick_to_shell_exec'], $fixers['escape_implicit_backslashes']],
  59. [$fixers['backtick_to_shell_exec'], $fixers['explicit_string_variable']],
  60. [$fixers['blank_line_after_opening_tag'], $fixers['no_blank_lines_before_namespace']],
  61. [$fixers['braces'], $fixers['array_indentation']],
  62. [$fixers['braces'], $fixers['method_argument_space']],
  63. [$fixers['braces'], $fixers['method_chaining_indentation']],
  64. [$fixers['class_attributes_separation'], $fixers['braces']],
  65. [$fixers['class_attributes_separation'], $fixers['indentation_type']],
  66. [$fixers['class_keyword_remove'], $fixers['no_unused_imports']],
  67. [$fixers['combine_consecutive_issets'], $fixers['multiline_whitespace_before_semicolons']],
  68. [$fixers['combine_consecutive_issets'], $fixers['no_singleline_whitespace_before_semicolons']],
  69. [$fixers['combine_consecutive_issets'], $fixers['no_spaces_inside_parenthesis']],
  70. [$fixers['combine_consecutive_issets'], $fixers['no_trailing_whitespace']],
  71. [$fixers['combine_consecutive_issets'], $fixers['no_whitespace_in_blank_line']],
  72. [$fixers['combine_consecutive_unsets'], $fixers['no_extra_blank_lines']],
  73. [$fixers['combine_consecutive_unsets'], $fixers['no_trailing_whitespace']],
  74. [$fixers['combine_consecutive_unsets'], $fixers['no_whitespace_in_blank_line']],
  75. [$fixers['combine_consecutive_unsets'], $fixers['space_after_semicolon']],
  76. [$fixers['combine_nested_dirname'], $fixers['method_argument_space']],
  77. [$fixers['combine_nested_dirname'], $fixers['no_spaces_inside_parenthesis']],
  78. [$fixers['declare_strict_types'], $fixers['blank_line_after_opening_tag']],
  79. [$fixers['declare_strict_types'], $fixers['declare_equal_normalize']],
  80. [$fixers['dir_constant'], $fixers['combine_nested_dirname']],
  81. [$fixers['doctrine_annotation_array_assignment'], $fixers['doctrine_annotation_spaces']],
  82. [$fixers['elseif'], $fixers['braces']],
  83. [$fixers['escape_implicit_backslashes'], $fixers['heredoc_to_nowdoc']],
  84. [$fixers['escape_implicit_backslashes'], $fixers['single_quote']],
  85. [$fixers['explicit_string_variable'], $fixers['simple_to_complex_string_variable']],
  86. [$fixers['fully_qualified_strict_types'], $fixers['no_superfluous_phpdoc_tags']],
  87. [$fixers['function_to_constant'], $fixers['native_function_casing']],
  88. [$fixers['function_to_constant'], $fixers['no_extra_blank_lines']],
  89. [$fixers['function_to_constant'], $fixers['no_singleline_whitespace_before_semicolons']],
  90. [$fixers['function_to_constant'], $fixers['no_trailing_whitespace']],
  91. [$fixers['function_to_constant'], $fixers['no_whitespace_in_blank_line']],
  92. [$fixers['general_phpdoc_annotation_remove'], $fixers['no_empty_phpdoc']],
  93. [$fixers['general_phpdoc_annotation_remove'], $fixers['phpdoc_separation']],
  94. [$fixers['general_phpdoc_annotation_remove'], $fixers['phpdoc_trim']],
  95. [$fixers['implode_call'], $fixers['method_argument_space']],
  96. [$fixers['indentation_type'], $fixers['phpdoc_indent']],
  97. [$fixers['is_null'], $fixers['yoda_style']],
  98. [$fixers['line_ending'], $fixers['braces']],
  99. [$fixers['list_syntax'], $fixers['binary_operator_spaces']],
  100. [$fixers['list_syntax'], $fixers['ternary_operator_spaces']],
  101. [$fixers['method_chaining_indentation'], $fixers['array_indentation']],
  102. [$fixers['method_separation'], $fixers['braces']],
  103. [$fixers['method_separation'], $fixers['indentation_type']],
  104. [$fixers['multiline_whitespace_before_semicolons'], $fixers['space_after_semicolon']],
  105. [$fixers['no_alias_functions'], $fixers['implode_call']],
  106. [$fixers['no_alias_functions'], $fixers['php_unit_dedicate_assert']],
  107. [$fixers['no_alternative_syntax'], $fixers['braces']],
  108. [$fixers['no_alternative_syntax'], $fixers['elseif']],
  109. [$fixers['no_alternative_syntax'], $fixers['no_superfluous_elseif']],
  110. [$fixers['no_alternative_syntax'], $fixers['no_useless_else']],
  111. [$fixers['no_blank_lines_after_phpdoc'], $fixers['header_comment']],
  112. [$fixers['no_blank_lines_after_phpdoc'], $fixers['single_blank_line_before_namespace']],
  113. [$fixers['no_empty_comment'], $fixers['no_extra_blank_lines']],
  114. [$fixers['no_empty_comment'], $fixers['no_trailing_whitespace']],
  115. [$fixers['no_empty_comment'], $fixers['no_whitespace_in_blank_line']],
  116. [$fixers['no_empty_phpdoc'], $fixers['no_extra_blank_lines']],
  117. [$fixers['no_empty_phpdoc'], $fixers['no_trailing_whitespace']],
  118. [$fixers['no_empty_phpdoc'], $fixers['no_whitespace_in_blank_line']],
  119. [$fixers['no_empty_statement'], $fixers['braces']],
  120. [$fixers['no_empty_statement'], $fixers['combine_consecutive_unsets']],
  121. [$fixers['no_empty_statement'], $fixers['multiline_whitespace_before_semicolons']],
  122. [$fixers['no_empty_statement'], $fixers['no_extra_blank_lines']],
  123. [$fixers['no_empty_statement'], $fixers['no_singleline_whitespace_before_semicolons']],
  124. [$fixers['no_empty_statement'], $fixers['no_trailing_whitespace']],
  125. [$fixers['no_empty_statement'], $fixers['no_useless_else']],
  126. [$fixers['no_empty_statement'], $fixers['no_useless_return']],
  127. [$fixers['no_empty_statement'], $fixers['no_whitespace_in_blank_line']],
  128. [$fixers['no_empty_statement'], $fixers['return_assignment']],
  129. [$fixers['no_empty_statement'], $fixers['space_after_semicolon']],
  130. [$fixers['no_empty_statement'], $fixers['switch_case_semicolon_to_colon']],
  131. [$fixers['no_extra_blank_lines'], $fixers['blank_line_before_statement']],
  132. [$fixers['no_leading_import_slash'], $fixers['ordered_imports']],
  133. [$fixers['no_multiline_whitespace_around_double_arrow'], $fixers['binary_operator_spaces']],
  134. [$fixers['no_multiline_whitespace_around_double_arrow'], $fixers['trailing_comma_in_multiline_array']],
  135. [$fixers['no_php4_constructor'], $fixers['ordered_class_elements']],
  136. [$fixers['no_short_bool_cast'], $fixers['cast_spaces']],
  137. [$fixers['no_short_echo_tag'], $fixers['no_mixed_echo_print']],
  138. [$fixers['no_spaces_after_function_name'], $fixers['function_to_constant']],
  139. [$fixers['no_spaces_inside_parenthesis'], $fixers['function_to_constant']],
  140. [$fixers['no_superfluous_phpdoc_tags'], $fixers['no_empty_phpdoc']],
  141. [$fixers['no_unneeded_control_parentheses'], $fixers['no_trailing_whitespace']],
  142. [$fixers['no_unneeded_curly_braces'], $fixers['no_useless_else']],
  143. [$fixers['no_unneeded_curly_braces'], $fixers['no_useless_return']],
  144. [$fixers['no_unneeded_curly_braces'], $fixers['return_assignment']],
  145. [$fixers['no_unset_on_property'], $fixers['combine_consecutive_unsets']],
  146. [$fixers['no_unused_imports'], $fixers['blank_line_after_namespace']],
  147. [$fixers['no_unused_imports'], $fixers['no_extra_blank_lines']],
  148. [$fixers['no_unused_imports'], $fixers['no_leading_import_slash']],
  149. [$fixers['no_unused_imports'], $fixers['single_line_after_imports']],
  150. [$fixers['no_useless_else'], $fixers['braces']],
  151. [$fixers['no_useless_else'], $fixers['combine_consecutive_unsets']],
  152. [$fixers['no_useless_else'], $fixers['no_extra_blank_lines']],
  153. [$fixers['no_useless_else'], $fixers['no_trailing_whitespace']],
  154. [$fixers['no_useless_else'], $fixers['no_useless_return']],
  155. [$fixers['no_useless_else'], $fixers['no_whitespace_in_blank_line']],
  156. [$fixers['no_useless_return'], $fixers['blank_line_before_return']],
  157. [$fixers['no_useless_return'], $fixers['blank_line_before_statement']],
  158. [$fixers['no_useless_return'], $fixers['no_extra_blank_lines']],
  159. [$fixers['no_useless_return'], $fixers['no_whitespace_in_blank_line']],
  160. [$fixers['ordered_class_elements'], $fixers['class_attributes_separation']],
  161. [$fixers['ordered_class_elements'], $fixers['method_separation']],
  162. [$fixers['ordered_class_elements'], $fixers['no_blank_lines_after_class_opening']],
  163. [$fixers['ordered_class_elements'], $fixers['space_after_semicolon']],
  164. [$fixers['php_unit_construct'], $fixers['php_unit_dedicate_assert']],
  165. [$fixers['php_unit_dedicate_assert'], $fixers['php_unit_dedicate_assert_internal_type']],
  166. [$fixers['php_unit_fqcn_annotation'], $fixers['no_unused_imports']],
  167. [$fixers['php_unit_fqcn_annotation'], $fixers['php_unit_ordered_covers']],
  168. [$fixers['php_unit_internal_class'], $fixers['final_internal_class']],
  169. [$fixers['php_unit_no_expectation_annotation'], $fixers['no_empty_phpdoc']],
  170. [$fixers['php_unit_no_expectation_annotation'], $fixers['php_unit_expectation']],
  171. [$fixers['php_unit_test_annotation'], $fixers['no_empty_phpdoc']],
  172. [$fixers['php_unit_test_annotation'], $fixers['php_unit_method_casing']],
  173. [$fixers['php_unit_test_annotation'], $fixers['phpdoc_trim']],
  174. [$fixers['phpdoc_add_missing_param_annotation'], $fixers['no_empty_phpdoc']],
  175. [$fixers['phpdoc_add_missing_param_annotation'], $fixers['no_superfluous_phpdoc_tags']],
  176. [$fixers['phpdoc_add_missing_param_annotation'], $fixers['phpdoc_align']],
  177. [$fixers['phpdoc_add_missing_param_annotation'], $fixers['phpdoc_order']],
  178. [$fixers['phpdoc_no_access'], $fixers['no_empty_phpdoc']],
  179. [$fixers['phpdoc_no_access'], $fixers['phpdoc_order']],
  180. [$fixers['phpdoc_no_access'], $fixers['phpdoc_separation']],
  181. [$fixers['phpdoc_no_access'], $fixers['phpdoc_trim']],
  182. [$fixers['phpdoc_no_alias_tag'], $fixers['phpdoc_add_missing_param_annotation']],
  183. [$fixers['phpdoc_no_alias_tag'], $fixers['phpdoc_single_line_var_spacing']],
  184. [$fixers['phpdoc_no_empty_return'], $fixers['no_empty_phpdoc']],
  185. [$fixers['phpdoc_no_empty_return'], $fixers['phpdoc_order']],
  186. [$fixers['phpdoc_no_empty_return'], $fixers['phpdoc_separation']],
  187. [$fixers['phpdoc_no_empty_return'], $fixers['phpdoc_trim']],
  188. [$fixers['phpdoc_no_package'], $fixers['no_empty_phpdoc']],
  189. [$fixers['phpdoc_no_package'], $fixers['phpdoc_order']],
  190. [$fixers['phpdoc_no_package'], $fixers['phpdoc_separation']],
  191. [$fixers['phpdoc_no_package'], $fixers['phpdoc_trim']],
  192. [$fixers['phpdoc_no_useless_inheritdoc'], $fixers['no_empty_phpdoc']],
  193. [$fixers['phpdoc_no_useless_inheritdoc'], $fixers['no_trailing_whitespace_in_comment']],
  194. [$fixers['phpdoc_order'], $fixers['phpdoc_separation']],
  195. [$fixers['phpdoc_order'], $fixers['phpdoc_trim']],
  196. [$fixers['phpdoc_return_self_reference'], $fixers['no_superfluous_phpdoc_tags']],
  197. [$fixers['phpdoc_scalar'], $fixers['phpdoc_to_return_type']],
  198. [$fixers['phpdoc_to_comment'], $fixers['no_empty_comment']],
  199. [$fixers['phpdoc_to_comment'], $fixers['phpdoc_no_useless_inheritdoc']],
  200. [$fixers['phpdoc_to_return_type'], $fixers['fully_qualified_strict_types']],
  201. [$fixers['phpdoc_to_return_type'], $fixers['no_superfluous_phpdoc_tags']],
  202. [$fixers['phpdoc_to_return_type'], $fixers['return_type_declaration']],
  203. [$fixers['phpdoc_types'], $fixers['phpdoc_to_return_type']],
  204. [$fixers['pow_to_exponentiation'], $fixers['binary_operator_spaces']],
  205. [$fixers['pow_to_exponentiation'], $fixers['method_argument_space']],
  206. [$fixers['pow_to_exponentiation'], $fixers['native_function_casing']],
  207. [$fixers['pow_to_exponentiation'], $fixers['no_spaces_after_function_name']],
  208. [$fixers['pow_to_exponentiation'], $fixers['no_spaces_inside_parenthesis']],
  209. [$fixers['protected_to_private'], $fixers['ordered_class_elements']],
  210. [$fixers['return_assignment'], $fixers['blank_line_before_statement']],
  211. [$fixers['simplified_null_return'], $fixers['no_useless_return']],
  212. [$fixers['single_import_per_statement'], $fixers['multiline_whitespace_before_semicolons']],
  213. [$fixers['single_import_per_statement'], $fixers['no_leading_import_slash']],
  214. [$fixers['single_import_per_statement'], $fixers['no_singleline_whitespace_before_semicolons']],
  215. [$fixers['single_import_per_statement'], $fixers['no_unused_imports']],
  216. [$fixers['single_import_per_statement'], $fixers['space_after_semicolon']],
  217. [$fixers['single_trait_insert_per_statement'], $fixers['braces']],
  218. [$fixers['single_trait_insert_per_statement'], $fixers['space_after_semicolon']],
  219. [$fixers['standardize_increment'], $fixers['increment_style']],
  220. [$fixers['standardize_not_equals'], $fixers['binary_operator_spaces']],
  221. [$fixers['strict_comparison'], $fixers['binary_operator_spaces']],
  222. [$fixers['unary_operator_spaces'], $fixers['not_operator_with_space']],
  223. [$fixers['unary_operator_spaces'], $fixers['not_operator_with_successor_space']],
  224. [$fixers['void_return'], $fixers['phpdoc_no_empty_return']],
  225. [$fixers['void_return'], $fixers['return_type_declaration']],
  226. ];
  227. }
  228. public function provideFixersPrioritySpecialPhpdocCases()
  229. {
  230. $factory = new FixerFactory();
  231. $factory->registerBuiltInFixers();
  232. $fixers = [];
  233. foreach ($factory->getFixers() as $fixer) {
  234. $fixers[$fixer->getName()] = $fixer;
  235. }
  236. $cases = [];
  237. // prepare bulk tests for phpdoc fixers to test that:
  238. // * `comment_to_phpdoc` is first
  239. // * `phpdoc_to_comment` is second
  240. // * `phpdoc_indent` is third
  241. // * `phpdoc_types` is fourth
  242. // * `phpdoc_scalar` is fifth
  243. // * `phpdoc_align` is last
  244. $cases[] = [$fixers['comment_to_phpdoc'], $fixers['phpdoc_to_comment']];
  245. $cases[] = [$fixers['phpdoc_indent'], $fixers['phpdoc_types']];
  246. $cases[] = [$fixers['phpdoc_to_comment'], $fixers['phpdoc_indent']];
  247. $cases[] = [$fixers['phpdoc_types'], $fixers['phpdoc_scalar']];
  248. $docFixerNames = array_filter(
  249. array_keys($fixers),
  250. static function ($name) {
  251. return false !== strpos($name, 'phpdoc');
  252. }
  253. );
  254. foreach ($docFixerNames as $docFixerName) {
  255. if (!\in_array($docFixerName, ['comment_to_phpdoc', 'phpdoc_to_comment', 'phpdoc_indent', 'phpdoc_types', 'phpdoc_scalar'], true)) {
  256. $cases[] = [$fixers['comment_to_phpdoc'], $fixers[$docFixerName]];
  257. $cases[] = [$fixers['phpdoc_indent'], $fixers[$docFixerName]];
  258. $cases[] = [$fixers['phpdoc_scalar'], $fixers[$docFixerName]];
  259. $cases[] = [$fixers['phpdoc_to_comment'], $fixers[$docFixerName]];
  260. $cases[] = [$fixers['phpdoc_types'], $fixers[$docFixerName]];
  261. }
  262. if ('phpdoc_align' !== $docFixerName) {
  263. $cases[] = [$fixers[$docFixerName], $fixers['phpdoc_align']];
  264. }
  265. }
  266. return $cases;
  267. }
  268. /**
  269. * @dataProvider provideFixersPriorityPairsHaveIntegrationTestCases
  270. */
  271. public function testFixersPriorityPairsHaveIntegrationTest(FixerInterface $first, FixerInterface $second)
  272. {
  273. // This structure contains older cases that are not yet covered by tests.
  274. // It may only shrink, never add anything to it.
  275. $casesWithoutTests = [
  276. 'indentation_type,phpdoc_indent.test',
  277. 'phpdoc_no_access,phpdoc_order.test',
  278. 'phpdoc_no_package,phpdoc_order.test',
  279. ];
  280. $integrationTestName = $this->generateIntegrationTestName($first, $second);
  281. $file = $this->getIntegrationPriorityDirectory().$integrationTestName;
  282. if (is_file($file)) {
  283. $description = sprintf('Integration of fixers: %s,%s.', $first->getName(), $second->getName());
  284. $integrationTestExists = true;
  285. } else {
  286. $file = $this->getIntegrationPriorityDirectory().$this->generateIntegrationTestName($second, $first);
  287. $description = sprintf('Integration of fixers: %s,%s.', $second->getName(), $first->getName());
  288. $integrationTestExists = is_file($file);
  289. }
  290. if (\in_array($integrationTestName, $casesWithoutTests, true)) {
  291. static::assertFalse($integrationTestExists, sprintf('Case "%s" already has an integration test, so it should be removed from "$casesWithoutTests".', $integrationTestName));
  292. static::markTestIncomplete(sprintf('Case "%s" has no integration test yet, please help and add it.', $integrationTestName));
  293. }
  294. static::assertTrue($integrationTestExists, sprintf('There shall be an integration test "%s". How do you know that priority set up is good, if there is no integration test to check it?', $integrationTestName));
  295. $file = realpath($file);
  296. $factory = new IntegrationCaseFactory();
  297. $test = $factory->create(new SplFileInfo($file, './', __DIR__));
  298. $rules = $test->getRuleset()->getRules();
  299. $expected = [$first->getName(), $second->getName()];
  300. $actual = array_keys($rules);
  301. sort($expected);
  302. sort($actual);
  303. static::assertSame($description, $test->getTitle(), sprintf('Please fix the title in "%s".', $file));
  304. static::assertCount(2, $rules, sprintf('Only the two rules that are tested for priority should be in the ruleset of "%s".', $file));
  305. foreach ($rules as $name => $config) {
  306. static::assertNotFalse($config, sprintf('The rule "%s" in "%s" may not be disabled for the test.', $name, $file));
  307. }
  308. static::assertSame($expected, $actual, sprintf('The ruleset of "%s" must contain the rules for the priority test.', $file));
  309. }
  310. public function provideFixersPriorityPairsHaveIntegrationTestCases()
  311. {
  312. return array_filter(
  313. $this->provideFixersPriorityCases(),
  314. // ignore speed-up only priorities set up
  315. function (array $case) {
  316. return !\in_array(
  317. $this->generateIntegrationTestName($case[0], $case[1]),
  318. [
  319. 'function_to_constant,native_function_casing.test',
  320. 'no_unused_imports,no_leading_import_slash.test',
  321. 'pow_to_exponentiation,method_argument_space.test',
  322. 'pow_to_exponentiation,native_function_casing.test',
  323. 'pow_to_exponentiation,no_spaces_after_function_name.test',
  324. 'pow_to_exponentiation,no_spaces_inside_parenthesis.test',
  325. ],
  326. true
  327. );
  328. }
  329. );
  330. }
  331. public function testPriorityIntegrationDirectoryOnlyContainsFiles()
  332. {
  333. foreach (new \DirectoryIterator($this->getIntegrationPriorityDirectory()) as $candidate) {
  334. if ($candidate->isDot()) {
  335. continue;
  336. }
  337. $fileName = $candidate->getFilename();
  338. static::assertTrue($candidate->isFile(), sprintf('Expected only files in the priority integration test directory, got "%s".', $fileName));
  339. static::assertFalse($candidate->isLink(), sprintf('No (sym)links expected the priority integration test directory, got "%s".', $fileName));
  340. }
  341. }
  342. /**
  343. * @dataProvider provideIntegrationTestFilesCases
  344. *
  345. * @param string $fileName
  346. */
  347. public function testPriorityIntegrationTestFilesAreListedPriorityCases($fileName)
  348. {
  349. static $priorityCases;
  350. if (null === $priorityCases) {
  351. $priorityCases = [];
  352. foreach ($this->provideFixersPriorityCases() as $priorityCase) {
  353. $fixerName = $priorityCase[0]->getName();
  354. if (!isset($priorityCases[$fixerName])) {
  355. $priorityCases[$fixerName] = [];
  356. }
  357. $priorityCases[$fixerName][$priorityCase[1]->getName()] = true;
  358. }
  359. ksort($priorityCases);
  360. }
  361. if (\in_array($fileName, [
  362. 'braces,indentation_type,no_break_comment.test',
  363. ], true)) {
  364. static::markTestIncomplete(sprintf('Case "%s" has unexpected name, please help fixing it.', $fileName));
  365. }
  366. static::assertSame(
  367. 1,
  368. preg_match('#^([a-z][a-z0-9_]*),([a-z][a-z_]*)(?:_\d{1,3})?\.test(-(in|out)\.php)?$#', $fileName, $matches),
  369. sprintf('File with unexpected name "%s" in the priority integration test directory.', $fileName)
  370. );
  371. $fixerName1 = $matches[1];
  372. $fixerName2 = $matches[2];
  373. static::assertTrue(
  374. isset($priorityCases[$fixerName1][$fixerName2]) || isset($priorityCases[$fixerName2][$fixerName1]),
  375. sprintf('Missing priority test entry for file "%s".', $fileName)
  376. );
  377. }
  378. public function provideIntegrationTestFilesCases()
  379. {
  380. $fileNames = [];
  381. foreach (new \DirectoryIterator($this->getIntegrationPriorityDirectory()) as $candidate) {
  382. if ($candidate->isDot()) {
  383. continue;
  384. }
  385. $fileNames[] = [$candidate->getFilename()];
  386. }
  387. sort($fileNames);
  388. return $fileNames;
  389. }
  390. public function testProvideFixersPriorityCasesAreSorted()
  391. {
  392. $cases = $this->provideFixersPriorityCases();
  393. $sorted = $cases;
  394. usort(
  395. $sorted,
  396. /**
  397. * @param array<FixerInterface> $priorityPair1
  398. * @param array<FixerInterface> $priorityPair2
  399. */
  400. static function (array $priorityPair1, array $priorityPair2) {
  401. $fixer1 = $priorityPair1[0];
  402. $fixer2 = $priorityPair2[0];
  403. if ($fixer1->getName() === $fixer2->getName()) {
  404. $fixer1 = $priorityPair1[1];
  405. $fixer2 = $priorityPair2[1];
  406. }
  407. return strcmp($fixer1->getName(), $fixer2->getName());
  408. }
  409. );
  410. if ($sorted !== $cases) { // PHPUnit takes a very long time creating a diff view on the arrays
  411. $casesDescription = '';
  412. foreach ($cases as $pair) {
  413. $casesDescription .= sprintf("\n%s/%s", $pair[0]->getName(), $pair[1]->getName());
  414. }
  415. $sortedDescription = '';
  416. foreach ($sorted as $pair) {
  417. $sortedDescription .= sprintf("\n%s/%s", $pair[0]->getName(), $pair[1]->getName());
  418. }
  419. static::assertSame($sortedDescription, $casesDescription);
  420. } else {
  421. $this->addToAssertionCount(1);
  422. }
  423. }
  424. public function testFixerPriorityComment()
  425. {
  426. $cases = array_merge(
  427. $this->provideFixersPriorityCases(),
  428. $this->provideFixersPrioritySpecialPhpdocCases()
  429. );
  430. $map = [];
  431. foreach ($cases as $beforeAfter) {
  432. list($before, $after) = $beforeAfter;
  433. $beforeClass = \get_class($before);
  434. $afterClass = \get_class($after);
  435. $beforeName = substr($beforeClass, strrpos($beforeClass, '\\') + 1);
  436. $afterName = substr($afterClass, strrpos($afterClass, '\\') + 1);
  437. if (!isset($map[$beforeName])) {
  438. $map[$beforeName] = [
  439. 'before' => [],
  440. 'after' => [],
  441. 'class' => $beforeClass,
  442. ];
  443. }
  444. $map[$beforeName]['before'][] = $afterName;
  445. if (!isset($map[$afterName])) {
  446. $map[$afterName] = [
  447. 'before' => [],
  448. 'after' => [],
  449. 'class' => $afterClass,
  450. ];
  451. }
  452. $map[$afterName]['after'][] = $beforeName;
  453. }
  454. $fixersPhpDocIssues = [];
  455. foreach ($map as $fixerName => $priorityMap) {
  456. $expectedMessage = "/**\n * {@inheritdoc}\n *";
  457. if (\count($priorityMap['before']) > 0) {
  458. sort($priorityMap['before']);
  459. $expectedMessage .= sprintf("\n * Must run before %s.", implode(', ', $priorityMap['before']));
  460. }
  461. if (\count($priorityMap['after']) > 0) {
  462. sort($priorityMap['after']);
  463. $expectedMessage .= sprintf("\n * Must run after %s.", implode(', ', $priorityMap['after']));
  464. }
  465. $expectedMessage .= "\n */";
  466. $reflection = new \ReflectionClass($priorityMap['class']);
  467. $method = $reflection->getMethod('getPriority');
  468. $phpDoc = $method->getDocComment();
  469. if (false === $phpDoc) {
  470. $fixersPhpDocIssues[$fixerName] = sprintf("PHPDoc for %s::getPriority is missing.\nExpected:\n%s", $fixerName, $expectedMessage);
  471. continue;
  472. }
  473. if ($expectedMessage !== $phpDoc) {
  474. $fixersPhpDocIssues[$fixerName] = sprintf("PHPDoc for %s::getPriority is not as expected.\nExpected:\n%s", $fixerName, $expectedMessage);
  475. continue;
  476. }
  477. }
  478. if (0 === \count($fixersPhpDocIssues)) {
  479. $this->addToAssertionCount(1);
  480. } else {
  481. $message = sprintf("There are %d priority PHPDoc issues found.\n", \count($fixersPhpDocIssues));
  482. ksort($fixersPhpDocIssues);
  483. foreach ($fixersPhpDocIssues as $fixerName => $issue) {
  484. $message .= sprintf("\n--------------------------------------------------\n%s\n%s", $fixerName, $issue);
  485. }
  486. static::fail($message);
  487. }
  488. }
  489. /**
  490. * @return string
  491. */
  492. private function generateIntegrationTestName(FixerInterface $first, FixerInterface $second)
  493. {
  494. return "{$first->getName()},{$second->getName()}.test";
  495. }
  496. /**
  497. * @return string
  498. */
  499. private function getIntegrationPriorityDirectory()
  500. {
  501. return __DIR__.'/../Fixtures/Integration/priority/';
  502. }
  503. }