/
DotenvTest.php
300 lines (247 loc) · 11.8 KB
/
DotenvTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Dotenv\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\Dotenv\Exception\FormatException;
class DotenvTest extends TestCase
{
/**
* @dataProvider getEnvDataWithFormatErrors
*/
public function testParseWithFormatError($data, $error)
{
$dotenv = new Dotenv();
try {
$dotenv->parse($data);
$this->fail('Should throw a FormatException');
} catch (FormatException $e) {
$this->assertStringMatchesFormat($error, $e->getMessage());
}
}
public function getEnvDataWithFormatErrors()
{
$tests = array(
array('FOO=BAR BAZ', "A value containing spaces must be surrounded by quotes in \".env\" at line 1.\n...FOO=BAR BAZ...\n ^ line 1 offset 11"),
array('FOO BAR=BAR', "Whitespace are not supported after the variable name in \".env\" at line 1.\n...FOO BAR=BAR...\n ^ line 1 offset 3"),
array('FOO', "Missing = in the environment variable declaration in \".env\" at line 1.\n...FOO...\n ^ line 1 offset 3"),
array('FOO="foo', "Missing quote to end the value in \".env\" at line 1.\n...FOO=\"foo...\n ^ line 1 offset 8"),
array('FOO=\'foo', "Missing quote to end the value in \".env\" at line 1.\n...FOO='foo...\n ^ line 1 offset 8"),
array('export FOO', "Unable to unset an environment variable in \".env\" at line 1.\n...export FOO...\n ^ line 1 offset 10"),
array('FOO=${FOO', "Unclosed braces on variable expansion in \".env\" at line 1.\n...FOO=\${FOO...\n ^ line 1 offset 9"),
);
if ('\\' !== \DIRECTORY_SEPARATOR) {
$tests[] = array('FOO=$((1dd2))', "Issue expanding a command (%s\n) in \".env\" at line 1.\n...FOO=$((1dd2))...\n ^ line 1 offset 13");
}
return $tests;
}
/**
* @dataProvider getEnvData
*/
public function testParse($data, $expected)
{
$dotenv = new Dotenv();
$this->assertSame($expected, $dotenv->parse($data));
}
public function getEnvData()
{
putenv('LOCAL=local');
$_ENV['REMOTE'] = 'remote';
$tests = array(
// backslashes
array('FOO=foo\\\\bar', array('FOO' => 'foo\\bar')),
array("FOO='foo\\\\bar'", array('FOO' => 'foo\\\\bar')),
array('FOO="foo\\\\bar"', array('FOO' => 'foo\\bar')),
// escaped backslash in front of variable
array("BAR=bar\nFOO=foo\\\\\$BAR", array('BAR' => 'bar', 'FOO' => 'foo\\bar')),
array("BAR=bar\nFOO='foo\\\\\$BAR'", array('BAR' => 'bar', 'FOO' => 'foo\\\\$BAR')),
array("BAR=bar\nFOO=\"foo\\\\\$BAR\"", array('BAR' => 'bar', 'FOO' => 'foo\\bar')),
array('FOO=foo\\\\\\$BAR', array('FOO' => 'foo\\$BAR')),
array('FOO=\'foo\\\\\\$BAR\'', array('FOO' => 'foo\\\\\\$BAR')),
array('FOO="foo\\\\\\$BAR"', array('FOO' => 'foo\\$BAR')),
// spaces
array('FOO=bar', array('FOO' => 'bar')),
array(' FOO=bar ', array('FOO' => 'bar')),
array('FOO=', array('FOO' => '')),
array("FOO=\n\n\nBAR=bar", array('FOO' => '', 'BAR' => 'bar')),
array('FOO= ', array('FOO' => '')),
array("FOO=\nBAR=bar", array('FOO' => '', 'BAR' => 'bar')),
// newlines
array("\n\nFOO=bar\r\n\n", array('FOO' => 'bar')),
array("FOO=bar\r\nBAR=foo", array('FOO' => 'bar', 'BAR' => 'foo')),
array("FOO=bar\rBAR=foo", array('FOO' => 'bar', 'BAR' => 'foo')),
array("FOO=bar\nBAR=foo", array('FOO' => 'bar', 'BAR' => 'foo')),
// quotes
array("FOO=\"bar\"\n", array('FOO' => 'bar')),
array("FOO=\"bar'foo\"\n", array('FOO' => 'bar\'foo')),
array("FOO='bar'\n", array('FOO' => 'bar')),
array("FOO='bar\"foo'\n", array('FOO' => 'bar"foo')),
array("FOO=\"bar\\\"foo\"\n", array('FOO' => 'bar"foo')),
array('FOO="bar\nfoo"', array('FOO' => "bar\nfoo")),
array('FOO="bar\rfoo"', array('FOO' => "bar\rfoo")),
array('FOO=\'bar\nfoo\'', array('FOO' => 'bar\nfoo')),
array('FOO=\'bar\rfoo\'', array('FOO' => 'bar\rfoo')),
array('FOO=" FOO "', array('FOO' => ' FOO ')),
array('FOO=" "', array('FOO' => ' ')),
array('PATH="c:\\\\"', array('PATH' => 'c:\\')),
array("FOO=\"bar\nfoo\"", array('FOO' => "bar\nfoo")),
array('FOO=BAR\\"', array('FOO' => 'BAR"')),
array("FOO=BAR\\'BAZ", array('FOO' => "BAR'BAZ")),
array('FOO=\\"BAR', array('FOO' => '"BAR')),
// concatenated values
array("FOO='bar''foo'\n", array('FOO' => 'barfoo')),
array("FOO='bar '' baz'", array('FOO' => 'bar baz')),
array("FOO=bar\nBAR='baz'\"\$FOO\"", array('FOO' => 'bar', 'BAR' => 'bazbar')),
array("FOO='bar '\\'' baz'", array('FOO' => "bar ' baz")),
// comments
array("#FOO=bar\nBAR=foo", array('BAR' => 'foo')),
array("#FOO=bar # Comment\nBAR=foo", array('BAR' => 'foo')),
array("FOO='bar foo' # Comment", array('FOO' => 'bar foo')),
array("FOO='bar#foo' # Comment", array('FOO' => 'bar#foo')),
array("# Comment\r\nFOO=bar\n# Comment\nBAR=foo", array('FOO' => 'bar', 'BAR' => 'foo')),
array("FOO=bar # Another comment\nBAR=foo", array('FOO' => 'bar', 'BAR' => 'foo')),
array("FOO=\n\n# comment\nBAR=bar", array('FOO' => '', 'BAR' => 'bar')),
array('FOO=NOT#COMMENT', array('FOO' => 'NOT#COMMENT')),
array('FOO= # Comment', array('FOO' => '')),
// edge cases (no conversions, only strings as values)
array('FOO=0', array('FOO' => '0')),
array('FOO=false', array('FOO' => 'false')),
array('FOO=null', array('FOO' => 'null')),
// export
array('export FOO=bar', array('FOO' => 'bar')),
array(' export FOO=bar', array('FOO' => 'bar')),
// variable expansion
array("FOO=BAR\nBAR=\$FOO", array('FOO' => 'BAR', 'BAR' => 'BAR')),
array("FOO=BAR\nBAR=\"\$FOO\"", array('FOO' => 'BAR', 'BAR' => 'BAR')),
array("FOO=BAR\nBAR='\$FOO'", array('FOO' => 'BAR', 'BAR' => '$FOO')),
array("FOO_BAR9=BAR\nBAR=\$FOO_BAR9", array('FOO_BAR9' => 'BAR', 'BAR' => 'BAR')),
array("FOO=BAR\nBAR=\${FOO}Z", array('FOO' => 'BAR', 'BAR' => 'BARZ')),
array("FOO=BAR\nBAR=\$FOO}", array('FOO' => 'BAR', 'BAR' => 'BAR}')),
array("FOO=BAR\nBAR=\\\$FOO", array('FOO' => 'BAR', 'BAR' => '$FOO')),
array('FOO=" \\$ "', array('FOO' => ' $ ')),
array('FOO=" $ "', array('FOO' => ' $ ')),
array('BAR=$LOCAL', array('BAR' => 'local')),
array('BAR=$REMOTE', array('BAR' => 'remote')),
array('FOO=$NOTDEFINED', array('FOO' => '')),
);
if ('\\' !== \DIRECTORY_SEPARATOR) {
$tests = array_merge($tests, array(
// command expansion
array('FOO=$(echo foo)', array('FOO' => 'foo')),
array('FOO=$((1+2))', array('FOO' => '3')),
array('FOO=FOO$((1+2))BAR', array('FOO' => 'FOO3BAR')),
array('FOO=$(echo "$(echo "$(echo "$(echo foo)")")")', array('FOO' => 'foo')),
array("FOO=$(echo \"Quotes won't be a problem\")", array('FOO' => 'Quotes won\'t be a problem')),
array("FOO=bar\nBAR=$(echo \"FOO is \$FOO\")", array('FOO' => 'bar', 'BAR' => 'FOO is bar')),
));
}
return $tests;
}
public function testLoad()
{
unset($_ENV['FOO']);
unset($_ENV['BAR']);
unset($_SERVER['FOO']);
unset($_SERVER['BAR']);
putenv('FOO');
putenv('BAR');
@mkdir($tmpdir = sys_get_temp_dir().'/dotenv');
$path1 = tempnam($tmpdir, 'sf-');
$path2 = tempnam($tmpdir, 'sf-');
file_put_contents($path1, 'FOO=BAR');
file_put_contents($path2, 'BAR=BAZ');
(new Dotenv())->load($path1, $path2);
$foo = getenv('FOO');
$bar = getenv('BAR');
putenv('FOO');
putenv('BAR');
unlink($path1);
unlink($path2);
rmdir($tmpdir);
$this->assertSame('BAR', $foo);
$this->assertSame('BAZ', $bar);
}
/**
* @expectedException \Symfony\Component\Dotenv\Exception\PathException
*/
public function testLoadDirectory()
{
$dotenv = new Dotenv();
$dotenv->load(__DIR__);
}
public function testServerSuperglobalIsNotOverriden()
{
$originalValue = $_SERVER['argc'];
$dotenv = new Dotenv();
$dotenv->populate(array('argc' => 'new_value'));
$this->assertSame($originalValue, $_SERVER['argc']);
}
public function testEnvVarIsNotOverriden()
{
putenv('TEST_ENV_VAR=original_value');
$_SERVER['TEST_ENV_VAR'] = 'original_value';
$dotenv = new Dotenv();
$dotenv->populate(array('TEST_ENV_VAR' => 'new_value'));
$this->assertSame('original_value', getenv('TEST_ENV_VAR'));
}
public function testHttpVarIsPartiallyOverriden()
{
$_SERVER['HTTP_TEST_ENV_VAR'] = 'http_value';
$dotenv = new Dotenv();
$dotenv->populate(array('HTTP_TEST_ENV_VAR' => 'env_value'));
$this->assertSame('env_value', getenv('HTTP_TEST_ENV_VAR'));
$this->assertSame('env_value', $_ENV['HTTP_TEST_ENV_VAR']);
$this->assertSame('http_value', $_SERVER['HTTP_TEST_ENV_VAR']);
}
public function testMemorizingLoadedVarsNamesInSpecialVar()
{
// Special variable not exists
unset($_ENV['SYMFONY_DOTENV_VARS']);
unset($_SERVER['SYMFONY_DOTENV_VARS']);
putenv('SYMFONY_DOTENV_VARS');
unset($_ENV['APP_DEBUG']);
unset($_SERVER['APP_DEBUG']);
putenv('APP_DEBUG');
unset($_ENV['DATABASE_URL']);
unset($_SERVER['DATABASE_URL']);
putenv('DATABASE_URL');
$dotenv = new Dotenv();
$dotenv->populate(array('APP_DEBUG' => '1', 'DATABASE_URL' => 'mysql://root@localhost/db'));
$this->assertSame('APP_DEBUG,DATABASE_URL', getenv('SYMFONY_DOTENV_VARS'));
// Special variable has a value
$_ENV['SYMFONY_DOTENV_VARS'] = 'APP_ENV';
$_SERVER['SYMFONY_DOTENV_VARS'] = 'APP_ENV';
putenv('SYMFONY_DOTENV_VARS=APP_ENV');
$_ENV['APP_DEBUG'] = '1';
$_SERVER['APP_DEBUG'] = '1';
putenv('APP_DEBUG=1');
unset($_ENV['DATABASE_URL']);
unset($_SERVER['DATABASE_URL']);
putenv('DATABASE_URL');
$dotenv = new Dotenv();
$dotenv->populate(array('APP_DEBUG' => '0', 'DATABASE_URL' => 'mysql://root@localhost/db'));
$dotenv->populate(array('DATABASE_URL' => 'sqlite:///somedb.sqlite'));
$this->assertSame('APP_ENV,DATABASE_URL', getenv('SYMFONY_DOTENV_VARS'));
}
public function testOverridingEnvVarsWithNamesMemorizedInSpecialVar()
{
putenv('SYMFONY_DOTENV_VARS='.$_SERVER['SYMFONY_DOTENV_VARS'] = 'FOO,BAR,BAZ');
putenv('FOO=foo');
putenv('BAR=bar');
putenv('BAZ=baz');
putenv('DOCUMENT_ROOT=/var/www');
$dotenv = new Dotenv();
$dotenv->populate(array('FOO' => 'foo1', 'BAR' => 'bar1', 'BAZ' => 'baz1', 'DOCUMENT_ROOT' => '/boot'));
$this->assertSame('foo1', getenv('FOO'));
$this->assertSame('bar1', getenv('BAR'));
$this->assertSame('baz1', getenv('BAZ'));
$this->assertSame('/var/www', getenv('DOCUMENT_ROOT'));
}
}