Skip to content

feat: Env helper — read process env, not just $_ENV / dotenv#52

Merged
PrestaEdit merged 1 commit into
devfrom
feat/env-helper
Jul 12, 2026
Merged

feat: Env helper — read process env, not just $_ENV / dotenv#52
PrestaEdit merged 1 commit into
devfrom
feat/env-helper

Conversation

@PrestaEdit

Copy link
Copy Markdown
Contributor

Why

Historically the lib reads env vars via $_ENV['PRESTAFLOW_...']. PHP populates $_ENV from the process environment only if variables_order includes E — which is often absent in CI (e.g. GitHub Actions' shivammathur/setup-php). The lib relied on phpdotenv to load .env / .env.local files 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 with Class \"...\\Pages\\v8\\Front\\GuestTracking\\Page\" not found because the version was invisible.

What

  • src/Utils/Env.php — new helper.
    • Env::get($key, $default = null): mixed — reads $_ENV first (preserving dotenv values and the normalized values TestsSuite writes at load time), falls back to getenv(), then to the default. Empty string in $_ENV still wins over the default (preserves the previous ?? $default semantics).
    • Env::has($key): bool — true if either source has the key.
  • Refactor sites (behaviour preserved everywhere):
    • TestsSuite.php — ~27 read sites; the 4 normalization blocks (DEBUG, HEADLESS, PREFIX_LOCALE, VERBOSE) now use Env::has()/Env::get() but keep writing the normalized value back to $_ENV so downstream direct reads (used inside the same big loadGlobals() block) see the bool. Deletes the stale variables_order comment near $envWidth/$envHeight.
    • Version.php, Screenshots.php, ExecuteSuite.php — 1 site each.
  • 7 unit tests on the helper (Env::get order, 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.local workaround becomes unnecessary — consumers can just set env: 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 -l clean 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).
  • After merge + tagging, re-run github-action-smoke and prestaflow-prestashop#2 to confirm the class-not-found error is gone.

🤖 Generated with Claude Code

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>
@PrestaEdit
PrestaEdit merged commit ea12978 into dev Jul 12, 2026
@PrestaEdit
PrestaEdit deleted the feat/env-helper branch July 12, 2026 07:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant