Skip to content

Commit a7eb77e

Browse files
committed
RunnerResult: accepts raw output & error output (#100)
1 parent 41c63c2 commit a7eb77e

File tree

4 files changed

+136
-10
lines changed

4 files changed

+136
-10
lines changed

src/RunnerResult.php

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ class RunnerResult
1111
/** @var int */
1212
private $exitCode;
1313

14-
/** @var string[] */
14+
/** @var string|string[] */
1515
private $output;
1616

17-
/** @var string[] */
17+
/** @var string|string[] */
1818
private $errorOutput;
1919

2020

2121
/**
2222
* @param string $command
2323
* @param int $exitCode
24-
* @param string[] $output
25-
* @param string[] $errorOutput
24+
* @param string|string[] $output
25+
* @param string|string[] $errorOutput
2626
*/
27-
public function __construct($command, $exitCode, array $output, array $errorOutput)
27+
public function __construct($command, $exitCode, $output, $errorOutput)
2828
{
2929
$this->command = (string) $command;
3030
$this->exitCode = (int) $exitCode;
@@ -65,6 +65,10 @@ public function getExitCode()
6565
*/
6666
public function getOutput()
6767
{
68+
if (is_string($this->output)) {
69+
return $this->splitOutput($this->output);
70+
}
71+
6872
return $this->output;
6973
}
7074

@@ -74,6 +78,10 @@ public function getOutput()
7478
*/
7579
public function getOutputAsString()
7680
{
81+
if (is_string($this->output)) {
82+
return $this->output;
83+
}
84+
7785
return implode("\n", $this->output);
7886
}
7987

@@ -83,7 +91,8 @@ public function getOutputAsString()
8391
*/
8492
public function getOutputLastLine()
8593
{
86-
$lastLine = end($this->output);
94+
$output = $this->getOutput();
95+
$lastLine = end($output);
8796
return is_string($lastLine) ? $lastLine : NULL;
8897
}
8998

@@ -93,6 +102,10 @@ public function getOutputLastLine()
93102
*/
94103
public function hasOutput()
95104
{
105+
if (is_string($this->output)) {
106+
return trim($this->output) !== '';
107+
}
108+
96109
return !empty($this->output);
97110
}
98111

@@ -102,15 +115,36 @@ public function hasOutput()
102115
*/
103116
public function getErrorOutput()
104117
{
118+
if (is_string($this->errorOutput)) {
119+
return $this->splitOutput($this->errorOutput);
120+
}
121+
105122
return $this->errorOutput;
106123
}
107124

108125

126+
/**
127+
* @return string
128+
*/
129+
public function getErrorOutputAsString()
130+
{
131+
if (is_string($this->errorOutput)) {
132+
return $this->errorOutput;
133+
}
134+
135+
return implode("\n", $this->errorOutput);
136+
}
137+
138+
109139
/**
110140
* @return bool
111141
*/
112142
public function hasErrorOutput()
113143
{
144+
if (is_string($this->errorOutput)) {
145+
return trim($this->errorOutput) !== '';
146+
}
147+
114148
return !empty($this->errorOutput);
115149
}
116150

@@ -127,4 +161,21 @@ public function toText()
127161
. implode("\n", $this->getErrorOutput()) . "\n\n"
128162
. '=> ' . $this->getExitCode() . "\n\n";
129163
}
164+
165+
166+
/**
167+
* @param string $output
168+
* @return string[]
169+
*/
170+
private function splitOutput($output)
171+
{
172+
$output = str_replace(["\r\n", "\r"], "\n", $output);
173+
$output = rtrim($output, "\n");
174+
175+
if ($output === '') {
176+
return [];
177+
}
178+
179+
return explode("\n", $output);
180+
}
130181
}

src/Runners/CliRunner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function run($cwd, array $args, array $env = NULL)
8080
}
8181

8282
$returnCode = proc_close($process);
83-
return new RunnerResult($command, $returnCode, $this->convertOutput($stdout), $this->convertOutput($stderr));
83+
return new RunnerResult($command, $returnCode, $stdout, $stderr);
8484
}
8585

