FixCommandTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 PHPUnit\Framework\TestCase;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Symfony\Component\Console\Tester\CommandTester;
  17. /**
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Console\Command\FixCommand
  21. */
  22. final class FixCommandTest extends TestCase
  23. {
  24. /**
  25. * @var Application
  26. */
  27. private $application;
  28. protected function setUp()
  29. {
  30. parent::setUp();
  31. $this->application = new Application();
  32. }
  33. public function testEmptyRulesValue()
  34. {
  35. $this->doTestExecute(
  36. ['--rules' => ''],
  37. [
  38. 'class' => 'PhpCsFixer\ConfigurationException\InvalidConfigurationException',
  39. 'regex' => '#^Empty rules value is not allowed\.$#',
  40. ]
  41. );
  42. }
  43. /**
  44. * @group legacy
  45. * @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".
  46. */
  47. public function testEmptyFormatValue()
  48. {
  49. $cmdTester = $this->doTestExecute(
  50. [
  51. '--using-cache' => 'not today',
  52. '--rules' => 'switch_case_semicolon_to_colon',
  53. ]
  54. );
  55. $this->assertSame(0, $cmdTester->getStatusCode(), "Expected exit code mismatch. Output:\n".$cmdTester->getDisplay());
  56. }
  57. /**
  58. * @param array $arguments
  59. * @param null|array $expectedException
  60. *
  61. * @return CommandTester
  62. */
  63. private function doTestExecute(array $arguments, array $expectedException = null)
  64. {
  65. $this->application->add(new FixCommand());
  66. $command = $this->application->find('fix');
  67. $commandTester = new CommandTester($command);
  68. if (null !== $expectedException) {
  69. $this->expectException($expectedException['class']);
  70. $this->expectExceptionMessageRegExp($expectedException['regex']);
  71. }
  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. private function getDefaultArguments()
  87. {
  88. return [
  89. 'path' => [__FILE__],
  90. '--path-mode' => 'override',
  91. '--allow-risky' => true,
  92. '--dry-run' => true,
  93. '--using-cache' => 'no',
  94. '--show-progress' => 'none',
  95. ];
  96. }
  97. }