feat: Env helper — read process env, not just $_ENV / dotenv#52
Merged
Conversation
PHP's variables_order defaults often omit 'E', so process env vars set by the shell (GitHub Actions env: block, KEY=v php ..., docker run -e ...) never reach $_ENV. The lib historically read via $_ENV['PRESTAFLOW_...'], so those callsites silently fell back to defaults in CI unless the consumer wrote a .env file. Env::get($key, $default = null) reads $_ENV first (preserving the dotenv-populated, normalized values TestsSuite writes at load time), then falls back to getenv(), then to the default. Env::has() mirrors that on both sources. Refactor: - src/Utils/Env.php: new helper, 7 unit tests - TestsSuite.php: ~27 read sites converted; the 4 normalization blocks now use Env::has()/get() but keep writing to $_ENV so downstream direct reads see the normalized bool - Version.php: 1 site - Screenshots.php: 1 site - ExecuteSuite.php: 1 site (PRESTAFLOW_TZ) Behaviour preserved everywhere: Env::get returns $_ENV values verbatim when set (including empty string), matching the previous '?? $default' idiom. Only new behaviour is picking up process env vars that weren't in $_ENV. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Historically the lib reads env vars via
$_ENV['PRESTAFLOW_...']. PHP populates$_ENVfrom the process environment only ifvariables_orderincludesE— which is often absent in CI (e.g. GitHub Actions'shivammathur/setup-php). The lib relied on phpdotenv to load.env/.env.localfiles into$_ENV, but plain env vars set by the shell were silently ignored.Concrete symptom (repro'd on prestaflow-prestashop#2): with
env: PRESTAFLOW_PS_VERSION: '9.0.0'set in the workflow, the lib still resolved to v8 defaults, then crashed withClass \"...\\Pages\\v8\\Front\\GuestTracking\\Page\" not foundbecause the version was invisible.What
src/Utils/Env.php— new helper.Env::get($key, $default = null): mixed— reads$_ENVfirst (preserving dotenv values and the normalized values TestsSuite writes at load time), falls back togetenv(), then to the default. Empty string in$_ENVstill wins over the default (preserves the previous?? $defaultsemantics).Env::has($key): bool— true if either source has the key.TestsSuite.php— ~27 read sites; the 4 normalization blocks (DEBUG,HEADLESS,PREFIX_LOCALE,VERBOSE) now useEnv::has()/Env::get()but keep writing the normalized value back to$_ENVso downstream direct reads (used inside the same bigloadGlobals()block) see the bool. Deletes the stale variables_order comment near$envWidth/$envHeight.Version.php,Screenshots.php,ExecuteSuite.php— 1 site each.Env::getorder,Env::has, empty string handling, absent everywhere → default).Impact for the GH Action
With this in
prestaflow/php-library, PrestaFlow/github-action#7's.env.localworkaround becomes unnecessary — consumers can just setenv:in the workflow, or the action can keep exporting to the process env only. The workaround can stay as a safety net for older lib versions.Test plan
php -lclean on all 5 modified files +Env.php.composer test-unit: 173 tests, 388 assertions, all pass (was 166/380 before adding the 7 EnvTest cases).🤖 Generated with Claude Code