From 7202605fc89dd925b1795765ce7f747281d63ad7 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 20 Jan 2022 20:40:34 +0100 Subject: [PATCH] Created EnvVarsTest --- module/Core/test/Config/EnvVarsTest.php | 139 ++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 module/Core/test/Config/EnvVarsTest.php diff --git a/module/Core/test/Config/EnvVarsTest.php b/module/Core/test/Config/EnvVarsTest.php new file mode 100644 index 00000000..a7ccbcee --- /dev/null +++ b/module/Core/test/Config/EnvVarsTest.php @@ -0,0 +1,139 @@ +expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid env var: "' . $envVar . '"'); + + EnvVars::{$envVar}(); + } + + public function provideInvalidEnvVars(): iterable + { + yield 'foo' => ['foo']; + yield 'bar' => ['bar']; + yield 'invalid' => ['invalid']; + } + + /** + * @test + * @dataProvider provideExistingEnvVars + */ + public function existsInEnvReturnsExpectedValue(EnvVars $envVar, bool $exists): void + { + self::assertEquals($exists, $envVar->existsInEnv()); + } + + public function provideExistingEnvVars(): iterable + { + yield 'DB_NAME' => [EnvVars::DB_NAME(), true]; + yield 'BASE_PATH' => [EnvVars::BASE_PATH(), true]; + yield 'DB_DRIVER' => [EnvVars::DB_DRIVER(), false]; + yield 'DEFAULT_REGULAR_404_REDIRECT' => [EnvVars::DEFAULT_REGULAR_404_REDIRECT(), false]; + } + + /** + * @test + * @dataProvider provideEnvVarsValues + */ + public function expectedValueIsLoadedFromEnv(EnvVars $envVar, mixed $expected, mixed $default): void + { + self::assertEquals($expected, $envVar->loadFromEnv($default)); + } + + public function provideEnvVarsValues(): iterable + { + yield 'DB_NAME without default' => [EnvVars::DB_NAME(), 'shlink', null]; + yield 'DB_NAME with default' => [EnvVars::DB_NAME(), 'shlink', 'foobar']; + yield 'BASE_PATH without default' => [EnvVars::BASE_PATH(), 'the_base_path', null]; + yield 'BASE_PATH with default' => [EnvVars::BASE_PATH(), 'the_base_path', 'foobar']; + yield 'DB_DRIVER without default' => [EnvVars::DB_DRIVER(), null, null]; + yield 'DB_DRIVER with default' => [EnvVars::DB_DRIVER(), 'foobar', 'foobar']; + } +}