Skip to content

Commit 147f32b

Browse files
committed
test: Loader
1 parent f90a085 commit 147f32b

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

tests/LoaderTest.php

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
namespace Ahc\Env\Test;
4+
5+
use Ahc\Env\Loader;
6+
7+
class LoaderTest extends \PHPUnit_Framework_TestCase
8+
{
9+
public function testLoadPutenv()
10+
{
11+
$loader = new Loader;
12+
13+
$_SERVER['x'] = $_ENV['x'] = 'X';
14+
15+
$loader->load(__DIR__ . '/stubs/test.env', false, 0);
16+
17+
$this->assertEquals('1', getenv('a'), 'Unquoted number');
18+
$this->assertEquals('2', getenv('b'), 'Quoted number');
19+
$this->assertEquals('$3#', getenv('c'), 'Unquoted string');
20+
$this->assertEquals('lol', getenv('d'), 'Quoted string');
21+
$this->assertEquals('', getenv('e'), 'Empty string');
22+
$this->assertEquals('"6"', getenv('f'), 'Escaped numeric string');
23+
$this->assertEquals('one_two', getenv('1_2'), 'Underscored string');
24+
$this->assertEquals('Apple Ball', getenv('A_B'), 'Multi word string');
25+
$this->assertEquals("line 1\nline 2", getenv('MUL'), 'Multi line string');
26+
27+
$this->assertFalse(getenv('MuL'), 'Key should be case sensitive');
28+
29+
$this->assertArrayNotHasKey('a', $_SERVER, 'By default should not set to $_SERVER');
30+
$this->assertArrayNotHasKey('b', $_ENV, 'By default should not set to $_ENV');
31+
}
32+
33+
public function testLoadGlobals()
34+
{
35+
$loader = new Loader;
36+
37+
$loader->load(__DIR__ . '/stubs/test.env', true, Loader::ENV | Loader::SERVER);
38+
39+
foreach (['SERVER', 'ENV'] as $name) {
40+
$source = $name === 'ENV' ? $_ENV : $_SERVER;
41+
42+
$this->assertEquals('1', $source['a'], 'Unquoted number');
43+
$this->assertEquals('2', $source['b'], 'Quoted number');
44+
$this->assertEquals('$3#', $source['c'], 'Unquoted string');
45+
$this->assertEquals('lol', $source['d'], 'Quoted string');
46+
$this->assertEquals('', $source['e'], 'Empty string');
47+
$this->assertEquals('"6"', $source['f'], 'Escaped numeric string');
48+
$this->assertEquals('one_two', $source['1_2'], 'Underscored string');
49+
$this->assertEquals('Apple Ball', $source['A_B'], 'Multi word string');
50+
$this->assertEquals("line 1\nline 2", $source['MUL'], 'Multi line string');
51+
52+
$this->assertArrayNotHasKey('mUl', $source, 'Key should be case sensitive');
53+
}
54+
}
55+
56+
public function testLoadOverrideAll()
57+
{
58+
$loader = new Loader;
59+
60+
$loader->load(__DIR__ . '/stubs/override.env', true, Loader::ALL);
61+
62+
$this->assertNotEquals('1', getenv('a'), 'Unquoted number old');
63+
$this->assertNotEquals('2', getenv('b'), 'Quoted number old');
64+
$this->assertNotEquals('$3#', getenv('c'), 'Unquoted string old');
65+
$this->assertNotEquals('lol', getenv('d'), 'Quoted string old');
66+
$this->assertNotEquals('"6"', getenv('f'), 'Escaped numeric string old');
67+
$this->assertNotEquals("line 1\nline 2", getenv('MUL'), 'Multi line string old');
68+
69+
$this->assertEquals('o1', getenv('a'), 'Unquoted number new');
70+
$this->assertEquals('o2', getenv('b'), 'Quoted number new');
71+
$this->assertEquals('o$3#', getenv('c'), 'Unquoted string new');
72+
$this->assertEquals('olol', getenv('d'), 'Quoted string new');
73+
$this->assertEquals('"o6"', getenv('f'), 'Escaped numeric string new');
74+
$this->assertEquals("oline 1\nline 2", getenv('MUL'), 'Multi line string new');
75+
76+
foreach (['SERVER', 'ENV'] as $name) {
77+
$source = $name === 'ENV' ? $_ENV : $_SERVER;
78+
79+
$this->assertNotEquals('1', $source['a'], 'Unquoted number old');
80+
$this->assertNotEquals('2', $source['b'], 'Quoted number old');
81+
$this->assertNotEquals('$3#', $source['c'], 'Unquoted string old');
82+
$this->assertNotEquals('lol', $source['d'], 'Quoted string old');
83+
$this->assertNotEquals('"6"', $source['f'], 'Escaped numeric string old');
84+
$this->assertNotEquals("line 1\nline 2", $source['MUL'], 'Multi line string old');
85+
86+
$this->assertEquals('o1', $source['a'], 'Unquoted number new');
87+
$this->assertEquals('o2', $source['b'], 'Quoted number new');
88+
$this->assertEquals('o$3#', $source['c'], 'Unquoted string new');
89+
$this->assertEquals('olol', $source['d'], 'Quoted string new');
90+
$this->assertEquals('"o6"', $source['f'], 'Escaped numeric string new');
91+
$this->assertEquals("oline 1\nline 2", $source['MUL'], 'Multi line string new');
92+
}
93+
}
94+
95+
/**
96+
* @expectedException \InvalidArgumentException
97+
* @expectedExceptionMessage The .env file does not exist or is not readable
98+
*/
99+
public function testLoadInvalidPath()
100+
{
101+
$loader = new Loader;
102+
103+
$loader->load(__DIR__ . '/' . rand(1, 1000));
104+
}
105+
106+
/**
107+
* @expectedException \RuntimeException
108+
* @expectedExceptionMessage The .env file cannot be parsed due to malformed values
109+
*/
110+
public function testLoadInvalidData()
111+
{
112+
$loader = new Loader;
113+
114+
@$loader->load(__DIR__ . '/stubs/invalid.env');
115+
}
116+
}

0 commit comments

Comments
 (0)