FixCommandTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. /**
  26. * @var Application
  27. */
  28. private $application;
  29. protected function setUp()
  30. {
  31. parent::setUp();
  32. $this->application = new Application();
  33. }
  34. public function testEmptyRulesValue()
  35. {
  36. $this->expectException(
  37. \PhpCsFixer\ConfigurationException\InvalidConfigurationException::class
  38. );
  39. $this->expectExceptionMessageRegExp(
  40. '#^Empty rules value is not allowed\.$#'
  41. );
  42. $this->doTestExecute(
  43. ['--rules' => '']
  44. );
  45. }
  46. /**
  47. * @group legacy
  48. * @expectedDeprecation Expected "yes" or "no" for option "using-cache", other values are deprecated and support will be removed in 3.0. Got "not today", this implicitly set the option to "false".
  49. */
  50. public function testEmptyFormatValue()
  51. {
  52. $cmdTester = $this->doTestExecute(
  53. [
  54. '--using-cache' => 'not today',
  55. '--rules' => 'switch_case_semicolon_to_colon',
  56. ]
  57. );
  58. $this->assertSame(0, $cmdTester->getStatusCode(), "Expected exit code mismatch. Output:\n".$cmdTester->getDisplay());
  59. }
  60. /**
  61. * @param array $arguments
  62. *
  63. * @return CommandTester
  64. */
  65. private function doTestExecute(array $arguments)
  66. {
  67. $this->application->add(new FixCommand(new ToolInfo()));
  68. $command = $this->application->find('fix');
  69. $commandTester = new CommandTester($command);
  70. $commandTester->execute(
  71. array_merge(
  72. ['command' => $command->getName()],
  73. $this->getDefaultArguments(),
  74. $arguments
  75. ),
  76. [
  77. 'interactive' => false,
  78. 'decorated' => false,
  79. 'verbosity' => OutputInterface::VERBOSITY_DEBUG,
  80. ]
  81. );
  82. return $commandTester;
  83. }
  84. private function getDefaultArguments()
  85. {
  86. return [
  87. 'path' => [__FILE__],
  88. '--path-mode' => 'override',
  89. '--allow-risky' => true,
  90. '--dry-run' => true,
  91. '--using-cache' => 'no',
  92. '--show-progress' => 'none',
  93. ];
  94. }
  95. }