Skip to content

Commit 5fb2940

Browse files
rodrigoprimojrfnl
authored andcommitted
Config: display user-friendly message when invalid generator is passed (#771)
This commit improves how `Config::processLongArgument()` handles the `--generator` parameter. Now it will show a user-friendly message if an invalid generator name is passed. Before, an invalid generator name caused a fatal error. The commit also ensures that the value for the `generator` CLI parameter is handled case-insensitively.
1 parent e58fc99 commit 5fb2940

File tree

2 files changed

+112
-14
lines changed

2 files changed

+112
-14
lines changed

src/Config.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,21 @@ class Config
170170
*/
171171
private $cliArgs = [];
172172

173+
/**
174+
* A list of valid generators.
175+
*
176+
* {@internal Once support for PHP < 5.6 is dropped, this property should be refactored into a
177+
* class constant.}
178+
*
179+
* @var array<string, string> Keys are the lowercase version of the generator name, while values
180+
* are the associated PHP generator class.
181+
*/
182+
private $validGenerators = [
183+
'text' => 'Text',
184+
'html' => 'HTML',
185+
'markdown' => 'Markdown',
186+
];
187+
173188
/**
174189
* Command line values that the user has supplied directly.
175190
*
@@ -1195,7 +1210,22 @@ public function processLongArgument($arg, $pos)
11951210
break;
11961211
}
11971212

1198-
$this->generator = substr($arg, 10);
1213+
$generatorName = substr($arg, 10);
1214+
$lowerCaseGeneratorName = strtolower($generatorName);
1215+
1216+
if (isset($this->validGenerators[$lowerCaseGeneratorName]) === false) {
1217+
$validOptions = implode(', ', $this->validGenerators);
1218+
$validOptions = substr_replace($validOptions, ' and', strrpos($validOptions, ','), 1);
1219+
$error = sprintf(
1220+
'ERROR: "%s" is not a valid generator. The following generators are supported: %s.'.PHP_EOL.PHP_EOL,
1221+
$generatorName,
1222+
$validOptions
1223+
);
1224+
$error .= $this->printShortUsage(true);
1225+
throw new DeepExitException($error, 3);
1226+
}
1227+
1228+
$this->generator = $this->validGenerators[$lowerCaseGeneratorName];
11991229
$this->overriddenDefaults['generator'] = true;
12001230
} else if (substr($arg, 0, 9) === 'encoding=') {
12011231
if (isset($this->overriddenDefaults['encoding']) === true) {

tests/Core/Config/GeneratorArgTest.php

Lines changed: 81 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,59 @@ final class GeneratorArgTest extends TestCase
2323
/**
2424
* Ensure that the generator property is set when the parameter is passed a valid value.
2525
*
26-
* @param string $generatorName Generator name.
26+
* @param string $argumentValue Generator name passed on the command line.
27+
* @param string $expectedPropertyValue Expected value of the generator property.
2728
*
28-
* @dataProvider dataGeneratorNames
29+
* @dataProvider dataValidGeneratorNames
2930
*
3031
* @return void
3132
*/
32-
public function testGenerators($generatorName)
33+
public function testValidGenerators($argumentValue, $expectedPropertyValue)
3334
{
34-
$config = new ConfigDouble(["--generator=$generatorName"]);
35+
$config = new ConfigDouble(["--generator=$argumentValue"]);
3536

36-
$this->assertSame($generatorName, $config->generator);
37+
$this->assertSame($expectedPropertyValue, $config->generator);
3738

38-
}//end testGenerators()
39+
}//end testValidGenerators()
3940

4041

4142
/**
42-
* Data provider for testGenerators().
43+
* Data provider for testValidGenerators().
4344
*
44-
* @see self::testGenerators()
45+
* @see self::testValidGenerators()
4546
*
4647
* @return array<int, array<string>>
4748
*/
48-
public static function dataGeneratorNames()
49+
public static function dataValidGeneratorNames()
4950
{
5051
return [
51-
['Text'],
52-
['HTML'],
53-
['Markdown'],
52+
'Text generator passed' => [
53+
'argumentValue' => 'Text',
54+
'expectedPropertyValue' => 'Text',
55+
],
56+
'HTML generator passed' => [
57+
'argumentValue' => 'HTML',
58+
'expectedPropertyValue' => 'HTML',
59+
],
60+
'Markdown generator passed' => [
61+
'argumentValue' => 'Markdown',
62+
'expectedPropertyValue' => 'Markdown',
63+
],
64+
'Uppercase Text generator passed' => [
65+
'argumentValue' => 'TEXT',
66+
'expectedPropertyValue' => 'Text',
67+
],
68+
'Mixed case Text generator passed' => [
69+
'argumentValue' => 'tEXt',
70+
'expectedPropertyValue' => 'Text',
71+
],
72+
'Lowercase HTML generator passed' => [
73+
'argumentValue' => 'html',
74+
'expectedPropertyValue' => 'HTML',
75+
],
5476
];
5577

56-
}//end dataGeneratorNames()
78+
}//end dataValidGeneratorNames()
5779

5880

5981
/**
@@ -76,4 +98,50 @@ public function testOnlySetOnce()
7698
}//end testOnlySetOnce()
7799

78100

101+
/**
102+
* Ensure that an exception is thrown for an invalid generator.
103+
*
104+
* @param string $generatorName Generator name.
105+
*
106+
* @dataProvider dataInvalidGeneratorNames
107+
*
108+
* @return void
109+
*/
110+
public function testInvalidGenerator($generatorName)
111+
{
112+
$exception = 'PHP_CodeSniffer\Exceptions\DeepExitException';
113+
$message = 'ERROR: "'.$generatorName.'" is not a valid generator. The following generators are supported: Text, HTML and Markdown.';
114+
115+
if (method_exists($this, 'expectException') === true) {
116+
// PHPUnit 5+.
117+
$this->expectException($exception);
118+
$this->expectExceptionMessage($message);
119+
} else {
120+
// PHPUnit 4.
121+
$this->setExpectedException($exception, $message);
122+
}
123+
124+
new ConfigDouble(["--generator={$generatorName}"]);
125+
126+
}//end testInvalidGenerator()
127+
128+
129+
/**
130+
* Data provider for testInvalidGenerator().
131+
*
132+
* @see self::testInvalidGenerator()
133+
*
134+
* @return array<int, array<string>>
135+
*/
136+
public static function dataInvalidGeneratorNames()
137+
{
138+
return [
139+
['InvalidGenerator'],
140+
['Text,HTML'],
141+
[''],
142+
];
143+
144+
}//end dataInvalidGeneratorNames()
145+
146+
79147
}//end class

0 commit comments

Comments
 (0)