dotenv

Форк
0
/
DotenvTest.php 
478 строк · 14.5 Кб
1
<?php
2

3
declare(strict_types=1);
4

5

6
use Enjoys\Dotenv\Dotenv;
7
use PHPUnit\Framework\TestCase;
8

9
class DotenvTest extends TestCase
10
{
11

12
    protected function setUp(): void
13
    {
14
        Dotenv::clear();
15
    }
16

17
    protected function tearDown(): void
18
    {
19
        Dotenv::clear();
20
    }
21

22
    /**
23
     * @param class-string|object $className
24
     * @param string $propertyName
25
     * @return ReflectionProperty
26
     * @throws ReflectionException
27
     */
28
    public function getPrivateProperty($className, string $propertyName): ReflectionProperty
29
    {
30
        $reflector = new ReflectionClass($className);
31
        $property = $reflector->getProperty($propertyName);
32
        $property->setAccessible(true);
33

34
        return $property;
35
    }
36

37

38
    public function testVariableReplace()
39
    {
40
        $dotenv = new Dotenv(__DIR__ . '/fixtures/1/.env');
41
        $dotenv->loadEnv();
42

43
        $this->assertSame('dev', $_ENV['APP_ENV']);
44
        $this->assertSame('C:/openserver/test', $_ENV['TEST_DIR']);
45
    }
46

47
    public function testVariableReplace2()
48
    {
49
        putenv('APP_ENV=test');
50
        $dotenv = new Dotenv(__DIR__ . '/fixtures/1/.env');
51
        $dotenv->loadEnv();
52

53
        $this->assertSame('test', $_ENV['APP_ENV']);
54
        $this->assertSame('/var/testing/test', $_ENV['TEST_DIR']);
55
        putenv('APP_ENV'); //unset
56
    }
57

58
    public function testVariableReplaceRecursive()
59
    {
60
        $dotenv = new Dotenv(__DIR__ . '/fixtures/4/env');
61
        $dotenv->loadEnv();
62

63
        $this->assertSame('/var/www/public', $_ENV['VAR2']);
64
        $this->assertSame('/var/www/public/upload', $_ENV['VAR3']);
65
        $this->assertSame('/var/www/public/upload', $_ENV['VAR4']);
66
        $this->assertSame('/var/www/public/upload', $_ENV['VAR5']);
67
        $this->assertSame('/var/www/public/upload', $_ENV['VAR6']);
68
        $this->assertSame('/var/www/public/upload', $_ENV['VAR7']);
69
    }
70

71
    public function testVariableReplaceRecursiveNonLineage()
72
    {
73
//        $this->expectException(InvalidArgumentException::class);
74
        $dotenv = new Dotenv(__DIR__ . '/fixtures/5/env');
75
        $dotenv->loadEnv();
76
        $this->assertSame('/var/www/public', $_ENV['VAR2']);
77
        $this->assertSame('/var/www/public/upload', $_ENV['VAR3']);
78
        $this->assertSame('/var/www/public/upload', $_ENV['VAR4']);
79
        $this->assertSame('/var/www/public/upload', $_ENV['VAR5']);
80
        $this->assertSame('/var/www/public/upload', $_ENV['VAR6']);
81
    }
82

83
    public function testVariablesNotFound()
84
    {
85
        $this->expectExceptionMessage('Not set variable ${BAZ}.');
86
        $dotenv = new Dotenv(__DIR__ . '/fixtures/invalid/.notfoundvars');
87
        $dotenv->loadEnv();
88
    }
89

90
    public function testCastType()
91
    {
92
        $dotenv = new Dotenv(__DIR__ . '/fixtures/2/.env');
93
        $dotenv->enableCastType();
94
        $dotenv->loadEnv();
95

96
        $this->assertSame("*int 42", $_ENV['VAR_1']);
97
        $this->assertSame(2, $_ENV['VAR_1_1']);
98
        $this->assertSame("*int              4", $_ENV['VAR_1_2']);
99
        $this->assertSame('*int not digit', $_ENV['VAR_1_7']);
100
        $this->assertSame("*string *int", $_ENV['VAR_1_8']);
101
        $this->assertSame(42, $_ENV['VAR_1_3']);
102
        $this->assertSame('test *int 5', $_ENV['VAR_1_4']);
103
        $this->assertSame(0755, $_ENV['VAR_1_5']);
104
        $this->assertSame(0xA, $_ENV['VAR_1_6']);
105
        $this->assertSame(true, $_ENV['VAR_2']);
106
        $this->assertSame(false, $_ENV['VAR_3']);
107
        $this->assertSame(null, $_ENV['VAR_4']);
108
        $this->assertSame(3.14, $_ENV['VAR_5']);
109
        $this->assertSame(3.14, $_ENV['VAR_6']);
110
        $this->assertSame(3.14, $_ENV['VAR_7']);
111
        $this->assertSame(3.14, $_ENV['VAR_7_1']);
112
        $this->assertSame('3.14', $_ENV['VAR_7_2']);
113
        $this->assertSame('', $_ENV['VAR_8']);
114
        $this->assertSame('va', $_ENV['VAR_9']);
115
    }
116

117
    public function testAutoCastType()
118
    {
119
        $dotenv = new Dotenv(__DIR__ . '/fixtures/2/.auto_cast_type');
120
        $dotenv->enableCastType();
121
        $dotenv->loadEnv();
122

123
        $this->assertSame(42, $_ENV['VAR_1']);
124
        $this->assertSame("42", $_ENV['VAR_2']);
125
        $this->assertSame("0755", $_ENV['VAR_3']);
126
        $this->assertSame("0xA", $_ENV['VAR_4']);
127
        $this->assertSame(true, $_ENV['VAR_5']);
128
        $this->assertSame(false, $_ENV['VAR_6']);
129
        $this->assertSame("", $_ENV['VAR_7']);
130
        $this->assertSame(null, $_ENV['VAR_8']);
131
        $this->assertSame(3.14, $_ENV['VAR_9']);
132
        $this->assertSame(3.14, $_ENV['VAR_10']);
133
        $this->assertSame("3.14", $_ENV['VAR_11']);
134
        $this->assertSame("true", $_ENV['VAR_12']);
135
        $this->assertSame("false", $_ENV['VAR_13']);
136
        $this->assertSame($_ENV['VAR_5'], $_ENV['VAR_14']);
137
        $this->assertSame($_ENV['VAR_6'], $_ENV['VAR_15']);
138
        $this->assertSame($_ENV['VAR_1'], $_ENV['VAR_16']);
139
        $this->assertSame($_ENV['VAR_10'], $_ENV['VAR_17']);
140
    }
141

142
    public function testEnvWithEq()
143
    {
144
        $dotenv = new Dotenv(__DIR__ . '/fixtures/3/.env.dist');
145
        $dotenv->loadEnv();
146
        $this->assertSame('foo bar = zed', $_ENV['VAR']);
147
    }
148

149
    public function testUsePutEnvTrue()
150
    {
151
        $dotenv = new Dotenv(__DIR__ . '/fixtures/3/.env.dist');
152
        $dotenv->loadEnv(true);
153
        $this->assertSame('foo bar = zed', getenv('VAR'));
154

155
        putenv('VAR'); //unset
156
    }
157

158
    public function testUsePutEnvFalse()
159
    {
160
        $dotenv = new Dotenv(__DIR__ . '/fixtures/3/.env.dist');
161
        $dotenv->loadEnv();
162
        $this->assertSame(false, getenv('VAR'));
163
    }
164

165
    public function testNotOverriding()
166
    {
167
        $php_version = 'test';
168
        putenv("PHP_VERSION=$php_version");
169
        $dotenv = new Dotenv(__DIR__ . '/fixtures/3/.env.dist');
170
        $dotenv->loadEnv();
171
        $this->assertSame($php_version, $_ENV['PHP_VERSION']);
172

173
        putenv('PHP_VERSION'); //unset
174
    }
175

176
    public function testWithComment()
177
    {
178
        $dotenv = new Dotenv(__DIR__ . '/fixtures/with_comment/.env');
179
        $dotenv->enableCastType();
180
        $dotenv->loadEnv();
181
        $this->assertSame(false, $_ENV['MY_VAR'] ?? false);
182
        $this->assertSame('      # 23', $_ENV['VAR2']);
183
        $this->assertSame('/var/www', $_ENV['VAR3']);
184
        $this->assertSame(null, $_ENV['VAR4']);
185
    }
186

187
    public function testInvalidKey()
188
    {
189
        $this->expectException(\Enjoys\Dotenv\Exception\InvalidArgumentException::class);
190
        $dotenv = new Dotenv(__DIR__ . '/fixtures/invalid/.error.key');
191
        $dotenv->loadEnv();
192
    }
193

194

195
    public function testReplaceValueInEnvFilesDefinedInPutenvOrExport()
196
    {
197
        putenv('SYS_VAR=123');
198
        putenv('SYS_VAR2=456');
199
        $dotenv = new Dotenv(__DIR__ . '/fixtures/without_defined_vars/.env');
200
        $dotenv->loadEnv();
201
        $this->assertSame('123', $_ENV['VAR2']);
202
        $this->assertSame('456', $_ENV['VAR3']);
203
        putenv('SYS_VAR');
204
        putenv('SYS_VAR2');
205
    }
206

207
    public function testReplaceValueInEnvFilesDefinedInPutenvOrExportButTheyWasBeDefinedInFiles()
208
    {
209
        putenv('SYS_VAR=SYS_VAR');
210
        putenv('SYS_VAR2=SYS_VAR2');
211
        putenv('DEFINED_VAR=987');
212
        $dotenv = new Dotenv(__DIR__ . '/fixtures/without_defined_vars/.env');
213
        $dotenv->loadEnv();
214
        $this->assertSame('987', $_ENV['VAR4']);
215
        putenv('SYS_VAR');
216
        putenv('SYS_VAR2');
217
        putenv('DEFINED_VAR');
218
    }
219

220
    public function testQuotes()
221
    {
222
        $dotenv = new Dotenv(__DIR__ . '/fixtures/with_qoutes_and_escaping/.quotes');
223
        $dotenv->loadEnv();
224
        $this->assertSame("value in double quotes", $_ENV['VAR1']);
225
        $this->assertSame('value without quotes', $_ENV['VAR2']);
226
        $this->assertSame('value in single quotes', $_ENV['VAR3']);
227
        $this->assertSame('va"', $_ENV['VAR4']);
228
        $this->assertSame('va" lue"', $_ENV['VAR5']);
229
        $this->assertSame("\#\"\\", $_ENV['VAR6']);
230
        $this->assertSame("it\'s single quote", $_ENV['VAR7']);
231
        $this->assertSame("it's double quote", $_ENV['VAR8']);
232
    }
233

234
    public function testSlashes()
235
    {
236
        putenv('VAR=test\"val');
237
        $dotenv = new Dotenv(__DIR__ . '/fixtures/with_qoutes_and_escaping/.slashes');
238
        $dotenv->loadEnv();
239
        $this->assertSame('double quote in middle " text', $_ENV['VAR1']);
240
        $this->assertSame("value. it\'s var #2", $_ENV['VAR2']);
241
        $this->assertSame("\\\\ - two backslashes. not's 4", $_ENV['VAR3']);
242
        $this->assertSame('test\"val', $_ENV['VAR4']);
243
        $this->assertSame($_ENV['VAR1'], $_ENV['VAR5']);
244
        $this->assertSame($_ENV['VAR4'], $_ENV['VAR']);
245
        putenv('VAR');
246
    }
247

248
    public function testEnvArrayAndEnvRawArray()
249
    {
250
        $dotenv = new Dotenv(__DIR__ . '/fixtures/1/.env');
251
        $dotenv->loadEnv();
252

253
        $this->assertSame([
254
            'APP_ENV' => 'dev',
255
            'TEST_DIR' => '${APP_DIR}/test',
256
            'APP_DIR' => 'C:/openserver',
257
        ], $dotenv->getEnvRawArray());
258

259
        $this->assertSame([
260
            'APP_ENV' => 'dev',
261
            'TEST_DIR' => 'C:/openserver/test',
262
            'APP_DIR' => 'C:/openserver',
263
        ], $dotenv->getEnvCollection()->getCollection());
264
    }
265

266
    public function testAutoCastAndEnablePutEnv()
267
    {
268
        $dotenv = new Dotenv(
269
            __DIR__ . '/fixtures/2/.auto_cast_type'
270
        );
271
        $dotenv->enableCastType();
272
        $dotenv->loadEnv(true);
273
        $this->assertSame('42', getenv('VAR_1'));
274
        $this->assertSame('3.14', getenv('VAR_10'));
275
        $this->assertSame('true', getenv('VAR_5'));
276
        $this->assertSame('false', getenv('VAR_6'));
277
        $this->assertSame('', getenv('VAR_8'));
278
    }
279

280
    public function testMultiline()
281
    {
282
        $dotenv = new Dotenv(__DIR__ . '/fixtures/multiline/.env');
283
        $dotenv->loadEnv(true);
284

285
        $this->assertSame(
286
            <<<ENV
287
1
288
2
289
ENV
290
            ,
291
            $_ENV['VAR_MULTILINE']
292
        );
293
        $this->assertSame(
294
            <<<ENV
295
1
296
2
297
ENV
298
            ,
299
            getenv('VAR_MULTILINE')
300
        );
301
        $this->assertSame('1\n2', $_ENV['VAR_NON_MULTILINE']);
302
        $this->assertSame('1\n2', getenv('VAR_NON_MULTILINE'));
303
        $this->assertSame(
304
            <<<ENV
305
A
306
B
307
C=
308
ENV
309
            ,
310
            $_ENV['VAR_MULTILINE2']
311
        );
312
        $this->assertSame(
313
            <<<ENV
314
A
315
B
316
C=
317
ENV,
318
            getenv('VAR_MULTILINE2')
319
        );
320
    }
321

322
    public function testMultiline1()
323
    {
324
        $dotenv = new Dotenv(__DIR__ . '/fixtures/multiline/1');
325
        $dotenv->loadEnv();
326
        $this->assertSame(
327
            <<<ENV
328
Г
329
Д
330
Ж=
331
ENV
332
            ,
333
            $_ENV['VAR']
334
        );
335
    }
336

337

338
    public function testMultiline2()
339
    {
340
        $dotenv = new Dotenv(__DIR__ . '/fixtures/multiline/2');
341
        $dotenv->loadEnv();
342
        $this->assertSame(
343
            <<<ENV
344
A
345
B
346
C=
347

348
ENV
349
            ,
350
            $_ENV['VAR']
351
        );
352
    }
353

354

355
    public function testMultiline3()
356
    {
357
        $dotenv = new Dotenv(__DIR__ . '/fixtures/multiline/3');
358
        $dotenv->loadEnv();
359
        $this->assertSame(
360
            <<<ENV
361
A
362
B
363
C
364
ENV
365
            ,
366
            $_ENV['VAR']
367
        );
368
    }
369

370
    public function testMultilineInvalid()
371
    {
372
        $this->expectException(\Enjoys\Dotenv\Exception\InvalidArgumentException::class);
373
        $dotenv = new Dotenv(__DIR__ . '/fixtures/multiline/.invalid');
374
        $dotenv->loadEnv();
375
    }
376

377
    public function testClear()
378
    {
379
        $dotenv = new Dotenv(__DIR__ . '/fixtures/1/.env');
380
        $dotenv->loadEnv(true);
381
        $enjoysDotenvArray = $dotenv->getEnvCollection()->getKeys();
382
        $this->assertSame(implode(",", $enjoysDotenvArray), getenv('ENJOYS_DOTENV'));
383
        Dotenv::clear();
384
        foreach ($enjoysDotenvArray as $key) {
385
            $this->assertFalse(getenv($key));
386
            $this->assertFalse($_ENV[$key] ?? false);
387
        }
388
        $this->assertFalse(getenv('ENJOYS_DOTENV'));
389
    }
390

391
    public function testLoadAppEnvFile()
392
    {
393
        $dotenv = new Dotenv(__DIR__ . '/fixtures/loadAppEnvFile/.env');
394
        $dotenv->loadEnv();
395
        $this->assertSame('loaded .env.true', $_ENV['LOADED']);
396
    }
397

398
    public function testNestedManyTimesAppEnvFromFiles()
399
    {
400
        $dotenv = new Dotenv(__DIR__ . '/fixtures/nestedAppEnv/.env');
401
        $dotenv->loadEnv();
402
        $this->assertCount(6, $dotenv->getLoadedPaths());
403
        $this->assertSame('5', $_ENV['VAR']);
404
        $this->assertSame([
405
            'APP_ENV' => '5',
406
            'VAR' => '5',
407
        ], $dotenv->getEnvCollection()->getCollection());
408
        $this->assertSame([
409
            'APP_ENV' => '5',
410
            'VAR' => '5',
411
        ], $dotenv->getEnvRawArray());
412
    }
413

414
    public function testNestedManyTimesAppEnvFromGetEnv()
415
    {
416
        putenv('APP_ENV=3');
417
        $dotenv = new Dotenv(__DIR__ . '/fixtures/nestedAppEnv/.env');
418
        $dotenv->loadEnv();
419
        $this->assertCount(2, $dotenv->getLoadedPaths());
420
        $this->assertSame([
421
            'APP_ENV' => '3',
422
            'VAR' => '3',
423
        ], $dotenv->getEnvCollection()->getCollection());
424
        $this->assertSame([
425
            'APP_ENV' => '4',
426
            'VAR' => '3',
427
        ], $dotenv->getEnvRawArray());
428
        $this->assertSame('3', $_ENV['VAR']);
429
    }
430

431
    public function testOtherImplementationStorage()
432
    {
433
        $storage = $this->getMockForAbstractClass(\Enjoys\Dotenv\StorageInterface::class);
434
        $dotenv = new Dotenv(__DIR__ . '/fixtures/1/.env', storage: $storage);
435
        $dotenv->loadEnv();
436
        $this->assertSame([], $dotenv->getLoadedPaths());
437
    }
438

439
    public function testOtherImplementationParser()
440
    {
441
        $parser = $this->getMockForAbstractClass(\Enjoys\Dotenv\Parser\ParserInterface::class);
442
        $dotenv = new Dotenv(__DIR__ . '/fixtures/1/.env', parser: $parser);
443
        $dotenv->loadEnv();
444
        $this->assertSame([], $dotenv->getEnvCollection()->getCollection());
445
    }
446

447

448
    /**
449
     * @dataProvider dataForTestHandleValue
450
     */
451
    public function testHandleValue($key, $string, $expect)
452
    {
453
        $dotenv = new Dotenv(__DIR__ . '/fixtures/1/.env', flags: Dotenv::CAST_TYPE_ENV_VALUE);
454
        $this->assertSame($expect, $dotenv->handleValue($key, $string));
455
    }
456

457

458
    public function dataForTestHandleValue()
459
    {
460
        return [
461
            ['VAR', '42', 42],
462
            ['VAR', '"42"', '42'],
463
            ['VAR', "'42'", '42'],
464
            ['VAR', null, null],
465
            ['VAR', 'str\\\'ing', 'str\\\'ing'],
466
            ['VAR', '"str\\\'ing"', 'str\'ing'],
467
            ['VAR', "'str\\'ing'", 'str\\\'ing'],
468
        ];
469
    }
470

471
    public function testCommentWithSpecificSymbols()
472
    {
473
        $dotenv = new Dotenv(__DIR__ . '/fixtures/.comment_with_specific_symbols');
474
        $dotenv->loadEnv();
475
        $this->assertSame("1", $_ENV['VAR']);
476
    }
477

478
}
479

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.