-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFakeProcessDescription.php
225 lines (192 loc) · 5.03 KB
/
FakeProcessDescription.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
namespace Illuminate\Process;
use Symfony\Component\Process\Process;
class FakeProcessDescription
{
/**
* The process' ID.
*
* @var int|null
*/
public $processId = 1000;
/**
* All of the process' output in the order it was described.
*
* @var array
*/
public $output = [];
/**
* The process' exit code.
*
* @var int
*/
public $exitCode = 0;
/**
* The number of times the process should indicate that it is "running".
*
* @var int
*/
public $runIterations = 0;
/**
* Specify the process ID that should be assigned to the process.
*
* @param int $processId
* @return $this
*/
public function id(int $processId)
{
$this->processId = $processId;
return $this;
}
/**
* Describe a line of standard output.
*
* @param array|string $output
* @return $this
*/
public function output(array|string $output)
{
if (is_array($output)) {
collect($output)->each(fn ($line) => $this->output($line));
return $this;
}
$this->output[] = ['type' => 'out', 'buffer' => rtrim($output, "\n")."\n"];
return $this;
}
/**
* Describe a line of error output.
*
* @param array|string $output
* @return $this
*/
public function errorOutput(array|string $output)
{
if (is_array($output)) {
collect($output)->each(fn ($line) => $this->errorOutput($line));
return $this;
}
$this->output[] = ['type' => 'err', 'buffer' => rtrim($output, "\n")."\n"];
return $this;
}
/**
* Replace the entire output buffer with the given string.
*
* @param string $output
* @return $this
*/
public function replaceOutput(string $output)
{
$this->output = collect($this->output)->reject(function ($output) {
return $output['type'] === 'out';
})->values()->all();
if (strlen($output) > 0) {
$this->output[] = [
'type' => 'out',
'buffer' => rtrim($output, "\n")."\n",
];
}
return $this;
}
/**
* Replace the entire error output buffer with the given string.
*
* @param string $output
* @return $this
*/
public function replaceErrorOutput(string $output)
{
$this->output = collect($this->output)->reject(function ($output) {
return $output['type'] === 'err';
})->values()->all();
if (strlen($output) > 0) {
$this->output[] = [
'type' => 'err',
'buffer' => rtrim($output, "\n")."\n",
];
}
return $this;
}
/**
* Specify the process exit code.
*
* @param int $exitCode
* @return $this
*/
public function exitCode(int $exitCode)
{
$this->exitCode = $exitCode;
return $this;
}
/**
* Specify how many times the "isRunning" method should return "true".
*
* @param int $iterations
* @return $this
*/
public function iterations(int $iterations)
{
return $this->runsFor(iterations: $iterations);
}
/**
* Specify how many times the "isRunning" method should return "true".
*
* @param int $iterations
* @return $this
*/
public function runsFor(int $iterations)
{
$this->runIterations = $iterations;
return $this;
}
/**
* Turn the fake process description into an actual process.
*
* @param string $command
* @return \Symfony\Component\Process\Process
*/
public function toSymfonyProcess(string $command)
{
return Process::fromShellCommandline($command);
}
/**
* Convert the process description into a process result.
*
* @param string $command
* @return \Illuminate\Contracts\Process\ProcessResult
*/
public function toProcessResult(string $command)
{
return new FakeProcessResult(
command: $command,
exitCode: $this->exitCode,
output: $this->resolveOutput(),
errorOutput: $this->resolveErrorOutput(),
);
}
/**
* Resolve the standard output as a string.
*
* @return string
*/
protected function resolveOutput()
{
$output = collect($this->output)
->filter(fn ($output) => $output['type'] === 'out');
return $output->isNotEmpty()
? rtrim($output->map->buffer->implode(''), "\n")."\n"
: '';
}
/**
* Resolve the error output as a string.
*
* @return string
*/
protected function resolveErrorOutput()
{
$output = collect($this->output)
->filter(fn ($output) => $output['type'] === 'err');
return $output->isNotEmpty()
? rtrim($output->map->buffer->implode(''), "\n")."\n"
: '';
}
}