ProcessHelperTest.php 4.6 KB

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