Skip to content

Commit

Permalink
Config: display user-friendly message when invalid generator is passed
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rodrigoprimo committed Dec 13, 2024
1 parent 3d14d00 commit 3b6c8c6
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 8 deletions.
15 changes: 14 additions & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,20 @@ public function processLongArgument($arg, $pos)
break;
}

$this->generator = substr($arg, 10);
$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);
throw new DeepExitException($error, 3);
}

$this->generator = $generatorName;
self::$overriddenDefaults['generator'] = true;
} else if (substr($arg, 0, 9) === 'encoding=') {
if (isset(self::$overriddenDefaults['encoding']) === true) {
Expand Down
61 changes: 54 additions & 7 deletions tests/Core/Config/GeneratorArgTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,35 @@ final class GeneratorArgTest extends TestCase
*
* @param string $generatorName Generator name.
*
* @dataProvider dataGeneratorNames
* @dataProvider dataValidGeneratorNames
*
* @return void
*/
public function testGenerators($generatorName)
public function testValidGenerators($generatorName)
{
$config = new ConfigDouble(["--generator=$generatorName"]);

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

}//end testGenerators()
}//end testValidGenerators()


/**
* Data provider for testGenerators().
* Data provider for testValidGenerators().
*
* @see self::testGenerators()
* @see self::testValidGenerators()
*
* @return array<int, array<string>>
*/
public static function dataGeneratorNames()
public static function dataValidGeneratorNames()
{
return [
['Text'],
['HTML'],
['Markdown'],
];

}//end dataGeneratorNames()
}//end dataValidGeneratorNames()


/**
Expand All @@ -76,4 +76,51 @@ public function testOnlySetOnce()
}//end testOnlySetOnce()


/**
* Ensure that an exception is thrown for an invalid generator.
*
* @param string $generatorName Generator name.
*
* @dataProvider dataInvalidGeneratorNames
*
* @return void
*/
public function testInvalidGenerator($generatorName)
{
$exception = 'PHP_CodeSniffer\Exceptions\DeepExitException';
$message = 'ERROR: "'.$generatorName.'" is not a valid generator. Valid options are: Text, HTML, and Markdown.';

if (method_exists($this, 'expectException') === true) {
// PHPUnit 5+.
$this->expectException($exception);
$this->expectExceptionMessage($message);
} else {
// PHPUnit 4.
$this->setExpectedException($exception, $message);
}

new ConfigDouble(["--generator={$generatorName}"]);

}//end testInvalidGenerator()


/**
* Data provider for testInvalidGenerator().
*
* @see self::testInvalidGenerator()
*
* @return array<int, array<string>>
*/
public static function dataInvalidGeneratorNames()
{
return [
['InvalidGenerator'],
['Text,HTML'],
['TEXT'],
[''],
];

}//end dataInvalidGeneratorNames()


}//end class

0 comments on commit 3b6c8c6

Please sign in to comment.