Fix loading of config options as env vars

This commit is contained in:
Alejandro Celaya 2022-08-08 19:23:16 +02:00
parent 6378e614b0
commit a2f9742cfc
3 changed files with 19 additions and 1 deletions

View File

@ -21,7 +21,7 @@ $isTestEnv = env('APP_ENV') === 'test';
return (new ConfigAggregator\ConfigAggregator([
! $isTestEnv
? new EnvVarLoaderProvider('config/params/generated_config.php', Core\Config\EnvVars::cases())
? new EnvVarLoaderProvider('config/params/generated_config.php', Core\Config\EnvVars::values())
: new ConfigAggregator\ArrayProvider([]),
Mezzio\ConfigProvider::class,
Mezzio\Router\ConfigProvider::class,

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Config;
use function Functional\map;
use function Shlinkio\Shlink\Config\env;
enum EnvVars: string
@ -74,4 +75,13 @@ enum EnvVars: string
{
return $this->loadFromEnv() !== null;
}
/**
* @return string[]
*/
public static function values(): array
{
static $values;
return $values ?? ($values = map(self::cases(), static fn (EnvVars $envVar) => $envVar->value));
}
}

View File

@ -7,6 +7,7 @@ namespace ShlinkioTest\Shlink\Core\Config;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\Config\EnvVars;
use function Functional\map;
use function putenv;
class EnvVarsTest extends TestCase
@ -58,4 +59,11 @@ class EnvVarsTest extends TestCase
yield 'DB_DRIVER without default' => [EnvVars::DB_DRIVER, null, null];
yield 'DB_DRIVER with default' => [EnvVars::DB_DRIVER, 'foobar', 'foobar'];
}
/** @test */
public function allValuesCanBeListed(): void
{
$expected = map(EnvVars::cases(), static fn (EnvVars $envVar) => $envVar->value);
self::assertEquals(EnvVars::values(), $expected);
}
}