TYPO3  7.6
ProcessHelperTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Symfony\Component\Console\Tests\Helper;
13 
18 use Symfony\Component\Process\Process;
19 
20 class ProcessHelperTest extends \PHPUnit_Framework_TestCase
21 {
25  public function testVariousProcessRuns($expected, $cmd, $verbosity, $error)
26  {
27  $helper = new ProcessHelper();
28  $helper->setHelperSet(new HelperSet(array(new DebugFormatterHelper())));
29  $output = $this->getOutputStream($verbosity);
30  $helper->run($output, $cmd, $error);
31  $this->assertEquals($expected, $this->getOutput($output));
32  }
33 
34  public function testPassedCallbackIsExecuted()
35  {
36  $helper = new ProcessHelper();
37  $helper->setHelperSet(new HelperSet(array(new DebugFormatterHelper())));
39 
40  $executed = false;
41  $callback = function () use (&$executed) { $executed = true; };
42 
43  $helper->run($output, 'php -r "echo 42;"', null, $callback);
44  $this->assertTrue($executed);
45  }
46 
47  public function provideCommandsAndOutput()
48  {
49  $successOutputVerbose = <<<EOT
50  RUN php -r "echo 42;"
51  RES Command ran successfully
52 
53 EOT;
54  $successOutputDebug = <<<EOT
55  RUN php -r "echo 42;"
56  OUT 42
57  RES Command ran successfully
58 
59 EOT;
60  $successOutputDebugWithTags = <<<EOT
61  RUN php -r "echo '<info>42</info>';"
62  OUT <info>42</info>
63  RES Command ran successfully
64 
65 EOT;
66  $successOutputProcessDebug = <<<EOT
67  RUN 'php' '-r' 'echo 42;'
68  OUT 42
69  RES Command ran successfully
70 
71 EOT;
72  $syntaxErrorOutputVerbose = <<<EOT
73  RUN php -r "fwrite(STDERR, 'error message');usleep(50000);fwrite(STDOUT, 'out message');exit(252);"
74  RES 252 Command did not run successfully
75 
76 EOT;
77  $syntaxErrorOutputDebug = <<<EOT
78  RUN php -r "fwrite(STDERR, 'error message');usleep(500000);fwrite(STDOUT, 'out message');exit(252);"
79  ERR error message
80  OUT out message
81  RES 252 Command did not run successfully
82 
83 EOT;
84 
85  $errorMessage = 'An error occurred';
86  if ('\\' === DIRECTORY_SEPARATOR) {
87  $successOutputProcessDebug = str_replace("'", '"', $successOutputProcessDebug);
88  }
89 
90  return array(
91  array('', 'php -r "echo 42;"', StreamOutput::VERBOSITY_VERBOSE, null),
92  array($successOutputVerbose, 'php -r "echo 42;"', StreamOutput::VERBOSITY_VERY_VERBOSE, null),
93  array($successOutputDebug, 'php -r "echo 42;"', StreamOutput::VERBOSITY_DEBUG, null),
94  array($successOutputDebugWithTags, 'php -r "echo \'<info>42</info>\';"', StreamOutput::VERBOSITY_DEBUG, null),
95  array('', 'php -r "syntax error"', StreamOutput::VERBOSITY_VERBOSE, null),
96  array($syntaxErrorOutputVerbose, 'php -r "fwrite(STDERR, \'error message\');usleep(50000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_VERY_VERBOSE, null),
97  array($syntaxErrorOutputDebug, 'php -r "fwrite(STDERR, \'error message\');usleep(500000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_DEBUG, null),
98  array($errorMessage.PHP_EOL, 'php -r "fwrite(STDERR, \'error message\');usleep(50000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_VERBOSE, $errorMessage),
99  array($syntaxErrorOutputVerbose.$errorMessage.PHP_EOL, 'php -r "fwrite(STDERR, \'error message\');usleep(50000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_VERY_VERBOSE, $errorMessage),
100  array($syntaxErrorOutputDebug.$errorMessage.PHP_EOL, 'php -r "fwrite(STDERR, \'error message\');usleep(500000);fwrite(STDOUT, \'out message\');exit(252);"', StreamOutput::VERBOSITY_DEBUG, $errorMessage),
101  array($successOutputProcessDebug, array('php', '-r', 'echo 42;'), StreamOutput::VERBOSITY_DEBUG, null),
102  array($successOutputDebug, new Process('php -r "echo 42;"'), StreamOutput::VERBOSITY_DEBUG, null),
103  );
104  }
105 
106  private function getOutputStream($verbosity)
107  {
108  return new StreamOutput(fopen('php://memory', 'r+', false), $verbosity, false);
109  }
110 
111  private function getOutput(StreamOutput $output)
112  {
113  rewind($output->getStream());
114 
115  return stream_get_contents($output->getStream());
116  }
117 }