Skip to content

Commit

Permalink
Ensures the generator CLI parameter is handled case-insensitively
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigoprimo committed Dec 18, 2024
1 parent 3b6c8c6 commit 6bb661d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 20 deletions.
40 changes: 29 additions & 11 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,23 @@ class Config
*/
private static $executablePaths = [];

/**
* A list of valid generators.
*
* - Keys: lowercase version of the generator name.
* - Values: name of the generator PHP class.
*
* Note: once support for PHP < 5.6 is dropped, this property should be refactored into a class
* constant.
*
* @var array<string, string>
*/
private static $validGenerators = [
'text' => 'Text',
'html' => 'HTML',
'markdown' => 'Markdown',
];


/**
* Get the value of an inaccessible property.
Expand Down Expand Up @@ -1233,20 +1250,21 @@ public function processLongArgument($arg, $pos)
break;
}

$generatorName = substr($arg, 10);
$validGenerators = [
'Text',
'HTML',
'Markdown',
];

if (in_array($generatorName, $validGenerators, true) === false) {
$error = 'ERROR: "'.$generatorName.'" is not a valid generator. Valid options are: Text, HTML, and Markdown.'.PHP_EOL.PHP_EOL;
$error .= $this->printShortUsage(true);
$generatorName = substr($arg, 10);
$lowerCaseGeneratorName = strtolower($generatorName);

if (isset(self::$validGenerators[$lowerCaseGeneratorName]) === false) {
$validOptions = implode(', ', array_values(self::$validGenerators));
$error = sprintf(
'ERROR: "%s" is not a valid generator. Valid options are: %s.'.PHP_EOL.PHP_EOL,
$generatorName,
$validOptions
);
$error .= $this->printShortUsage(true);
throw new DeepExitException($error, 3);
}

$this->generator = $generatorName;
$this->generator = self::$validGenerators[$lowerCaseGeneratorName];
self::$overriddenDefaults['generator'] = true;
} else if (substr($arg, 0, 9) === 'encoding=') {
if (isset(self::$overriddenDefaults['encoding']) === true) {
Expand Down
39 changes: 30 additions & 9 deletions tests/Core/Config/GeneratorArgTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ final class GeneratorArgTest extends TestCase
/**
* Ensure that the generator property is set when the parameter is passed a valid value.
*
* @param string $generatorName Generator name.
* @param string $argumentValue Generator name passed in the command line.
* @param string $expectedPropertyValue Expected value of the generator property.
*
* @dataProvider dataValidGeneratorNames
*
* @return void
*/
public function testValidGenerators($generatorName)
public function testValidGenerators($argumentValue, $expectedPropertyValue)
{
$config = new ConfigDouble(["--generator=$generatorName"]);
$config = new ConfigDouble(["--generator=$argumentValue"]);

$this->assertSame($generatorName, $config->generator);
$this->assertSame($expectedPropertyValue, $config->generator);

}//end testValidGenerators()

Expand All @@ -48,9 +49,30 @@ public function testValidGenerators($generatorName)
public static function dataValidGeneratorNames()
{
return [
['Text'],
['HTML'],
['Markdown'],
[
'Text',
'Text',
],
[
'HTML',
'HTML',
],
[
'Markdown',
'Markdown',
],
[
'TEXT',
'Text',
],
[
'tEXt',
'Text',
],
[
'html',
'HTML',
],
];

}//end dataValidGeneratorNames()
Expand Down Expand Up @@ -88,7 +110,7 @@ public function testOnlySetOnce()
public function testInvalidGenerator($generatorName)
{
$exception = 'PHP_CodeSniffer\Exceptions\DeepExitException';
$message = 'ERROR: "'.$generatorName.'" is not a valid generator. Valid options are: Text, HTML, and Markdown.';
$message = 'ERROR: "'.$generatorName.'" is not a valid generator. Valid options are: Text, HTML, Markdown.';

if (method_exists($this, 'expectException') === true) {
// PHPUnit 5+.
Expand Down Expand Up @@ -116,7 +138,6 @@ public static function dataInvalidGeneratorNames()
return [
['InvalidGenerator'],
['Text,HTML'],
['TEXT'],
[''],
];

Expand Down

0 comments on commit 6bb661d

Please sign in to comment.