FixCommandTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Console\Command;
  13. use PhpCsFixer\ConfigurationException\InvalidConfigurationException;
  14. use PhpCsFixer\Console\Application;
  15. use PhpCsFixer\Console\Command\FixCommand;
  16. use PhpCsFixer\Tests\TestCase;
  17. use PhpCsFixer\ToolInfo;
  18. use Symfony\Component\Console\Output\OutputInterface;
  19. use Symfony\Component\Console\Tester\CommandTester;
  20. /**
  21. * @internal
  22. *
  23. * @covers \PhpCsFixer\Console\Command\FixCommand
  24. */
  25. final class FixCommandTest extends TestCase
  26. {
  27. public function testEmptyRulesValue(): void
  28. {
  29. $this->expectException(
  30. InvalidConfigurationException::class
  31. );
  32. $this->expectExceptionMessageMatches(
  33. '#^Empty rules value is not allowed\.$#'
  34. );
  35. $this->doTestExecute(
  36. ['--rules' => '']
  37. );
  38. }
  39. public function testEmptyFormatValue(): void
  40. {
  41. $this->expectException(InvalidConfigurationException::class);
  42. $this->expectExceptionMessage('Expected "yes" or "no" for option "using-cache", got "not today".');
  43. $cmdTester = $this->doTestExecute(
  44. [
  45. '--using-cache' => 'not today',
  46. '--rules' => 'switch_case_semicolon_to_colon',
  47. ]
  48. );
  49. $cmdTester->getStatusCode();
  50. }
  51. private function doTestExecute(array $arguments): CommandTester
  52. {
  53. $application = new Application();
  54. $application->add(new FixCommand(new ToolInfo()));
  55. $command = $application->find('fix');
  56. $commandTester = new CommandTester($command);
  57. $commandTester->execute(
  58. array_merge(
  59. ['command' => $command->getName()],
  60. $this->getDefaultArguments(),
  61. $arguments
  62. ),
  63. [
  64. 'interactive' => false,
  65. 'decorated' => false,
  66. 'verbosity' => OutputInterface::VERBOSITY_DEBUG,
  67. ]
  68. );
  69. return $commandTester;
  70. }
  71. private function getDefaultArguments(): array
  72. {
  73. return [
  74. 'path' => [__FILE__],
  75. '--path-mode' => 'override',
  76. '--allow-risky' => true,
  77. '--dry-run' => true,
  78. '--using-cache' => 'no',
  79. '--show-progress' => 'none',
  80. ];
  81. }
  82. }