PharTest.php 4.9 KB

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