PharTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. * @group covers-nothing
  25. * @large
  26. */
  27. final class PharTest extends AbstractSmokeTest
  28. {
  29. /**
  30. * @var string
  31. */
  32. private static $pharCwd;
  33. /**
  34. * @var string
  35. */
  36. private static $pharName;
  37. public static function setUpBeforeClass(): void
  38. {
  39. parent::setUpBeforeClass();
  40. self::$pharCwd = __DIR__.'/../..';
  41. self::$pharName = 'php-cs-fixer.phar';
  42. if (!file_exists(self::$pharCwd.'/'.self::$pharName)) {
  43. static::markTestSkippedOrFail('No phar file available.');
  44. }
  45. }
  46. public function testVersion(): void
  47. {
  48. /** @phpstan-ignore-next-line to avoid `Ternary operator condition is always true|false.` */
  49. $shouldExpectCodename = Application::VERSION_CODENAME ? 1 : 0;
  50. static::assertMatchesRegularExpression(
  51. 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),
  52. self::executePharCommand('--version')->getOutput()
  53. );
  54. }
  55. public function testDescribe(): void
  56. {
  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. static::assertSame(
  66. $commandTester->getDisplay(),
  67. self::executePharCommand('describe header_comment')->getOutput()
  68. );
  69. }
  70. public function testFix(): void
  71. {
  72. static::assertSame(
  73. 0,
  74. self::executePharCommand('fix src/Config.php -vvv --dry-run --diff --using-cache=no 2>&1')->getCode()
  75. );
  76. }
  77. public function testFixHelp(): void
  78. {
  79. static::assertSame(
  80. 0,
  81. self::executePharCommand('fix --help')->getCode()
  82. );
  83. }
  84. private static function executePharCommand(string $params): CliResult
  85. {
  86. return CommandExecutor::create('php '.self::$pharName.' '.$params, self::$pharCwd)->getResult();
  87. }
  88. }