ProcessHelperTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Tests\Helper;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Helper\DebugFormatterHelper;
  13. use Symfony\Component\Console\Helper\HelperSet;
  14. use Symfony\Component\Console\Helper\ProcessHelper;
  15. use Symfony\Component\Console\Output\StreamOutput;
  16. use Symfony\Component\Process\Process;
  17. class ProcessHelperTest extends TestCase
  18. {
  19. /**
  20. * @dataProvider provideCommandsAndOutput
  21. */
  22. public function testVariousProcessRuns($expected, $cmd, $verbosity, $error)
  23. {
  24. if (\is_string($cmd)) {
  25. $cmd = method_exists(Process::class, 'fromShellCommandline') ? Process::fromShellCommandline($cmd) : new Process($cmd);
  26. }
  27. $helper = new ProcessHelper();
  28. $helper->setHelperSet(new HelperSet([new DebugFormatterHelper()]));
  29. $output = $this->getOutputStream($verbosity);
  30. $helper->run($output, $cmd, $error);
  31. $this->assertEquals($expected, $this->getOutput($output));
  32. }
  33. public function testPassedCallbackIsExecuted()
  34. {
  35. $helper = new ProcessHelper();
  36. $helper->setHelperSet(new HelperSet([new DebugFormatterHelper()]));
  37. $output = $this->getOutputStream(StreamOutput::VERBOSITY_NORMAL);
  38. $executed = false;
  39. $callback = function () use (&$executed) { $executed = true; };
  40. $helper->run($output, ['php', '-r', 'echo 42;'], null, $callback);
  41. $this->assertTrue($executed);
  42. }
  43. public function provideCommandsAndOutput()
  44. {
  45. $successOutputVerbose = <<<'EOT'
  46. RUN php -r "echo 42;"
  47. RES Command ran successfully
  48. EOT;
  49. $successOutputDebug = <<<'EOT'
  50. RUN php -r "echo 42;"
  51. OUT 42
  52. RES Command ran successfully
  53. EOT;
  54. $successOutputDebugWithTags = <<<'EOT'
  55. RUN php -r "echo '<info>42</info>';"
  56. OUT <info>42</info>
  57. RES Command ran successfully
  58. EOT;
  59. $successOutputProcessDebug = <<<'EOT'
  60. RUN 'php' '-r' 'echo 42;'
  61. OUT 42
  62. RES Command ran successfully
  63. EOT;
  64. $syntaxErrorOutputVerbose = <<<'EOT'
  65. RUN php -r "fwrite(STDERR, 'error message');usleep(50000);fwrite(STDOUT, 'out message');exit(252);"
  66. RES 252 Command did not run successfully
  67. EOT;
  68. $syntaxErrorOutputDebug = <<<'EOT'
  69. RUN php -r "fwrite(STDERR, 'error message');usleep(500000);fwrite(STDOUT, 'out message');exit(252);"
  70. ERR error message
  71. OUT out message
  72. RES 252 Command did not run successfully
  73. EOT;
  74. $PHP = '\\' === \DIRECTORY_SEPARATOR ? '"!PHP!"' : '"$PHP"';
  75. $successOutputPhp = <<<EOT
  76. RUN php -r $PHP
  77. OUT 42
  78. RES Command ran successfully
  79. EOT;
  80. $errorMessage = 'An error occurred';
  81. $args = new Process(['php', '-r', 'echo 42;']);
  82. $args = $args->getCommandLine();
  83. $successOutputProcessDebug = str_replace("'php' '-r' 'echo 42;'", $args, $successOutputProcessDebug);
  84. $fromShellCommandline = method_exists(Process::class, 'fromShellCommandline') ? [Process::class, 'fromShellCommandline'] : function ($cmd) { return new Process($cmd); };
  85. return [
  86. ['', 'php -r "echo 42;"', StreamOutput::VERBOSITY_VERBOSE, null],
  87. [$successOutputVerbose, 'php -r "echo 42;"', StreamOutput::VERBOSITY_VERY_VERBOSE, null],
  88. [$successOutputDebug, 'php -r "echo 42;"', StreamOutput::VERBOSITY_DEBUG, null],
  89. [$successOutputDebugWithTags, 'php -r "echo \'<info>42</info>\';"', StreamOutput::VERBOSITY_DEBUG, null],
  90. ['', 'php -r "syntax error"', StreamOutput::VERBOSITY_VERBOSE, null],
  91. [$syntaxErrorOutputVerbose, 'php -r "fwrite(STDERR, \'error message\');usleep(50000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_VERY_VERBOSE, null],
  92. [$syntaxErrorOutputDebug, 'php -r "fwrite(STDERR, \'error message\');usleep(500000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_DEBUG, null],
  93. [$errorMessage.PHP_EOL, 'php -r "fwrite(STDERR, \'error message\');usleep(50000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_VERBOSE, $errorMessage],
  94. [$syntaxErrorOutputVerbose.$errorMessage.PHP_EOL, 'php -r "fwrite(STDERR, \'error message\');usleep(50000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_VERY_VERBOSE, $errorMessage],
  95. [$syntaxErrorOutputDebug.$errorMessage.PHP_EOL, 'php -r "fwrite(STDERR, \'error message\');usleep(500000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_DEBUG, $errorMessage],
  96. [$successOutputProcessDebug, ['php', '-r', 'echo 42;'], StreamOutput::VERBOSITY_DEBUG, null],
  97. [$successOutputDebug, $fromShellCommandline('php -r "echo 42;"'), StreamOutput::VERBOSITY_DEBUG, null],
  98. [$successOutputProcessDebug, [new Process(['php', '-r', 'echo 42;'])], StreamOutput::VERBOSITY_DEBUG, null],
  99. [$successOutputPhp, [$fromShellCommandline('php -r '.$PHP), 'PHP' => 'echo 42;'], StreamOutput::VERBOSITY_DEBUG, null],
  100. ];
  101. }
  102. private function getOutputStream($verbosity)
  103. {
  104. return new StreamOutput(fopen('php://memory', 'r+', false), $verbosity, false);
  105. }
  106. private function getOutput(StreamOutput $output)
  107. {
  108. rewind($output->getStream());
  109. return stream_get_contents($output->getStream());
  110. }
  111. }