StdinTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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\CommandExecutor;
  14. use PhpCsFixer\Preg;
  15. /**
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @requires OS Linux|Darwin
  21. *
  22. * @coversNothing
  23. *
  24. * @group covers-nothing
  25. *
  26. * @large
  27. */
  28. final class StdinTest extends AbstractSmokeTestCase
  29. {
  30. public function testFixingStdin(): void
  31. {
  32. $cwd = __DIR__.'/../..';
  33. $command = 'php php-cs-fixer fix --sequential --rules=@PSR2 --dry-run --diff --using-cache=no';
  34. $inputFile = 'tests/Fixtures/Integration/set/@PSR2.test-in.php';
  35. $fileResult = CommandExecutor::create("{$command} {$inputFile}", $cwd)->getResult(false);
  36. $stdinResult = CommandExecutor::create("{$command} - < {$inputFile}", $cwd)->getResult(false);
  37. self::assertSame($fileResult->getCode(), $stdinResult->getCode());
  38. $expectedError = str_replace(
  39. 'Paths from configuration file have been overridden by paths provided as command arguments.'."\n",
  40. '',
  41. $fileResult->getError()
  42. );
  43. self::assertSame($expectedError, $stdinResult->getError());
  44. $fileResult = $this->unifyFooter($fileResult->getOutput());
  45. $file = realpath($cwd).'/'.$inputFile;
  46. $path = str_replace('/', \DIRECTORY_SEPARATOR, $file);
  47. $fileResult = str_replace("\n--- ".$path."\n", "\n--- php://stdin\n", $fileResult);
  48. $fileResult = str_replace("\n+++ ".$path."\n", "\n+++ php://stdin\n", $fileResult);
  49. $fileResult = Preg::replace(
  50. '#/?'.preg_quote($inputFile, '#').'#',
  51. 'php://stdin',
  52. $fileResult
  53. );
  54. self::assertSame(
  55. $fileResult,
  56. $this->unifyFooter($stdinResult->getOutput())
  57. );
  58. }
  59. private function unifyFooter(string $output): string
  60. {
  61. return preg_replace(
  62. '/Found \d+ of \d+ files that can be fixed in \d+\.\d+ seconds, \d+\.\d+ MB memory used/',
  63. 'Footer',
  64. $output
  65. );
  66. }
  67. }