From 57070ef155bb19dda0e1b7cf0adf79137687a368 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 1 Dec 2019 12:04:31 +0100 Subject: [PATCH] Improved Rest's ConfigProvider test to kill more mutants --- module/Rest/src/ConfigProvider.php | 17 ++++++++++++----- module/Rest/test/ConfigProviderTest.php | 24 +++++++++++++++++++++++- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/module/Rest/src/ConfigProvider.php b/module/Rest/src/ConfigProvider.php index d942cf51..0c0e99a5 100644 --- a/module/Rest/src/ConfigProvider.php +++ b/module/Rest/src/ConfigProvider.php @@ -4,19 +4,26 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Rest; -use Zend\Config\Factory; -use Zend\Stdlib\Glob; - +use function Shlinkio\Shlink\Common\loadConfigFromGlob; use function sprintf; class ConfigProvider { private const ROUTES_PREFIX = '/rest/v{version:1|2}'; + /** @var callable */ + private $loadConfig; + + public function __construct(?callable $loadConfig = null) + { + $this->loadConfig = $loadConfig ?? function (string $glob) { + return loadConfigFromGlob($glob); + }; + } + public function __invoke() { - /** @var array $config */ - $config = Factory::fromFiles(Glob::glob(__DIR__ . '/../config/{,*.}config.php', Glob::GLOB_BRACE)); + $config = ($this->loadConfig)(__DIR__ . '/../config/{,*.}config.php'); return $this->applyRoutesPrefix($config); } diff --git a/module/Rest/test/ConfigProviderTest.php b/module/Rest/test/ConfigProviderTest.php index 3cd574b3..2922faad 100644 --- a/module/Rest/test/ConfigProviderTest.php +++ b/module/Rest/test/ConfigProviderTest.php @@ -20,9 +20,31 @@ class ConfigProviderTest extends TestCase /** @test */ public function properConfigIsReturned(): void { - $config = $this->configProvider->__invoke(); + $config = ($this->configProvider)(); $this->assertArrayHasKey('routes', $config); $this->assertArrayHasKey('dependencies', $config); } + + /** @test */ + public function routesAreProperlyPrefixed(): void + { + $configProvider = new ConfigProvider(function () { + return [ + 'routes' => [ + ['path' => '/foo'], + ['path' => '/bar'], + ['path' => '/baz/foo'], + ], + ]; + }); + + $config = $configProvider(); + + $this->assertEquals([ + ['path' => '/rest/v{version:1|2}/foo'], + ['path' => '/rest/v{version:1|2}/bar'], + ['path' => '/rest/v{version:1|2}/baz/foo'], + ], $config['routes']); + } }