@@ -236,61 +236,117 @@ public function testLoadEnv()
236
236
putenv ('SYMFONY_DOTENV_VARS ' );
237
237
putenv ('FOO ' );
238
238
putenv ('TEST_APP_ENV ' );
239
+
240
+ $ _ENV ['EXISTING_KEY ' ] = $ _SERVER ['EXISTING_KEY ' ] = 'EXISTING_VALUE ' ;
241
+ putenv ('EXISTING_KEY=EXISTING_VALUE ' );
239
242
};
240
243
241
244
@mkdir ($ tmpdir = sys_get_temp_dir ().'/dotenv ' );
242
245
243
246
$ path = tempnam ($ tmpdir , 'sf- ' );
244
247
245
248
// .env
246
- file_put_contents ($ path , ' FOO=BAR ' );
249
+ file_put_contents ($ path , " FOO=BAR \n EXISTING_KEY=NEW_VALUE " );
247
250
248
251
$ resetContext ();
249
252
(new Dotenv ())->usePutenv ()->loadEnv ($ path , 'TEST_APP_ENV ' );
250
253
$ this ->assertSame ('BAR ' , getenv ('FOO ' ));
251
254
$ this ->assertSame ('dev ' , getenv ('TEST_APP_ENV ' ));
255
+ $ this ->assertSame ('EXISTING_VALUE ' , getenv ('EXISTING_KEY ' ));
256
+ $ this ->assertSame ('EXISTING_VALUE ' , $ _ENV ['EXISTING_KEY ' ]);
257
+
258
+ $ resetContext ();
259
+ (new Dotenv ())->usePutenv ()->loadEnv ($ path , 'TEST_APP_ENV ' , 'dev ' , ['test ' ], true );
260
+ $ this ->assertSame ('BAR ' , getenv ('FOO ' ));
261
+ $ this ->assertSame ('dev ' , getenv ('TEST_APP_ENV ' ));
262
+ $ this ->assertSame ('NEW_VALUE ' , getenv ('EXISTING_KEY ' ));
263
+ $ this ->assertSame ('NEW_VALUE ' , $ _ENV ['EXISTING_KEY ' ]);
252
264
253
265
// .env.local
254
- file_put_contents ("$ path.local " , ' FOO=localBAR ' );
266
+ file_put_contents ("$ path.local " , " FOO=localBAR \n EXISTING_KEY=localNEW_VALUE " );
255
267
256
268
$ resetContext ();
257
269
$ _SERVER ['TEST_APP_ENV ' ] = 'local ' ;
258
270
(new Dotenv ())->usePutenv ()->loadEnv ($ path , 'TEST_APP_ENV ' );
259
271
$ this ->assertSame ('localBAR ' , getenv ('FOO ' ));
272
+ $ this ->assertSame ('EXISTING_VALUE ' , getenv ('EXISTING_KEY ' ));
273
+ $ this ->assertSame ('EXISTING_VALUE ' , $ _ENV ['EXISTING_KEY ' ]);
274
+
275
+ $ resetContext ();
276
+ $ _SERVER ['TEST_APP_ENV ' ] = 'local ' ;
277
+ (new Dotenv ())->usePutenv ()->loadEnv ($ path , 'TEST_APP_ENV ' , 'dev ' , ['test ' ], true );
278
+ $ this ->assertSame ('localBAR ' , getenv ('FOO ' ));
279
+ $ this ->assertSame ('localNEW_VALUE ' , getenv ('EXISTING_KEY ' ));
280
+ $ this ->assertSame ('localNEW_VALUE ' , $ _ENV ['EXISTING_KEY ' ]);
260
281
261
282
// special case for test
262
283
$ resetContext ();
263
284
$ _SERVER ['TEST_APP_ENV ' ] = 'test ' ;
264
285
(new Dotenv ())->usePutenv ()->loadEnv ($ path , 'TEST_APP_ENV ' );
265
286
$ this ->assertSame ('BAR ' , getenv ('FOO ' ));
287
+ $ this ->assertSame ('EXISTING_VALUE ' , getenv ('EXISTING_KEY ' ));
288
+ $ this ->assertSame ('EXISTING_VALUE ' , $ _ENV ['EXISTING_KEY ' ]);
289
+
290
+ $ resetContext ();
291
+ $ _SERVER ['TEST_APP_ENV ' ] = 'test ' ;
292
+ (new Dotenv ())->usePutenv ()->loadEnv ($ path , 'TEST_APP_ENV ' , 'dev ' , ['test ' ], true );
293
+ $ this ->assertSame ('BAR ' , getenv ('FOO ' ));
294
+ $ this ->assertSame ('NEW_VALUE ' , getenv ('EXISTING_KEY ' ));
295
+ $ this ->assertSame ('NEW_VALUE ' , $ _ENV ['EXISTING_KEY ' ]);
266
296
267
297
// .env.dev
268
- file_put_contents ("$ path.dev " , ' FOO=devBAR ' );
298
+ file_put_contents ("$ path.dev " , " FOO=devBAR \n EXISTING_KEY=devNEW_VALUE " );
269
299
270
300
$ resetContext ();
271
301
(new Dotenv ())->usePutenv ()->loadEnv ($ path , 'TEST_APP_ENV ' );
272
302
$ this ->assertSame ('devBAR ' , getenv ('FOO ' ));
303
+ $ this ->assertSame ('EXISTING_VALUE ' , getenv ('EXISTING_KEY ' ));
304
+ $ this ->assertSame ('EXISTING_VALUE ' , $ _ENV ['EXISTING_KEY ' ]);
305
+
306
+ $ resetContext ();
307
+ (new Dotenv ())->usePutenv ()->loadEnv ($ path , 'TEST_APP_ENV ' , 'dev ' , ['test ' ], true );
308
+ $ this ->assertSame ('devBAR ' , getenv ('FOO ' ));
309
+ $ this ->assertSame ('devNEW_VALUE ' , getenv ('EXISTING_KEY ' ));
310
+ $ this ->assertSame ('devNEW_VALUE ' , $ _ENV ['EXISTING_KEY ' ]);
273
311
274
312
// .env.dev.local
275
- file_put_contents ("$ path.dev.local " , ' FOO=devlocalBAR ' );
313
+ file_put_contents ("$ path.dev.local " , " FOO=devlocalBAR \n EXISTING_KEY=devlocalNEW_VALUE " );
276
314
277
315
$ resetContext ();
278
316
(new Dotenv ())->usePutenv ()->loadEnv ($ path , 'TEST_APP_ENV ' );
279
317
$ this ->assertSame ('devlocalBAR ' , getenv ('FOO ' ));
318
+ $ this ->assertSame ('EXISTING_VALUE ' , getenv ('EXISTING_KEY ' ));
319
+ $ this ->assertSame ('EXISTING_VALUE ' , $ _ENV ['EXISTING_KEY ' ]);
320
+
321
+ $ resetContext ();
322
+ (new Dotenv ())->usePutenv ()->loadEnv ($ path , 'TEST_APP_ENV ' , 'dev ' , ['test ' ], true );
323
+ $ this ->assertSame ('devlocalBAR ' , getenv ('FOO ' ));
324
+ $ this ->assertSame ('devlocalNEW_VALUE ' , getenv ('EXISTING_KEY ' ));
325
+ $ this ->assertSame ('devlocalNEW_VALUE ' , $ _ENV ['EXISTING_KEY ' ]);
280
326
unlink ("$ path.local " );
281
327
unlink ("$ path.dev " );
282
328
unlink ("$ path.dev.local " );
283
329
284
330
// .env.dist
285
- file_put_contents ("$ path.dist " , ' FOO=distBAR ' );
331
+ file_put_contents ("$ path.dist " , " FOO=distBAR \n EXISTING_KEY=distNEW_VALUE " );
286
332
287
333
$ resetContext ();
288
334
unlink ($ path );
289
335
(new Dotenv ())->usePutenv ()->loadEnv ($ path , 'TEST_APP_ENV ' );
290
336
$ this ->assertSame ('distBAR ' , getenv ('FOO ' ));
337
+ $ this ->assertSame ('EXISTING_VALUE ' , getenv ('EXISTING_KEY ' ));
338
+ $ this ->assertSame ('EXISTING_VALUE ' , $ _ENV ['EXISTING_KEY ' ]);
339
+
340
+ $ resetContext ();
341
+ (new Dotenv ())->usePutenv ()->loadEnv ($ path , 'TEST_APP_ENV ' , 'dev ' , ['test ' ], true );
342
+ $ this ->assertSame ('distBAR ' , getenv ('FOO ' ));
343
+ $ this ->assertSame ('distNEW_VALUE ' , getenv ('EXISTING_KEY ' ));
344
+ $ this ->assertSame ('distNEW_VALUE ' , $ _ENV ['EXISTING_KEY ' ]);
291
345
unlink ("$ path.dist " );
292
346
293
347
$ resetContext ();
348
+ unset($ _ENV ['EXISTING_KEY ' ], $ _SERVER ['EXISTING_KEY ' ]);
349
+ putenv ('EXISTING_KEY ' );
294
350
rmdir ($ tmpdir );
295
351
}
296
352
@@ -490,22 +546,37 @@ public function testBootEnv()
490
546
unset($ _SERVER ['TEST_APP_ENV ' ], $ _ENV ['TEST_APP_ENV ' ]);
491
547
unset($ _SERVER ['TEST_APP_DEBUG ' ], $ _ENV ['TEST_APP_DEBUG ' ]);
492
548
unset($ _SERVER ['FOO ' ], $ _ENV ['FOO ' ]);
549
+
550
+ $ _ENV ['EXISTING_KEY ' ] = $ _SERVER ['EXISTING_KEY ' ] = 'EXISTING_VALUE ' ;
493
551
};
494
552
495
553
@mkdir ($ tmpdir = sys_get_temp_dir ().'/dotenv ' );
496
554
$ path = tempnam ($ tmpdir , 'sf- ' );
497
555
498
- file_put_contents ($ path , ' FOO=BAR ' );
556
+ file_put_contents ($ path , " FOO=BAR \n EXISTING_KEY=NEW_VALUE " );
499
557
$ resetContext ();
500
558
(new Dotenv ('TEST_APP_ENV ' , 'TEST_APP_DEBUG ' ))->bootEnv ($ path );
501
559
$ this ->assertSame ('BAR ' , $ _SERVER ['FOO ' ]);
560
+ $ this ->assertSame ('EXISTING_VALUE ' , $ _SERVER ['EXISTING_KEY ' ]);
561
+
562
+ $ resetContext ();
563
+ (new Dotenv ('TEST_APP_ENV ' , 'TEST_APP_DEBUG ' ))->bootEnv ($ path , 'dev ' , ['test ' ], true );
564
+ $ this ->assertSame ('BAR ' , $ _SERVER ['FOO ' ]);
565
+ $ this ->assertSame ('NEW_VALUE ' , $ _SERVER ['EXISTING_KEY ' ]);
502
566
unlink ($ path );
503
567
504
- file_put_contents ($ path .'.local.php ' , '<?php return ["TEST_APP_ENV" => "dev", "FOO" => "BAR"]; ' );
568
+ file_put_contents ($ path .'.local.php ' , '<?php return ["TEST_APP_ENV" => "dev", "FOO" => "BAR", "EXISTING_KEY" => "localphpNEW_VALUE" ]; ' );
505
569
$ resetContext ();
506
570
(new Dotenv ('TEST_APP_ENV ' , 'TEST_APP_DEBUG ' ))->bootEnv ($ path );
507
571
$ this ->assertSame ('BAR ' , $ _SERVER ['FOO ' ]);
508
572
$ this ->assertSame ('1 ' , $ _SERVER ['TEST_APP_DEBUG ' ]);
573
+ $ this ->assertSame ('EXISTING_VALUE ' , $ _SERVER ['EXISTING_KEY ' ]);
574
+
575
+ $ resetContext ();
576
+ (new Dotenv ('TEST_APP_ENV ' , 'TEST_APP_DEBUG ' ))->bootEnv ($ path , 'dev ' , ['test ' ], true );
577
+ $ this ->assertSame ('BAR ' , $ _SERVER ['FOO ' ]);
578
+ $ this ->assertSame ('1 ' , $ _SERVER ['TEST_APP_DEBUG ' ]);
579
+ $ this ->assertSame ('localphpNEW_VALUE ' , $ _SERVER ['EXISTING_KEY ' ]);
509
580
unlink ($ path .'.local.php ' );
510
581
511
582
$ resetContext ();
0 commit comments