FixCommandTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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\Console\Command;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Output\NullOutput;
  13. use Symfony\Component\Stopwatch\Stopwatch;
  14. use Symfony\CS\Console\Command\FixCommand;
  15. use Symfony\CS\Error\Error;
  16. use Symfony\CS\Error\ErrorsManager;
  17. use Symfony\CS\Fixer;
  18. /**
  19. * @author Andreas Möller <am@localheinz.com>
  20. *
  21. * @internal
  22. */
  23. final class FixCommandTest extends \PHPUnit_Framework_TestCase
  24. {
  25. public function testCommandHasCacheFileOption()
  26. {
  27. $command = new FixCommand();
  28. $definition = $command->getDefinition();
  29. $this->assertTrue($definition->hasOption('cache-file'));
  30. $option = $definition->getOption('cache-file');
  31. $this->assertNull($option->getShortcut());
  32. $this->assertTrue($option->isValueRequired());
  33. $this->assertSame('The path to the cache file', $option->getDescription());
  34. $this->assertNull($option->getDefault());
  35. }
  36. public function testExitCodeDryRun()
  37. {
  38. $command = new FixCommand();
  39. $input = $this->getInputMock(array(
  40. 'dry-run' => true,
  41. ));
  42. $exitCode = $command->run(
  43. $input,
  44. new NullOutput()
  45. );
  46. $this->assertSame(0, $exitCode);
  47. }
  48. public function testExitCodeActualRun()
  49. {
  50. $fixer = $this->getFixerMock();
  51. $command = new FixCommand($fixer);
  52. $input = $this->getInputMock(array(
  53. 'dry-run' => false,
  54. ));
  55. $exitCode = $command->run(
  56. $input,
  57. new NullOutput()
  58. );
  59. $this->assertSame(0, $exitCode);
  60. }
  61. public function testExitCodeDryRunWithChangedFiles()
  62. {
  63. $fixer = $this->getFixerMock(array(
  64. 'Changed.php',
  65. ));
  66. $command = new FixCommand($fixer);
  67. $input = $this->getInputMock(array(
  68. 'dry-run' => true,
  69. ));
  70. $exitCode = $command->run(
  71. $input,
  72. new NullOutput()
  73. );
  74. $this->assertSame(8, $exitCode);
  75. }
  76. public function testExitCodeActualRunWithChangedFiles()
  77. {
  78. $fixer = $this->getFixerMock(array(
  79. 'Changed.php',
  80. ));
  81. $command = new FixCommand($fixer);
  82. $input = $this->getInputMock(array(
  83. 'dry-run' => false,
  84. ));
  85. $exitCode = $command->run(
  86. $input,
  87. new NullOutput()
  88. );
  89. $this->assertSame(0, $exitCode);
  90. }
  91. public function testExitCodeDryRunWithInvalidFiles()
  92. {
  93. $errorsManager = new ErrorsManager();
  94. $errorsManager->report(new Error(
  95. Error::TYPE_INVALID,
  96. 'Invalid.php'
  97. ));
  98. $fixer = $this->getFixerMock(array(), $errorsManager);
  99. $command = new FixCommand($fixer);
  100. $input = $this->getInputMock(array(
  101. 'dry-run' => true,
  102. ));
  103. $exitCode = $command->run(
  104. $input,
  105. new NullOutput()
  106. );
  107. $this->assertSame(4, $exitCode);
  108. }
  109. public function testExitCodeActualRunWithInvalidFiles()
  110. {
  111. $errorsManager = new ErrorsManager();
  112. $errorsManager->report(new Error(
  113. Error::TYPE_INVALID,
  114. 'Invalid.php'
  115. ));
  116. $fixer = $this->getFixerMock(array(), $errorsManager);
  117. $command = new FixCommand($fixer);
  118. $input = $this->getInputMock(array(
  119. 'dry-run' => false,
  120. ));
  121. $exitCode = $command->run(
  122. $input,
  123. new NullOutput()
  124. );
  125. $this->assertSame(0, $exitCode);
  126. }
  127. public function testExitCodeDryRunWithChangedAndInvalidFiles()
  128. {
  129. $errorsManager = new ErrorsManager();
  130. $errorsManager->report(new Error(
  131. Error::TYPE_INVALID,
  132. 'Invalid.php'
  133. ));
  134. $fixer = $this->getFixerMock(
  135. array(
  136. 'Changed.php',
  137. ),
  138. $errorsManager
  139. );
  140. $command = new FixCommand($fixer);
  141. $input = $this->getInputMock(array(
  142. 'dry-run' => true,
  143. ));
  144. $exitCode = $command->run(
  145. $input,
  146. new NullOutput()
  147. );
  148. $this->assertSame(12, $exitCode);
  149. }
  150. public function testExitCodeActualRunWithChangedAndInvalidFiles()
  151. {
  152. $errorsManager = new ErrorsManager();
  153. $errorsManager->report(new Error(
  154. Error::TYPE_INVALID,
  155. 'Invalid.php'
  156. ));
  157. $fixer = $this->getFixerMock(
  158. array(
  159. 'Changed.php',
  160. ),
  161. $errorsManager
  162. );
  163. $command = new FixCommand($fixer);
  164. $input = $this->getInputMock(array(
  165. 'dry-run' => false,
  166. ));
  167. $exitCode = $command->run(
  168. $input,
  169. new NullOutput()
  170. );
  171. $this->assertSame(0, $exitCode);
  172. }
  173. /**
  174. * @param array $options
  175. *
  176. * @return InputInterface
  177. */
  178. private function getInputMock(array $options = array())
  179. {
  180. $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
  181. $arguments = array(
  182. 'path' => __DIR__.'/../../Fixtures/FixCommand',
  183. );
  184. $input
  185. ->expects($this->any())
  186. ->method('getArgument')
  187. ->willReturnCallback(function ($name) use ($arguments) {
  188. if (array_key_exists($name, $arguments)) {
  189. return $arguments[$name];
  190. }
  191. })
  192. ;
  193. $options = array_merge(
  194. array(
  195. 'config' => __DIR__.'/../../Fixtures/FixCommand/.phpcs',
  196. 'format' => 'txt',
  197. ),
  198. $options
  199. );
  200. $input
  201. ->expects($this->any())
  202. ->method('getOption')
  203. ->willReturnCallback(function ($name) use ($options) {
  204. if (array_key_exists($name, $options)) {
  205. return $options[$name];
  206. }
  207. })
  208. ;
  209. return $input;
  210. }
  211. /**
  212. * @param array $changed
  213. * @param ErrorsManager $errorsManager
  214. *
  215. * @return Fixer
  216. */
  217. private function getFixerMock(array $changed = array(), ErrorsManager $errorsManager = null)
  218. {
  219. $fixer = $this->getMockBuilder('Symfony\CS\Fixer')
  220. ->disableOriginalConstructor()
  221. ->getMock()
  222. ;
  223. $fixer
  224. ->expects($this->once())
  225. ->method('fix')
  226. ->with($this->anything())
  227. ->willReturn($changed)
  228. ;
  229. $fixer
  230. ->expects($this->any())
  231. ->method('getConfigs')
  232. ->willReturn(array())
  233. ;
  234. $fixer
  235. ->expects($this->any())
  236. ->method('getStopwatch')
  237. ->willReturn(new Stopwatch())
  238. ;
  239. $errorsManager = $errorsManager ?: new ErrorsManager();
  240. $fixer
  241. ->expects($this->any())
  242. ->method('getErrorsManager')
  243. ->willReturn($errorsManager)
  244. ;
  245. return $fixer;
  246. }
  247. }