ProcessHelperTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\Output\StreamOutput;
  15. use Symfony\Component\Console\Helper\ProcessHelper;
  16. use Symfony\Component\Process\Process;
  17. use Symfony\Component\Process\ProcessBuilder;
  18. class ProcessHelperTest extends TestCase
  19. {
  20. /**
  21. * @dataProvider provideCommandsAndOutput
  22. */
  23. public function testVariousProcessRuns($expected, $cmd, $verbosity, $error)
  24. {
  25. $helper = new ProcessHelper();
  26. $helper->setHelperSet(new HelperSet(array(new DebugFormatterHelper())));
  27. $output = $this->getOutputStream($verbosity);
  28. $helper->run($output, $cmd, $error);
  29. $this->assertEquals($expected, $this->getOutput($output));
  30. }
  31. public function testPassedCallbackIsExecuted()
  32. {
  33. $helper = new ProcessHelper();
  34. $helper->setHelperSet(new HelperSet(array(new DebugFormatterHelper())));
  35. $output = $this->getOutputStream(StreamOutput::VERBOSITY_NORMAL);
  36. $executed = false;
  37. $callback = function () use (&$executed) { $executed = true; };
  38. $helper->run($output, 'php -r "echo 42;"', null, $callback);
  39. $this->assertTrue($executed);
  40. }
  41. public function provideCommandsAndOutput()
  42. {
  43. $successOutputVerbose = <<<'EOT'
  44. RUN php -r "echo 42;"
  45. RES Command ran successfully
  46. EOT;
  47. $successOutputDebug = <<<'EOT'
  48. RUN php -r "echo 42;"
  49. OUT 42
  50. RES Command ran successfully
  51. EOT;
  52. $successOutputDebugWithTags = <<<'EOT'
  53. RUN php -r "echo '<info>42</info>';"
  54. OUT <info>42</info>
  55. RES Command ran successfully
  56. EOT;
  57. $successOutputProcessDebug = <<<'EOT'
  58. RUN 'php' '-r' 'echo 42;'
  59. OUT 42
  60. RES Command ran successfully
  61. EOT;
  62. $syntaxErrorOutputVerbose = <<<'EOT'
  63. RUN php -r "fwrite(STDERR, 'error message');usleep(50000);fwrite(STDOUT, 'out message');exit(252);"
  64. RES 252 Command did not run successfully
  65. EOT;
  66. $syntaxErrorOutputDebug = <<<'EOT'
  67. RUN php -r "fwrite(STDERR, 'error message');usleep(500000);fwrite(STDOUT, 'out message');exit(252);"
  68. ERR error message
  69. OUT out message
  70. RES 252 Command did not run successfully
  71. EOT;
  72. $errorMessage = 'An error occurred';
  73. $args = new ProcessBuilder(array('php', '-r', 'echo 42;'));
  74. $args = $args->getProcess()->getCommandLine();
  75. $successOutputProcessDebug = str_replace("'php' '-r' 'echo 42;'", $args, $successOutputProcessDebug);
  76. return array(
  77. array('', 'php -r "echo 42;"', StreamOutput::VERBOSITY_VERBOSE, null),
  78. array($successOutputVerbose, 'php -r "echo 42;"', StreamOutput::VERBOSITY_VERY_VERBOSE, null),
  79. array($successOutputDebug, 'php -r "echo 42;"', StreamOutput::VERBOSITY_DEBUG, null),
  80. array($successOutputDebugWithTags, 'php -r "echo \'<info>42</info>\';"', StreamOutput::VERBOSITY_DEBUG, null),
  81. array('', 'php -r "syntax error"', StreamOutput::VERBOSITY_VERBOSE, null),
  82. array($syntaxErrorOutputVerbose, 'php -r "fwrite(STDERR, \'error message\');usleep(50000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_VERY_VERBOSE, null),
  83. array($syntaxErrorOutputDebug, 'php -r "fwrite(STDERR, \'error message\');usleep(500000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_DEBUG, null),
  84. array($errorMessage.PHP_EOL, 'php -r "fwrite(STDERR, \'error message\');usleep(50000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_VERBOSE, $errorMessage),
  85. array($syntaxErrorOutputVerbose.$errorMessage.PHP_EOL, 'php -r "fwrite(STDERR, \'error message\');usleep(50000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_VERY_VERBOSE, $errorMessage),
  86. array($syntaxErrorOutputDebug.$errorMessage.PHP_EOL, 'php -r "fwrite(STDERR, \'error message\');usleep(500000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_DEBUG, $errorMessage),
  87. array($successOutputProcessDebug, array('php', '-r', 'echo 42;'), StreamOutput::VERBOSITY_DEBUG, null),
  88. array($successOutputDebug, new Process('php -r "echo 42;"'), StreamOutput::VERBOSITY_DEBUG, null),
  89. );
  90. }
  91. private function getOutputStream($verbosity)
  92. {
  93. return new StreamOutput(fopen('php://memory', 'r+', false), $verbosity, false);
  94. }
  95. private function getOutput(StreamOutput $output)
  96. {
  97. rewind($output->getStream());
  98. return stream_get_contents($output->getStream());
  99. }
  100. }