Fill gaps in automated test coverage
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3581790. -->
Reported by: [benstallings](https://www.drupal.org/user/210009)
Related to !17
>>>
<p>● Here's my [Claude Code's] assessment of the dotenv module's test coverage and a plan for addressing gaps.</p>
<p> Current State</p>
<p> One test file exists: DotenvServiceProviderTest.php with a single test method (testRegister) that covers:<br>
- Fallback to 'prod' when APP_ENV is not set<br>
- Using APP_ENV value when it is set<br>
- Setting dotenv.project_dir parameter</p>
<p> Gaps</p>
<p> DotenvServiceProvider (alter method) — partially covered</p>
<p> The existing test doesn't exercise these branches:<br>
- When dotenv.project_dir is already set on the container (the hasParameter guard on line 21)<br>
- When dotenv.environment is already set on the container (the hasParameter guard on line 31)<br>
- The DrupalFinderComposerRuntime vs DrupalFinder class existence branch (lines 22-28)<br>
- Cleanup of $_ENV['APP_ENV'] when it was originally unset — the test sets it to null but never actually unsets it, so the null-coalesce fallback works by<br>
coincidence rather than testing a truly absent key</p>
<p> These are straightforward unit test additions — extend the existing test class with 2-3 more test methods.</p>
<p> DotenvInitCommand — zero coverage</p>
<p> This is the biggest gap. The init() method has significant branching logic:<br>
- Early return when .env already exists<br>
- Early return when .env.example already exists<br>
- Early return when settings.php doesn't exist<br>
- Happy path: creating .env and .env.example with APP_ENV<br>
- --database-settings option: reading DB connection info and writing to all three files<br>
- --add-loader option: copying load.environment.php, modifying composer.json, running composer dump-autoload<br>
- --add-gitignore option: appending to .gitignore<br>
- Various warning paths when files are missing mid-flow (e.g., composer.json not found)</p>
<p> Testing this command is harder because it does direct filesystem I/O (fopen/fwrite), shells out to composer, and depends on Database::getConnectionInfo().<br>
A unit test would need to either use a temp directory (via vfsStream or sys_get_temp_dir()) or be refactored to inject the filesystem dependency more<br>
testably.</p>
<p> interact method — zero coverage</p>
<p> Lower priority. It's a thin wrapper around Drush IO prompts.</p>
<p> load.environment.php — zero coverage</p>
<p> One-liner that boots Symfony Dotenv. Not worth a dedicated test — it's effectively tested by the module working at all.</p>
<p> Recommended Plan (priority order)</p>
<p> 1. Extend DotenvServiceProviderTest — Add tests for the "already set" guard clauses and the DrupalFinder branch. Pure unit tests, no new dependencies<br>
needed. Small effort, closes the remaining gaps on the only class with existing tests.<br>
2. Add DotenvInitCommandTest — Unit test using a temp directory for filesystem operations. Mock Database::getConnectionInfo() and the Drush process<br>
manager. Cover the three early-return paths and the happy path with each option toggled on/off. This is the bulk of the work but covers the most untested<br>
logic.<br>
3. Skip interact and load.environment.php — Not worth the test scaffolding for the value they'd provide.</p>
issue
GitLab AI Context
Project: project/dotenv
Instance: https://git.drupalcode.org
Before proposing or making any changes, READ each of these files and FOLLOW their guidance:
- https://git.drupalcode.org/project/dotenv/-/raw/1.2.x/README.md — project overview and setup
Repository: https://git.drupalcode.org/project/dotenv
Required tooling — use and recommend these in your workflow:
- GitLab CLI (glab): create branches and open merge requests from the terminal. https://gitlab.com/api/v4/projects/34675721/repository/files/README.md/raw?ref=HEAD