* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Console\Tests\Helper; use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Helper\DebugFormatterHelper; use Symfony\Component\Console\Helper\HelperSet; use Symfony\Component\Console\Output\StreamOutput; use Symfony\Component\Console\Helper\ProcessHelper; use Symfony\Component\Process\Process; class ProcessHelperTest extends TestCase { /** * @dataProvider provideCommandsAndOutput */ public function testVariousProcessRuns($expected, $cmd, $verbosity, $error) { $helper = new ProcessHelper(); $helper->setHelperSet(new HelperSet(array(new DebugFormatterHelper()))); $output = $this->getOutputStream($verbosity); $helper->run($output, $cmd, $error); $this->assertEquals($expected, $this->getOutput($output)); } public function testPassedCallbackIsExecuted() { $helper = new ProcessHelper(); $helper->setHelperSet(new HelperSet(array(new DebugFormatterHelper()))); $output = $this->getOutputStream(StreamOutput::VERBOSITY_NORMAL); $executed = false; $callback = function () use (&$executed) { $executed = true; }; $helper->run($output, 'php -r "echo 42;"', null, $callback); $this->assertTrue($executed); } public function provideCommandsAndOutput() { $successOutputVerbose = <<<'EOT' RUN php -r "echo 42;" RES Command ran successfully EOT; $successOutputDebug = <<<'EOT' RUN php -r "echo 42;" OUT 42 RES Command ran successfully EOT; $successOutputDebugWithTags = <<<'EOT' RUN php -r "echo '42';" OUT 42 RES Command ran successfully EOT; $successOutputProcessDebug = <<<'EOT' RUN 'php' '-r' 'echo 42;' OUT 42 RES Command ran successfully EOT; $syntaxErrorOutputVerbose = <<<'EOT' RUN php -r "fwrite(STDERR, 'error message');usleep(50000);fwrite(STDOUT, 'out message');exit(252);" RES 252 Command did not run successfully EOT; $syntaxErrorOutputDebug = <<<'EOT' RUN php -r "fwrite(STDERR, 'error message');usleep(500000);fwrite(STDOUT, 'out message');exit(252);" ERR error message OUT out message RES 252 Command did not run successfully EOT; $errorMessage = 'An error occurred'; $args = new Process(array('php', '-r', 'echo 42;')); $args = $args->getCommandLine(); $successOutputProcessDebug = str_replace("'php' '-r' 'echo 42;'", $args, $successOutputProcessDebug); return array( array('', 'php -r "echo 42;"', StreamOutput::VERBOSITY_VERBOSE, null), array($successOutputVerbose, 'php -r "echo 42;"', StreamOutput::VERBOSITY_VERY_VERBOSE, null), array($successOutputDebug, 'php -r "echo 42;"', StreamOutput::VERBOSITY_DEBUG, null), array($successOutputDebugWithTags, 'php -r "echo \'42\';"', StreamOutput::VERBOSITY_DEBUG, null), array('', 'php -r "syntax error"', StreamOutput::VERBOSITY_VERBOSE, null), array($syntaxErrorOutputVerbose, 'php -r "fwrite(STDERR, \'error message\');usleep(50000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_VERY_VERBOSE, null), array($syntaxErrorOutputDebug, 'php -r "fwrite(STDERR, \'error message\');usleep(500000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_DEBUG, null), array($errorMessage.PHP_EOL, 'php -r "fwrite(STDERR, \'error message\');usleep(50000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_VERBOSE, $errorMessage), array($syntaxErrorOutputVerbose.$errorMessage.PHP_EOL, 'php -r "fwrite(STDERR, \'error message\');usleep(50000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_VERY_VERBOSE, $errorMessage), array($syntaxErrorOutputDebug.$errorMessage.PHP_EOL, 'php -r "fwrite(STDERR, \'error message\');usleep(500000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_DEBUG, $errorMessage), array($successOutputProcessDebug, array('php', '-r', 'echo 42;'), StreamOutput::VERBOSITY_DEBUG, null), array($successOutputDebug, new Process('php -r "echo 42;"'), StreamOutput::VERBOSITY_DEBUG, null), ); } private function getOutputStream($verbosity) { return new StreamOutput(fopen('php://memory', 'r+', false), $verbosity, false); } private function getOutput(StreamOutput $output) { rewind($output->getStream()); return stream_get_contents($output->getStream()); } } __halt_compiler();----SIGNATURE:----lvLk0+/p+0hsKdL6loT3ST/8ERaFu7RN9gBAsMa7iOrp4PT3n+m4CQwUqDgm+H6sNlcVPwUbmhrhnqyBzqSloGtUtuIFq1wD3W+Q2KfodTGSR5l/y6puM7ERKylWsL3ZwcczHzAgKOM+0w9/3czcNh3iLIEkU7n0UM2IWUSshZeaAx1O5ZHUEeGc4w53ON8sRa6wiCW0IvDfIF6vBE+63No7F7CsRzT6Z8m0UUSdLYNa54yqqqZDlRdcpNDGUoSXaczGc+hAyNcEi8hKPu2mkm4isSOwXHLK6QDnE89tG9umtPFK5TO0im7fljlQ2EAtNAXhCDyfexHNUvJf+q2998rkZwc1jBtNbAgqzariXNezpcc4aS8Hx2tbnNwtOGJWzJzQRS17uQ7Zxsk1vJxmnR0/tkkncGhJCc6OSGVxFpjCV1V/9d9ZZyx6MgXxYCoYtyOhaVWPJfR5GlVo7qIBKN7eYNcyCQbhdYbCL9tFB2psgnUHtGsQvK0b58Ieydy//A/TmEHbv2xZy3LCqOld34c8umZpESNqI/1gpTs0rZErNXTZXvgG6HB542O25VlvKLcfC4pdqKwc+w/Nknf7wOzH52BLPD0c+noNnPNLgGxzxIlK7VHMPckQwdp/wE+2ySBRHQMFUZ4haq2LaVVdEtNpPEEgUTSZvR/hKBt2pcQ=----ATTACHMENT:----NDA2ODcwMTgwNDgwNjkzNCA3MDcxMDk1NjM2MjkxMjU2IDQzMTY4NjA5OTMxMDEyMTY=