ProcessHelperTest.php 4.7KB

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