PharTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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\Smoke;
  13. use Keradus\CliExecutor\CliResult;
  14. use Keradus\CliExecutor\CommandExecutor;
  15. use PhpCsFixer\Console\Application;
  16. use PhpCsFixer\Console\Command\DescribeCommand;
  17. use Symfony\Component\Console\Tester\CommandTester;
  18. /**
  19. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  20. *
  21. * @internal
  22. *
  23. * @coversNothing
  24. *
  25. * @group covers-nothing
  26. * @group legacy
  27. *
  28. * @large
  29. */
  30. final class PharTest extends AbstractSmokeTestCase
  31. {
  32. /**
  33. * @var string
  34. */
  35. private static $pharCwd;
  36. /**
  37. * @var string
  38. */
  39. private static $pharName;
  40. public static function setUpBeforeClass(): void
  41. {
  42. parent::setUpBeforeClass();
  43. self::$pharCwd = __DIR__.'/../..';
  44. self::$pharName = 'php-cs-fixer.phar';
  45. if (!file_exists(self::$pharCwd.'/'.self::$pharName)) {
  46. self::fail('No phar file available.');
  47. }
  48. }
  49. public function testVersion(): void
  50. {
  51. /** @phpstan-ignore-next-line to avoid `Ternary operator condition is always true|false.` */
  52. $shouldExpectCodename = Application::VERSION_CODENAME ? 1 : 0;
  53. self::assertMatchesRegularExpression(
  54. \sprintf("/^PHP CS Fixer (?<version>%s)(?<git_sha> \\([a-z0-9]+\\))?(?<codename> %s){%d}(?<by> by .*)\nPHP runtime: (?<php_version>\\d\\.\\d+\\..*)$/", Application::VERSION, Application::VERSION_CODENAME, $shouldExpectCodename),
  55. self::executePharCommand('--version')->getOutput()
  56. );
  57. }
  58. public function testDescribe(): void
  59. {
  60. // @TODO 4.0 Remove this expectations
  61. $this->expectDeprecation('Rule set "@PER" is deprecated. Use "@PER-CS" instead.');
  62. $this->expectDeprecation('Rule set "@PER:risky" is deprecated. Use "@PER-CS:risky" instead.');
  63. $command = new DescribeCommand();
  64. $application = new Application();
  65. $application->add($command);
  66. $commandTester = new CommandTester($command);
  67. $commandTester->execute([
  68. 'command' => $command->getName(),
  69. 'name' => 'header_comment',
  70. ]);
  71. self::assertSame(
  72. $commandTester->getDisplay(),
  73. self::executePharCommand('describe header_comment')->getOutput()
  74. );
  75. }
  76. public function testFixSequential(): void
  77. {
  78. $command = self::executePharCommand('fix src/Config.php -vvv --dry-run --sequential --diff --using-cache=no 2>&1');
  79. self::assertSame(0, $command->getCode());
  80. self::assertMatchesRegularExpression(
  81. '/Running analysis on 1 core sequentially/',
  82. $command->getOutput()
  83. );
  84. }
  85. public function testFixParallel(): void
  86. {
  87. $command = self::executePharCommand('fix src/Config.php -vvv --dry-run --diff --using-cache=no --config=.php-cs-fixer.dist.php 2>&1');
  88. self::assertSame(0, $command->getCode());
  89. self::assertMatchesRegularExpression(
  90. '/Running analysis on [0-9]+ cores with [0-9]+ files per process/',
  91. $command->getOutput()
  92. );
  93. }
  94. public function testFixHelp(): void
  95. {
  96. self::assertSame(
  97. 0,
  98. self::executePharCommand('fix --help')->getCode()
  99. );
  100. }
  101. /**
  102. * @return iterable<array{string}>
  103. */
  104. public static function provideReportCases(): iterable
  105. {
  106. yield ['no'];
  107. yield ['yes'];
  108. }
  109. /**
  110. * @dataProvider provideReportCases
  111. */
  112. public function testReport(string $usingCache): void
  113. {
  114. try {
  115. $json = self::executePharCommand(\sprintf(
  116. 'fix %s --dry-run --sequential --format=json --rules=\'%s\' --using-cache=%s',
  117. __FILE__,
  118. json_encode(['concat_space' => ['spacing' => 'one']], JSON_THROW_ON_ERROR),
  119. $usingCache,
  120. ))->getOutput();
  121. self::assertJson($json);
  122. $report = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
  123. self::assertIsArray($report);
  124. self::assertArrayHasKey('files', $report);
  125. self::assertCount(1, $report['files']);
  126. self::assertArrayHasKey(0, $report['files']);
  127. self::assertSame(
  128. 'tests/Smoke/PharTest.php',
  129. $report['files'][0]['name'],
  130. );
  131. } catch (\Throwable $exception) {
  132. throw $exception;
  133. } finally {
  134. $cacheFile = __DIR__.'/../../.php-cs-fixer.cache';
  135. if (file_exists($cacheFile)) {
  136. unlink($cacheFile);
  137. }
  138. }
  139. }
  140. private static function executePharCommand(string $params): CliResult
  141. {
  142. return CommandExecutor::create('php '.self::$pharName.' '.$params, self::$pharCwd)->getResult(false);
  143. }
  144. }