FixerFactoryTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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;
  12. use PhpCsFixer\Fixer\ConfigurableFixerInterface;
  13. use PhpCsFixer\Fixer\DefinedFixerInterface;
  14. use PhpCsFixer\Fixer\FixerInterface;
  15. use PhpCsFixer\FixerDefinition\ShortFixerDefinition;
  16. use PhpCsFixer\FixerFactory;
  17. use PhpCsFixer\RuleSet;
  18. use Prophecy\Argument;
  19. /**
  20. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  21. *
  22. * @internal
  23. */
  24. final class FixerFactoryTest extends \PHPUnit_Framework_TestCase
  25. {
  26. public function testInterfaceIsFluent()
  27. {
  28. $factory = new FixerFactory();
  29. $testInstance = $factory->registerBuiltInFixers();
  30. $this->assertSame($factory, $testInstance);
  31. $testInstance = $factory->registerCustomFixers(
  32. array($this->createFixerDouble('f1'), $this->createFixerDouble('f2'))
  33. );
  34. $this->assertSame($factory, $testInstance);
  35. $testInstance = $factory->registerFixer(
  36. $this->createFixerDouble('f3')
  37. );
  38. $this->assertSame($factory, $testInstance);
  39. $ruleSetProphecy = $this->prophesize('PhpCsFixer\RuleSetInterface');
  40. $ruleSetProphecy->getRules()->willReturn(array());
  41. $testInstance = $factory->useRuleSet(
  42. $ruleSetProphecy->reveal()
  43. );
  44. $this->assertSame($factory, $testInstance);
  45. }
  46. /**
  47. * @covers \PhpCsFixer\FixerFactory::create
  48. */
  49. public function testCreate()
  50. {
  51. $factory = FixerFactory::create();
  52. $this->assertInstanceOf('PhpCsFixer\FixerFactory', $factory);
  53. }
  54. /**
  55. * @covers \PhpCsFixer\FixerFactory::registerBuiltInFixers
  56. */
  57. public function testRegisterBuiltInFixers()
  58. {
  59. $factory = new FixerFactory();
  60. $factory->registerBuiltInFixers();
  61. $this->assertGreaterThan(0, count($factory->getFixers()));
  62. }
  63. /**
  64. * @covers \PhpCsFixer\FixerFactory::getFixers
  65. * @covers \PhpCsFixer\FixerFactory::sortFixers
  66. */
  67. public function testThatFixersAreSorted()
  68. {
  69. $factory = new FixerFactory();
  70. $fxs = array(
  71. $this->createFixerDouble('f1', 0),
  72. $this->createFixerDouble('f2', -10),
  73. $this->createFixerDouble('f3', 10),
  74. $this->createFixerDouble('f4', -10),
  75. );
  76. foreach ($fxs as $fx) {
  77. $factory->registerFixer($fx);
  78. }
  79. // There are no rules that forces $fxs[1] to be prioritized before $fxs[3]. We should not test against that
  80. $this->assertSame(array($fxs[2], $fxs[0]), array_slice($factory->getFixers(), 0, 2));
  81. }
  82. /**
  83. * @covers \PhpCsFixer\FixerFactory::getFixers
  84. * @covers \PhpCsFixer\FixerFactory::registerCustomFixers
  85. * @covers \PhpCsFixer\FixerFactory::registerFixer
  86. */
  87. public function testThatCanRegisterAndGetFixers()
  88. {
  89. $factory = new FixerFactory();
  90. $f1 = $this->createFixerDouble('f1');
  91. $f2 = $this->createFixerDouble('f2');
  92. $f3 = $this->createFixerDouble('f3');
  93. $factory->registerFixer($f1);
  94. $factory->registerCustomFixers(array($f2, $f3));
  95. $this->assertTrue(in_array($f1, $factory->getFixers(), true));
  96. $this->assertTrue(in_array($f2, $factory->getFixers(), true));
  97. $this->assertTrue(in_array($f3, $factory->getFixers(), true));
  98. }
  99. /**
  100. * @covers \PhpCsFixer\FixerFactory::registerFixer
  101. * @expectedException \UnexpectedValueException
  102. * @expectedExceptionMessage Fixer named "non_unique_name" is already registered.
  103. */
  104. public function testRegisterFixerWithOccupiedName()
  105. {
  106. $factory = new FixerFactory();
  107. $f1 = $this->createFixerDouble('non_unique_name');
  108. $f2 = $this->createFixerDouble('non_unique_name');
  109. $factory->registerFixer($f1);
  110. $factory->registerFixer($f2);
  111. }
  112. /**
  113. * @covers \PhpCsFixer\FixerFactory::useRuleSet
  114. */
  115. public function testUseRuleSet()
  116. {
  117. $factory = FixerFactory::create()
  118. ->registerBuiltInFixers()
  119. ->useRuleSet(new RuleSet(array()))
  120. ;
  121. $this->assertCount(0, $factory->getFixers());
  122. $factory = FixerFactory::create()
  123. ->registerBuiltInFixers()
  124. ->useRuleSet(new RuleSet(array('strict_comparison' => true, 'blank_line_before_return' => false)))
  125. ;
  126. $fixers = $factory->getFixers();
  127. $this->assertCount(1, $fixers);
  128. $this->assertSame('strict_comparison', $fixers[0]->getName());
  129. }
  130. /**
  131. * @covers \PhpCsFixer\FixerFactory::useRuleSet
  132. * @expectedException \UnexpectedValueException
  133. * @expectedExceptionMessage Rule "non_existing_rule" does not exist.
  134. */
  135. public function testUseRuleSetWithNonExistingRule()
  136. {
  137. $factory = FixerFactory::create()
  138. ->registerBuiltInFixers()
  139. ->useRuleSet(new RuleSet(array('non_existing_rule' => true)))
  140. ;
  141. $fixers = $factory->getFixers();
  142. $this->assertCount(1, $fixers);
  143. $this->assertSame('strict_comparison', $fixers[0]->getName());
  144. }
  145. public function testFixersPriorityEdgeFixers()
  146. {
  147. $factory = new FixerFactory();
  148. $factory->registerBuiltInFixers();
  149. $fixers = $factory->getFixers();
  150. $this->assertSame('encoding', $fixers[0]->getName());
  151. $this->assertSame('full_opening_tag', $fixers[1]->getName());
  152. $this->assertSame('single_blank_line_at_eof', $fixers[count($fixers) - 1]->getName());
  153. }
  154. /**
  155. * @dataProvider getFixersPriorityCases
  156. */
  157. public function testFixersPriority(FixerInterface $first, FixerInterface $second)
  158. {
  159. $this->assertLessThan($first->getPriority(), $second->getPriority());
  160. }
  161. public function getFixersPriorityCases()
  162. {
  163. $factory = new FixerFactory();
  164. $factory->registerBuiltInFixers();
  165. $fixers = array();
  166. foreach ($factory->getFixers() as $fixer) {
  167. $fixers[$fixer->getName()] = $fixer;
  168. }
  169. $cases = array(
  170. array($fixers['elseif'], $fixers['braces']),
  171. array($fixers['method_separation'], $fixers['braces']),
  172. array($fixers['method_separation'], $fixers['indentation_type']),
  173. array($fixers['no_leading_import_slash'], $fixers['ordered_imports']), // tested also in: no_leading_import_slash,ordered_imports.test
  174. array($fixers['no_multiline_whitespace_around_double_arrow'], $fixers['binary_operator_spaces']), // tested also in: no_multiline_whitespace_around_double_arrow,binary_operator_spaces.test
  175. array($fixers['no_multiline_whitespace_around_double_arrow'], $fixers['trailing_comma_in_multiline_array']),
  176. array($fixers['no_php4_constructor'], $fixers['ordered_class_elements']), // tested also in: no_php4_constructor,ordered_class_elements.test
  177. array($fixers['no_short_bool_cast'], $fixers['cast_spaces']), // tested also in: no_short_bool_cast,cast_spaces.test
  178. array($fixers['no_short_echo_tag'], $fixers['no_mixed_echo_print']), // tested also in: no_mixed_echo_print,no_short_echo_tag.test
  179. array($fixers['indentation_type'], $fixers['phpdoc_indent']),
  180. array($fixers['no_unneeded_control_parentheses'], $fixers['no_trailing_whitespace']), // tested also in: no_trailing_whitespace,no_unneeded_control_parentheses.test
  181. array($fixers['no_unused_imports'], $fixers['blank_line_after_namespace']), // tested also in: no_unused_imports,blank_line_after_namespace.test and no_unused_imports,blank_line_after_namespace_2.test
  182. array($fixers['no_unused_imports'], $fixers['no_extra_consecutive_blank_lines']), // tested also in: no_unused_imports,no_extra_consecutive_blank_lines.test
  183. array($fixers['no_unused_imports'], $fixers['no_leading_import_slash']),
  184. array($fixers['ordered_class_elements'], $fixers['method_separation']), // tested also in: ordered_class_elements,method_separation.test
  185. array($fixers['ordered_class_elements'], $fixers['no_blank_lines_after_class_opening']), // tested also in: ordered_class_elements,no_blank_lines_after_class_opening.test
  186. array($fixers['ordered_class_elements'], $fixers['space_after_semicolon']), // tested also in: ordered_class_elements,space_after_semicolon.test
  187. array($fixers['php_unit_strict'], $fixers['php_unit_construct']),
  188. array($fixers['phpdoc_no_access'], $fixers['phpdoc_order']),
  189. array($fixers['phpdoc_no_access'], $fixers['phpdoc_separation']),
  190. array($fixers['phpdoc_no_access'], $fixers['phpdoc_trim']),
  191. array($fixers['phpdoc_no_empty_return'], $fixers['phpdoc_order']), // tested also in: phpdoc_no_empty_return,phpdoc_separation.test
  192. array($fixers['phpdoc_no_empty_return'], $fixers['phpdoc_separation']), // tested also in: phpdoc_no_empty_return,phpdoc_separation.test
  193. array($fixers['phpdoc_no_empty_return'], $fixers['phpdoc_trim']),
  194. array($fixers['phpdoc_no_package'], $fixers['phpdoc_order']),
  195. array($fixers['phpdoc_no_package'], $fixers['phpdoc_separation']), // tested also in: phpdoc_no_package,phpdoc_separation.test
  196. array($fixers['phpdoc_no_package'], $fixers['phpdoc_trim']),
  197. array($fixers['phpdoc_order'], $fixers['phpdoc_separation']),
  198. array($fixers['phpdoc_order'], $fixers['phpdoc_trim']),
  199. array($fixers['phpdoc_separation'], $fixers['phpdoc_trim']),
  200. array($fixers['phpdoc_summary'], $fixers['phpdoc_trim']),
  201. array($fixers['phpdoc_var_without_name'], $fixers['phpdoc_trim']),
  202. array($fixers['pow_to_exponentiation'], $fixers['binary_operator_spaces']), // tested also in: pow_to_exponentiation,binary_operator_spaces.test
  203. array($fixers['pow_to_exponentiation'], $fixers['method_argument_space']), // no priority issue; for speed only
  204. array($fixers['pow_to_exponentiation'], $fixers['native_function_casing']), // no priority issue; for speed only
  205. array($fixers['pow_to_exponentiation'], $fixers['no_spaces_after_function_name']), // no priority issue; for speed only
  206. array($fixers['pow_to_exponentiation'], $fixers['no_spaces_inside_parenthesis']), // no priority issue; for speed only
  207. array($fixers['single_import_per_statement'], $fixers['ordered_imports']), // tested also in: single_import_per_statement,ordered_imports.test
  208. array($fixers['single_import_per_statement'], $fixers['no_singleline_whitespace_before_semicolons']), // tested also in: single_import_per_statement,no_singleline_whitespace_before_semicolons.test
  209. array($fixers['single_import_per_statement'], $fixers['space_after_semicolon']), // tested also in: single_import_per_statement,space_after_semicolon.test
  210. array($fixers['single_import_per_statement'], $fixers['no_multiline_whitespace_before_semicolons']), // single_import_per_statement,no_multiline_whitespace_before_semicolons.test
  211. array($fixers['single_import_per_statement'], $fixers['no_leading_import_slash']), // tested also in: single_import_per_statement,no_leading_import_slash.test
  212. array($fixers['single_import_per_statement'], $fixers['no_unused_imports']), // tested also in: single_import_per_statement,no_unused_imports.test
  213. array($fixers['unary_operator_spaces'], $fixers['not_operator_with_space']),
  214. array($fixers['unary_operator_spaces'], $fixers['not_operator_with_successor_space']),
  215. array($fixers['line_ending'], $fixers['single_blank_line_at_eof']),
  216. array($fixers['simplified_null_return'], $fixers['no_useless_return']), // tested also in: simplified_null_return,no_useless_return.test
  217. array($fixers['no_useless_return'], $fixers['no_whitespace_in_blank_line']), // tested also in: no_useless_return,no_whitespace_in_blank_line.test
  218. array($fixers['no_useless_return'], $fixers['no_extra_consecutive_blank_lines']), // tested also in: no_useless_return,no_extra_consecutive_blank_lines.test
  219. array($fixers['no_useless_return'], $fixers['blank_line_before_return']), // tested also in: no_useless_return,blank_line_before_return.test
  220. array($fixers['no_empty_phpdoc'], $fixers['no_extra_consecutive_blank_lines']), // tested also in: no_empty_phpdoc,no_extra_consecutive_blank_lines.test
  221. array($fixers['no_empty_phpdoc'], $fixers['no_trailing_whitespace']), // tested also in: no_empty_phpdoc,no_trailing_whitespace.test
  222. array($fixers['no_empty_phpdoc'], $fixers['no_whitespace_in_blank_line']), // tested also in: no_empty_phpdoc,no_whitespace_in_blank_line.test
  223. array($fixers['phpdoc_no_access'], $fixers['no_empty_phpdoc']), // tested also in: phpdoc_no_access,no_empty_phpdoc.test
  224. array($fixers['phpdoc_no_empty_return'], $fixers['no_empty_phpdoc']), // tested also in: phpdoc_no_empty_return,no_empty_phpdoc.test
  225. array($fixers['phpdoc_no_package'], $fixers['no_empty_phpdoc']), // tested also in: phpdoc_no_package,no_empty_phpdoc.test
  226. array($fixers['combine_consecutive_unsets'], $fixers['space_after_semicolon']), // tested also in: combine_consecutive_unsets,space_after_semicolon.test
  227. array($fixers['combine_consecutive_unsets'], $fixers['no_whitespace_in_blank_line']), // tested also in: combine_consecutive_unsets,no_whitespace_in_blank_line.test
  228. array($fixers['combine_consecutive_unsets'], $fixers['no_trailing_whitespace']), // tested also in: combine_consecutive_unsets,no_trailing_whitespace.test
  229. array($fixers['combine_consecutive_unsets'], $fixers['no_extra_consecutive_blank_lines']), // tested also in: combine_consecutive_unsets,no_extra_consecutive_blank_lines.test
  230. array($fixers['phpdoc_no_alias_tag'], $fixers['phpdoc_single_line_var_spacing']), // tested also in: phpdoc_no_alias_tag,phpdoc_single_line_var_spacing.test
  231. array($fixers['blank_line_after_opening_tag'], $fixers['no_blank_lines_before_namespace']), // tested also in: blank_line_after_opening_tag,no_blank_lines_before_namespace.test
  232. array($fixers['phpdoc_to_comment'], $fixers['no_empty_comment']), // tested also in: phpdoc_to_comment,no_empty_comment.test
  233. array($fixers['no_empty_comment'], $fixers['no_extra_consecutive_blank_lines']), // tested also in: no_empty_comment,no_extra_consecutive_blank_lines.test
  234. array($fixers['no_empty_comment'], $fixers['no_trailing_whitespace']), // tested also in: no_empty_comment,no_trailing_whitespace.test
  235. array($fixers['no_empty_comment'], $fixers['no_whitespace_in_blank_line']), // tested also in: no_empty_comment,no_whitespace_in_blank_line.test
  236. array($fixers['no_alias_functions'], $fixers['php_unit_dedicate_assert']), // tested also in: no_alias_functions,php_unit_dedicate_assert.test
  237. array($fixers['no_empty_statement'], $fixers['braces']),
  238. array($fixers['no_empty_statement'], $fixers['combine_consecutive_unsets']), // tested also in: no_empty_statement,combine_consecutive_unsets.test
  239. array($fixers['no_empty_statement'], $fixers['no_extra_consecutive_blank_lines']), // tested also in: no_empty_statement,no_extra_consecutive_blank_lines.test
  240. array($fixers['no_empty_statement'], $fixers['no_multiline_whitespace_before_semicolons']),
  241. array($fixers['no_empty_statement'], $fixers['no_singleline_whitespace_before_semicolons']),
  242. array($fixers['no_empty_statement'], $fixers['no_trailing_whitespace']), // tested also in: no_empty_statement,no_trailing_whitespace.test
  243. array($fixers['no_empty_statement'], $fixers['no_useless_else']), // tested also in: no_empty_statement,no_useless_else.test
  244. array($fixers['no_empty_statement'], $fixers['no_useless_return']), // tested also in: no_empty_statement,no_useless_return.test
  245. array($fixers['no_empty_statement'], $fixers['no_whitespace_in_blank_line']), // tested also in: no_empty_statement,no_whitespace_in_blank_line.test
  246. array($fixers['no_empty_statement'], $fixers['space_after_semicolon']), // tested also in: no_empty_statement,space_after_semicolon.test
  247. array($fixers['no_empty_statement'], $fixers['switch_case_semicolon_to_colon']), // tested also in: no_empty_statement,switch_case_semicolon_to_colon.test
  248. array($fixers['no_useless_else'], $fixers['braces']),
  249. array($fixers['no_useless_else'], $fixers['combine_consecutive_unsets']), // tested also in: no_useless_else,combine_consecutive_unsets.test
  250. array($fixers['no_useless_else'], $fixers['no_extra_consecutive_blank_lines']), // tested also in: no_useless_else,no_extra_consecutive_blank_lines.test
  251. array($fixers['no_useless_else'], $fixers['no_useless_return']), // tested also in: no_useless_else,no_useless_return.test
  252. array($fixers['no_useless_else'], $fixers['no_trailing_whitespace']), // tested also in: no_useless_else,no_trailing_whitespace.test
  253. array($fixers['no_useless_else'], $fixers['no_whitespace_in_blank_line']), // tested also in: no_useless_else,no_whitespace_in_blank_line.test
  254. array($fixers['declare_strict_types'], $fixers['no_blank_lines_before_namespace']), // tested also in: declare_strict_types,no_blank_lines_before_namespace.test
  255. array($fixers['declare_strict_types'], $fixers['no_extra_consecutive_blank_lines']), // tested also in: declare_strict_types,no_extra_consecutive_blank_lines.test
  256. array($fixers['declare_strict_types'], $fixers['no_whitespace_in_blank_line']), // tested also in: declare_strict_types,no_whitespace_in_blank_line.test
  257. array($fixers['declare_strict_types'], $fixers['single_blank_line_before_namespace']), // tested also in: declare_strict_types,single_blank_line_before_namespace.test
  258. array($fixers['array_syntax'], $fixers['binary_operator_spaces']), // tested also in: array_syntax,binary_operator_spaces.test
  259. array($fixers['array_syntax'], $fixers['ternary_operator_spaces']), // tested also in: array_syntax,ternary_operator_spaces.test
  260. array($fixers['class_keyword_remove'], $fixers['no_unused_imports']), // tested also in: class_keyword_remove,no_unused_imports.test
  261. array($fixers['no_blank_lines_after_phpdoc'], $fixers['single_blank_line_before_namespace']), // tested also in: no_blank_lines_after_phpdoc,single_blank_line_before_namespace.test
  262. array($fixers['php_unit_fqcn_annotation'], $fixers['no_unused_imports']), // tested also in: php_unit_fqcn_annotation,unused_use.test
  263. array($fixers['protected_to_private'], $fixers['ordered_class_elements']), // tested also in: protected_to_private,ordered_class_elements.test
  264. array($fixers['phpdoc_add_missing_param_annotation'], $fixers['phpdoc_align']), // tested also in: phpdoc_add_missing_param_annotation,phpdoc_align.test
  265. array($fixers['phpdoc_no_alias_tag'], $fixers['phpdoc_add_missing_param_annotation']), // tested also in: phpdoc_no_alias_tag,phpdoc_add_missing_param_annotation.test
  266. );
  267. // prepare bulk tests for phpdoc fixers to test that:
  268. // * `phpdoc_to_comment` is first
  269. // * `phpdoc_indent` is second
  270. // * `phpdoc_types` is third
  271. // * `phpdoc_scalar` is fourth
  272. // * `phpdoc_align` is last
  273. $cases[] = array($fixers['phpdoc_to_comment'], $fixers['phpdoc_indent']);
  274. $cases[] = array($fixers['phpdoc_indent'], $fixers['phpdoc_types']);
  275. $cases[] = array($fixers['phpdoc_types'], $fixers['phpdoc_scalar']);
  276. $docFixerNames = array_filter(
  277. array_keys($fixers),
  278. function ($name) {
  279. return false !== strpos($name, 'phpdoc');
  280. }
  281. );
  282. foreach ($docFixerNames as $docFixerName) {
  283. if (!in_array($docFixerName, array('phpdoc_to_comment', 'phpdoc_indent', 'phpdoc_types', 'phpdoc_scalar'), true)) {
  284. $cases[] = array($fixers['phpdoc_to_comment'], $fixers[$docFixerName]);
  285. $cases[] = array($fixers['phpdoc_indent'], $fixers[$docFixerName]);
  286. $cases[] = array($fixers['phpdoc_types'], $fixers[$docFixerName]);
  287. $cases[] = array($fixers['phpdoc_scalar'], $fixers[$docFixerName]);
  288. }
  289. if ('phpdoc_align' !== $docFixerName) {
  290. $cases[] = array($fixers[$docFixerName], $fixers['phpdoc_align']);
  291. }
  292. }
  293. return $cases;
  294. }
  295. public function testHasRule()
  296. {
  297. $factory = new FixerFactory();
  298. $f1 = $this->createFixerDouble('f1');
  299. $f2 = $this->createFixerDouble('f2');
  300. $f3 = $this->createFixerDouble('f3');
  301. $factory->registerFixer($f1);
  302. $factory->registerCustomFixers(array($f2, $f3));
  303. $this->assertTrue($factory->hasRule('f1'), 'Should have f1 fixer');
  304. $this->assertTrue($factory->hasRule('f2'), 'Should have f2 fixer');
  305. $this->assertTrue($factory->hasRule('f3'), 'Should have f3 fixer');
  306. $this->assertFalse($factory->hasRule('dummy'), 'Should not have dummy fixer');
  307. }
  308. public function testHasRuleWithChangedRuleSet()
  309. {
  310. $factory = new FixerFactory();
  311. $f1 = $this->createFixerDouble('f1');
  312. $f2 = $this->createFixerDouble('f2');
  313. $factory->registerFixer($f1);
  314. $factory->registerFixer($f2);
  315. $this->assertTrue($factory->hasRule('f1'), 'Should have f1 fixer');
  316. $this->assertTrue($factory->hasRule('f2'), 'Should have f2 fixer');
  317. $factory->useRuleSet(new RuleSet(array('f2' => true)));
  318. $this->assertFalse($factory->hasRule('f1'), 'Should not have f1 fixer');
  319. $this->assertTrue($factory->hasRule('f2'), 'Should have f2 fixer');
  320. }
  321. /**
  322. * @param FixerInterface $fixer
  323. *
  324. * @dataProvider provideFixerDefinitionsCases
  325. */
  326. public function testFixerDefinitions(FixerInterface $fixer)
  327. {
  328. $this->assertInstanceOf('PhpCsFixer\Fixer\DefinedFixerInterface', $fixer);
  329. /** @var DefinedFixerInterface $fixer */
  330. $definition = $fixer->getDefinition();
  331. $this->assertRegExp('/^[A-Z@].*\.$/', $definition->getSummary(), 'Description must start with capital letter or an @ and end with dot.');
  332. if ($definition instanceof ShortFixerDefinition) {
  333. $this->markTestIncomplete('ShortFixerDefinition does not contains all needed information.');
  334. }
  335. $this->assertNotEmpty($definition->getCodeSamples(), 'Code samples are required.');
  336. if ($fixer instanceof ConfigurableFixerInterface) {
  337. $this->assertNotEmpty($definition->getConfigurationDescription(), 'Configuration description is required.');
  338. $this->assertNotEmpty($definition->getDefaultConfiguration(), 'Default configuration is required.');
  339. }
  340. if ($fixer->isRisky()) {
  341. $this->assertNotEmpty($definition->getRiskyDescription(), 'Risky reasoning is required.');
  342. }
  343. }
  344. public function provideFixerDefinitionsCases()
  345. {
  346. return array_map(function (FixerInterface $fixer) {
  347. return array($fixer);
  348. }, $this->getAllFixers());
  349. }
  350. /**
  351. * @dataProvider provideFixersForFinalCheckCases
  352. */
  353. public function testFixersAreFinal(\ReflectionClass $class)
  354. {
  355. $this->assertTrue($class->isFinal());
  356. }
  357. public function provideFixersForFinalCheckCases()
  358. {
  359. $cases = array();
  360. foreach ($this->getAllFixers() as $fixer) {
  361. $cases[] = array(new \ReflectionClass($fixer));
  362. }
  363. return $cases;
  364. }
  365. /**
  366. * This method is a guard to not introduce new Fixer using `ShortFixerDefinition`.
  367. *
  368. * Will be removed with `ShortFixerDefinition` removal.
  369. */
  370. public function testShortFixerDefinition()
  371. {
  372. $guard = 126;
  373. $this->assertCount(
  374. $guard,
  375. array_filter(array_map(function (FixerInterface $fixer) {
  376. return
  377. !$fixer instanceof DefinedFixerInterface
  378. || $fixer->getDefinition() instanceof ShortFixerDefinition
  379. ;
  380. }, $this->getAllFixers())),
  381. implode("\n", array(
  382. 'Not valid amount of fixers using ShortFixerDefinition.',
  383. 'If this test is failing it means one of those scenario occurred:',
  384. '- you introduced new Fixer using `ShortFixerDefinition`, you should use `FixerDefinition` instead,',
  385. '- you update the Fixer to stop using `ShortFixerDefinition`, you should decrease the guard value.',
  386. ))
  387. );
  388. }
  389. /**
  390. * @dataProvider provideConflictingFixersRules
  391. * @expectedException \UnexpectedValueException
  392. * @expectedExceptionMessageRegExp #^Rule contains conflicting fixers:\n#
  393. */
  394. public function testConflictingFixers(RuleSet $ruleSet)
  395. {
  396. FixerFactory::create()->registerBuiltInFixers()->useRuleSet($ruleSet);
  397. }
  398. public function provideConflictingFixersRules()
  399. {
  400. return array(
  401. array(new RuleSet(array('no_blank_lines_before_namespace' => true, 'single_blank_line_before_namespace' => true))),
  402. array(new RuleSet(array('single_blank_line_before_namespace' => true, 'no_blank_lines_before_namespace' => true))),
  403. );
  404. }
  405. public function testNoDoubleConflictReporting()
  406. {
  407. $factory = new FixerFactory();
  408. $method = new \ReflectionMethod($factory, 'generateConflictMessage');
  409. $method->setAccessible(true);
  410. $this->assertSame(
  411. 'Rule contains conflicting fixers:
  412. - "a" with "b"
  413. - "c" with "d", "e", "f"
  414. - "d" with "g", "h"
  415. - "e" with "a"',
  416. $method->invoke(
  417. $factory,
  418. array(
  419. 'a' => array('b'),
  420. 'b' => array('a'),
  421. 'c' => array('d', 'e', 'f'),
  422. 'd' => array('c', 'g', 'h'),
  423. 'e' => array('a'),
  424. )
  425. )
  426. );
  427. }
  428. private function getAllFixers()
  429. {
  430. $factory = new FixerFactory();
  431. return $factory->registerBuiltInFixers()->getFixers();
  432. }
  433. private function createFixerDouble($name, $priority = 0)
  434. {
  435. /** @var FixerInterface $fixer */
  436. $fixer = $this->prophesize('PhpCsFixer\Fixer\FixerInterface');
  437. $fixer->getName()->willReturn($name);
  438. $fixer->getPriority()->willReturn($priority);
  439. //$fixer->configure(Argument::is(null))->willReturn(null); Needed?
  440. return $fixer->reveal();
  441. }
  442. }