8686

src/Runners/MemoryRunner.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ public function __construct($cwd)
3333
/**
3434
* @param array<mixed> $args
3535
* @param array<string, scalar> $env
36-
* @param array<string> $output
37-
* @param array<string> $errorOutput
36+
* @param string|array<string> $output
37+
* @param string|array<string> $errorOutput
3838
* @param int $exitCode
3939
* @return self
4040
*/
41-
public function setResult(array $args, array $env, array $output, array $errorOutput = [], $exitCode = 0)
41+
public function setResult(array $args, array $env, $output, $errorOutput = [], $exitCode = 0)
4242
{
4343
$cmd = $this->commandProcessor->process('git', $args, $env);
4444
$this->results[$cmd] = new RunnerResult($cmd, $exitCode, $output, $errorOutput);

tests/GitPhp/RunnerResult.phpt

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
use Tester\Assert;
4+
use CzProject\GitPhp\RunnerResult;
5+
6+
require __DIR__ . '/bootstrap.php';
7+
8+
test(function () {
9+
$result = new RunnerResult('cat-file', 0, "foo\r\nbar\r\n", "error\r\nerror2\r\n");
10+
11+
Assert::true($result->hasOutput());
12+
Assert::same(['foo', 'bar'], $result->getOutput());
13+
Assert::same("foo\r\nbar\r\n", $result->getOutputAsString());
14+
Assert::same('bar', $result->getOutputLastLine());
15+
16+
Assert::true($result->hasErrorOutput());
17+
Assert::same(['error', 'error2'], $result->getErrorOutput());
18+
Assert::same("error\r\nerror2\r\n", $result->getErrorOutputAsString());
19+
});
20+
21+
22+
test(function () {
23+
$result = new RunnerResult('cat-file', 0, "\r\n", "\r\n");
24+
25+
Assert::false($result->hasOutput());
26+
Assert::same([], $result->getOutput());
27+
Assert::same("\r\n", $result->getOutputAsString());
28+
Assert::null($result->getOutputLastLine());
29+
30+
Assert::false($result->hasErrorOutput());
31+
Assert::same([], $result->getErrorOutput());
32+
Assert::same("\r\n", $result->getErrorOutputAsString());
33+
});
34+
35+
36+
test(function () {
37+
$result = new RunnerResult('cat-file', 0, '', '');
38+
39+
Assert::false($result->hasOutput());
40+
Assert::same([], $result->getOutput());
41+
Assert::same('', $result->getOutputAsString());
42+
Assert::null($result->getOutputLastLine());
43+
44+
Assert::false($result->hasErrorOutput());
45+
Assert::same([], $result->getErrorOutput());
46+
Assert::same('', $result->getErrorOutputAsString());
47+
});
48+
49+
50+
test(function () {
51+
$result = new RunnerResult('cat-file', 0, ['foo', 'bar'], ['error', 'error2']);
52+
53+
Assert::true($result->hasOutput());
54+
Assert::same(['foo', 'bar'], $result->getOutput());
55+
Assert::same("foo\nbar", $result->getOutputAsString());
56+
Assert::same('bar', $result->getOutputLastLine());
57+
58+
Assert::true($result->hasErrorOutput());
59+
Assert::same(['error', 'error2'], $result->getErrorOutput());
60+
Assert::same("error\nerror2", $result->getErrorOutputAsString());
61+
});
62+
63+
64+
test(function () {
65+
$result = new RunnerResult('cat-file', 0, [], []);
66+
67+
Assert::false($result->hasOutput());
68+
Assert::same([], $result->getOutput());
69+
Assert::same('', $result->getOutputAsString());
70+
Assert::null($result->getOutputLastLine());
71+
72+
Assert::false($result->hasErrorOutput());
73+
Assert::same([], $result->getErrorOutput());
74+
Assert::same('', $result->getErrorOutputAsString());
75+
});

0 commit comments

Comments
 (0)