FixerFactoryTest.php 24 KB

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