Skip to content

Commit

Permalink
Validate the config (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Jul 14, 2022
1 parent 8b832b8 commit bc9cbe6
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 3 deletions.
48 changes: 45 additions & 3 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Bamarni\Composer\Bin;

use Composer\Composer;
use UnexpectedValueException;
use function array_merge;

final class Config
Expand Down Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Bamarni\Composer\Bin\Config;
use PHPUnit\Framework\TestCase;
use UnexpectedValueException;

final class ConfigTest extends TestCase
{
Expand Down Expand Up @@ -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".',
];
}
}

0 comments on commit bc9cbe6

Please sign in to comment.