Skip to content

Commit aec8d0f

Browse files
committed
Tests: add generic IOTestCase
... with a helper method to retrieve `stdout`, `stderr` and the exit code of a command line command run for use in follow-up assertions. The `IOTestCase` has full access to all polyfill traits from the PHPUnit Polyfills.
1 parent e7bf5b6 commit aec8d0f

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

Tests/IOTestCase.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* PHPCSDevTools, tools for PHP_CodeSniffer sniff developers.
4+
*
5+
* @package PHPCSDevTools
6+
* @copyright 2019 PHPCSDevTools Contributors
7+
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
8+
* @link https://github.com/PHPCSStandards/PHPCSDevTools
9+
*/
10+
11+
namespace PHPCSDevTools\Tests;
12+
13+
use Yoast\PHPUnitPolyfills\TestCases\XTestCase;
14+
15+
/**
16+
* Abstract test case for checking the output of command execution.
17+
*/
18+
abstract class IOTestCase extends XTestCase
19+
{
20+
21+
/**
22+
* Helper function to execute a CLI command and retrieve the results.
23+
*
24+
* @param string $command The CLI command to execute.
25+
* @param string|null $workingDir Optional. The directory in which to execute the command.
26+
* Defaults to `null` = the working directory of the current PHP process.
27+
* Note: if the command itself already contains a "working directory" argument,
28+
* this parameter will normally not need to be passed.
29+
*
30+
* @return array Format:
31+
* 'exitcode' int The exit code from the command.
32+
* 'stdout' string The output send to stdout.
33+
* 'stderr' string The output send to stderr.
34+
*
35+
* @throws \RuntimeException When the passed arguments do not comply.
36+
* @throws \RuntimeException When no resource could be obtained to execute the command.
37+
*/
38+
protected function executeCliCommand($command, $workingDir = null)
39+
{
40+
if (\is_string($command) === false || $command === '') {
41+
throw new RuntimeException('Command must be a non-empty string.');
42+
}
43+
44+
if (\is_null($workingDir) === false && (\is_string($workingDir) === false || $workingDir === '')) {
45+
throw new RuntimeException('Working directory must be a non-empty string or null.');
46+
}
47+
48+
$descriptorspec = [
49+
0 => ['pipe', 'r'], // stdin
50+
1 => ['pipe', 'w'], // stdout
51+
2 => ['pipe', 'w'], // stderr
52+
];
53+
54+
$process = \proc_open($command, $descriptorspec, $pipes, $workingDir);
55+
56+
if (\is_resource($process) === false) {
57+
throw new RuntimeException('Could not obtain a resource with proc_open() to execute the command.');
58+
}
59+
60+
$result = [];
61+
\fclose($pipes[0]);
62+
63+
$result['stdout'] = \stream_get_contents($pipes[1]);
64+
\fclose($pipes[1]);
65+
66+
$result['stderr'] = \stream_get_contents($pipes[2]);
67+
\fclose($pipes[2]);
68+
69+
$result['exitcode'] = \proc_close($process);
70+
71+
return $result;
72+
}
73+
}

0 commit comments

Comments
 (0)