-
-
Notifications
You must be signed in to change notification settings - Fork 973
Expand file tree
/
Copy pathInstallerCommandTest.php
More file actions
330 lines (285 loc) · 11.4 KB
/
Copy pathInstallerCommandTest.php
File metadata and controls
330 lines (285 loc) · 11.4 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
declare(strict_types=1);
namespace ApiPlatform\Installer\Tests;
use ApiPlatform\Installer\InstallerCommand;
use ApiPlatform\Installer\Scaffold\ScaffoldOptions;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Tester\CommandTester;
final class InstallerCommandTest extends TestCase
{
/**
* @return array<array{string}>
*/
public static function validNames(): array
{
return [['my-app'], ['MyApp'], ['app1'], ['app.test'], ['app_test'], ['1app']];
}
/**
* @return array<array{string}>
*/
public static function invalidNames(): array
{
return [[''], ['-leading-dash'], ['has space'], ['has/slash'], ['has@at']];
}
#[DataProvider('validNames')]
public function testAcceptsValidNames(string $name): void
{
$this->assertSame($name, InstallerCommand::validateName($name));
}
#[DataProvider('invalidNames')]
public function testRejectsInvalidNames(string $name): void
{
$this->expectException(InvalidArgumentException::class);
InstallerCommand::validateName($name);
}
public function testRejectsExistingDirectory(): void
{
$tmp = sys_get_temp_dir().'/installer-test-'.bin2hex(random_bytes(4));
mkdir($tmp);
try {
$cwd = getcwd();
chdir(\dirname($tmp));
$tester = $this->tester();
$tester->execute(
['name' => basename($tmp), '--framework' => 'symfony', '--with-docker' => false, '--with-pwa' => false],
['interactive' => false],
);
if (false !== $cwd) {
chdir($cwd);
}
$this->assertSame(2, $tester->getStatusCode());
$this->assertStringContainsString('already exists', $tester->getDisplay());
} finally {
rmdir($tmp);
}
}
public function testRejectsInvalidProjectName(): void
{
$tester = $this->tester();
$tester->execute(['name' => 'has space', '--framework' => 'symfony'], ['interactive' => false]);
$this->assertSame(2, $tester->getStatusCode());
$this->assertStringContainsString('start with a letter', $tester->getDisplay());
}
public function testRejectsUnknownFramework(): void
{
$tester = $this->tester();
$tester->execute(['name' => 'demo', '--framework' => 'rails'], ['interactive' => false]);
$this->assertSame(2, $tester->getStatusCode());
$this->assertStringContainsString('Unsupported framework', $tester->getDisplay());
}
public function testRejectsUnknownDocs(): void
{
$tester = $this->tester();
$tester->execute(['name' => 'demo', '--framework' => 'symfony', '--docs' => ['foo']], ['interactive' => false]);
$this->assertSame(2, $tester->getStatusCode());
$this->assertStringContainsString('Unknown --docs', $tester->getDisplay());
}
public function testRejectsWithDockerOnLaravel(): void
{
$tester = $this->tester();
$tester->execute(
['name' => 'demo', '--framework' => 'laravel', '--with-docker' => true],
['interactive' => false],
);
$this->assertSame(2, $tester->getStatusCode());
$this->assertStringContainsString('--with-docker is not supported with Laravel', $tester->getDisplay());
}
public function testRejectsWithPwaOnLaravel(): void
{
$tester = $this->tester();
$tester->execute(
['name' => 'demo', '--framework' => 'laravel', '--with-pwa' => true],
['interactive' => false],
);
$this->assertSame(2, $tester->getStatusCode());
$this->assertStringContainsString('--with-pwa is not supported with Laravel', $tester->getDisplay());
}
public function testAdminOptionIsAcceptedOnSymfony(): void
{
$command = new InstallerCommand();
$this->assertTrue($command->getDefinition()->hasOption('with-admin'));
}
public function testAdminOptionIsAcceptedOnLaravel(): void
{
$command = new InstallerCommand();
$input = new ArrayInput([
'name' => 'demo',
'--framework' => 'laravel',
'--with-admin' => true,
]);
$input->bind($command->getDefinition());
$input->setInteractive(false);
$io = new SymfonyStyle($input, new BufferedOutput());
$method = new \ReflectionMethod($command, 'resolveOptions');
$opts = $method->invoke($command, $io, $input, InstallerCommand::FRAMEWORK_LARAVEL);
$this->assertTrue($opts->withAdmin);
}
public function testAdminDefaultsToFalseInNonInteractiveMode(): void
{
$opts = $this->resolveDefaultOptions();
$this->assertFalse($opts->withAdmin);
}
public function testDocsOptionDescriptionMentionsScalar(): void
{
$command = new InstallerCommand();
$description = $command->getDefinition()->getOption('docs')->getDescription();
$this->assertStringContainsString('scalar', $description);
}
public function testAcceptsScalarAsDocsOption(): void
{
$this->assertContains('scalar', InstallerCommand::DOCS);
}
public function testReportsDevVersionWhenPlaceholderUnsubstituted(): void
{
// No version-injection step is wired into the build yet, so the raw
// @package_version@ placeholder must collapse to a "dev" string.
$this->assertSame('dev', InstallerCommand::version());
}
public function testNonInteractiveDefaultsSelectEveryFormatAndDocViewer(): void
{
$opts = $this->resolveDefaultOptions();
$this->assertSame(InstallerCommand::FORMATS, $opts->formats);
$this->assertSame(InstallerCommand::DOCS, $opts->docs);
}
public function testEmptyDocsOptionDisablesDocViewers(): void
{
$opts = $this->resolveOptions([
'name' => 'demo',
'--framework' => 'symfony',
'--with-docker' => false,
'--with-pwa' => false,
'--with-admin' => false,
'--docs' => [''],
]);
$this->assertSame([], $opts->docs);
}
public function testRejectsEmptyDocsOptionCombinedWithDocViewers(): void
{
$tester = $this->tester();
$tester->execute(
['name' => 'demo', '--framework' => 'symfony', '--docs' => ['', 'redoc']],
['interactive' => false],
);
$this->assertSame(2, $tester->getStatusCode());
$this->assertStringContainsString('Cannot combine empty --docs with other values', $tester->getDisplay());
}
/**
* @return array<string, array{string, array<string>}>
*/
public static function interactiveDocsSelections(): array
{
return [
'swagger-ui numeric key' => ['0', ['swagger_ui']],
'redoc numeric key' => ['1', ['redoc']],
'scalar numeric key' => ['2', ['scalar']],
'all numeric keys' => ['0,1,2', ['swagger_ui', 'redoc', 'scalar']],
'redoc value' => ['redoc', ['redoc']],
'scalar value' => ['scalar', ['scalar']],
'all values' => ['swagger_ui,redoc,scalar', ['swagger_ui', 'redoc', 'scalar']],
'default' => ['', ['swagger_ui', 'redoc', 'scalar']],
];
}
/**
* @param array<string> $expected
*/
#[DataProvider('interactiveDocsSelections')]
public function testInteractiveDocsPromptAcceptsDisplayedChoices(string $answer, array $expected): void
{
$this->assertSame($expected, $this->resolveInteractiveDocs($answer));
}
public function testInteractiveMultiselectPromptsEmitNoPhpWarnings(): void
{
$tmp = sys_get_temp_dir().'/installer-test-'.bin2hex(random_bytes(4));
mkdir($tmp);
$errors = [];
set_error_handler(static function (int $severity, string $message, string $file, int $line) use (&$errors): bool {
$errors[] = ['severity' => $severity, 'message' => $message, 'file' => $file, 'line' => $line];
return true;
});
try {
$cwd = getcwd();
chdir(\dirname($tmp));
$tester = $this->tester();
// Accept defaults for the two multiselect prompts (formats, docs).
$tester->setInputs(['', '']);
$tester->execute(
['name' => basename($tmp), '--framework' => 'symfony', '--with-docker' => false, '--with-pwa' => false, '--with-admin' => false],
['interactive' => true],
);
if (false !== $cwd) {
chdir($cwd);
}
} finally {
restore_error_handler();
rmdir($tmp);
}
$warnings = array_values(array_filter($errors, static fn (array $e): bool => \E_WARNING === $e['severity']));
$this->assertSame([], $warnings, 'Expected no PHP warnings from interactive prompts, got: '.json_encode($warnings));
$this->assertStringContainsString('API documentation (comma-separated)', $tester->getDisplay());
$this->assertStringNotContainsString('empty for none', $tester->getDisplay());
}
private function tester(): CommandTester
{
$command = new InstallerCommand();
$app = new Application();
$app->add($command);
return new CommandTester($app->find((string) $command->getName()));
}
private function resolveDefaultOptions(): ScaffoldOptions
{
return $this->resolveOptions([
'name' => 'demo',
'--framework' => 'symfony',
'--with-docker' => false,
'--with-pwa' => false,
'--with-admin' => false,
]);
}
/**
* @param array<string, mixed> $inputValues
*/
private function resolveOptions(array $inputValues, bool $interactive = false): ScaffoldOptions
{
$command = new InstallerCommand();
$input = new ArrayInput($inputValues);
$input->bind($command->getDefinition());
$input->setInteractive($interactive);
$io = new SymfonyStyle($input, new BufferedOutput());
$method = new \ReflectionMethod($command, 'resolveOptions');
return $method->invoke($command, $io, $input, InstallerCommand::FRAMEWORK_SYMFONY);
}
/**
* @return array<string>
*/
private function resolveInteractiveDocs(string $answer): array
{
$command = new InstallerCommand();
$input = new ArrayInput([
'name' => 'demo',
'--framework' => 'symfony',
'--with-docker' => false,
'--with-pwa' => false,
'--with-admin' => false,
'--format' => ['jsonld'],
]);
$input->bind($command->getDefinition());
$input->setInteractive(true);
$stream = fopen('php://memory', 'r+');
if (false === $stream) {
throw new \RuntimeException('Could not open memory stream.');
}
fwrite($stream, $answer."\n");
rewind($stream);
$input->setStream($stream);
$io = new SymfonyStyle($input, new BufferedOutput());
$method = new \ReflectionMethod($command, 'resolveOptions');
$opts = $method->invoke($command, $io, $input, InstallerCommand::FRAMEWORK_SYMFONY);
return $opts->docs;
}
}