forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDotEnvTest.php
More file actions
128 lines (100 loc) · 4.24 KB
/
Copy pathDotEnvTest.php
File metadata and controls
128 lines (100 loc) · 4.24 KB
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
<?php
//require_once 'system/Benchmark/Timer.php';
use CodeIgniter\Config\DotEnv;
class DotEnvTest extends CIUnitTestCase {
protected $fixturesFolder;
//--------------------------------------------------------------------
public function setup()
{
$this->fixturesFolder = dirname(__FILE__).'/fixtures';
}
//--------------------------------------------------------------------
public function testReturnsFalseIfCannotFindFile()
{
$dotenv = new DotEnv(__DIR__);
$this->assertFalse($dotenv->load());
}
//--------------------------------------------------------------------
public function testLoadsVars()
{
$dotenv = new DotEnv($this->fixturesFolder);
$dotenv->load();
$this->assertEquals('bar', getenv('FOO'));
$this->assertEquals('baz', getenv('BAR'));
$this->assertEquals('with spaces', getenv('SPACED'));
$this->assertEquals('', getenv('NULL'));
}
//--------------------------------------------------------------------
public function testCommentedLoadsVars()
{
$dotenv = new DotEnv($this->fixturesFolder, 'commented.env');
$dotenv->load();
$this->assertEquals('bar', getenv('CFOO'));
$this->assertEquals(false, getenv('CBAR'));
$this->assertEquals(false, getenv('CZOO'));
$this->assertEquals('with spaces', getenv('CSPACED'));
$this->assertEquals('a value with a # character', getenv('CQUOTES'));
$this->assertEquals('a value with a # character & a quote " character inside quotes', getenv('CQUOTESWITHQUOTE'));
$this->assertEquals('', getenv('CNULL'));
}
//--------------------------------------------------------------------
public function testQuotedDotenvLoadsEnvironmentVars()
{
$dotenv = new Dotenv($this->fixturesFolder, 'quoted.env');
$dotenv->load();
$this->assertEquals('bar', getenv('QFOO'));
$this->assertEquals('baz', getenv('QBAR'));
$this->assertEquals('with spaces', getenv('QSPACED'));
$this->assertEquals('', getenv('QNULL'));
$this->assertEquals('pgsql:host=localhost;dbname=test', getenv('QEQUALS'));
$this->assertEquals('test some escaped characters like a quote (") or maybe a backslash (\\)', getenv('QESCAPED'));
}
//--------------------------------------------------------------------
public function testSpacedValuesWithoutQuotesThrowsException()
{
$this->setExpectedException('InvalidArgumentException', '.env values containing spaces must be surrounded by quotes.');
$dotenv = new Dotenv($this->fixturesFolder, 'spaced-wrong.env');
$dotenv->load();
}
//--------------------------------------------------------------------
public function testLoadsServerGlobals()
{
$dotenv = new Dotenv($this->fixturesFolder, '.env');
$dotenv->load();
$this->assertEquals('bar', $_SERVER['FOO']);
$this->assertEquals('baz', $_SERVER['BAR']);
$this->assertEquals('with spaces', $_SERVER['SPACED']);
$this->assertEquals('', $_SERVER['NULL']);
}
//--------------------------------------------------------------------
public function testLoadsEnvGlobals()
{
$dotenv = new Dotenv($this->fixturesFolder);
$dotenv->load();
$this->assertEquals('bar', $_ENV['FOO']);
$this->assertEquals('baz', $_ENV['BAR']);
$this->assertEquals('with spaces', $_ENV['SPACED']);
$this->assertEquals('', $_ENV['NULL']);
}
//--------------------------------------------------------------------
public function testNestedEnvironmentVars()
{
$dotenv = new Dotenv($this->fixturesFolder, 'nested.env');
$dotenv->load();
$this->assertEquals('{$NVAR1} {$NVAR2}', $_ENV['NVAR3']); // not resolved
$this->assertEquals('Hello World!', $_ENV['NVAR4']);
$this->assertEquals('$NVAR1 {NVAR2}', $_ENV['NVAR5']); // not resolved
}
//--------------------------------------------------------------------
public function testDotenvAllowsSpecialCharacters()
{
$dotenv = new Dotenv($this->fixturesFolder, 'specialchars.env');
$dotenv->load();
$this->assertEquals('$a6^C7k%zs+e^.jvjXk', getenv('SPVAR1'));
$this->assertEquals('?BUty3koaV3%GA*hMAwH}B', getenv('SPVAR2'));
$this->assertEquals('jdgEB4{QgEC]HL))&GcXxokB+wqoN+j>xkV7K?m$r', getenv('SPVAR3'));
$this->assertEquals('22222:22#2^{', getenv('SPVAR4'));
$this->assertEquals('test some escaped characters like a quote " or maybe a backslash \\', getenv('SPVAR5'));
}
//--------------------------------------------------------------------
}