From 80497db810a204356c9a4efe12739e78d2fdf9e7 Mon Sep 17 00:00:00 2001 From: Vitor de Souza Date: Mon, 27 Aug 2018 00:14:54 +0200 Subject: [PATCH] Fixing the boolean representation when using 1/0 Until now, if we had something like APP_DEBUG=0 in `.env.dist` file, it was displayed like: ``` > Companienv\Composer\ScriptHandler::run It looks like you are missing some configuration (1 variables). I will help you to sort this out. Let's fix this? (y) APP_DEBUG ? ``` So you have no idea what type of value you should place here (true? 1? something else?). There's no problems with using "true" and "false", however, since we can represent booleans with 1 and 0 as well, we need to display it properly, as in: ``` > Companienv\Composer\ScriptHandler::run It looks like you are missing some configuration (1 variables). I will help you to sort this out. Let's fix this? (y) APP_DEBUG ? (0) ``` So now you know you should place either "0" or "1" here. The problem was actually just checking the if($variable) instead of comparing using "!== ''", so it was considering the string "0" as false and thus not display properly the default value after the question mark. I've also added a test for checking this behaviour and added parenthesis some lines up the "if" I've changed, for clarity of the null-coalesce/ternary precedence. --- features/fill-variables.feature | 18 ++++++++++++++++++ .../Interaction/AskVariableValues.php | 4 ++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/features/fill-variables.feature b/features/fill-variables.feature index 5659b38..548d183 100644 --- a/features/fill-variables.feature +++ b/features/fill-variables.feature @@ -107,3 +107,21 @@ Feature: """ A_BASE64_VALUE=abc123= """ + + Scenario: It displays correctly boolean with 1 and 0 + Given the file ".env.dist" contains: + """ + ## Something + MY_VARIABLE=0 + """ + And the file ".env" contains: + """ + MY_VARIABLE= + """ + When I run the companion with the following answers: + | Let's fix this? (y) | y | + | MY_VARIABLE ? (0) | 0 | + And the file ".env" should contain: + """ + MY_VARIABLE=0 + """ diff --git a/src/Companienv/Interaction/AskVariableValues.php b/src/Companienv/Interaction/AskVariableValues.php index d2c5c61..91744f1 100644 --- a/src/Companienv/Interaction/AskVariableValues.php +++ b/src/Companienv/Interaction/AskVariableValues.php @@ -15,10 +15,10 @@ class AskVariableValues implements Extension public function getVariableValue(Companion $companion, Block $block, Variable $variable) { $definedVariablesHash = $companion->getDefinedVariablesHash(); - $defaultValue = $definedVariablesHash[$variable->getName()] ?? $variable->getValue() ?: $variable->getValue(); + $defaultValue = ($definedVariablesHash[$variable->getName()] ?? $variable->getValue()) ?: $variable->getValue(); $question = sprintf('%s ? ', $variable->getName()); - if ($defaultValue) { + if ($defaultValue !== '') { $question .= '('.$defaultValue.') '; }