FixerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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\Config\Config;
  12. use Symfony\CS\Fixer;
  13. use Symfony\CS\FixerInterface;
  14. class FixerTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @covers Symfony\CS\Fixer::sortFixers
  18. */
  19. public function testThatFixersAreSorted()
  20. {
  21. $fixer = new Fixer();
  22. $fxPrototypes = array(
  23. array('getPriority' => 0),
  24. array('getPriority' => -10),
  25. array('getPriority' => 10),
  26. array('getPriority' => -10),
  27. );
  28. $fxs = array();
  29. foreach ($fxPrototypes as $fxPrototype) {
  30. $fx = $this->getMock('Symfony\CS\FixerInterface');
  31. $fx->expects($this->any())->method('getPriority')->willReturn($fxPrototype['getPriority']);
  32. $fixer->addFixer($fx);
  33. $fxs[] = $fx;
  34. }
  35. // There are no rules that forces $fxs[1] to be prioritized before $fxs[3]. We should not test against that
  36. $this->assertSame(array($fxs[2], $fxs[0]), array_slice($fixer->getFixers(), 0, 2));
  37. }
  38. /**
  39. * @covers Symfony\CS\Fixer::registerBuiltInFixers
  40. */
  41. public function testThatRegisterBuiltInFixers()
  42. {
  43. $fixer = new Fixer();
  44. $this->assertCount(0, $fixer->getFixers());
  45. $fixer->registerBuiltInFixers();
  46. $this->assertGreaterThan(0, count($fixer->getFixers()));
  47. }
  48. /**
  49. * @covers Symfony\CS\Fixer::registerBuiltInConfigs
  50. */
  51. public function testThatRegisterBuiltInConfigs()
  52. {
  53. $fixer = new Fixer();
  54. $this->assertCount(0, $fixer->getConfigs());
  55. $fixer->registerBuiltInConfigs();
  56. $this->assertGreaterThan(0, count($fixer->getConfigs()));
  57. }
  58. /**
  59. * @covers Symfony\CS\Fixer::addFixer
  60. * @covers Symfony\CS\Fixer::getFixers
  61. */
  62. public function testThatCanAddAndGetFixers()
  63. {
  64. $fixer = new Fixer();
  65. $f1 = $this->getMock('Symfony\CS\FixerInterface');
  66. $f2 = $this->getMock('Symfony\CS\FixerInterface');
  67. $fixer->addFixer($f1);
  68. $fixer->addFixer($f2);
  69. $this->assertTrue(in_array($f1, $fixer->getFixers(), true));
  70. $this->assertTrue(in_array($f2, $fixer->getFixers(), true));
  71. }
  72. /**
  73. * @covers Symfony\CS\Fixer::addConfig
  74. * @covers Symfony\CS\Fixer::getConfigs
  75. */
  76. public function testThatCanAddAndGetConfigs()
  77. {
  78. $fixer = new Fixer();
  79. $c1 = $this->getMock('Symfony\CS\ConfigInterface');
  80. $c2 = $this->getMock('Symfony\CS\ConfigInterface');
  81. $fixer->addConfig($c1);
  82. $fixer->addConfig($c2);
  83. $this->assertSame(array($c1, $c2), $fixer->getConfigs());
  84. }
  85. /**
  86. * @covers Symfony\CS\Fixer::fix
  87. * @covers Symfony\CS\Fixer::fixFile
  88. * @covers Symfony\CS\Fixer::prepareFixers
  89. */
  90. public function testThatFixSuccessfully()
  91. {
  92. $fixer = new Fixer();
  93. $fixer->addFixer(new \Symfony\CS\Fixer\PSR2\VisibilityFixer());
  94. $fixer->addFixer(new \Symfony\CS\Fixer\PSR0\Psr0Fixer()); //will be ignored cause of test keyword in namespace
  95. $config = Config::create()->finder(new \DirectoryIterator(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'FixerTest'));
  96. $config->fixers($fixer->getFixers());
  97. $changed = $fixer->fix($config, true, true);
  98. $pathToInvalidFile = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'FixerTest'.DIRECTORY_SEPARATOR.'somefile.php';
  99. $this->assertCount(1, $changed);
  100. $this->assertCount(2, $changed[$pathToInvalidFile]);
  101. $this->assertSame(array('appliedFixers', 'diff'), array_keys($changed[$pathToInvalidFile]));
  102. $this->assertSame('visibility', $changed[$pathToInvalidFile]['appliedFixers'][0]);
  103. }
  104. /**
  105. * @covers Symfony\CS\Fixer::getLevelAsString
  106. * @dataProvider getFixerLevels
  107. */
  108. public function testThatCanGetFixerLevelString($level, $expectedLevelString)
  109. {
  110. $fixer = $this->getMock('Symfony\CS\FixerInterface');
  111. $fixer->expects($this->any())->method('getLevel')->will($this->returnValue($level));
  112. $this->assertSame($expectedLevelString, Fixer::getLevelAsString($fixer));
  113. }
  114. public function testFixersPriorityEdgeFixers()
  115. {
  116. $fixer = new Fixer();
  117. $fixer->registerBuiltInFixers();
  118. $fixers = $fixer->getFixers();
  119. $this->assertSame('encoding', $fixers[0]->getName());
  120. $this->assertSame('short_tag', $fixers[1]->getName());
  121. $this->assertSame('eof_ending', $fixers[count($fixers) - 1]->getName());
  122. }
  123. /**
  124. * @dataProvider getFixersPriorityCases
  125. */
  126. public function testFixersPriority(FixerInterface $first, FixerInterface $second)
  127. {
  128. $this->assertLessThan($first->getPriority(), $second->getPriority());
  129. }
  130. public function getFixersPriorityCases()
  131. {
  132. $fixer = new Fixer();
  133. $fixer->registerBuiltInFixers();
  134. $fixers = array();
  135. foreach ($fixer->getFixers() as $fixer) {
  136. $fixers[$fixer->getName()] = $fixer;
  137. }
  138. $cases = array(
  139. array($fixers['unused_use'], $fixers['extra_empty_lines']), // tested also in: unused_use,extra_empty_lines.test
  140. array($fixers['multiple_use'], $fixers['unused_use']), // tested also in: multiple_use,unused_use.test
  141. array($fixers['remove_leading_slash_use'], $fixers['ordered_use']), // tested also in: remove_leading_slash_use,ordered_use.test
  142. array($fixers['remove_lines_between_uses'], $fixers['ordered_use']),
  143. array($fixers['unused_use'], $fixers['remove_leading_slash_use']),
  144. array($fixers['multiple_use'], $fixers['remove_leading_slash_use']),
  145. array($fixers['concat_without_spaces'], $fixers['concat_with_spaces']),
  146. array($fixers['elseif'], $fixers['braces']),
  147. array($fixers['duplicate_semicolon'], $fixers['braces']),
  148. array($fixers['duplicate_semicolon'], $fixers['spaces_before_semicolon']),
  149. array($fixers['duplicate_semicolon'], $fixers['multiline_spaces_before_semicolon']),
  150. array($fixers['duplicate_semicolon'], $fixers['switch_case_semicolon_to_colon']), // tested also in: duplicate_semicolon,switch_case_semicolon_to_colon.test
  151. array($fixers['double_arrow_multiline_whitespaces'], $fixers['multiline_array_trailing_comma']),
  152. array($fixers['double_arrow_multiline_whitespaces'], $fixers['align_double_arrow']), // tested also in: double_arrow_multiline_whitespaces,align_double_arrow.test
  153. array($fixers['operators_spaces'], $fixers['align_double_arrow']), // tested also in: align_double_arrow,operators_spaces.test
  154. array($fixers['operators_spaces'], $fixers['align_equals']), // tested also in: align_double_arrow,align_equals.test
  155. array($fixers['indentation'], $fixers['phpdoc_indent']),
  156. array($fixers['phpdoc_order'], $fixers['phpdoc_separation']),
  157. array($fixers['phpdoc_no_access'], $fixers['phpdoc_separation']),
  158. array($fixers['phpdoc_no_access'], $fixers['phpdoc_order']),
  159. array($fixers['phpdoc_no_empty_return'], $fixers['phpdoc_separation']), // tested also in: phpdoc_no_empty_return,phpdoc_separation.test
  160. array($fixers['phpdoc_no_empty_return'], $fixers['phpdoc_order']), // tested also in: phpdoc_no_empty_return,phpdoc_separation.test
  161. array($fixers['phpdoc_no_package'], $fixers['phpdoc_separation']), // tested also in: phpdoc_no_package,phpdoc_separation.test
  162. array($fixers['phpdoc_no_package'], $fixers['phpdoc_order']),
  163. array($fixers['phpdoc_no_access'], $fixers['phpdoc_trim']),
  164. array($fixers['phpdoc_no_empty_return'], $fixers['phpdoc_trim']),
  165. array($fixers['phpdoc_no_package'], $fixers['phpdoc_trim']),
  166. array($fixers['phpdoc_separation'], $fixers['phpdoc_trim']),
  167. array($fixers['phpdoc_short_description'], $fixers['phpdoc_trim']),
  168. array($fixers['phpdoc_var_without_name'], $fixers['phpdoc_trim']),
  169. array($fixers['phpdoc_order'], $fixers['phpdoc_trim']),
  170. array($fixers['unused_use'], $fixers['line_after_namespace']), // tested also in: unused_use,line_after_namespace.test
  171. array($fixers['linefeed'], $fixers['eof_ending']),
  172. array($fixers['php_unit_strict'], $fixers['php_unit_construct']),
  173. array($fixers['unary_operators_spaces'], $fixers['logical_not_operators_with_spaces']),
  174. array($fixers['unary_operators_spaces'], $fixers['logical_not_operators_with_successor_space']),
  175. array($fixers['short_echo_tag'], $fixers['echo_to_print']), // tested also in: echo_to_print,short_echo_tag.test
  176. array($fixers['short_bool_cast'], $fixers['spaces_cast']), // tested also in: short_bool_cast,spaces_cast.test
  177. array($fixers['unneeded_control_parentheses'], $fixers['trailing_spaces']), // tested also in: trailing_spaces,unneeded_control_parentheses.test
  178. array($fixers['class_definition'], $fixers['trailing_spaces']), // tested also in: class_definition,trailing_spaces.test
  179. );
  180. $docFixerNames = array_filter(
  181. array_keys($fixers),
  182. function ($name) {
  183. return false !== strpos($name, 'phpdoc');
  184. }
  185. );
  186. // prepare bulk tests for phpdoc fixers to test that:
  187. // * `phpdoc_to_comment` is first
  188. // * `phpdoc_indent` is second
  189. // * `phpdoc_types` is third
  190. // * `phpdoc_scalar` is fourth
  191. // * `phpdoc_params` is last
  192. $cases[] = array($fixers['phpdoc_to_comment'], $fixers['phpdoc_indent']);
  193. $cases[] = array($fixers['phpdoc_indent'], $fixers['phpdoc_types']);
  194. $cases[] = array($fixers['phpdoc_types'], $fixers['phpdoc_scalar']);
  195. foreach ($docFixerNames as $docFixerName) {
  196. if (!in_array($docFixerName, array('phpdoc_to_comment', 'phpdoc_indent', 'phpdoc_types', 'phpdoc_scalar'), true)) {
  197. $cases[] = array($fixers['phpdoc_to_comment'], $fixers[$docFixerName]);
  198. $cases[] = array($fixers['phpdoc_indent'], $fixers[$docFixerName]);
  199. $cases[] = array($fixers['phpdoc_types'], $fixers[$docFixerName]);
  200. $cases[] = array($fixers['phpdoc_scalar'], $fixers[$docFixerName]);
  201. }
  202. if ('phpdoc_params' !== $docFixerName) {
  203. $cases[] = array($fixers[$docFixerName], $fixers['phpdoc_params']);
  204. }
  205. }
  206. return $cases;
  207. }
  208. public static function getFixerLevels()
  209. {
  210. return array(
  211. array(FixerInterface::NONE_LEVEL, 'none'),
  212. array(FixerInterface::PSR0_LEVEL, 'PSR-0'),
  213. array(FixerInterface::PSR1_LEVEL, 'PSR-1'),
  214. array(FixerInterface::PSR2_LEVEL, 'PSR-2'),
  215. array(FixerInterface::SYMFONY_LEVEL, 'symfony'),
  216. array(FixerInterface::CONTRIB_LEVEL, 'contrib'),
  217. );
  218. }
  219. /**
  220. * @dataProvider provideFixersDescriptionConsistencyCases
  221. */
  222. public function testFixersDescriptionConsistency(FixerInterface $fixer)
  223. {
  224. $this->assertRegExp('/^[A-Z@].*\.$/', $fixer->getDescription(), 'Description must start with capital letter or an @ and end with dot.');
  225. }
  226. public function provideFixersDescriptionConsistencyCases()
  227. {
  228. $fixer = new Fixer();
  229. $fixer->registerBuiltInFixers();
  230. $fixers = $fixer->getFixers();
  231. $cases = array();
  232. foreach ($fixers as $fixer) {
  233. $cases[] = array($fixer);
  234. }
  235. return $cases;
  236. }
  237. }