FixCommandTest.php 3.0 KB

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