From e17556a7ae86f6151c32e8ad8fca640cb45a8fd9 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 14 Oct 2024 08:55:09 +0200 Subject: [PATCH] Add ReadEnvVarCommand test --- docker/docker-entrypoint.sh | 12 ++--- .../src/Command/Config/ReadEnvVarCommand.php | 12 ++++- .../Command/Config/ReadEnvVarCommandTest.php | 54 +++++++++++++++++++ 3 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 module/CLI/test/Command/Config/ReadEnvVarCommandTest.php diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh index 6dddf104..e2cce68f 100644 --- a/docker/docker-entrypoint.sh +++ b/docker/docker-entrypoint.sh @@ -9,18 +9,18 @@ mkdir -p data/cache data/locks data/log data/proxies flags="--no-interaction --clear-db-cache" # Read env vars through Shlink command, so that it applies the `_FILE` env var fallback logic -GEOLITE_LICENSE_KEY=$(bin/cli env-var:read GEOLITE_LICENSE_KEY) -SKIP_INITIAL_GEOLITE_DOWNLOAD=$(bin/cli env-var:read SKIP_INITIAL_GEOLITE_DOWNLOAD) -INITIAL_API_KEY=$(bin/cli env-var:read INITIAL_API_KEY) +geolite_license_key=$(bin/cli env-var:read GEOLITE_LICENSE_KEY) +skip_initial_geolite_download=$(bin/cli env-var:read SKIP_INITIAL_GEOLITE_DOWNLOAD) +initial_api_key=$(bin/cli env-var:read INITIAL_API_KEY) # Skip downloading GeoLite2 db file if the license key env var was not defined or skipping was explicitly set -if [ -z "${GEOLITE_LICENSE_KEY}" ] || [ "${SKIP_INITIAL_GEOLITE_DOWNLOAD}" = "true" ]; then +if [ -z "${geolite_license_key}" ] || [ "${skip_initial_geolite_download}" = "true" ]; then flags="${flags} --skip-download-geolite" fi # If INITIAL_API_KEY was provided, create an initial API key -if [ -n "${INITIAL_API_KEY}" ]; then - flags="${flags} --initial-api-key=${INITIAL_API_KEY}" +if [ -n "${initial_api_key}" ]; then + flags="${flags} --initial-api-key=${initial_api_key}" fi php vendor/bin/shlink-installer init ${flags} diff --git a/module/CLI/src/Command/Config/ReadEnvVarCommand.php b/module/CLI/src/Command/Config/ReadEnvVarCommand.php index 4eac16e4..1f436eeb 100644 --- a/module/CLI/src/Command/Config/ReadEnvVarCommand.php +++ b/module/CLI/src/Command/Config/ReadEnvVarCommand.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Shlinkio\Shlink\CLI\Command\Config; +use Closure; use Shlinkio\Shlink\CLI\Util\ExitCode; use Shlinkio\Shlink\Core\Config\EnvVars; use Symfony\Component\Console\Command\Command; @@ -22,6 +23,15 @@ class ReadEnvVarCommand extends Command { public const NAME = 'env-var:read'; + /** @var Closure(string $envVar): mixed */ + private readonly Closure $loadEnvVar; + + public function __construct(?Closure $loadEnvVar = null) + { + $this->loadEnvVar = $loadEnvVar ?? static fn (string $envVar) => EnvVars::from($envVar)->loadFromEnv(); + parent::__construct(); + } + protected function configure(): void { $this @@ -51,7 +61,7 @@ class ReadEnvVarCommand extends Command protected function execute(InputInterface $input, OutputInterface $output): int { $envVar = $input->getArgument('envVar'); - $output->writeln(formatEnvVarValue(EnvVars::from($envVar)->loadFromEnv())); + $output->writeln(formatEnvVarValue(($this->loadEnvVar)($envVar))); return ExitCode::EXIT_SUCCESS; } diff --git a/module/CLI/test/Command/Config/ReadEnvVarCommandTest.php b/module/CLI/test/Command/Config/ReadEnvVarCommandTest.php new file mode 100644 index 00000000..c377cf86 --- /dev/null +++ b/module/CLI/test/Command/Config/ReadEnvVarCommandTest.php @@ -0,0 +1,54 @@ +commandTester = CliTestUtils::testerForCommand(new ReadEnvVarCommand(fn () => $this->envVarValue)); + } + + #[Test] + public function errorIsThrownIfProvidedEnvVarIsInvalid(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('foo is not a valid Shlink environment variable'); + + $this->commandTester->execute(['envVar' => 'foo']); + } + + #[Test] + public function valueIsPrintedIfProvidedEnvVarIsValid(): void + { + $this->commandTester->execute(['envVar' => EnvVars::BASE_PATH->value]); + $output = $this->commandTester->getDisplay(); + + self::assertStringNotContainsString('Select the env var to read', $output); + self::assertStringContainsString($this->envVarValue, $output); + } + + #[Test] + public function envVarNameIsRequestedIfArgumentIsMissing(): void + { + $this->commandTester->setInputs([EnvVars::BASE_PATH->value]); + $this->commandTester->execute([]); + $output = $this->commandTester->getDisplay(); + + self::assertStringContainsString('Select the env var to read', $output); + self::assertStringContainsString($this->envVarValue, $output); + } +}