shlink/module/Core/test/Config/EnvVarsTest.php

58 lines
1.9 KiB
PHP
Raw Normal View History

2022-01-20 13:40:34 -06:00
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Config;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
2022-01-20 13:40:34 -06:00
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\Config\EnvVars;
use function putenv;
class EnvVarsTest extends TestCase
{
protected function setUp(): void
{
2022-04-23 05:44:17 -05:00
putenv(EnvVars::BASE_PATH->value . '=the_base_path');
putenv(EnvVars::DB_NAME->value . '=shlink');
2022-01-20 13:40:34 -06:00
}
protected function tearDown(): void
{
2022-04-23 05:44:17 -05:00
putenv(EnvVars::BASE_PATH->value . '=');
putenv(EnvVars::DB_NAME->value . '=');
2022-01-20 13:40:34 -06:00
}
#[Test, DataProvider('provideExistingEnvVars')]
2022-01-20 13:40:34 -06:00
public function existsInEnvReturnsExpectedValue(EnvVars $envVar, bool $exists): void
{
self::assertEquals($exists, $envVar->existsInEnv());
}
2023-02-09 02:32:38 -06:00
public static function provideExistingEnvVars(): iterable
2022-01-20 13:40:34 -06:00
{
2022-04-23 05:44:17 -05:00
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];
2022-01-20 13:40:34 -06:00
}
#[Test, DataProvider('provideEnvVarsValues')]
2022-01-20 13:40:34 -06:00
public function expectedValueIsLoadedFromEnv(EnvVars $envVar, mixed $expected, mixed $default): void
{
self::assertEquals($expected, $envVar->loadFromEnv($default));
}
2023-02-09 02:32:38 -06:00
public static function provideEnvVarsValues(): iterable
2022-01-20 13:40:34 -06:00
{
2022-04-23 05:44:17 -05:00
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'];
2022-01-20 13:40:34 -06:00
}
}