[lang-ref] ( get_command_standard_error ) ( php )
<?php
public function testGetCommandStandardError(): void
{
// proc_open() ..
$descriptorspec = [
0 => ['pipe', 'r'], // stdin
1 => ['pipe', 'w'], // stdout
2 => ['pipe', 'w'], // stderr
];
$process = proc_open(
'ls -l /no/such/file',
$descriptorspec,
$pipes
);
if (!is_resource($process)) {
throw new RuntimeException('Failed to start process.');
}
fclose($pipes[0]);
$stdout = stream_get_contents($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);
$status = proc_close($process);
$this->assertStringContainsString('no such file', strtolower($stderr));
$this->assertSame('', $stdout);
$this->assertSame(1, $status);
}
<?php
public function testGetCommandStandardErrorAlternative(): void
{
// shell_exec('.... >2>&1')
$out = shell_exec('ls -l /no/such/file 2>&1'); // this is not what I want
$out = rtrim($out, "\r\n");
$this->assertStringContainsString('no such file', strtolower($out));
}