FixCommandTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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\Console\Command;
  12. use PhpCsFixer\Console\Application;
  13. use PhpCsFixer\Console\Command\FixCommand;
  14. use PhpCsFixer\Tests\TestCase;
  15. use PhpCsFixer\ToolInfo;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\Console\Tester\CommandTester;
  18. /**
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Console\Command\FixCommand
  22. */
  23. final class FixCommandTest extends TestCase
  24. {
  25. public function testEmptyRulesValue()
  26. {
  27. $this->expectException(
  28. \PhpCsFixer\ConfigurationException\InvalidConfigurationException::class
  29. );
  30. $this->expectExceptionMessageMatches(
  31. '#^Empty rules value is not allowed\.$#'
  32. );
  33. $this->doTestExecute(
  34. ['--rules' => '']
  35. );
  36. }
  37. public function testEmptyFormatValue()
  38. {
  39. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidConfigurationException::class);
  40. $this->expectExceptionMessage('Expected "yes" or "no" for option "using-cache", got "not today".');
  41. $cmdTester = $this->doTestExecute(
  42. [
  43. '--using-cache' => 'not today',
  44. '--rules' => 'switch_case_semicolon_to_colon',
  45. ]
  46. );
  47. $cmdTester->getStatusCode();
  48. }
  49. /**
  50. * @return CommandTester
  51. */
  52. private function doTestExecute(array $arguments)
  53. {
  54. $application = new Application();
  55. $application->add(new FixCommand(new ToolInfo()));
  56. $command = $application->find('fix');
  57. $commandTester = new CommandTester($command);
  58. $commandTester->execute(
  59. array_merge(
  60. ['command' => $command->getName()],
  61. $this->getDefaultArguments(),
  62. $arguments
  63. ),
  64. [
  65. 'interactive' => false,
  66. 'decorated' => false,
  67. 'verbosity' => OutputInterface::VERBOSITY_DEBUG,
  68. ]
  69. );
  70. return $commandTester;
  71. }
  72. private function getDefaultArguments()
  73. {
  74. return [
  75. 'path' => [__FILE__],
  76. '--path-mode' => 'override',
  77. '--allow-risky' => true,
  78. '--dry-run' => true,
  79. '--using-cache' => 'no',
  80. '--show-progress' => 'none',
  81. ];
  82. }
  83. }