DotenvTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. use Dotenv\Dotenv;
  3. use PHPUnit\Framework\TestCase;
  4. class DotenvTest extends TestCase
  5. {
  6. /**
  7. * @var string
  8. */
  9. private $fixturesFolder;
  10. public function setUp()
  11. {
  12. $this->fixturesFolder = dirname(__DIR__).'/fixtures/env';
  13. }
  14. /**
  15. * @expectedException \Dotenv\Exception\InvalidPathException
  16. * @expectedExceptionMessage Unable to read any of the environment file(s) at
  17. */
  18. public function testDotenvThrowsExceptionIfUnableToLoadFile()
  19. {
  20. $dotenv = Dotenv::create(__DIR__);
  21. $dotenv->load();
  22. }
  23. /**
  24. * @expectedException \Dotenv\Exception\InvalidPathException
  25. * @expectedExceptionMessage Unable to read any of the environment file(s) at
  26. */
  27. public function testDotenvThrowsExceptionIfUnableToLoadFiles()
  28. {
  29. $dotenv = Dotenv::create([__DIR__, __DIR__.'/foo/bar']);
  30. $dotenv->load();
  31. }
  32. public function testDotenvTriesPathsToLoad()
  33. {
  34. $dotenv = Dotenv::create([__DIR__, $this->fixturesFolder]);
  35. $this->assertCount(4, $dotenv->load());
  36. }
  37. public function testDotenvSkipsLoadingIfFileIsMissing()
  38. {
  39. $dotenv = Dotenv::create(__DIR__);
  40. $this->assertSame([], $dotenv->safeLoad());
  41. }
  42. public function testDotenvLoadsEnvironmentVars()
  43. {
  44. $dotenv = Dotenv::create($this->fixturesFolder);
  45. $this->assertSame(
  46. ['FOO' => 'bar', 'BAR' => 'baz', 'SPACED' => 'with spaces', 'NULL' => ''],
  47. $dotenv->load()
  48. );
  49. $this->assertSame('bar', getenv('FOO'));
  50. $this->assertSame('baz', getenv('BAR'));
  51. $this->assertSame('with spaces', getenv('SPACED'));
  52. $this->assertEmpty(getenv('NULL'));
  53. }
  54. public function testCommentedDotenvLoadsEnvironmentVars()
  55. {
  56. $dotenv = Dotenv::create($this->fixturesFolder, 'commented.env');
  57. $dotenv->load();
  58. $this->assertSame('bar', getenv('CFOO'));
  59. $this->assertFalse(getenv('CBAR'));
  60. $this->assertFalse(getenv('CZOO'));
  61. $this->assertSame('with spaces', getenv('CSPACED'));
  62. $this->assertSame('a value with a # character', getenv('CQUOTES'));
  63. $this->assertSame('a value with a # character & a quote " character inside quotes', getenv('CQUOTESWITHQUOTE'));
  64. $this->assertEmpty(getenv('CNULL'));
  65. $this->assertEmpty(getenv('EMPTY'));
  66. $this->assertEmpty(getenv('EMPTY2'));
  67. $this->assertSame('foo', getenv('FOOO'));
  68. }
  69. public function testQuotedDotenvLoadsEnvironmentVars()
  70. {
  71. $dotenv = Dotenv::create($this->fixturesFolder, 'quoted.env');
  72. $dotenv->load();
  73. $this->assertSame('bar', getenv('QFOO'));
  74. $this->assertSame('baz', getenv('QBAR'));
  75. $this->assertSame('with spaces', getenv('QSPACED'));
  76. $this->assertEmpty(getenv('QNULL'));
  77. $this->assertSame('pgsql:host=localhost;dbname=test', getenv('QEQUALS'));
  78. $this->assertSame('test some escaped characters like a quote (") or maybe a backslash (\\)', getenv('QESCAPED'));
  79. $this->assertSame('iiiiviiiixiiiiviiii\\n', getenv('QSLASH'));
  80. $this->assertSame('test some escaped characters like a quote (\') or maybe a backslash (\\)', getenv('SQESCAPED'));
  81. $this->assertSame('iiiiviiiixiiiiviiii\\n', getenv('SQSLASH'));
  82. }
  83. public function testLargeDotenvLoadsEnvironmentVars()
  84. {
  85. $dotenv = Dotenv::create($this->fixturesFolder, 'large.env');
  86. $dotenv->load();
  87. $this->assertNotEmpty(getenv('LARGE'));
  88. }
  89. public function testMultipleDotenvLoadsEnvironmentVars()
  90. {
  91. $dotenv = Dotenv::create($this->fixturesFolder, 'multiple.env');
  92. $dotenv->load();
  93. $this->assertSame('bar', getenv('MULTI1'));
  94. $this->assertSame('foo', getenv('MULTI2'));
  95. }
  96. public function testExportedDotenvLoadsEnvironmentVars()
  97. {
  98. $dotenv = Dotenv::create($this->fixturesFolder, 'exported.env');
  99. $dotenv->load();
  100. $this->assertSame('bar', getenv('EFOO'));
  101. $this->assertSame('baz', getenv('EBAR'));
  102. $this->assertSame('with spaces', getenv('ESPACED'));
  103. $this->assertEmpty(getenv('ENULL'));
  104. }
  105. public function testDotenvLoadsEnvGlobals()
  106. {
  107. $dotenv = Dotenv::create($this->fixturesFolder);
  108. $dotenv->load();
  109. $this->assertSame('bar', $_SERVER['FOO']);
  110. $this->assertSame('baz', $_SERVER['BAR']);
  111. $this->assertSame('with spaces', $_SERVER['SPACED']);
  112. $this->assertEmpty($_SERVER['NULL']);
  113. }
  114. public function testDotenvLoadsServerGlobals()
  115. {
  116. $dotenv = Dotenv::create($this->fixturesFolder);
  117. $dotenv->load();
  118. $this->assertSame('bar', $_ENV['FOO']);
  119. $this->assertSame('baz', $_ENV['BAR']);
  120. $this->assertSame('with spaces', $_ENV['SPACED']);
  121. $this->assertEmpty($_ENV['NULL']);
  122. }
  123. public function testDotenvNestedEnvironmentVars()
  124. {
  125. $dotenv = Dotenv::create($this->fixturesFolder, 'nested.env');
  126. $dotenv->load();
  127. $this->assertSame('{$NVAR1} {$NVAR2}', $_ENV['NVAR3']); // not resolved
  128. $this->assertSame('Hello World!', $_ENV['NVAR4']);
  129. $this->assertSame('$NVAR1 {NVAR2}', $_ENV['NVAR5']); // not resolved
  130. $this->assertSame('Special Value', $_ENV['N.VAR6']); // new '.' (dot) in var name
  131. $this->assertSame('Special Value', $_ENV['NVAR7']); // nested '.' (dot) variable
  132. $this->assertSame('', $_ENV['NVAR8']);
  133. $this->assertSame('', $_ENV['NVAR9']); // nested variable is empty string
  134. $this->assertSame('${NVAR888}', $_ENV['NVAR10']); // nested variable is not set
  135. }
  136. public function testDotenvNullFileArgumentUsesDefault()
  137. {
  138. $dotenv = Dotenv::create($this->fixturesFolder, null);
  139. $dotenv->load();
  140. $this->assertSame('bar', getenv('FOO'));
  141. }
  142. /**
  143. * The fixture data has whitespace between the key and in the value string.
  144. *
  145. * Test that these keys are trimmed down.
  146. */
  147. public function testDotenvTrimmedKeys()
  148. {
  149. $dotenv = Dotenv::create($this->fixturesFolder, 'quoted.env');
  150. $dotenv->load();
  151. $this->assertSame('no space', getenv('QWHITESPACE'));
  152. }
  153. public function testDotenvLoadDoesNotOverwriteEnv()
  154. {
  155. putenv('IMMUTABLE=true');
  156. $dotenv = Dotenv::create($this->fixturesFolder, 'immutable.env');
  157. $dotenv->load();
  158. $this->assertSame('true', getenv('IMMUTABLE'));
  159. }
  160. public function testDotenvLoadAfterOverload()
  161. {
  162. putenv('IMMUTABLE=true');
  163. $dotenv = Dotenv::create($this->fixturesFolder, 'immutable.env');
  164. $dotenv->overload();
  165. $this->assertSame('false', getenv('IMMUTABLE'));
  166. putenv('IMMUTABLE=true');
  167. $dotenv->load();
  168. $this->assertSame('true', getenv('IMMUTABLE'));
  169. }
  170. public function testDotenvOverloadAfterLoad()
  171. {
  172. putenv('IMMUTABLE=true');
  173. $dotenv = Dotenv::create($this->fixturesFolder, 'immutable.env');
  174. $dotenv->load();
  175. $this->assertSame('true', getenv('IMMUTABLE'));
  176. putenv('IMMUTABLE=true');
  177. $dotenv->overload();
  178. $this->assertSame('false', getenv('IMMUTABLE'));
  179. }
  180. public function testDotenvOverloadDoesOverwriteEnv()
  181. {
  182. $dotenv = Dotenv::create($this->fixturesFolder, 'mutable.env');
  183. $dotenv->overload();
  184. $this->assertSame('true', getenv('MUTABLE'));
  185. }
  186. public function testDotenvAllowsSpecialCharacters()
  187. {
  188. $dotenv = Dotenv::create($this->fixturesFolder, 'specialchars.env');
  189. $dotenv->load();
  190. $this->assertSame('$a6^C7k%zs+e^.jvjXk', getenv('SPVAR1'));
  191. $this->assertSame('?BUty3koaV3%GA*hMAwH}B', getenv('SPVAR2'));
  192. $this->assertSame('jdgEB4{QgEC]HL))&GcXxokB+wqoN+j>xkV7K?m$r', getenv('SPVAR3'));
  193. $this->assertSame('22222:22#2^{', getenv('SPVAR4'));
  194. $this->assertSame('test some escaped characters like a quote " or maybe a backslash \\', getenv('SPVAR5'));
  195. $this->assertSame('secret!@', getenv('SPVAR6'));
  196. $this->assertSame('secret!@#', getenv('SPVAR7'));
  197. $this->assertSame('secret!@#', getenv('SPVAR8'));
  198. }
  199. public function testMutlilineLoading()
  200. {
  201. $dotenv = Dotenv::create($this->fixturesFolder, 'multiline.env');
  202. $dotenv->load();
  203. $this->assertSame("test\n test\"test\"\n test", getenv('TEST'));
  204. $this->assertSame("test\ntest", getenv('TEST_ND'));
  205. $this->assertSame("test\ntest", getenv('TEST_NS'));
  206. $this->assertSame('https://vision.googleapis.com/v1/images:annotate?key=', getenv('TEST_EQD'));
  207. $this->assertSame('https://vision.googleapis.com/v1/images:annotate?key=', getenv('TEST_EQS'));
  208. }
  209. public function testGetEnvironmentVariablesList()
  210. {
  211. $dotenv = Dotenv::create($this->fixturesFolder);
  212. $dotenv->load();
  213. $this->assertSame(['FOO', 'BAR', 'SPACED', 'NULL'], $dotenv->getEnvironmentVariableNames());
  214. }
  215. }