Skip to content

Commit

Permalink
Add tests for the config (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Jul 14, 2022
1 parent 8757e20 commit cb30cae
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 12 deletions.
46 changes: 34 additions & 12 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,32 @@

final class Config
{
public const EXTRA_CONFIG_KEY = 'bamarni-bin';

public const BIN_LINKS_ENABLED = 'bin-links';
public const TARGET_DIRECTORY = 'target-directory';
public const FORWARD_COMMAND = 'forward-command';

private const DEFAULT_CONFIG = [
self::BIN_LINKS_ENABLED => true,
self::TARGET_DIRECTORY => 'vendor-bin',
self::FORWARD_COMMAND => false,
];

/**
* @var bool
*/
private $binLinks;

/**
* @var array{'bin-links': bool, 'target-directory': string, 'forward-command': bool}
* @var string
*/
private $config;
private $targetDirectory;

/**
* @var bool
*/
private $forwardCommand;

public static function fromComposer(Composer $composer): self
{
Expand All @@ -24,28 +46,28 @@ public static function fromComposer(Composer $composer): self
*/
public function __construct(array $extra)
{
$this->config = array_merge(
[
'bin-links' => true,
'target-directory' => 'vendor-bin',
'forward-command' => false,
],
$extra['bamarni-bin'] ?? []
$config = array_merge(
self::DEFAULT_CONFIG,
$extra[self::EXTRA_CONFIG_KEY] ?? []
);

$this->binLinks = $config[self::BIN_LINKS_ENABLED];
$this->targetDirectory = $config[self::TARGET_DIRECTORY];
$this->forwardCommand = $config[self::FORWARD_COMMAND];
}

public function binLinksAreEnabled(): bool
{
return true === $this->config['bin-links'];
return $this->binLinks;
}

public function getTargetDirectory(): string
{
return $this->config['target-directory'];
return $this->targetDirectory;
}

public function isCommandForwarded(): bool
{
return $this->config['forward-command'];
return $this->forwardCommand;
}
}
57 changes: 57 additions & 0 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Bamarni\Composer\Bin\Tests;

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

final class ConfigTest extends TestCase
{
/**
* @dataProvider provideExtraConfig
*/
public function test_it_can_be_instantiated(
array $extra,
bool $expectedBinLinksEnabled,
string $expectedTargetDirectory,
bool $expectedForwardCommand
): void {
$config = new Config($extra);

self::assertSame($expectedBinLinksEnabled, $config->binLinksAreEnabled());
self::assertSame($expectedTargetDirectory, $config->getTargetDirectory());
self::assertSame($expectedForwardCommand, $config->isCommandForwarded());
}

public static function provideExtraConfig(): iterable
{
yield 'default values' => [
[],
true,
'vendor-bin',
false,
];

yield 'unknown extra entry' => [
['unknown' => 'foo'],
true,
'vendor-bin',
false,
];

yield 'nominal' => [
[
Config::EXTRA_CONFIG_KEY => [
Config::BIN_LINKS_ENABLED => false,
Config::TARGET_DIRECTORY => 'tools',
Config::FORWARD_COMMAND => true,
],
],
false,
'tools',
true,
];
}
}

0 comments on commit cb30cae

Please sign in to comment.