From bc9cbe64f12bf885ed021b166704e7e64ce84a1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= <5175937+theofidry@users.noreply.github.com> Date: Thu, 14 Jul 2022 11:23:29 +0200 Subject: [PATCH] Validate the config (#130) --- src/Config.php | 48 +++++++++++++++++++++++++++++++++++++++++--- tests/ConfigTest.php | 44 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 3 deletions(-) diff --git a/src/Config.php b/src/Config.php index 02100ba..e4c77b9 100644 --- a/src/Config.php +++ b/src/Config.php @@ -5,6 +5,7 @@ namespace Bamarni\Composer\Bin; use Composer\Composer; +use UnexpectedValueException; use function array_merge; final class Config @@ -51,9 +52,50 @@ public function __construct(array $extra) $extra[self::EXTRA_CONFIG_KEY] ?? [] ); - $this->binLinks = $config[self::BIN_LINKS_ENABLED]; - $this->targetDirectory = $config[self::TARGET_DIRECTORY]; - $this->forwardCommand = $config[self::FORWARD_COMMAND]; + $getType = function_exists('get_debug_type') ? 'get_debug_type' : 'gettype'; + + $binLinks = $config[self::BIN_LINKS_ENABLED]; + + if (!is_bool($binLinks)) { + throw new UnexpectedValueException( + sprintf( + 'Expected setting "%s.%s" to be a boolean value. Got "%s".', + self::EXTRA_CONFIG_KEY, + self::BIN_LINKS_ENABLED, + $getType($binLinks) + ) + ); + } + + $targetDirectory = $config[self::TARGET_DIRECTORY]; + + if (!is_string($targetDirectory)) { + throw new UnexpectedValueException( + sprintf( + 'Expected setting "%s.%s" to be a string. Got "%s".', + self::EXTRA_CONFIG_KEY, + self::TARGET_DIRECTORY, + $getType($targetDirectory) + ) + ); + } + + $forwardCommand = $config[self::FORWARD_COMMAND]; + + if (!is_bool($forwardCommand)) { + throw new UnexpectedValueException( + sprintf( + 'Expected setting "%s.%s" to be a boolean value. Got "%s".', + self::EXTRA_CONFIG_KEY, + self::FORWARD_COMMAND, + gettype($forwardCommand) + ) + ); + } + + $this->binLinks = $binLinks; + $this->targetDirectory = $targetDirectory; + $this->forwardCommand = $forwardCommand; } public function binLinksAreEnabled(): bool diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 94c96f1..948d5f2 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -6,6 +6,7 @@ use Bamarni\Composer\Bin\Config; use PHPUnit\Framework\TestCase; +use UnexpectedValueException; final class ConfigTest extends TestCase { @@ -54,4 +55,47 @@ public static function provideExtraConfig(): iterable true, ]; } + + /** + * @dataProvider provideInvalidExtraConfig + */ + public function test_it_cannot_be_instantiated_with_invalid_config( + array $extra, + string $expectedMessage + ): void { + $this->expectException(UnexpectedValueException::class); + $this->expectExceptionMessage($expectedMessage); + + new Config($extra); + } + + public static function provideInvalidExtraConfig(): iterable + { + yield 'non bool bin links' => [ + [ + Config::EXTRA_CONFIG_KEY => [ + Config::BIN_LINKS_ENABLED => 'foo', + ], + ], + 'Expected setting "bamarni-bin.bin-links" to be a boolean value. Got "string".', + ]; + + yield 'non string target directory' => [ + [ + Config::EXTRA_CONFIG_KEY => [ + Config::TARGET_DIRECTORY => false, + ], + ], + 'Expected setting "bamarni-bin.target-directory" to be a string. Got "bool".', + ]; + + yield 'non bool forward command' => [ + [ + Config::EXTRA_CONFIG_KEY => [ + Config::FORWARD_COMMAND => 'foo', + ], + ], + 'Expected setting "bamarni-bin.forward-command" to be a boolean value. Got "string".', + ]; + } }