Repository navigation
Expand file tree
/
Copy pathExecuteProcessCommandTest.php
More file actions
249 lines (207 loc) · 10.1 KB
/
Copy pathExecuteProcessCommandTest.php
File metadata and controls
249 lines (207 loc) · 10.1 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
declare(strict_types=1);
/*
* This file is part of the CleverAge/ProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CleverAge\ProcessBundle\Tests\Command;
use CleverAge\ProcessBundle\Command\ExecuteProcessCommand;
use CleverAge\ProcessBundle\Configuration\ProcessConfiguration;
use CleverAge\ProcessBundle\Configuration\TaskConfiguration;
use CleverAge\ProcessBundle\Event\ConsoleProcessEvent;
use CleverAge\ProcessBundle\Filesystem\JsonStreamFile;
use CleverAge\ProcessBundle\Manager\ProcessManager;
use CleverAge\ProcessBundle\Registry\ProcessConfigurationRegistry;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\EventDispatcher\EventDispatcher;
#[CoversClass(ExecuteProcessCommand::class)]
#[UsesClass(ProcessConfiguration::class)]
#[UsesClass(TaskConfiguration::class)]
#[UsesClass(ProcessConfigurationRegistry::class)]
#[UsesClass(ConsoleProcessEvent::class)]
#[UsesClass(JsonStreamFile::class)]
#[UsesClass(\CleverAge\ProcessBundle\Configuration\DeprecatedCodeAliases::class)]
#[UsesClass(\CleverAge\ProcessBundle\Configuration\DidYouMean::class)]
class ExecuteProcessCommandTest extends TestCase
{
private string $outputFile;
protected function setUp(): void
{
$this->outputFile = sys_get_temp_dir().\DIRECTORY_SEPARATOR.uniqid('execute_process_command_test_', true).'.json-stream';
}
protected function tearDown(): void
{
if (file_exists($this->outputFile)) {
unlink($this->outputFile);
}
}
public function testUnknownProcessCodesAreDetectedBeforeAnyExecution(): void
{
$tester = $this->createTester($this->never());
$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage('Unknown process test.unknown');
$tester->execute(['processCodes' => ['test.process', 'test.unknown']]);
}
public function testAnInvalidOutputFormatIsDetectedBeforeAnyExecution(): void
{
$tester = $this->createTester($this->never());
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage("Cannot handle data output with format 'xml'");
$tester->execute(['processCodes' => ['test.process'], '--output-format' => 'xml']);
}
public function testTheDumpFormatIsOnlyForTheStandardOutput(): void
{
$tester = $this->createTester($this->never());
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage("Cannot handle data output with format 'dump'");
$tester->execute(['processCodes' => ['test.process'], '--output' => $this->outputFile, '--output-format' => 'dump']);
}
public function testTheJsonOutputIsPrintedWithoutVerbosity(): void
{
$tester = $this->createTester($this->once(), ['foo' => 'bar']);
self::assertSame(0, $tester->execute(['processCodes' => ['test.process'], '--output-format' => 'json-stream']));
self::assertStringContainsString('{"foo":"bar"}', $tester->getDisplay());
}
public function testInvalidUtf8IsReplacedInTheJsonOutput(): void
{
$tester = $this->createTester($this->once(), ['name' => "Caf\xe9"]);
self::assertSame(0, $tester->execute(['processCodes' => ['test.process'], '--output-format' => 'json-stream']));
self::assertStringContainsString('{"name":"Caf\ufffd"}', $tester->getDisplay());
}
public function testAnOutputThatCannotBeEncodedAsJsonIsReportedAndTheNextProcessesAreExecuted(): void
{
$tester = $this->createTester($this->exactly(2), ['value' => \NAN]);
self::assertSame(0, $tester->execute(
['processCodes' => ['test.process', 'test.process'], '--output-format' => 'json-stream'],
['capture_stderr_separately' => true],
));
self::assertSame(2, substr_count($tester->getErrorOutput(), 'Output not printed: it cannot be encoded as JSON (Inf and NaN cannot be JSON encoded)'));
self::assertSame(2, substr_count($tester->getDisplay(), "Process 'test.process' executed successfully"));
}
public function testAnArrayOutputIsStoredInTheFile(): void
{
$tester = $this->createTester($this->once(), ['foo' => 'bar']);
self::assertSame(0, $tester->execute(['processCodes' => ['test.process'], '--output' => $this->outputFile, '--output-format' => 'json-stream']));
self::assertSame('{"foo":"bar"}', trim((string) file_get_contents($this->outputFile)));
}
/**
* As on STDOUT: a JsonException was thrown (e.g. for the buffer of the process_launcher task).
*/
public function testInvalidUtf8IsReplacedInTheFile(): void
{
$tester = $this->createTester($this->once(), ['name' => "Caf\xe9"]);
self::assertSame(0, $tester->execute(['processCodes' => ['test.process'], '--output' => $this->outputFile, '--output-format' => 'json-stream']));
self::assertSame('{"name":"Caf\ufffd"}', trim((string) file_get_contents($this->outputFile)));
}
public function testAnOutputThatCannotBeEncodedAsJsonIsReportedInsteadOfBeingStored(): void
{
$tester = $this->createTester($this->once(), ['value' => \NAN]);
self::assertSame(0, $tester->execute(
['processCodes' => ['test.process'], '--output' => $this->outputFile, '--output-format' => 'json-stream'],
['capture_stderr_separately' => true],
));
self::assertStringContainsString(
"Output not stored in '{$this->outputFile}': it cannot be encoded as JSON (Inf and NaN cannot be JSON encoded)",
$tester->getErrorOutput()
);
}
public function testANonArrayOutputIsReportedInsteadOfBeingSilentlyLost(): void
{
$tester = $this->createTester($this->once(), 'scalar');
self::assertSame(0, $tester->execute(
['processCodes' => ['test.process'], '--output' => $this->outputFile, '--output-format' => 'json-stream'],
['capture_stderr_separately' => true],
));
self::assertFileDoesNotExist($this->outputFile);
self::assertStringContainsString(
"Output not stored in '{$this->outputFile}': the JSON stream format requires an array, string given",
$tester->getErrorOutput()
);
}
/**
* Only logged as a warning by the process manager, not displayed without -v: the most common mistake went unnoticed.
*/
public function testAnInputIgnoredByAProcessWithoutEntryPointIsReported(): void
{
$tester = $this->createTester($this->exactly(2));
self::assertSame(0, $tester->execute(
['processCodes' => ['test.process', 'test.entry_point'], '--input' => 'data.csv'],
['capture_stderr_separately' => true],
));
self::assertSame(
"The input is ignored by the process 'test.process': it has no entry_point",
trim($tester->getErrorOutput())
);
}
public function testNoWarningWithoutInput(): void
{
$tester = $this->createTester($this->once());
self::assertSame(0, $tester->execute(['processCodes' => ['test.process']], ['capture_stderr_separately' => true]));
self::assertSame('', $tester->getErrorOutput());
}
private function createTester(\PHPUnit\Framework\MockObject\Rule\InvocationOrder $executions, mixed $result = null): CommandTester
{
$processManager = $this->createMock(ProcessManager::class);
$processManager->expects($executions)->method('execute')->willReturn($result);
$registry = new ProcessConfigurationRegistry(
[
'test.process' => [
'options' => [],
'entry_point' => null,
'end_point' => null,
'description' => '',
'help' => '',
'public' => true,
'tasks' => [],
],
'test.entry_point' => [
'options' => [],
'entry_point' => 'first',
'end_point' => null,
'description' => '',
'help' => '',
'public' => true,
'tasks' => ['first' => [
'service' => '@test.task',
'options' => [],
'description' => '',
'help' => '',
'outputs' => [],
'errors' => [],
'error_outputs' => [],
'error_strategy' => null,
'log_level' => null,
]],
],
],
'stop'
);
return new CommandTester(new ExecuteProcessCommand($processManager, new EventDispatcher(), $registry));
}
public function testUnknownProcessSuggestsTheClosestOne(): void
{
$tester = $this->createTester($this->never());
$this->expectExceptionMessage('Unknown process test.proces. Did you mean "test.process"? The processes are listed by the cleverage:process:list command.');
$tester->execute(['processCodes' => ['test.proces']]);
}
public function testInvalidContextGivesTheExpectedFormat(): void
{
$tester = $this->createTester($this->never());
$this->expectExceptionMessage('Invalid context foo: expected key:value, e.g. -c date:2024-01-01 (the value is parsed as YAML)');
$tester->execute(['processCodes' => ['test.process'], '--context' => ['foo']]);
}
public function testUnknownOutputFormatGivesTheFormats(): void
{
$tester = $this->createTester($this->never());
$this->expectExceptionMessage("Cannot handle data output with format 'jsn-stream': use 'dump' (STDOUT only) or 'json-stream'. Did you mean \"json-stream\"?");
$tester->execute(['processCodes' => ['test.process'], '--output-format' => 'jsn-stream']);
}
}