FixerFactoryTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. /*
  3. * This file is part of the PHP CS utility.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\CS\Tests;
  11. use Symfony\CS\Fixer\Contrib\Psr0Fixer;
  12. use Symfony\CS\FixerFactory;
  13. use Symfony\CS\FixerInterface;
  14. use Symfony\CS\RuleSet;
  15. use Symfony\CS\Test\AccessibleObject;
  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. $mocks = array($this->createFixerMock('f1'), $this->createFixerMock('f2'));
  29. $testInstance = $factory->registerCustomFixers($mocks);
  30. $this->assertSame($factory, $testInstance);
  31. $mock = $this->createFixerMock('f3');
  32. $testInstance = $factory->registerFixer($mock);
  33. $this->assertSame($factory, $testInstance);
  34. $mock = $this->getMock('Symfony\CS\RuleSetInterface');
  35. $mock->expects($this->any())->method('getRules')->willReturn(array());
  36. $testInstance = $factory->useRuleSet($mock);
  37. $this->assertSame($factory, $testInstance);
  38. $mock = $this->getMock('Symfony\CS\ConfigInterface');
  39. $testInstance = $factory->attachConfig($mock);
  40. $this->assertSame($factory, $testInstance);
  41. }
  42. /**
  43. * @covers Symfony\CS\FixerFactory::create
  44. */
  45. public function testCreate()
  46. {
  47. $factory = FixerFactory::create();
  48. $this->assertInstanceOf('Symfony\CS\FixerFactory', $factory);
  49. }
  50. /**
  51. * @covers Symfony\CS\FixerFactory::registerBuiltInFixers
  52. */
  53. public function testRegisterBuiltInFixers()
  54. {
  55. $factory = new FixerFactory();
  56. $factory->registerBuiltInFixers();
  57. $this->assertGreaterThan(0, count($factory->getFixers()));
  58. }
  59. /**
  60. * @covers Symfony\CS\FixerFactory::attachConfig
  61. */
  62. public function testMethodAttachConfig()
  63. {
  64. $factory = new FixerFactory();
  65. $fixer = new Psr0Fixer();
  66. $factory->registerFixer($fixer);
  67. $mock = $this->getMock('Symfony\CS\ConfigInterface');
  68. $testInstance = $factory->attachConfig($mock);
  69. $property = AccessibleObject::create($fixer)->config;
  70. $this->assertSame($mock, $property);
  71. }
  72. /**
  73. * @covers Symfony\CS\FixerFactory::getFixers
  74. * @covers Symfony\CS\FixerFactory::sortFixers
  75. */
  76. public function testThatFixersAreSorted()
  77. {
  78. $factory = new FixerFactory();
  79. $fxs = array(
  80. $this->createFixerMock('f1', 0),
  81. $this->createFixerMock('f2', -10),
  82. $this->createFixerMock('f3', 10),
  83. $this->createFixerMock('f4', -10),
  84. );
  85. foreach ($fxs as $fx) {
  86. $factory->registerFixer($fx);
  87. }
  88. // There are no rules that forces $fxs[1] to be prioritized before $fxs[3]. We should not test against that
  89. $this->assertSame(array($fxs[2], $fxs[0]), array_slice($factory->getFixers(), 0, 2));
  90. }
  91. /**
  92. * @covers Symfony\CS\FixerFactory::getFixers
  93. * @covers Symfony\CS\FixerFactory::registerCustomFixers
  94. * @covers Symfony\CS\FixerFactory::registerFixer
  95. */
  96. public function testThatCanRegisterAndGetFixers()
  97. {
  98. $factory = new FixerFactory();
  99. $f1 = $this->createFixerMock('f1');
  100. $f2 = $this->createFixerMock('f2');
  101. $f3 = $this->createFixerMock('f3');
  102. $factory->registerFixer($f1);
  103. $factory->registerCustomFixers(array($f2, $f3));
  104. $this->assertTrue(in_array($f1, $factory->getFixers(), true));
  105. $this->assertTrue(in_array($f2, $factory->getFixers(), true));
  106. $this->assertTrue(in_array($f3, $factory->getFixers(), true));
  107. }
  108. /**
  109. * @covers Symfony\CS\FixerFactory::registerFixer
  110. * @expectedException \UnexpectedValueException
  111. * @expectedExceptionMessage Fixer named "non_unique_name" is already registered.
  112. */
  113. public function testRegisterFixerWithOccupiedName()
  114. {
  115. $factory = new FixerFactory();
  116. $f1 = $this->createFixerMock('non_unique_name');
  117. $f2 = $this->createFixerMock('non_unique_name');
  118. $factory->registerFixer($f1);
  119. $factory->registerFixer($f2);
  120. }
  121. /**
  122. * @covers Symfony\CS\FixerFactory::useRuleSet
  123. */
  124. public function testUseRuleSet()
  125. {
  126. $factory = FixerFactory::create()
  127. ->registerBuiltInFixers()
  128. ->useRuleSet(new RuleSet(array()))
  129. ;
  130. $this->assertCount(0, $factory->getFixers());
  131. $factory = FixerFactory::create()
  132. ->registerBuiltInFixers()
  133. ->useRuleSet(new RuleSet(array('strict' => true, 'blank_line_before_return' => false)))
  134. ;
  135. $fixers = $factory->getFixers();
  136. $this->assertCount(1, $fixers);
  137. $this->assertSame('strict', $fixers[0]->getName());
  138. }
  139. /**
  140. * @covers Symfony\CS\FixerFactory::useRuleSet
  141. * @expectedException \UnexpectedValueException
  142. * @expectedExceptionMessage Rule "non_existing_rule" does not exist.
  143. */
  144. public function testUseRuleSetWithNonExistingRule()
  145. {
  146. $factory = FixerFactory::create()
  147. ->registerBuiltInFixers()
  148. ->useRuleSet(new RuleSet(array('non_existing_rule' => true)))
  149. ;
  150. $fixers = $factory->getFixers();
  151. $this->assertCount(1, $fixers);
  152. $this->assertSame('strict', $fixers[0]->getName());
  153. }
  154. public function testFixersPriorityEdgeFixers()
  155. {
  156. $factory = new FixerFactory();
  157. $factory->registerBuiltInFixers();
  158. $fixers = $factory->getFixers();
  159. $this->assertSame('encoding', $fixers[0]->getName());
  160. $this->assertSame('full_opening_tag', $fixers[1]->getName());
  161. $this->assertSame('single_blank_line_at_eof', $fixers[count($fixers) - 1]->getName());
  162. }
  163. /**
  164. * @dataProvider getFixersPriorityCases
  165. */
  166. public function testFixersPriority(FixerInterface $first, FixerInterface $second)
  167. {
  168. $this->assertLessThan($first->getPriority(), $second->getPriority());
  169. }
  170. public function getFixersPriorityCases()
  171. {
  172. $factory = new FixerFactory();
  173. $factory->registerBuiltInFixers();
  174. $fixers = array();
  175. foreach ($factory->getFixers() as $fixer) {
  176. $fixers[$fixer->getName()] = $fixer;
  177. }
  178. $cases = array(
  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['single_import_per_statement'], $fixers['no_unused_imports']), // tested also in: single_import_per_statement,no_unused_imports.test
  181. array($fixers['no_leading_import_slash'], $fixers['ordered_imports']), // tested also in: no_leading_import_slash,ordered_imports.test
  182. array($fixers['no_blank_lines_between_uses'], $fixers['ordered_imports']),
  183. array($fixers['no_unused_imports'], $fixers['no_leading_import_slash']),
  184. array($fixers['single_import_per_statement'], $fixers['no_leading_import_slash']),
  185. array($fixers['concat_without_spaces'], $fixers['concat_with_spaces']),
  186. array($fixers['elseif'], $fixers['braces']),
  187. array($fixers['no_duplicate_semicolons'], $fixers['braces']),
  188. array($fixers['no_duplicate_semicolons'], $fixers['no_singleline_whitespace_before_semicolons']),
  189. array($fixers['no_duplicate_semicolons'], $fixers['no_multiline_whitespace_before_semicolons']),
  190. array($fixers['no_duplicate_semicolons'], $fixers['switch_case_semicolon_to_colon']), // tested also in: no_duplicate_semicolons,switch_case_semicolon_to_colon.test
  191. array($fixers['double_arrow_no_multiline_whitespace'], $fixers['trailing_comma_in_multiline_array']),
  192. array($fixers['double_arrow_no_multiline_whitespace'], $fixers['align_double_arrow']), // tested also in: double_arrow_no_multiline_whitespace,align_double_arrow.test
  193. array($fixers['binary_operator_spaces'], $fixers['align_double_arrow']), // tested also in: align_double_arrow,binary_operator_spaces.test
  194. array($fixers['binary_operator_spaces'], $fixers['align_equals']), // tested also in: align_double_arrow,align_equals.test
  195. array($fixers['no_tab_indentation'], $fixers['phpdoc_indent']),
  196. array($fixers['phpdoc_order'], $fixers['phpdoc_separation']),
  197. array($fixers['phpdoc_no_access'], $fixers['phpdoc_separation']),
  198. array($fixers['phpdoc_no_access'], $fixers['phpdoc_order']),
  199. array($fixers['phpdoc_no_simplified_null_return'], $fixers['phpdoc_separation']), // tested also in: phpdoc_no_simplified_null_return,phpdoc_separation.test
  200. array($fixers['phpdoc_no_simplified_null_return'], $fixers['phpdoc_order']), // tested also in: phpdoc_no_simplified_null_return,phpdoc_separation.test
  201. array($fixers['phpdoc_no_package'], $fixers['phpdoc_separation']), // tested also in: phpdoc_no_package,phpdoc_separation.test
  202. array($fixers['phpdoc_no_package'], $fixers['phpdoc_order']),
  203. array($fixers['phpdoc_no_access'], $fixers['phpdoc_trim']),
  204. array($fixers['phpdoc_no_simplified_null_return'], $fixers['phpdoc_trim']),
  205. array($fixers['phpdoc_no_package'], $fixers['phpdoc_trim']),
  206. array($fixers['phpdoc_separation'], $fixers['phpdoc_trim']),
  207. array($fixers['phpdoc_summary'], $fixers['phpdoc_trim']),
  208. array($fixers['phpdoc_var_without_name'], $fixers['phpdoc_trim']),
  209. array($fixers['phpdoc_order'], $fixers['phpdoc_trim']),
  210. array($fixers['no_unused_imports'], $fixers['blank_line_after_namespace']), // tested also in: no_unused_imports,blank_line_after_namespace.test
  211. array($fixers['unix_line_endings'], $fixers['single_blank_line_at_eof']),
  212. array($fixers['php_unit_strict'], $fixers['php_unit_construct']),
  213. array($fixers['unary_operator_spaces'], $fixers['not_operators_with_space']),
  214. array($fixers['unary_operator_spaces'], $fixers['not_operator_with_successor_space']),
  215. array($fixers['method_separation'], $fixers['braces']),
  216. array($fixers['method_separation'], $fixers['no_tab_indentation']),
  217. array($fixers['no_short_echo_tag'], $fixers['echo_to_print']), // tested also in: echo_to_print,no_short_echo_tag.test
  218. array($fixers['no_short_bool_cast'], $fixers['spaces_cast']), // tested also in: no_short_bool_cast,spaces_cast.test
  219. array($fixers['no_unneeded_control_parentheses'], $fixers['no_trailing_whitespace']), // tested also in: no_trailing_whitespace,no_unneeded_control_parentheses.test
  220. array($fixers['class_definition'], $fixers['no_trailing_whitespace']), // tested also in: class_definition,no_trailing_whitespace.test
  221. );
  222. // prepare bulk tests for phpdoc fixers to test that:
  223. // * `phpdoc_to_comment` is first
  224. // * `phpdoc_indent` is second
  225. // * `phpdoc_types` is third
  226. // * `phpdoc_scalar` is fourth
  227. // * `phpdoc_align` is last
  228. $cases[] = array($fixers['phpdoc_to_comment'], $fixers['phpdoc_indent']);
  229. $cases[] = array($fixers['phpdoc_indent'], $fixers['phpdoc_types']);
  230. $cases[] = array($fixers['phpdoc_types'], $fixers['phpdoc_scalar']);
  231. $docFixerNames = array_filter(
  232. array_keys($fixers),
  233. function ($name) {
  234. return false !== strpos($name, 'phpdoc');
  235. }
  236. );
  237. foreach ($docFixerNames as $docFixerName) {
  238. if (!in_array($docFixerName, array('phpdoc_to_comment', 'phpdoc_indent', 'phpdoc_types', 'phpdoc_scalar'), true)) {
  239. $cases[] = array($fixers['phpdoc_to_comment'], $fixers[$docFixerName]);
  240. $cases[] = array($fixers['phpdoc_indent'], $fixers[$docFixerName]);
  241. $cases[] = array($fixers['phpdoc_types'], $fixers[$docFixerName]);
  242. $cases[] = array($fixers['phpdoc_scalar'], $fixers[$docFixerName]);
  243. }
  244. if ('phpdoc_align' !== $docFixerName) {
  245. $cases[] = array($fixers[$docFixerName], $fixers['phpdoc_align']);
  246. }
  247. }
  248. return $cases;
  249. }
  250. public function testHasRule()
  251. {
  252. $factory = new FixerFactory();
  253. $f1 = $this->createFixerMock('f1');
  254. $f2 = $this->createFixerMock('f2');
  255. $f3 = $this->createFixerMock('f3');
  256. $factory->registerFixer($f1);
  257. $factory->registerCustomFixers(array($f2, $f3));
  258. $this->assertTrue($factory->hasRule('f1'), 'Should have f1 fixer');
  259. $this->assertTrue($factory->hasRule('f2'), 'Should have f2 fixer');
  260. $this->assertTrue($factory->hasRule('f3'), 'Should have f3 fixer');
  261. $this->assertFalse($factory->hasRule('dummy'), 'Should not have dummy fixer');
  262. }
  263. public function testHasRuleWithChangedRuleSet()
  264. {
  265. $factory = new FixerFactory();
  266. $f1 = $this->createFixerMock('f1');
  267. $f2 = $this->createFixerMock('f2');
  268. $factory->registerFixer($f1);
  269. $factory->registerFixer($f2);
  270. $this->assertTrue($factory->hasRule('f1'), 'Should have f1 fixer');
  271. $this->assertTrue($factory->hasRule('f2'), 'Should have f2 fixer');
  272. $factory->useRuleSet(new RuleSet(array('f2' => true)));
  273. $this->assertFalse($factory->hasRule('f1'), 'Should not have f1 fixer');
  274. $this->assertTrue($factory->hasRule('f2'), 'Should have f2 fixer');
  275. }
  276. private function createFixerMock($name, $priority = 0)
  277. {
  278. $fixer = $this->getMock('Symfony\CS\FixerInterface');
  279. $fixer->expects($this->any())->method('getName')->willReturn($name);
  280. $fixer->expects($this->any())->method('getPriority')->willReturn($priority);
  281. return $fixer;
  282. }
  283. }