Yaffs site version 1.1
[yaffs-website] / vendor / symfony / console / Tests / Helper / ProcessHelperTest.php
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
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;
21
22 class ProcessHelperTest extends TestCase
23 {
24     /**
25      * @dataProvider provideCommandsAndOutput
26      */
27     public function testVariousProcessRuns($expected, $cmd, $verbosity, $error)
28     {
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));
34     }
35
36     public function testPassedCallbackIsExecuted()
37     {
38         $helper = new ProcessHelper();
39         $helper->setHelperSet(new HelperSet(array(new DebugFormatterHelper())));
40         $output = $this->getOutputStream(StreamOutput::VERBOSITY_NORMAL);
41
42         $executed = false;
43         $callback = function () use (&$executed) { $executed = true; };
44
45         $helper->run($output, 'php -r "echo 42;"', null, $callback);
46         $this->assertTrue($executed);
47     }
48
49     public function provideCommandsAndOutput()
50     {
51         $successOutputVerbose = <<<'EOT'
52   RUN  php -r "echo 42;"
53   RES  Command ran successfully
54
55 EOT;
56         $successOutputDebug = <<<'EOT'
57   RUN  php -r "echo 42;"
58   OUT  42
59   RES  Command ran successfully
60
61 EOT;
62         $successOutputDebugWithTags = <<<'EOT'
63   RUN  php -r "echo '<info>42</info>';"
64   OUT  <info>42</info>
65   RES  Command ran successfully
66
67 EOT;
68         $successOutputProcessDebug = <<<'EOT'
69   RUN  'php' '-r' 'echo 42;'
70   OUT  42
71   RES  Command ran successfully
72
73 EOT;
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
77
78 EOT;
79         $syntaxErrorOutputDebug = <<<'EOT'
80   RUN  php -r "fwrite(STDERR, 'error message');usleep(500000);fwrite(STDOUT, 'out message');exit(252);"
81   ERR  error message
82   OUT  out message
83   RES  252 Command did not run successfully
84
85 EOT;
86
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);
91
92         return array(
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),
105         );
106     }
107
108     private function getOutputStream($verbosity)
109     {
110         return new StreamOutput(fopen('php://memory', 'r+', false), $verbosity, false);
111     }
112
113     private function getOutput(StreamOutput $output)
114     {
115         rewind($output->getStream());
116
117         return stream_get_contents($output->getStream());
118     }
119 }