@@ -23,17 +23,65 @@ class ProcessHelperTest extends TestCase
2323 /**
2424 * @dataProvider provideCommandsAndOutput
2525 */
26- public function testVariousProcessRuns (string $ expected , Process |string |array $ cmd , int $ verbosity , ?string $ error )
26+ public function testVariousProcessRuns (array $ expectedOutputLines , bool $ successful , Process |string |array $ cmd , int $ verbosity , ?string $ error )
2727 {
2828 if (\is_string ($ cmd )) {
2929 $ cmd = Process::fromShellCommandline ($ cmd );
3030 }
3131
3232 $ helper = new ProcessHelper ();
3333 $ helper ->setHelperSet (new HelperSet ([new DebugFormatterHelper ()]));
34- $ output = $ this ->getOutputStream ($ verbosity );
35- $ helper ->run ($ output , $ cmd , $ error );
36- $ this ->assertEquals ($ expected , $ this ->getOutput ($ output ));
34+ $ outputStream = $ this ->getOutputStream ($ verbosity );
35+ $ helper ->run ($ outputStream , $ cmd , $ error );
36+
37+ $ expectedLines = 1 + \count ($ expectedOutputLines );
38+
39+ if (StreamOutput::VERBOSITY_VERY_VERBOSE <= $ verbosity ) {
40+ // the executed command and the result are displayed
41+ $ expectedLines += 2 ;
42+ }
43+
44+ if (null !== $ error ) {
45+ ++$ expectedLines ;
46+ }
47+
48+ $ output = explode ("\n" , $ this ->getOutput ($ outputStream ));
49+
50+ $ this ->assertCount ($ expectedLines , $ output );
51+
52+ // remove the trailing newline
53+ array_pop ($ output );
54+
55+ if (null !== $ error ) {
56+ $ this ->assertSame ($ error , array_pop ($ output ));
57+ }
58+
59+ if (StreamOutput::VERBOSITY_VERY_VERBOSE <= $ verbosity ) {
60+ if ($ cmd instanceof Process) {
61+ $ expectedCommandLine = $ cmd ->getCommandLine ();
62+ } elseif (\is_array ($ cmd ) && $ cmd [0 ] instanceof Process) {
63+ $ expectedCommandLine = $ cmd [0 ]->getCommandLine ();
64+ } elseif (\is_array ($ cmd )) {
65+ $ expectedCommandLine = (new Process ($ cmd ))->getCommandLine ();
66+ } else {
67+ $ expectedCommandLine = $ cmd ;
68+ }
69+
70+ $ this ->assertSame (' RUN ' .$ expectedCommandLine , array_shift ($ output ));
71+
72+ if ($ successful ) {
73+ $ this ->assertSame (' RES Command ran successfully ' , array_pop ($ output ));
74+ } else {
75+ $ this ->assertSame (' RES 252 Command did not run successfully ' , array_pop ($ output ));
76+ }
77+ }
78+
79+ if ([] !== $ expectedOutputLines ) {
80+ sort ($ expectedOutputLines );
81+ sort ($ output );
82+
83+ $ this ->assertEquals ($ expectedOutputLines , $ output );
84+ }
3785 }
3886
3987 public function testPassedCallbackIsExecuted ()
@@ -51,70 +99,23 @@ public function testPassedCallbackIsExecuted()
5199
52100 public static function provideCommandsAndOutput (): array
53101 {
54- $ successOutputVerbose = <<<'EOT'
55- RUN php -r "echo 42;"
56- RES Command ran successfully
57-
58- EOT;
59- $ successOutputDebug = <<<'EOT'
60- RUN php -r "echo 42;"
61- OUT 42
62- RES Command ran successfully
63-
64- EOT;
65- $ successOutputDebugWithTags = <<<'EOT'
66- RUN php -r "echo '<info>42</info>';"
67- OUT <info>42</info>
68- RES Command ran successfully
69-
70- EOT;
71- $ successOutputProcessDebug = <<<'EOT'
72- RUN 'php' '-r' 'echo 42;'
73- OUT 42
74- RES Command ran successfully
75-
76- EOT;
77- $ syntaxErrorOutputVerbose = <<<'EOT'
78- RUN php -r "fwrite(STDERR, 'error message');usleep(50000);fwrite(STDOUT, 'out message');exit(252);"
79- RES 252 Command did not run successfully
80-
81- EOT;
82- $ syntaxErrorOutputDebug = <<<'EOT'
83- RUN php -r "fwrite(STDERR, 'error message');usleep(500000);fwrite(STDOUT, 'out message');exit(252);"
84- ERR error message
85- OUT out message
86- RES 252 Command did not run successfully
87-
88- EOT;
89-
90102 $ PHP = '\\' === \DIRECTORY_SEPARATOR ? '"!PHP!" ' : '"$PHP" ' ;
91- $ successOutputPhp = <<<EOT
92- RUN php -r $ PHP
93- OUT 42
94- RES Command ran successfully
95-
96- EOT ;
97-
98- $ errorMessage = 'An error occurred ' ;
99- $ args = new Process (['php ' , '-r ' , 'echo 42; ' ]);
100- $ args = $ args ->getCommandLine ();
101- $ successOutputProcessDebug = str_replace ("'php' '-r' 'echo 42;' " , $ args , $ successOutputProcessDebug );
102103
103104 return [
104- ['' , 'php -r "echo 42;" ' , StreamOutput::VERBOSITY_VERBOSE , null ],
105- [$ successOutputVerbose , 'php -r "echo 42;" ' , StreamOutput::VERBOSITY_VERY_VERBOSE , null ],
106- [$ successOutputDebug , 'php -r "echo 42;" ' , StreamOutput::VERBOSITY_DEBUG , null ],
107- [$ successOutputDebugWithTags , 'php -r "echo \'<info>42</info> \';" ' , StreamOutput::VERBOSITY_DEBUG , null ],
108- ['' , 'php -r "syntax error" ' , StreamOutput::VERBOSITY_VERBOSE , null ],
109- [$ syntaxErrorOutputVerbose , 'php -r "fwrite(STDERR, \'error message \');usleep(50000);fwrite(STDOUT, \'out message \');exit(252);" ' , StreamOutput::VERBOSITY_VERY_VERBOSE , null ],
110- [$ syntaxErrorOutputDebug , 'php -r "fwrite(STDERR, \'error message \');usleep(500000 );fwrite(STDOUT, \'out message \');exit(252);" ' , StreamOutput::VERBOSITY_DEBUG , null ],
111- [$ errorMessage .\ PHP_EOL , 'php -r "fwrite(STDERR, \'error message \');usleep(50000);fwrite(STDOUT, \'out message \');exit(252);" ' , StreamOutput::VERBOSITY_VERBOSE , $ errorMessage ],
112- [$ syntaxErrorOutputVerbose . $ errorMessage .\ PHP_EOL , 'php -r "fwrite(STDERR, \'error message \');usleep(50000);fwrite(STDOUT, \'out message \');exit(252);" ' , StreamOutput::VERBOSITY_VERY_VERBOSE , $ errorMessage ],
113- [$ syntaxErrorOutputDebug . $ errorMessage .\ PHP_EOL , 'php -r "fwrite(STDERR, \'error message \');usleep(500000);fwrite(STDOUT, \'out message \');exit(252);" ' , StreamOutput::VERBOSITY_DEBUG , $ errorMessage ],
114- [$ successOutputProcessDebug , ['php ' , '-r ' , 'echo 42; ' ], StreamOutput::VERBOSITY_DEBUG , null ],
115- [$ successOutputDebug , Process::fromShellCommandline ('php -r "echo 42;" ' ), StreamOutput::VERBOSITY_DEBUG , null ],
116- [$ successOutputProcessDebug , [new Process (['php ' , '-r ' , 'echo 42; ' ])], StreamOutput::VERBOSITY_DEBUG , null ],
117- [$ successOutputPhp , [Process::fromShellCommandline ('php -r ' .$ PHP ), 'PHP ' => 'echo 42; ' ], StreamOutput::VERBOSITY_DEBUG , null ],
105+ [[], true , 'php -r "echo 42;" ' , StreamOutput::VERBOSITY_VERBOSE , null ],
106+ [[], true , 'php -r "echo 42;" ' , StreamOutput::VERBOSITY_VERY_VERBOSE , null ],
107+ [[ ' OUT 42 ' ], true , 'php -r "echo 42;" ' , StreamOutput::VERBOSITY_DEBUG , null ],
108+ [[ ' OUT <info>42</info> ' ], true , 'php -r "echo \'<info>42</info> \';" ' , StreamOutput::VERBOSITY_DEBUG , null ],
109+ [[], false , 'php -r "syntax error" ' , StreamOutput::VERBOSITY_VERBOSE , null ],
110+ [[], false , 'php -r "fwrite(STDERR, \'error message \');usleep(50000);fwrite(STDOUT, \'out message \');exit(252);" ' , StreamOutput::VERBOSITY_VERY_VERBOSE , null ],
111+ [[ ' ERR error message ' , ' OUT out message ' ], false , ' php -r "fwrite(STDERR, \'error message \');usleep(50000 );fwrite(STDOUT, \'out message \');exit(252);" ' , StreamOutput::VERBOSITY_DEBUG , null ],
112+ [[], false , 'php -r "fwrite(STDERR, \'error message \');usleep(50000);fwrite(STDOUT, \'out message \');exit(252);" ' , StreamOutput::VERBOSITY_VERBOSE , ' An error occurred ' ],
113+ [[], false , 'php -r "fwrite(STDERR, \'error message \');usleep(50000);fwrite(STDOUT, \'out message \');exit(252);" ' , StreamOutput::VERBOSITY_VERY_VERBOSE , ' An error occurred ' ],
114+ [[ ' ERR error message ' , ' OUT out message ' ], false , ' php -r "fwrite(STDERR, \'error message \');usleep(500000);fwrite(STDOUT, \'out message \');exit(252);" ' , StreamOutput::VERBOSITY_DEBUG , ' An error occurred ' ],
115+ [[ ' OUT 42 ' ], true , ['php ' , '-r ' , 'echo 42; ' ], StreamOutput::VERBOSITY_DEBUG , null ],
116+ [[ ' OUT 42 ' ], true , Process::fromShellCommandline ('php -r "echo 42;" ' ), StreamOutput::VERBOSITY_DEBUG , null ],
117+ [[ ' OUT 42 ' ], true , [new Process (['php ' , '-r ' , 'echo 42; ' ])], StreamOutput::VERBOSITY_DEBUG , null ],
118+ [[ ' OUT 42 ' ], true , [Process::fromShellCommandline ('php -r ' .$ PHP ), 'PHP ' => 'echo 42; ' ], StreamOutput::VERBOSITY_DEBUG , null ],
118119 ];
119120 }
120121
@@ -127,6 +128,6 @@ private function getOutput(StreamOutput $output): string
127128 {
128129 rewind ($ output ->getStream ());
129130
130- return stream_get_contents ($ output ->getStream ());
131+ return str_replace (\ PHP_EOL , "\n" , stream_get_contents ($ output ->getStream () ));
131132 }
132133}
0 commit comments