StoreTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. declare(strict_types=1);
  3. namespace Dotenv\Tests\Store;
  4. use Dotenv\Exception\InvalidEncodingException;
  5. use Dotenv\Store\File\Paths;
  6. use Dotenv\Store\File\Reader;
  7. use Dotenv\Store\StoreBuilder;
  8. use PHPUnit\Framework\TestCase;
  9. final class StoreTest extends TestCase
  10. {
  11. /**
  12. * @var string
  13. */
  14. private static $folder;
  15. /**
  16. * @beforeClass
  17. *
  18. * @return void
  19. */
  20. public static function setFolder()
  21. {
  22. self::$folder = \dirname(\dirname(__DIR__)).'/fixtures/env';
  23. }
  24. public function testBasicReadDirect()
  25. {
  26. self::assertSame(
  27. [
  28. self::$folder.\DIRECTORY_SEPARATOR.'.env' => "FOO=bar\nBAR=baz\nSPACED=\"with spaces\"\n\nNULL=\n",
  29. ],
  30. Reader::read(
  31. Paths::filePaths([self::$folder], ['.env'])
  32. )
  33. );
  34. }
  35. public function testBasicRead()
  36. {
  37. $builder = StoreBuilder::createWithDefaultName()
  38. ->addPath(self::$folder);
  39. self::assertSame(
  40. "FOO=bar\nBAR=baz\nSPACED=\"with spaces\"\n\nNULL=\n",
  41. $builder->make()->read()
  42. );
  43. }
  44. public function testBasicReadWindowsEncoding()
  45. {
  46. $builder = StoreBuilder::createWithNoNames()
  47. ->addPath(self::$folder)
  48. ->addName('windows.env')
  49. ->fileEncoding('Windows-1252');
  50. self::assertSame(
  51. "MBW=\"ñá\"\n",
  52. $builder->make()->read()
  53. );
  54. }
  55. public function testBasicReadBadEncoding()
  56. {
  57. $builder = StoreBuilder::createWithNoNames()
  58. ->addPath(self::$folder)
  59. ->addName('windows.env')
  60. ->fileEncoding('Windowss-1252');
  61. $this->expectException(InvalidEncodingException::class);
  62. $this->expectExceptionMessage('Illegal character encoding [Windowss-1252] specified.');
  63. $builder->make()->read();
  64. }
  65. public function testFileReadMultipleShortCircuitModeDirect()
  66. {
  67. self::assertSame(
  68. [
  69. self::$folder.\DIRECTORY_SEPARATOR.'.env' => "FOO=bar\nBAR=baz\nSPACED=\"with spaces\"\n\nNULL=\n",
  70. ],
  71. Reader::read(
  72. Paths::filePaths([self::$folder], ['.env', 'example.env'])
  73. )
  74. );
  75. }
  76. public function testFileReadMultipleShortCircuitMode()
  77. {
  78. $builder = StoreBuilder::createWithNoNames()
  79. ->addPath(self::$folder)
  80. ->addName('.env')
  81. ->addName('example.env')
  82. ->shortCircuit();
  83. self::assertSame(
  84. "FOO=bar\nBAR=baz\nSPACED=\"with spaces\"\n\nNULL=\n",
  85. $builder->make()->read()
  86. );
  87. }
  88. public function testFileReadMultipleWithoutShortCircuitModeDirect()
  89. {
  90. self::assertSame(
  91. [
  92. self::$folder.\DIRECTORY_SEPARATOR.'.env' => "FOO=bar\nBAR=baz\nSPACED=\"with spaces\"\n\nNULL=\n",
  93. self::$folder.\DIRECTORY_SEPARATOR.'example.env' => "EG=\"example\"\n",
  94. ],
  95. Reader::read(
  96. Paths::filePaths([self::$folder], ['.env', 'example.env']),
  97. false
  98. )
  99. );
  100. }
  101. public function testFileReadMultipleWithoutShortCircuitMode()
  102. {
  103. $builder = StoreBuilder::createWithDefaultName()
  104. ->addPath(self::$folder)
  105. ->addName('example.env');
  106. self::assertSame(
  107. "FOO=bar\nBAR=baz\nSPACED=\"with spaces\"\n\nNULL=\n\nEG=\"example\"\n",
  108. $builder->make()->read()
  109. );
  110. }
  111. public function testFileReadWithUtf8WithBomEncoding()
  112. {
  113. self::assertSame(
  114. [
  115. self::$folder.\DIRECTORY_SEPARATOR.'utf8-with-bom-encoding.env' => "FOO=bar\nBAR=baz\nSPACED=\"with spaces\"\n",
  116. ],
  117. Reader::read(
  118. Paths::filePaths([self::$folder], ['utf8-with-bom-encoding.env'])
  119. )
  120. );
  121. }
  122. }