PharTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\Smoke;
  12. use Keradus\CliExecutor\CommandExecutor;
  13. use PhpCsFixer\Console\Application;
  14. use PhpCsFixer\Console\Command\DescribeCommand;
  15. use PhpCsFixer\Console\Command\HelpCommand;
  16. use PHPUnit\Framework\TestCase;
  17. use Symfony\Component\Console\Tester\CommandTester;
  18. /**
  19. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  20. *
  21. * @internal
  22. *
  23. * @coversNothing
  24. * @large
  25. */
  26. final class PharTest extends TestCase
  27. {
  28. private static $pharCwd;
  29. private static $pharName;
  30. private static $pharPath;
  31. public static function setUpBeforeClass()
  32. {
  33. self::$pharCwd = __DIR__.'/../..';
  34. self::$pharName = 'php-cs-fixer.phar';
  35. self::$pharPath = self::$pharCwd.'/'.self::$pharName;
  36. if (!file_exists(self::$pharPath)) {
  37. static::markTestSkipped('No phar file available.');
  38. }
  39. }
  40. public function testVersion()
  41. {
  42. $this->assertRegExp(
  43. '/^.* '.Application::VERSION.'(?: '.Application::VERSION_CODENAME.')? by .*$/',
  44. self::executePharCommand('--version')->getOutput()
  45. );
  46. }
  47. public function testReadme()
  48. {
  49. $this->assertSame(
  50. str_replace(
  51. HelpCOmmand::getLatestReleaseVersionFromChangeLog(),
  52. Application::VERSION,
  53. file_get_contents(__DIR__.'/../../README.rst')
  54. ),
  55. self::executePharCommand('readme')->getOutput()
  56. );
  57. }
  58. public function testDescribe()
  59. {
  60. $command = new DescribeCommand();
  61. $application = new Application();
  62. $application->add($command);
  63. $commandTester = new CommandTester($command);
  64. $commandTester->execute([
  65. 'command' => $command->getName(),
  66. 'name' => 'header_comment',
  67. ]);
  68. $this->assertSame(
  69. $commandTester->getDisplay(),
  70. self::executePharCommand('describe header_comment')->getOutput()
  71. );
  72. }
  73. public function testFix()
  74. {
  75. $this->assertSame(
  76. 0,
  77. self::executePharCommand('fix src/Config.php -vvv --dry-run --diff --using-cache=no 2>&1')->getCode()
  78. );
  79. }
  80. /**
  81. * @param string $params
  82. *
  83. * @return CliResult
  84. */
  85. private static function executePharCommand($params)
  86. {
  87. return CommandExecutor::create('php '.self::$pharName.' '.$params, self::$pharCwd)->getResult();
  88. }
  89. }