No Description

Dotenv.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. class DotenvTest extends \PHPUnit_Framework_TestCase
  3. {
  4. public function testDotenvLoadsEnvironmentVars()
  5. {
  6. Dotenv::load(dirname(__DIR__) . '/fixtures');
  7. $this->assertEquals('bar', getenv('FOO'));
  8. $this->assertEquals('baz', getenv('BAR'));
  9. $this->assertEquals('with spaces', getenv('SPACED'));
  10. $this->assertEquals('', getenv('NULL'));
  11. }
  12. public function testCommentedDotenvLoadsEnvironmentVars()
  13. {
  14. Dotenv::load(dirname(__DIR__) . '/fixtures', 'commented.env');
  15. $this->assertEquals('bar', getenv('CFOO'));
  16. $this->assertEquals(false, getenv('CBAR'));
  17. $this->assertEquals(false, getenv('CZOO'));
  18. $this->assertEquals('with spaces', getenv('CSPACED'));
  19. $this->assertEquals('a value with a # character', getenv('CQUOTES'));
  20. $this->assertEquals('a value with a # character & a quote " character inside quotes', getenv('CQUOTESWITHQUOTE'));
  21. $this->assertEquals('', getenv('CNULL'));
  22. }
  23. public function testQuotedDotenvLoadsEnvironmentVars()
  24. {
  25. Dotenv::load(dirname(__DIR__) . '/fixtures', 'quoted.env');
  26. $this->assertEquals('bar', getenv('QFOO'));
  27. $this->assertEquals('baz', getenv('QBAR'));
  28. $this->assertEquals('with spaces', getenv('QSPACED'));
  29. $this->assertEquals('', getenv('QNULL'));
  30. $this->assertEquals('pgsql:host=localhost;dbname=test', getenv('QEQUALS'));
  31. $this->assertEquals("test some escaped characters like a quote (') or maybe a backslash (\\)", getenv('QESCAPED'));
  32. }
  33. public function testExportedDotenvLoadsEnvironmentVars()
  34. {
  35. Dotenv::load(dirname(__DIR__) . '/fixtures', 'exported.env');
  36. $this->assertEquals('bar', getenv('EFOO'));
  37. $this->assertEquals('baz', getenv('EBAR'));
  38. $this->assertEquals('with spaces', getenv('ESPACED'));
  39. $this->assertEquals('', getenv('ENULL'));
  40. }
  41. public function testDotenvLoadsEnvGlobals()
  42. {
  43. Dotenv::load(dirname(__DIR__) . '/fixtures');
  44. $this->assertEquals('bar', $_SERVER['FOO']);
  45. $this->assertEquals('baz', $_SERVER['BAR']);
  46. $this->assertEquals('with spaces', $_SERVER['SPACED']);
  47. $this->assertEquals('', $_SERVER['NULL']);
  48. }
  49. public function testDotenvLoadsServerGlobals()
  50. {
  51. Dotenv::load(dirname(__DIR__) . '/fixtures');
  52. $this->assertEquals('bar', $_ENV['FOO']);
  53. $this->assertEquals('baz', $_ENV['BAR']);
  54. $this->assertEquals('with spaces', $_ENV['SPACED']);
  55. $this->assertEquals('', $_ENV['NULL']);
  56. }
  57. public function testDotenvRequiredStringEnvironmentVars()
  58. {
  59. Dotenv::load(dirname(__DIR__) . '/fixtures');
  60. $res = Dotenv::required('FOO');
  61. $this->assertTrue($res);
  62. }
  63. public function testDotenvRequiredArrayEnvironmentVars()
  64. {
  65. Dotenv::load(dirname(__DIR__) . '/fixtures');
  66. $res = Dotenv::required(array('FOO', 'BAR'));
  67. $this->assertTrue($res);
  68. }
  69. public function testDotenvNestedEnvironmentVars()
  70. {
  71. Dotenv::load(dirname(__DIR__) . '/fixtures', 'nested.env');
  72. $this->assertEquals('Hello World!', $_ENV['NVAR3']);
  73. $this->assertEquals('${NVAR1} ${NVAR2}', $_ENV['NVAR4']); // not resolved
  74. $this->assertEquals('$NVAR1 {NVAR2}', $_ENV['NVAR5']); // not resolved
  75. }
  76. public function testDotenvAllowedValues()
  77. {
  78. Dotenv::load(dirname(__DIR__) . '/fixtures');
  79. $res = Dotenv::required('FOO', array('bar', 'baz'));
  80. $this->assertTrue($res);
  81. }
  82. /**
  83. * @expectedException RuntimeException
  84. * @expectedExceptionMessage Required environment variable missing, or value not allowed: 'FOO'
  85. */
  86. public function testDotenvProhibitedValues()
  87. {
  88. Dotenv::load(dirname(__DIR__) . '/fixtures');
  89. $res = Dotenv::required('FOO', array('buzz'));
  90. $this->assertTrue($res);
  91. }
  92. /**
  93. * @expectedException RuntimeException
  94. * @expectedExceptionMessage Required environment variable missing, or value not allowed: 'FOOX', 'NOPE'
  95. */
  96. public function testDotenvRequiredThrowsRuntimeException()
  97. {
  98. Dotenv::load(dirname(__DIR__) . '/fixtures');
  99. $res = Dotenv::required(array('FOOX', 'NOPE'));
  100. }
  101. public function testDotenvNullFileArgumentUsesDefault()
  102. {
  103. Dotenv::load(dirname(__DIR__) . '/fixtures', null);
  104. $this->assertEquals('bar', getenv('FOO'));
  105. }
  106. /**
  107. * The fixture data has whitespace between the key and in the value string
  108. * Test that these keys are trimmed down
  109. */
  110. public function testDotenvTrimmedKeys()
  111. {
  112. Dotenv::load(dirname(__DIR__) . '/fixtures', 'quoted.env');
  113. $this->assertTrue(isset($_ENV['QWHITESPACE']));
  114. }
  115. public function testDotenvDoesNotOverwriteEnvWhenImmutable()
  116. {
  117. Dotenv::makeMutable(); // only need this because we've previously set the variable
  118. Dotenv::setEnvironmentVariable('QFOO=external');
  119. Dotenv::makeImmutable();
  120. Dotenv::load(dirname(__DIR__) . '/fixtures', 'quoted.env');
  121. $this->assertEquals('external', getenv('QFOO'));
  122. }
  123. public function testDotenvDoesNotOverwriteEnvWhenMutable()
  124. {
  125. Dotenv::makeMutable();
  126. Dotenv::setEnvironmentVariable('QFOO=external');
  127. Dotenv::load(dirname(__DIR__) . '/fixtures', 'quoted.env');
  128. $this->assertEquals('bar', getenv('QFOO'));
  129. }
  130. public function testDotenvAllowsSpecialCharacters()
  131. {
  132. Dotenv::load(dirname(__DIR__) . '/fixtures', 'specialchars.env');
  133. $this->assertEquals('$a6^C7k%zs+e^.jvjXk', getenv('SPVAR1'));
  134. $this->assertEquals('?BUty3koaV3%GA*hMAwH}B', getenv('SPVAR2'));
  135. $this->assertEquals('jdgEB4{QgEC]HL))&GcXxokB+wqoN+j>xkV7K?m$r', getenv('SPVAR3'));
  136. $this->assertEquals('22222:22#2^{', getenv('SPVAR4'));
  137. $this->assertEquals("test some escaped characters like a quote \\' or maybe a backslash \\\\", getenv('SPVAR5'));
  138. }
  139. }