Skip to content

Commit d374d4b

Browse files
authored
Check invalid cli options combination (#131)
1 parent c55b7bc commit d374d4b

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

src/Initializer.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,17 +234,21 @@ public function initCliOptions(string $cwd, array $argv): CliOptions
234234
*/
235235
public function initFormatter(CliOptions $options): ResultFormatter
236236
{
237-
switch ($options->format) {
238-
case 'junit':
239-
return new JunitFormatter($this->cwd, $this->stdOutPrinter);
237+
$format = $options->format ?? 'console';
240238

241-
case 'console':
242-
case null:
243-
return new ConsoleFormatter($this->cwd, $this->stdOutPrinter);
239+
if ($format === 'junit') {
240+
if ($options->dumpUsages !== null) {
241+
throw new InvalidConfigException("Cannot use 'junit' format with '--dump-usages' option.");
242+
}
243+
244+
return new JunitFormatter($this->cwd, $this->stdOutPrinter);
245+
}
244246

245-
default:
246-
throw new InvalidConfigException("Invalid format option provided, allowed are 'console' or 'junit'.");
247+
if ($format === 'console') {
248+
return new ConsoleFormatter($this->cwd, $this->stdOutPrinter);
247249
}
250+
251+
throw new InvalidConfigException("Invalid format option provided, allowed are 'console' or 'junit'.");
248252
}
249253

250254
}

tests/BinTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ public function test(): void
1919
$noComposerJsonError = 'File composer.json not found';
2020
$noPackagesError = 'No packages found';
2121
$parseError = 'Failure while parsing';
22+
$junitDumpError = "Cannot use 'junit' format with '--dump-usages' option";
2223

2324
$okOutput = 'No composer issues found';
25+
$dumpingOutput = 'Dumping sample usages of';
2426
$helpOutput = 'Usage:';
2527

2628
$usingConfig = 'Using config';
@@ -38,7 +40,9 @@ public function test(): void
3840
$this->runCommand('php ../bin/composer-dependency-analyser --composer-json=composer.json', $testsDir, 255, null, $noComposerJsonError);
3941
$this->runCommand('php ../bin/composer-dependency-analyser --composer-json=../composer.json --config=../composer-dependency-analyser.php', $testsDir, 0, $okOutput, $usingConfig);
4042
$this->runCommand('php bin/composer-dependency-analyser --composer-json=composer.json --format=console', $rootDir, 0, $okOutput, $usingConfig);
43+
$this->runCommand('php bin/composer-dependency-analyser --composer-json=composer.json --format=console --dump-usages=symfony/*', $rootDir, 1, $dumpingOutput, $usingConfig);
4144
$this->runCommand('php bin/composer-dependency-analyser --composer-json=composer.json --format=junit', $rootDir, 0, $junitOutput, $usingConfig);
45+
$this->runCommand('php bin/composer-dependency-analyser --composer-json=composer.json --format=junit --dump-usages=symfony/*', $rootDir, 255, null, $junitDumpError);
4246
}
4347

4448
private function runCommand(

tests/InitializerTest.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,43 @@ public function testInitFormatter(): void
130130
$optionsFormatJunit = new CliOptions();
131131
$optionsFormatJunit->format = 'junit';
132132
self::assertInstanceOf(JunitFormatter::class, $initializer->initFormatter($optionsFormatJunit));
133+
}
133134

134-
self::expectException(InvalidConfigException::class);
135-
$optionsFormatUnknown = new CliOptions();
136-
$optionsFormatUnknown->format = 'unknown';
137-
$initializer->initFormatter($optionsFormatUnknown);
135+
/**
136+
* @dataProvider provideInitFormatterFailures
137+
*/
138+
public function testInitFormatterFailures(CliOptions $options, string $message): void
139+
{
140+
$printer = $this->createMock(Printer::class);
141+
142+
$initializer = new Initializer(__DIR__, $printer, $printer);
143+
144+
$this->expectException(InvalidConfigException::class);
145+
$this->expectExceptionMessage($message);
146+
$initializer->initFormatter($options);
147+
}
148+
149+
/**
150+
* @return iterable<string, array{CliOptions, string}>
151+
*/
152+
public static function provideInitFormatterFailures(): iterable
153+
{
154+
$junitWithDumpUsages = new CliOptions();
155+
$junitWithDumpUsages->format = 'junit';
156+
$junitWithDumpUsages->dumpUsages = 'symfony/*';
157+
158+
$unknownFormat = new CliOptions();
159+
$unknownFormat->format = 'unknown';
160+
161+
yield 'junit with dump-usages' => [
162+
$junitWithDumpUsages,
163+
"Cannot use 'junit' format with '--dump-usages' option.",
164+
];
165+
166+
yield 'unknown format' => [
167+
$unknownFormat,
168+
"Invalid format option provided, allowed are 'console' or 'junit'.",
169+
];
138170
}
139171

140172
}

0 commit comments

Comments
 (0)