Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 396f1bf

Browse filesBrowse files
minor #43692 [Dotenv] Fix testLoadEnv() to start from a fresh context (fancyweb)
This PR was merged into the 4.4 branch. Discussion ---------- [Dotenv] Fix testLoadEnv() to start from a fresh context | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - The current code doesn't test properly because `SYMFONY_DOTENV_VARS` state is not cleaned for example. We want to test "from the start" everytime here (with no state from a previous load, like it would be in a real app). Commits ------- ed70b78 [Dotenv] Fix testLoadEnv() to start from a fresh context
2 parents 20ad4e6 + ed70b78 commit 396f1bf
Copy full SHA for 396f1bf

File tree

1 file changed

+21
-9
lines changed
Filter options

1 file changed

+21
-9
lines changed

‎src/Symfony/Component/Dotenv/Tests/DotenvTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Dotenv/Tests/DotenvTest.php
+21-9Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -226,59 +226,71 @@ public function testLoad()
226226

227227
public function testLoadEnv()
228228
{
229-
unset($_ENV['FOO']);
230-
unset($_ENV['BAR']);
231-
unset($_SERVER['FOO']);
232-
unset($_SERVER['BAR']);
233-
putenv('FOO');
234-
putenv('BAR');
229+
$resetContext = static function (): void {
230+
unset($_ENV['SYMFONY_DOTENV_VARS']);
231+
unset($_ENV['FOO']);
232+
unset($_ENV['BAR']);
233+
unset($_ENV['TEST_APP_ENV']);
234+
unset($_SERVER['SYMFONY_DOTENV_VARS']);
235+
unset($_SERVER['FOO']);
236+
unset($_SERVER['BAR']);
237+
unset($_SERVER['TEST_APP_ENV']);
238+
putenv('SYMFONY_DOTENV_VARS');
239+
putenv('FOO');
240+
putenv('BAR');
241+
putenv('TEST_APP_ENV');
242+
};
235243

236244
@mkdir($tmpdir = sys_get_temp_dir().'/dotenv');
237245

238246
$path = tempnam($tmpdir, 'sf-');
239247

240248
// .env
241249

250+
$resetContext();
242251
file_put_contents($path, 'FOO=BAR');
243252
(new Dotenv(true))->loadEnv($path, 'TEST_APP_ENV');
244253
$this->assertSame('BAR', getenv('FOO'));
245254
$this->assertSame('dev', getenv('TEST_APP_ENV'));
246255

247256
// .env.local
248257

258+
$resetContext();
249259
$_SERVER['TEST_APP_ENV'] = 'local';
250260
file_put_contents("$path.local", 'FOO=localBAR');
251261
(new Dotenv(true))->loadEnv($path, 'TEST_APP_ENV');
252262
$this->assertSame('localBAR', getenv('FOO'));
253263

254264
// special case for test
255265

266+
$resetContext();
256267
$_SERVER['TEST_APP_ENV'] = 'test';
257268
(new Dotenv(true))->loadEnv($path, 'TEST_APP_ENV');
258269
$this->assertSame('BAR', getenv('FOO'));
259270

260271
// .env.dev
261272

262-
unset($_SERVER['TEST_APP_ENV']);
273+
$resetContext();
263274
file_put_contents("$path.dev", 'FOO=devBAR');
264275
(new Dotenv(true))->loadEnv($path, 'TEST_APP_ENV');
265276
$this->assertSame('devBAR', getenv('FOO'));
266277

267278
// .env.dev.local
268279

280+
$resetContext();
269281
file_put_contents("$path.dev.local", 'FOO=devlocalBAR');
270282
(new Dotenv(true))->loadEnv($path, 'TEST_APP_ENV');
271283
$this->assertSame('devlocalBAR', getenv('FOO'));
272284

273285
// .env.dist
274286

287+
$resetContext();
275288
unlink($path);
276289
file_put_contents("$path.dist", 'BAR=distBAR');
277290
(new Dotenv(true))->loadEnv($path, 'TEST_APP_ENV');
278291
$this->assertSame('distBAR', getenv('BAR'));
279292

280-
putenv('FOO');
281-
putenv('BAR');
293+
$resetContext();
282294
unlink("$path.dist");
283295
unlink("$path.local");
284296
unlink("$path.dev");

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.