4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Tests\Helper;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Console\Helper\DebugFormatterHelper;
16 use Symfony\Component\Console\Helper\HelperSet;
17 use Symfony\Component\Console\Output\StreamOutput;
18 use Symfony\Component\Console\Helper\ProcessHelper;
19 use Symfony\Component\Process\Process;
20 use Symfony\Component\Process\ProcessBuilder;
22 class ProcessHelperTest extends TestCase
25 * @dataProvider provideCommandsAndOutput
27 public function testVariousProcessRuns($expected, $cmd, $verbosity, $error)
29 $helper = new ProcessHelper();
30 $helper->setHelperSet(new HelperSet(array(new DebugFormatterHelper())));
31 $output = $this->getOutputStream($verbosity);
32 $helper->run($output, $cmd, $error);
33 $this->assertEquals($expected, $this->getOutput($output));
36 public function testPassedCallbackIsExecuted()
38 $helper = new ProcessHelper();
39 $helper->setHelperSet(new HelperSet(array(new DebugFormatterHelper())));
40 $output = $this->getOutputStream(StreamOutput::VERBOSITY_NORMAL);
43 $callback = function () use (&$executed) { $executed = true; };
45 $helper->run($output, 'php -r "echo 42;"', null, $callback);
46 $this->assertTrue($executed);
49 public function provideCommandsAndOutput()
51 $successOutputVerbose = <<<'EOT'
53 RES Command ran successfully
56 $successOutputDebug = <<<'EOT'
59 RES Command ran successfully
62 $successOutputDebugWithTags = <<<'EOT'
63 RUN php -r "echo '<info>42</info>';"
65 RES Command ran successfully
68 $successOutputProcessDebug = <<<'EOT'
69 RUN 'php' '-r' 'echo 42;'
71 RES Command ran successfully
74 $syntaxErrorOutputVerbose = <<<'EOT'
75 RUN php -r "fwrite(STDERR, 'error message');usleep(50000);fwrite(STDOUT, 'out message');exit(252);"
76 RES 252 Command did not run successfully
79 $syntaxErrorOutputDebug = <<<'EOT'
80 RUN php -r "fwrite(STDERR, 'error message');usleep(500000);fwrite(STDOUT, 'out message');exit(252);"
83 RES 252 Command did not run successfully
87 $errorMessage = 'An error occurred';
88 $args = new ProcessBuilder(array('php', '-r', 'echo 42;'));
89 $args = $args->getProcess()->getCommandLine();
90 $successOutputProcessDebug = str_replace("'php' '-r' 'echo 42;'", $args, $successOutputProcessDebug);
93 array('', 'php -r "echo 42;"', StreamOutput::VERBOSITY_VERBOSE, null),
94 array($successOutputVerbose, 'php -r "echo 42;"', StreamOutput::VERBOSITY_VERY_VERBOSE, null),
95 array($successOutputDebug, 'php -r "echo 42;"', StreamOutput::VERBOSITY_DEBUG, null),
96 array($successOutputDebugWithTags, 'php -r "echo \'<info>42</info>\';"', StreamOutput::VERBOSITY_DEBUG, null),
97 array('', 'php -r "syntax error"', StreamOutput::VERBOSITY_VERBOSE, null),
98 array($syntaxErrorOutputVerbose, 'php -r "fwrite(STDERR, \'error message\');usleep(50000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_VERY_VERBOSE, null),
99 array($syntaxErrorOutputDebug, 'php -r "fwrite(STDERR, \'error message\');usleep(500000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_DEBUG, null),
100 array($errorMessage.PHP_EOL, 'php -r "fwrite(STDERR, \'error message\');usleep(50000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_VERBOSE, $errorMessage),
101 array($syntaxErrorOutputVerbose.$errorMessage.PHP_EOL, 'php -r "fwrite(STDERR, \'error message\');usleep(50000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_VERY_VERBOSE, $errorMessage),
102 array($syntaxErrorOutputDebug.$errorMessage.PHP_EOL, 'php -r "fwrite(STDERR, \'error message\');usleep(500000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_DEBUG, $errorMessage),
103 array($successOutputProcessDebug, array('php', '-r', 'echo 42;'), StreamOutput::VERBOSITY_DEBUG, null),
104 array($successOutputDebug, new Process('php -r "echo 42;"'), StreamOutput::VERBOSITY_DEBUG, null),
108 private function getOutputStream($verbosity)
110 return new StreamOutput(fopen('php://memory', 'r+', false), $verbosity, false);
113 private function getOutput(StreamOutput $output)
115 rewind($output->getStream());
117 return stream_get_contents($output->getStream());