StdinTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. * @coversNothing
  22. * @group covers-nothing
  23. * @large
  24. */
  25. final class StdinTest extends AbstractSmokeTest
  26. {
  27. public function testFixingStdin(): void
  28. {
  29. $cwd = __DIR__.'/../..';
  30. $command = 'php php-cs-fixer fix --rules=@PSR2 --dry-run --diff --using-cache=no';
  31. $inputFile = 'tests/Fixtures/Integration/set/@PSR2.test-in.php';
  32. $fileResult = CommandExecutor::create("{$command} {$inputFile}", $cwd)->getResult(false);
  33. $stdinResult = CommandExecutor::create("{$command} - < {$inputFile}", $cwd)->getResult(false);
  34. static::assertSame($fileResult->getCode(), $stdinResult->getCode());
  35. $expectedError = str_replace(
  36. 'Paths from configuration file have been overridden by paths provided as command arguments.'."\n",
  37. '',
  38. $fileResult->getError()
  39. );
  40. static::assertSame($expectedError, $stdinResult->getError());
  41. $fileResult = $this->unifyFooter($fileResult->getOutput());
  42. $file = realpath($cwd).'/'.$inputFile;
  43. $path = str_replace('/', \DIRECTORY_SEPARATOR, $file);
  44. $fileResult = str_replace("\n--- ".$path."\n", "\n--- php://stdin\n", $fileResult);
  45. $fileResult = str_replace("\n+++ ".$path."\n", "\n+++ php://stdin\n", $fileResult);
  46. $path = str_replace('/', \DIRECTORY_SEPARATOR, basename(realpath($cwd)).'/'.$inputFile);
  47. $fileResult = Preg::replace(
  48. '#/?'.preg_quote($path, '#').'#',
  49. 'php://stdin',
  50. $fileResult
  51. );
  52. static::assertSame(
  53. $fileResult,
  54. $this->unifyFooter($stdinResult->getOutput())
  55. );
  56. }
  57. private function unifyFooter(string $output): string
  58. {
  59. return preg_replace(
  60. '/Checked all files in \d+\.\d+ seconds, \d+\.\d+ MB memory used/',
  61. 'Footer',
  62. $output
  63. );
  64. }
  65. }