FixerFactoryTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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\TestCase;
  15. /**
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @coversNothing
  21. * @group auto-review
  22. */
  23. final class FixerFactoryTest extends TestCase
  24. {
  25. public function testFixersPriorityEdgeFixers()
  26. {
  27. $factory = new FixerFactory();
  28. $factory->registerBuiltInFixers();
  29. $fixers = $factory->getFixers();
  30. $this->assertSame('encoding', $fixers[0]->getName(), 'Expected "encoding" fixer to have the highest priority.');
  31. $this->assertSame('full_opening_tag', $fixers[1]->getName(), 'Expected "full_opening_tag" fixer has second highest priority.');
  32. $this->assertSame('single_blank_line_at_eof', $fixers[count($fixers) - 1]->getName(), 'Expected "single_blank_line_at_eof" to have the lowest priority.');
  33. }
  34. /**
  35. * @dataProvider provideFixersPriorityCases
  36. * @dataProvider provideFixersPrioritySpecialPhpdocCases
  37. */
  38. public function testFixersPriority(FixerInterface $first, FixerInterface $second)
  39. {
  40. $this->assertLessThan($first->getPriority(), $second->getPriority(), sprintf('"%s" should have less priority than "%s"', get_class($second), get_class($first)));
  41. }
  42. public function provideFixersPriorityCases()
  43. {
  44. $factory = new FixerFactory();
  45. $factory->registerBuiltInFixers();
  46. $fixers = [];
  47. foreach ($factory->getFixers() as $fixer) {
  48. $fixers[$fixer->getName()] = $fixer;
  49. }
  50. return [
  51. [$fixers['array_syntax'], $fixers['binary_operator_spaces']],
  52. [$fixers['array_syntax'], $fixers['ternary_operator_spaces']],
  53. [$fixers['backtick_to_shell_exec'], $fixers['escape_implicit_backslashes']],
  54. [$fixers['blank_line_after_opening_tag'], $fixers['no_blank_lines_before_namespace']],
  55. [$fixers['class_attributes_separation'], $fixers['braces']],
  56. [$fixers['class_attributes_separation'], $fixers['indentation_type']],
  57. [$fixers['class_keyword_remove'], $fixers['no_unused_imports']],
  58. [$fixers['combine_consecutive_issets'], $fixers['multiline_whitespace_before_semicolons']],
  59. [$fixers['combine_consecutive_issets'], $fixers['no_spaces_inside_parenthesis']],
  60. [$fixers['combine_consecutive_issets'], $fixers['no_trailing_whitespace']],
  61. [$fixers['combine_consecutive_issets'], $fixers['no_whitespace_in_blank_line']],
  62. [$fixers['combine_consecutive_unsets'], $fixers['no_extra_blank_lines']],
  63. [$fixers['combine_consecutive_unsets'], $fixers['no_trailing_whitespace']],
  64. [$fixers['combine_consecutive_unsets'], $fixers['no_whitespace_in_blank_line']],
  65. [$fixers['combine_consecutive_unsets'], $fixers['space_after_semicolon']],
  66. [$fixers['declare_strict_types'], $fixers['blank_line_after_opening_tag']],
  67. [$fixers['declare_strict_types'], $fixers['declare_equal_normalize']],
  68. [$fixers['declare_strict_types'], $fixers['single_blank_line_before_namespace']],
  69. [$fixers['doctrine_annotation_array_assignment'], $fixers['doctrine_annotation_spaces']],
  70. [$fixers['elseif'], $fixers['braces']],
  71. [$fixers['escape_implicit_backslashes'], $fixers['heredoc_to_nowdoc']],
  72. [$fixers['escape_implicit_backslashes'], $fixers['single_quote']],
  73. [$fixers['function_to_constant'], $fixers['native_function_casing']],
  74. [$fixers['function_to_constant'], $fixers['no_extra_blank_lines']],
  75. [$fixers['function_to_constant'], $fixers['no_singleline_whitespace_before_semicolons']],
  76. [$fixers['function_to_constant'], $fixers['no_trailing_whitespace']],
  77. [$fixers['function_to_constant'], $fixers['no_whitespace_in_blank_line']],
  78. [$fixers['general_phpdoc_annotation_remove'], $fixers['phpdoc_separation']],
  79. [$fixers['general_phpdoc_annotation_remove'], $fixers['phpdoc_trim']],
  80. [$fixers['general_phpdoc_annotation_remove'], $fixers['no_empty_phpdoc']],
  81. [$fixers['indentation_type'], $fixers['phpdoc_indent']],
  82. [$fixers['is_null'], $fixers['yoda_style']],
  83. [$fixers['line_ending'], $fixers['single_blank_line_at_eof']],
  84. [$fixers['list_syntax'], $fixers['binary_operator_spaces']],
  85. [$fixers['list_syntax'], $fixers['ternary_operator_spaces']],
  86. [$fixers['method_separation'], $fixers['braces']],
  87. [$fixers['method_separation'], $fixers['indentation_type']],
  88. [$fixers['no_alias_functions'], $fixers['php_unit_dedicate_assert']],
  89. [$fixers['no_blank_lines_after_phpdoc'], $fixers['single_blank_line_before_namespace']],
  90. [$fixers['no_empty_comment'], $fixers['no_extra_blank_lines']],
  91. [$fixers['no_empty_comment'], $fixers['no_trailing_whitespace']],
  92. [$fixers['no_empty_comment'], $fixers['no_whitespace_in_blank_line']],
  93. [$fixers['no_empty_phpdoc'], $fixers['no_extra_blank_lines']],
  94. [$fixers['no_empty_phpdoc'], $fixers['no_trailing_whitespace']],
  95. [$fixers['no_empty_phpdoc'], $fixers['no_whitespace_in_blank_line']],
  96. [$fixers['no_empty_statement'], $fixers['braces']],
  97. [$fixers['no_empty_statement'], $fixers['combine_consecutive_unsets']],
  98. [$fixers['no_empty_statement'], $fixers['no_extra_blank_lines']],
  99. [$fixers['no_empty_statement'], $fixers['multiline_whitespace_before_semicolons']],
  100. [$fixers['no_empty_statement'], $fixers['no_singleline_whitespace_before_semicolons']],
  101. [$fixers['no_empty_statement'], $fixers['no_trailing_whitespace']],
  102. [$fixers['no_empty_statement'], $fixers['no_useless_else']],
  103. [$fixers['no_empty_statement'], $fixers['no_useless_return']],
  104. [$fixers['no_empty_statement'], $fixers['no_whitespace_in_blank_line']],
  105. [$fixers['no_empty_statement'], $fixers['space_after_semicolon']],
  106. [$fixers['no_empty_statement'], $fixers['switch_case_semicolon_to_colon']],
  107. [$fixers['no_leading_import_slash'], $fixers['ordered_imports']],
  108. [$fixers['no_multiline_whitespace_around_double_arrow'], $fixers['binary_operator_spaces']],
  109. [$fixers['no_multiline_whitespace_around_double_arrow'], $fixers['trailing_comma_in_multiline_array']],
  110. [$fixers['multiline_whitespace_before_semicolons'], $fixers['space_after_semicolon']],
  111. [$fixers['no_php4_constructor'], $fixers['ordered_class_elements']],
  112. [$fixers['no_short_bool_cast'], $fixers['cast_spaces']],
  113. [$fixers['no_short_echo_tag'], $fixers['no_mixed_echo_print']],
  114. [$fixers['no_spaces_after_function_name'], $fixers['function_to_constant']],
  115. [$fixers['no_spaces_inside_parenthesis'], $fixers['function_to_constant']],
  116. [$fixers['no_unneeded_control_parentheses'], $fixers['no_trailing_whitespace']],
  117. [$fixers['no_unneeded_curly_braces'], $fixers['no_useless_else']],
  118. [$fixers['no_unneeded_curly_braces'], $fixers['no_useless_return']],
  119. [$fixers['no_unused_imports'], $fixers['blank_line_after_namespace']],
  120. [$fixers['no_unused_imports'], $fixers['no_extra_blank_lines']],
  121. [$fixers['no_unused_imports'], $fixers['no_leading_import_slash']],
  122. [$fixers['no_useless_else'], $fixers['braces']],
  123. [$fixers['no_useless_else'], $fixers['combine_consecutive_unsets']],
  124. [$fixers['no_useless_else'], $fixers['no_extra_blank_lines']],
  125. [$fixers['no_useless_else'], $fixers['no_trailing_whitespace']],
  126. [$fixers['no_useless_else'], $fixers['no_useless_return']],
  127. [$fixers['no_useless_else'], $fixers['no_whitespace_in_blank_line']],
  128. [$fixers['no_useless_return'], $fixers['blank_line_before_return']],
  129. [$fixers['no_useless_return'], $fixers['blank_line_before_statement']],
  130. [$fixers['no_useless_return'], $fixers['no_extra_blank_lines']],
  131. [$fixers['no_useless_return'], $fixers['no_whitespace_in_blank_line']],
  132. [$fixers['no_whitespace_in_blank_line'], $fixers['no_blank_lines_after_phpdoc']],
  133. [$fixers['ordered_class_elements'], $fixers['class_attributes_separation']],
  134. [$fixers['ordered_class_elements'], $fixers['method_separation']],
  135. [$fixers['ordered_class_elements'], $fixers['no_blank_lines_after_class_opening']],
  136. [$fixers['ordered_class_elements'], $fixers['space_after_semicolon']],
  137. [$fixers['php_unit_construct'], $fixers['php_unit_dedicate_assert']],
  138. [$fixers['php_unit_fqcn_annotation'], $fixers['no_unused_imports']],
  139. [$fixers['php_unit_fqcn_annotation'], $fixers['php_unit_ordered_covers']],
  140. [$fixers['php_unit_no_expectation_annotation'], $fixers['no_empty_phpdoc']],
  141. [$fixers['php_unit_no_expectation_annotation'], $fixers['php_unit_expectation']],
  142. [$fixers['php_unit_strict'], $fixers['php_unit_construct']],
  143. [$fixers['phpdoc_add_missing_param_annotation'], $fixers['phpdoc_align']],
  144. [$fixers['phpdoc_add_missing_param_annotation'], $fixers['phpdoc_order']],
  145. [$fixers['phpdoc_no_access'], $fixers['no_empty_phpdoc']],
  146. [$fixers['phpdoc_no_access'], $fixers['phpdoc_order']],
  147. [$fixers['phpdoc_no_access'], $fixers['phpdoc_separation']],
  148. [$fixers['phpdoc_no_access'], $fixers['phpdoc_trim']],
  149. [$fixers['phpdoc_no_alias_tag'], $fixers['phpdoc_add_missing_param_annotation']],
  150. [$fixers['phpdoc_no_alias_tag'], $fixers['phpdoc_single_line_var_spacing']],
  151. [$fixers['phpdoc_no_empty_return'], $fixers['no_empty_phpdoc']],
  152. [$fixers['phpdoc_no_empty_return'], $fixers['phpdoc_order']],
  153. [$fixers['phpdoc_no_empty_return'], $fixers['phpdoc_separation']],
  154. [$fixers['phpdoc_no_empty_return'], $fixers['phpdoc_trim']],
  155. [$fixers['phpdoc_no_package'], $fixers['no_empty_phpdoc']],
  156. [$fixers['phpdoc_no_package'], $fixers['phpdoc_order']],
  157. [$fixers['phpdoc_no_package'], $fixers['phpdoc_separation']],
  158. [$fixers['phpdoc_no_package'], $fixers['phpdoc_trim']],
  159. [$fixers['phpdoc_no_useless_inheritdoc'], $fixers['no_empty_phpdoc']],
  160. [$fixers['phpdoc_no_useless_inheritdoc'], $fixers['no_trailing_whitespace_in_comment']],
  161. [$fixers['phpdoc_order'], $fixers['phpdoc_separation']],
  162. [$fixers['phpdoc_order'], $fixers['phpdoc_trim']],
  163. [$fixers['phpdoc_separation'], $fixers['phpdoc_trim']],
  164. [$fixers['phpdoc_summary'], $fixers['phpdoc_trim']],
  165. [$fixers['phpdoc_to_comment'], $fixers['no_empty_comment']],
  166. [$fixers['phpdoc_to_comment'], $fixers['phpdoc_no_useless_inheritdoc']],
  167. [$fixers['phpdoc_var_without_name'], $fixers['phpdoc_trim']],
  168. [$fixers['pow_to_exponentiation'], $fixers['binary_operator_spaces']],
  169. [$fixers['pow_to_exponentiation'], $fixers['method_argument_space']],
  170. [$fixers['pow_to_exponentiation'], $fixers['native_function_casing']],
  171. [$fixers['pow_to_exponentiation'], $fixers['no_spaces_after_function_name']],
  172. [$fixers['pow_to_exponentiation'], $fixers['no_spaces_inside_parenthesis']],
  173. [$fixers['protected_to_private'], $fixers['ordered_class_elements']],
  174. [$fixers['simplified_null_return'], $fixers['no_useless_return']],
  175. [$fixers['single_import_per_statement'], $fixers['no_leading_import_slash']],
  176. [$fixers['single_import_per_statement'], $fixers['multiline_whitespace_before_semicolons']],
  177. [$fixers['single_import_per_statement'], $fixers['no_singleline_whitespace_before_semicolons']],
  178. [$fixers['single_import_per_statement'], $fixers['no_unused_imports']],
  179. [$fixers['single_import_per_statement'], $fixers['space_after_semicolon']],
  180. [$fixers['standardize_increment'], $fixers['increment_style']],
  181. [$fixers['standardize_not_equals'], $fixers['binary_operator_spaces']],
  182. [$fixers['strict_comparison'], $fixers['binary_operator_spaces']],
  183. [$fixers['unary_operator_spaces'], $fixers['not_operator_with_space']],
  184. [$fixers['unary_operator_spaces'], $fixers['not_operator_with_successor_space']],
  185. [$fixers['void_return'], $fixers['phpdoc_no_empty_return']],
  186. [$fixers['void_return'], $fixers['return_type_declaration']],
  187. [$fixers['php_unit_test_annotation'], $fixers['no_empty_phpdoc']],
  188. [$fixers['php_unit_test_annotation'], $fixers['phpdoc_trim']],
  189. ];
  190. }
  191. public function provideFixersPrioritySpecialPhpdocCases()
  192. {
  193. $factory = new FixerFactory();
  194. $factory->registerBuiltInFixers();
  195. $fixers = [];
  196. foreach ($factory->getFixers() as $fixer) {
  197. $fixers[$fixer->getName()] = $fixer;
  198. }
  199. $cases = [];
  200. // prepare bulk tests for phpdoc fixers to test that:
  201. // * `phpdoc_to_comment` is first
  202. // * `phpdoc_indent` is second
  203. // * `phpdoc_types` is third
  204. // * `phpdoc_scalar` is fourth
  205. // * `phpdoc_align` is last
  206. $cases[] = [$fixers['phpdoc_indent'], $fixers['phpdoc_types']];
  207. $cases[] = [$fixers['phpdoc_to_comment'], $fixers['phpdoc_indent']];
  208. $cases[] = [$fixers['phpdoc_types'], $fixers['phpdoc_scalar']];
  209. $docFixerNames = array_filter(
  210. array_keys($fixers),
  211. static function ($name) {
  212. return false !== strpos($name, 'phpdoc');
  213. }
  214. );
  215. foreach ($docFixerNames as $docFixerName) {
  216. if (!in_array($docFixerName, ['phpdoc_to_comment', 'phpdoc_indent', 'phpdoc_types', 'phpdoc_scalar'], true)) {
  217. $cases[] = [$fixers['phpdoc_indent'], $fixers[$docFixerName]];
  218. $cases[] = [$fixers['phpdoc_scalar'], $fixers[$docFixerName]];
  219. $cases[] = [$fixers['phpdoc_to_comment'], $fixers[$docFixerName]];
  220. $cases[] = [$fixers['phpdoc_types'], $fixers[$docFixerName]];
  221. }
  222. if ('phpdoc_align' !== $docFixerName) {
  223. $cases[] = [$fixers[$docFixerName], $fixers['phpdoc_align']];
  224. }
  225. }
  226. return $cases;
  227. }
  228. /**
  229. * @dataProvider provideFixersPriorityPairsHaveIntegrationTestCases
  230. */
  231. public function testFixersPriorityPairsHaveIntegrationTest(FixerInterface $first, FixerInterface $second)
  232. {
  233. // This structure contains older cases that are not yet covered by tests.
  234. // It may only shrink, never add anything to it.
  235. $casesWithoutTests = [
  236. 'class_attributes_separation,braces.test',
  237. 'class_attributes_separation,indentation_type.test',
  238. 'indentation_type,phpdoc_indent.test',
  239. 'line_ending,single_blank_line_at_eof.test',
  240. 'method_separation,braces.test',
  241. 'method_separation,indentation_type.test',
  242. 'no_empty_statement,multiline_whitespace_before_semicolons.test',
  243. 'no_empty_statement,no_multiline_whitespace_before_semicolons.test',
  244. 'no_empty_statement,no_singleline_whitespace_before_semicolons.test',
  245. 'php_unit_strict,php_unit_construct.test',
  246. 'phpdoc_no_access,phpdoc_order.test',
  247. 'phpdoc_no_access,phpdoc_separation.test',
  248. 'phpdoc_no_empty_return,phpdoc_trim.test',
  249. 'phpdoc_no_package,phpdoc_order.test',
  250. 'phpdoc_no_package,phpdoc_trim.test',
  251. 'phpdoc_order,phpdoc_separation.test',
  252. 'phpdoc_order,phpdoc_trim.test',
  253. 'phpdoc_separation,phpdoc_trim.test',
  254. 'phpdoc_summary,phpdoc_trim.test',
  255. 'phpdoc_var_without_name,phpdoc_trim.test',
  256. 'unary_operator_spaces,not_operator_with_space.test',
  257. 'unary_operator_spaces,not_operator_with_successor_space.test',
  258. ];
  259. $integrationTestExists = $this->doesIntegrationTestExist($first, $second);
  260. if (in_array($this->generateIntegrationTestName($first, $second), $casesWithoutTests, true)) {
  261. $this->assertFalse($integrationTestExists, sprintf('Case "%s" already has an integration test, so it should be removed from "$casesWithoutTests".', $this->generateIntegrationTestName($first, $second)));
  262. $this->markTestIncomplete(sprintf('Case "%s" has no integration test yet, please help and add it.', $this->generateIntegrationTestName($first, $second)));
  263. }
  264. $this->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?', $this->generateIntegrationTestName($first, $second)));
  265. }
  266. public function provideFixersPriorityPairsHaveIntegrationTestCases()
  267. {
  268. return array_filter(
  269. $this->provideFixersPriorityCases(),
  270. // ignore speed-up only priorities set up
  271. function (array $case) {
  272. return !in_array(
  273. $this->generateIntegrationTestName($case[0], $case[1]),
  274. [
  275. 'function_to_constant,native_function_casing.test',
  276. 'no_unused_imports,no_leading_import_slash.test',
  277. 'pow_to_exponentiation,method_argument_space.test',
  278. 'pow_to_exponentiation,native_function_casing.test',
  279. 'pow_to_exponentiation,no_spaces_after_function_name.test',
  280. 'pow_to_exponentiation,no_spaces_inside_parenthesis.test',
  281. ],
  282. true
  283. );
  284. }
  285. );
  286. }
  287. public function testPriorityIntegrationTestFilesAreListedPriorityCases()
  288. {
  289. $priorityCases = [];
  290. foreach ($this->provideFixersPriorityCases() as $priorityCase) {
  291. $fixerName = $priorityCase[0]->getName();
  292. if (!isset($priorityCases[$fixerName])) {
  293. $priorityCases[$fixerName] = [];
  294. }
  295. $priorityCases[$fixerName][$priorityCase[1]->getName()] = true;
  296. }
  297. foreach (new \DirectoryIterator($this->getIntegrationPriorityDirectory()) as $candidate) {
  298. if ($candidate->isDot()) {
  299. continue;
  300. }
  301. $fileName = $candidate->getFilename();
  302. $this->assertTrue($candidate->isFile(), sprintf('Expected only files in the priority integration test directory, got "%s".', $fileName));
  303. $this->assertFalse($candidate->isLink(), sprintf('No (sym)links expected the priority integration test directory, got "%s".', $fileName));
  304. if (in_array($fileName, [
  305. 'braces,indentation_type,no_break_comment.test',
  306. ], true)) {
  307. $this->markTestIncomplete(sprintf('Case "%s" has unexpected name, please help fixing it.', $fileName));
  308. }
  309. $this->assertSame(
  310. 1,
  311. preg_match('#^([a-z][a-z0-9_]*),([a-z][a-z_]*)(?:_\d{1,3})?\.test$#', $fileName, $matches),
  312. sprintf('File with unexpected name "%s" in the priority integration test directory.', $fileName)
  313. );
  314. $fixerName1 = $matches[1];
  315. $fixerName2 = $matches[2];
  316. $this->assertTrue(
  317. isset($priorityCases[$fixerName1][$fixerName2]) || isset($priorityCases[$fixerName2][$fixerName1]),
  318. sprintf('Missing priority test entry for file "%s".', $fileName)
  319. );
  320. }
  321. }
  322. /**
  323. * @param FixerInterface $first
  324. * @param FixerInterface $second
  325. *
  326. * @return string
  327. */
  328. private function generateIntegrationTestName(FixerInterface $first, FixerInterface $second)
  329. {
  330. return "{$first->getName()},{$second->getName()}.test";
  331. }
  332. /**
  333. * @param FixerInterface $first
  334. * @param FixerInterface $second
  335. *
  336. * @return bool
  337. */
  338. private function doesIntegrationTestExist(FixerInterface $first, FixerInterface $second)
  339. {
  340. return
  341. is_file($this->getIntegrationPriorityDirectory().$this->generateIntegrationTestName($first, $second))
  342. || is_file($this->getIntegrationPriorityDirectory().$this->generateIntegrationTestName($second, $first))
  343. ;
  344. }
  345. /**
  346. * @return string
  347. */
  348. private function getIntegrationPriorityDirectory()
  349. {
  350. return __DIR__.'/../Fixtures/Integration/priority/';
  351. }
  352. }