1 | <?php |
||||
2 | |||||
3 | namespace Codervio\Envmanager\Tests; |
||||
4 | |||||
5 | use Codervio\Envmanager\Envparser; |
||||
6 | use Codervio\Envmanager\Exceptions\ValueException; |
||||
7 | use Codervio\Envmanager\Exceptions\ParseException; |
||||
8 | use Codervio\Envmanager\Exceptions\ValidationException; |
||||
9 | use PHPUnit\Framework\TestCase; |
||||
10 | |||||
11 | /** |
||||
12 | * Envparser unit test |
||||
13 | * |
||||
14 | */ |
||||
15 | class EnvparserTest extends TestCase |
||||
16 | { |
||||
17 | public $tmpPathEnv; |
||||
18 | public $instance; |
||||
19 | |||||
20 | const DS = DIRECTORY_SEPARATOR; |
||||
21 | |||||
22 | protected function setUp() |
||||
23 | { |
||||
24 | $this->tmpPathEnv = sys_get_temp_dir().self::DS.'EvnmanagerTestEnv.env'; |
||||
25 | |||||
26 | if (!touch($this->tmpPathEnv)) { |
||||
27 | throw new \RuntimeException(sprintf('A test failed. Could not write a file to "%s" path', $this->tmpPathEnv)); |
||||
28 | } |
||||
29 | |||||
30 | $tmpData = <<<EOF |
||||
31 | FOO=bar |
||||
32 | EOF; |
||||
33 | |||||
34 | file_put_contents($this->tmpPathEnv, $tmpData); |
||||
35 | } |
||||
36 | |||||
37 | public function testInstance() |
||||
38 | { |
||||
39 | $this->instance = new Envparser; |
||||
40 | $instanceClass = $this->instance instanceof Envparser; |
||||
41 | |||||
42 | $this->assertEquals($instanceClass, true); |
||||
43 | } |
||||
44 | |||||
45 | private function getFileTest($envFile) |
||||
46 | { |
||||
47 | return __DIR__.self::DS.'fixtures'.self::DS.$envFile; |
||||
48 | } |
||||
49 | |||||
50 | public function testRead() |
||||
51 | { |
||||
52 | $envfile = $this->getFileTest('.env'); |
||||
53 | |||||
54 | $this->assertTrue(is_readable($envfile)); |
||||
55 | } |
||||
56 | |||||
57 | public function testEnvLoadsFile() |
||||
58 | { |
||||
59 | $envfile = $this->getFileTest('.env'); |
||||
60 | |||||
61 | $envparser = new Envparser($envfile); |
||||
62 | $envparserLoader = $envparser->load(); |
||||
63 | |||||
64 | $this->assertTrue($envparserLoader); |
||||
65 | } |
||||
66 | |||||
67 | public function testEnvVar() |
||||
68 | { |
||||
69 | $envfile = $this->getFileTest('.env'); |
||||
70 | |||||
71 | $envparser = new Envparser($envfile); |
||||
72 | $envparser->load(); |
||||
73 | |||||
74 | $envparser->run(); |
||||
75 | |||||
76 | $this->assertSame('bar', $envparser->getValue('FOO')); |
||||
77 | $this->assertSame($envparser->getValue('FOO1'), getenv('FOO1')); |
||||
78 | $this->assertEmpty($envparser->getValue('VAREMPTY')); |
||||
79 | $this->assertEmpty($envparser->getValue('NOTEXISTSVARIABLE')); |
||||
80 | $this->assertEmpty($envparser->getValue('VAREMPTYSTRING')); |
||||
81 | $this->assertSame('with space value', $envparser->getValue('VARSPACES')); |
||||
82 | $this->assertSame('with quote value', $envparser->getValue('VARWITHQUOTE')); |
||||
83 | } |
||||
84 | |||||
85 | public function testEnvComments() |
||||
86 | { |
||||
87 | $envfile = $this->getFileTest('comments.env'); |
||||
88 | |||||
89 | $envparser = new Envparser($envfile); |
||||
90 | $envparser->load(); |
||||
91 | |||||
92 | $envparser->run(); |
||||
93 | |||||
94 | $this->assertSame($envparser->getValue('COMMENT'), 'BAR'); |
||||
95 | $this->assertFalse(getenv('COM1')); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
96 | $this->assertFalse(getenv('COM2')); |
||||
97 | $this->assertFalse(getenv('COM3')); |
||||
98 | $this->assertSame('with spaces', getenv('COM4')); |
||||
99 | $this->assertSame('with # spaces and hash', getenv('COM5')); |
||||
100 | $this->assertSame('with spaces', getenv('COM6')); |
||||
101 | $this->assertSame('with spaces', getenv('COM7')); |
||||
102 | $this->assertSame('with spaces', getenv('COM8')); |
||||
103 | $this->assertSame('with spaces', getenv('COM9')); |
||||
104 | $this->assertSame('with spaces', getenv('COM10')); |
||||
105 | $this->assertSame('with spaces', getenv('COM11')); |
||||
106 | $this->assertSame('with tab spaced', getenv('COM12')); |
||||
107 | $this->assertSame("test with ' inside", getenv('COM13')); |
||||
108 | $this->assertSame('with spaces', getenv('COM10')); |
||||
109 | $this->assertSame('a value with & mark and " mark with # sign', getenv('COM14')); |
||||
110 | $this->assertEmpty(getenv('COM15')); |
||||
111 | } |
||||
112 | |||||
113 | public function testEnvSpecial() |
||||
114 | { |
||||
115 | $envfile = $this->getFileTest('special.env'); |
||||
116 | |||||
117 | $envparser = new Envparser($envfile); |
||||
118 | $envparser->load(); |
||||
119 | |||||
120 | $envparser->run(); |
||||
121 | |||||
122 | $this->assertSame('mysql:host=localhost;dbname=db_name;charset=utf-8', getenv('SPEC1')); |
||||
123 | $this->assertSame('mysql:host=localhost;dbname=db_name;', getenv('SPEC2')); |
||||
124 | $this->assertSame('test qoute " mark', getenv('SPEC3')); |
||||
125 | $this->assertSame('test backslash \\', getenv('SPEC4')); |
||||
126 | $this->assertSame('test backslash [\\]', getenv('SPEC5')); |
||||
127 | } |
||||
128 | |||||
129 | /** |
||||
130 | * @expectedException Codervio\Envmanager\Exceptions\ValueException |
||||
131 | */ |
||||
132 | public function testAssertException() |
||||
133 | { |
||||
134 | $envfile = $this->getFileTest('exception.env'); |
||||
135 | |||||
136 | $envparser = new Envparser($envfile); |
||||
137 | $envparser->load(); |
||||
138 | |||||
139 | $envparser->run(); |
||||
140 | |||||
141 | getenv('EXC1'); |
||||
142 | } |
||||
143 | |||||
144 | public function testEnvExportsSetenv() |
||||
145 | { |
||||
146 | $envfile = $this->getFileTest('export_setenv.env'); |
||||
147 | |||||
148 | $envparser = new Envparser($envfile); |
||||
149 | $envparser->load(); |
||||
150 | |||||
151 | $envparser->run(); |
||||
152 | |||||
153 | $this->assertSame('bar', getenv('EX1')); |
||||
154 | $this->assertSame('bar', getenv('EX2')); |
||||
155 | $this->assertSame('with spaces', getenv('EX3')); |
||||
156 | $this->assertEmpty(getenv('EX4')); |
||||
157 | |||||
158 | $this->assertSame('bar', getenv('SETENV1')); |
||||
159 | $this->assertSame('bar', getenv('SETENV2')); |
||||
160 | $this->assertSame('with spaces', getenv('SETENV3')); |
||||
161 | $this->assertEmpty(getenv('SETENV4')); |
||||
162 | } |
||||
163 | |||||
164 | /** |
||||
165 | * @expectedException Codervio\Envmanager\Exceptions\ParseException |
||||
166 | */ |
||||
167 | public function testAssertExceptionVariable() |
||||
168 | { |
||||
169 | $envfile = $this->getFileTest('invalidvar.env'); |
||||
170 | |||||
171 | $envparser = new Envparser($envfile); |
||||
172 | $envparser->load(); |
||||
173 | |||||
174 | $envparser->run(); |
||||
175 | |||||
176 | getenv('2FOO'); |
||||
177 | } |
||||
178 | |||||
179 | public function testEnvExportsServerVariables() |
||||
180 | { |
||||
181 | $envfile = $this->getFileTest('export_setenv.env'); |
||||
182 | |||||
183 | $envparser = new Envparser($envfile); |
||||
184 | $envparser->load(); |
||||
185 | |||||
186 | $envparser->run(); |
||||
187 | |||||
188 | $this->assertSame('bar', $_SERVER['EX1']); |
||||
189 | $this->assertSame('bar', $_SERVER['EX2']); |
||||
190 | $this->assertSame('with spaces', $_SERVER['EX3']); |
||||
191 | $this->assertEmpty($_SERVER['EX4']); |
||||
192 | |||||
193 | $this->assertSame('bar', $_SERVER['SETENV1']); |
||||
194 | $this->assertSame('bar', $_SERVER['SETENV2']); |
||||
195 | $this->assertSame('with spaces', $_SERVER['SETENV3']); |
||||
196 | $this->assertEmpty($_SERVER['SETENV4']); |
||||
197 | } |
||||
198 | |||||
199 | public function testEnvExportsEnvVariables() |
||||
200 | { |
||||
201 | $envfile = $this->getFileTest('export_setenv.env'); |
||||
202 | |||||
203 | $envparser = new Envparser($envfile); |
||||
204 | $envparser->load(); |
||||
205 | |||||
206 | $envparser->run(); |
||||
207 | |||||
208 | $this->assertSame('bar', $_ENV['EX1']); |
||||
209 | $this->assertSame('bar', $_ENV['EX2']); |
||||
210 | $this->assertSame('with spaces', $_ENV['EX3']); |
||||
211 | $this->assertEmpty($_ENV['EX4']); |
||||
212 | |||||
213 | $this->assertSame('bar', $_ENV['SETENV1']); |
||||
214 | $this->assertSame('bar', $_ENV['SETENV2']); |
||||
215 | $this->assertSame('with spaces', $_ENV['SETENV3']); |
||||
216 | $this->assertEmpty($_ENV['SETENV4']); |
||||
217 | |||||
218 | $this->assertSame('bar', $_ENV['SETENV5']); |
||||
219 | } |
||||
220 | |||||
221 | public function testParsingCommentsAndAllValues() |
||||
222 | { |
||||
223 | $envfile = $this->getFileTest('comments.env'); |
||||
224 | |||||
225 | $envparser = new Envparser($envfile, true); |
||||
226 | $envparser->load(); |
||||
227 | |||||
228 | $envparser->run(); |
||||
229 | |||||
230 | $parsers = $envparser->getAllValues(); |
||||
231 | |||||
232 | $this->assertSame('BAR2', $parsers['# COM2']); |
||||
233 | $this->assertSame('BAR1', $envparser->getValue('#COM1')); |
||||
234 | } |
||||
235 | |||||
236 | public function testGetSystemVars() |
||||
237 | { |
||||
238 | $envparser = new Envparser(); |
||||
239 | $envparser->load(true, true); |
||||
240 | $envparser->run(); |
||||
241 | |||||
242 | $systemvars = $envparser->getSystemVars(); |
||||
243 | |||||
244 | $this->assertSame($systemvars['PWD'], getenv('PWD')); |
||||
245 | $this->assertSame($envparser->getSystemVars('PWD'), getenv('PWD')); |
||||
246 | $this->assertTrue($envparser->checkSystemVar('PWD')); |
||||
247 | } |
||||
248 | |||||
249 | public function testStrictBool() |
||||
250 | { |
||||
251 | $envfile = $this->getFileTest('bool_strict.env'); |
||||
252 | |||||
253 | $envparser = new Envparser($envfile, true); |
||||
254 | $envparser->setStrictBool(false); |
||||
255 | $envparser->load(); |
||||
256 | $envparser->run(); |
||||
257 | |||||
258 | $this->assertSame('y', $envparser->getValue('STRICTVARY_ONLYVAR')); |
||||
259 | $this->assertSame('n', $envparser->getValue('STRICTVARN_ONLYVAR')); |
||||
260 | |||||
261 | $this->assertTrue($envparser->getValue('BOOLYES')); |
||||
0 ignored issues
–
show
It seems like
$envparser->getValue('BOOLYES') can also be of type string ; however, parameter $condition of PHPUnit\Framework\Assert::assertTrue() does only seem to accept boolean , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
262 | $this->assertFalse($envparser->getValue('BOOLFALSE')); |
||||
0 ignored issues
–
show
It seems like
$envparser->getValue('BOOLFALSE') can also be of type string ; however, parameter $condition of PHPUnit\Framework\Assert::assertFalse() does only seem to accept boolean , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
263 | $this->assertTrue($envparser->getValue('BOOLTRUE')); |
||||
264 | $this->assertFalse($envparser->getValue('BOOLFALSE')); |
||||
265 | $this->assertTrue($envparser->getValue('BOOLON')); |
||||
266 | $this->assertFalse($envparser->getValue('BOOLOFF')); |
||||
267 | } |
||||
268 | |||||
269 | public function testBoolVars() |
||||
270 | { |
||||
271 | unset($_ENV['STRICTVARY']); |
||||
272 | putenv("STRICTVARY="); |
||||
273 | |||||
274 | $envfile = $this->getFileTest('bool_strict.env'); |
||||
275 | |||||
276 | $envparser = new Envparser($envfile, false); |
||||
277 | $envparser->setStrictBool(true); |
||||
278 | $envparser->load(); |
||||
279 | $envparser->run(); |
||||
280 | |||||
281 | $this->assertTrue($envparser->getValue('STRICTVARY')); |
||||
0 ignored issues
–
show
It seems like
$envparser->getValue('STRICTVARY') can also be of type string ; however, parameter $condition of PHPUnit\Framework\Assert::assertTrue() does only seem to accept boolean , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
282 | } |
||||
283 | |||||
284 | public function testRequire() |
||||
285 | { |
||||
286 | $envfile = $this->getFileTest('checkvars.env'); |
||||
287 | |||||
288 | $envparser = new Envparser($envfile, false); |
||||
289 | $envparser->load(); |
||||
290 | $envparser->run(); |
||||
291 | |||||
292 | $this->assertTrue($envparser->required('CVFOOEMPTY')->isEmpty()); |
||||
293 | $this->assertFalse($envparser->required('CVFOONOTEMPTY')->isEmpty()); |
||||
294 | |||||
295 | $this->assertTrue($envparser->required('CVBOOL')->isBoolean()); |
||||
296 | $this->assertTrue($envparser->required('CVBOOL2')->isBoolean()); |
||||
297 | $this->assertTrue($envparser->required('CVBOOL3')->isBoolean()); |
||||
298 | $this->assertFalse($envparser->required('CVBOOL4')->isBoolean()); |
||||
299 | |||||
300 | $this->assertTrue($envparser->required('CVNUMBER')->isNumber()); |
||||
301 | $this->assertFalse($envparser->required('CVNUMBER2')->isNumber()); |
||||
302 | $this->assertTrue($envparser->required('CVNUMBER3')->isFloat()); |
||||
303 | $this->assertFalse($envparser->required('CVNUMBER2')->isFloat()); |
||||
304 | } |
||||
305 | |||||
306 | public function testCheckValuesValidator() |
||||
307 | { |
||||
308 | $envfile = $this->getFileTest('checkvars.env'); |
||||
309 | |||||
310 | $envparser = new Envparser($envfile, false); |
||||
311 | $envparser->load(); |
||||
312 | $envparser->run(); |
||||
313 | |||||
314 | $this->assertTrue($envparser->required('CVFOONOTEMPTY')->checkValues('notempty')); |
||||
315 | $this->assertTrue($envparser->required('CVFOONOTEMPTY')->checkValues(array('notempty'))); |
||||
316 | } |
||||
317 | |||||
318 | /** |
||||
319 | * @expectedException Codervio\Envmanager\Exceptions\ValidationException |
||||
320 | */ |
||||
321 | public function testCheckValuesValidationException() |
||||
322 | { |
||||
323 | $envfile = $this->getFileTest('checkvars.env'); |
||||
324 | |||||
325 | $envparser = new Envparser($envfile, false); |
||||
326 | $envparser->load(); |
||||
327 | $envparser->run(); |
||||
328 | |||||
329 | $this->assertTrue($envparser->required('CVFOONOTEMPTY')->checkValues(array('notempty', 'abc'))); |
||||
330 | } |
||||
331 | |||||
332 | public function testVariables() |
||||
333 | { |
||||
334 | $envfile = $this->getFileTest('variables.env'); |
||||
335 | |||||
336 | $envparser = new Envparser($envfile, false); |
||||
337 | $envparser->load(); |
||||
338 | $envparser->run(); |
||||
339 | |||||
340 | $this->assertSame(123, $envparser->getValue('MYVARTEST')); |
||||
341 | $this->assertSame("123", $envparser->getValue('MYVARTEST2')); |
||||
342 | $this->assertSame("0123", $envparser->getValue('MYVARTEST3')); |
||||
343 | $this->assertSame("123456", $envparser->getValue('MYVARTEST4')); |
||||
344 | } |
||||
345 | |||||
346 | public function testSpecialCharacters() |
||||
347 | { |
||||
348 | $envfile = $this->getFileTest('special.env'); |
||||
349 | |||||
350 | $envparser = new Envparser($envfile, false); |
||||
351 | $envparser->load(); |
||||
352 | $envparser->run(); |
||||
353 | |||||
354 | $this->assertSame("0.k$", $envparser->getValue('SP.EC6')); |
||||
355 | $this->assertSame('!@@$54#b5f&(*(#5FDS/?"{fdsfs3)fsdf__4+}|?"><a', $envparser->getValue('SPEC7')); |
||||
356 | $this->assertSame('������', $envparser->getValue('SPEC8')); |
||||
357 | $this->assertSame('�����������������������������', $envparser->getValue('SPEC9')); |
||||
358 | } |
||||
359 | |||||
360 | public function testGetComment() |
||||
361 | { |
||||
362 | $envfile = $this->getFileTest('.env'); |
||||
363 | |||||
364 | $envparser = new Envparser($envfile, false); |
||||
365 | $envparser->load(); |
||||
366 | $envparser->run(); |
||||
367 | |||||
368 | $this->assertSame('comment', $envparser->getComment('FOO1')); |
||||
369 | } |
||||
370 | } |
||||
371 |