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