LoaderTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. use Dotenv\Environment\Adapter\ArrayAdapter;
  3. use Dotenv\Environment\DotenvFactory;
  4. use Dotenv\Loader;
  5. use PHPUnit\Framework\TestCase;
  6. class LoaderTest extends TestCase
  7. {
  8. /**
  9. * @var string
  10. */
  11. protected $folder;
  12. /**
  13. * @var string[]|null
  14. */
  15. protected $keyVal;
  16. public function setUp()
  17. {
  18. $this->folder = dirname(__DIR__).'/fixtures/env';
  19. $this->keyVal(true);
  20. }
  21. /**
  22. * Generates a new key/value pair or returns the previous one.
  23. *
  24. * Since most of our functionality revolves around setting/retrieving keys
  25. * and values, we have this utility function to help generate new, unique
  26. * key/value pairs.
  27. *
  28. * @param bool $reset
  29. *
  30. * @return array
  31. */
  32. protected function keyVal($reset = false)
  33. {
  34. if (!isset($this->keyVal) || $reset) {
  35. $this->keyVal = [uniqid() => uniqid()];
  36. }
  37. return $this->keyVal;
  38. }
  39. /**
  40. * Returns the key from keyVal(), without reset.
  41. *
  42. * @return string
  43. */
  44. protected function key()
  45. {
  46. $keyVal = $this->keyVal();
  47. return key($keyVal);
  48. }
  49. /**
  50. * Returns the value from keyVal(), without reset.
  51. *
  52. * @return string
  53. */
  54. protected function value()
  55. {
  56. $keyVal = $this->keyVal();
  57. return reset($keyVal);
  58. }
  59. public function testMutableLoaderClearsEnvironmentVars()
  60. {
  61. $loader = new Loader(["{$this->folder}/.env"], new DotenvFactory(), false);
  62. // Set an environment variable.
  63. $loader->setEnvironmentVariable($this->key(), $this->value());
  64. // Clear the set environment variable.
  65. $loader->clearEnvironmentVariable($this->key());
  66. $this->assertSame(null, $loader->getEnvironmentVariable($this->key()));
  67. $this->assertSame(false, getenv($this->key()));
  68. $this->assertSame(false, isset($_ENV[$this->key()]));
  69. $this->assertSame(false, isset($_SERVER[$this->key()]));
  70. $this->assertSame([$this->key()], $loader->getEnvironmentVariableNames());
  71. }
  72. public function testImmutableLoaderCannotClearEnvironmentVars()
  73. {
  74. $loader = new Loader(["{$this->folder}/.env"], new DotenvFactory(), false);
  75. $loader->setImmutable(true);
  76. // Set an environment variable.
  77. $loader->setEnvironmentVariable($this->key(), $this->value());
  78. // Attempt to clear the environment variable, check that it fails.
  79. $loader->clearEnvironmentVariable($this->key());
  80. $this->assertSame($this->value(), $loader->getEnvironmentVariable($this->key()));
  81. $this->assertSame($this->value(), getenv($this->key()));
  82. $this->assertSame(true, isset($_ENV[$this->key()]));
  83. $this->assertSame(true, isset($_SERVER[$this->key()]));
  84. $this->assertSame([$this->key()], $loader->getEnvironmentVariableNames());
  85. }
  86. /**
  87. * @expectedException \Dotenv\Exception\InvalidPathException
  88. * @expectedExceptionMessage At least one environment file path must be provided.
  89. */
  90. public function testLoaderWithNoPaths()
  91. {
  92. (new Loader([], new DotenvFactory(), false))->load();
  93. }
  94. /**
  95. * @expectedException \Dotenv\Exception\InvalidPathException
  96. * @expectedExceptionMessage Unable to read any of the environment file(s) at
  97. */
  98. public function testLoaderWithBadPaths()
  99. {
  100. (new Loader(["{$this->folder}/BAD1", "{$this->folder}/BAD2"], new DotenvFactory(), false))->load();
  101. }
  102. public function testLoaderWithOneGoodPath()
  103. {
  104. $loader = (new Loader(["{$this->folder}/BAD1", "{$this->folder}/.env"], new DotenvFactory(), false));
  105. $this->assertCount(4, $loader->load());
  106. }
  107. public function testLoaderWithNoAdapters()
  108. {
  109. $loader = (new Loader([], new DotenvFactory([])));
  110. $content = "NVAR1=\"Hello\"\nNVAR2=\"World!\"\nNVAR3=\"{\$NVAR1} {\$NVAR2}\"\nNVAR4=\"\${NVAR1} \${NVAR2}\"";
  111. $expected = ['NVAR1' => 'Hello', 'NVAR2' => 'World!', 'NVAR3' => '{$NVAR1} {$NVAR2}', 'NVAR4' => '${NVAR1} ${NVAR2}'];
  112. $this->assertSame($expected, $loader->loadDirect($content));
  113. }
  114. public function testLoaderWithArrayAdapter()
  115. {
  116. $loader = (new Loader([], new DotenvFactory([new ArrayAdapter()])));
  117. $content = "NVAR1=\"Hello\"\nNVAR2=\"World!\"\nNVAR3=\"{\$NVAR1} {\$NVAR2}\"\nNVAR4=\"\${NVAR1} \${NVAR2}\"";
  118. $expected = ['NVAR1' => 'Hello', 'NVAR2' => 'World!', 'NVAR3' => '{$NVAR1} {$NVAR2}', 'NVAR4' => 'Hello World!'];
  119. $this->assertSame($expected, $loader->loadDirect($content));
  120. }
  121. }