ParserTest.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. declare(strict_types=1);
  3. namespace Dotenv\Tests\Parser;
  4. use Dotenv\Exception\InvalidFileException;
  5. use Dotenv\Parser\Entry;
  6. use Dotenv\Parser\Parser;
  7. use Dotenv\Parser\ParserInterface;
  8. use Dotenv\Parser\Value;
  9. use PHPUnit\Framework\TestCase;
  10. final class ParserTest extends TestCase
  11. {
  12. public function testParserInstanceOf()
  13. {
  14. self::assertInstanceOf(ParserInterface::class, new Parser());
  15. }
  16. public function testFullParse()
  17. {
  18. $result = (new Parser())->parse("FOO=BAR\nFOO\nFOO=\"BAR \n\"\nFOO=\"\\n\"");
  19. self::assertIsArray($result);
  20. self::assertCount(4, $result);
  21. $this->checkPositiveEntry($result[0], 'FOO', 'BAR');
  22. $this->checkEmptyEntry($result[1], 'FOO');
  23. $this->checkPositiveEntry($result[2], 'FOO', "BAR \n");
  24. $this->checkPositiveEntry($result[3], 'FOO', "\n");
  25. }
  26. public function testBadEscapeParse()
  27. {
  28. $this->expectException(InvalidFileException::class);
  29. $this->expectExceptionMessage('Failed to parse dotenv file. Encountered an unexpected escape sequence at ["\q"].');
  30. (new Parser())->parse('FOO="\q"');
  31. }
  32. public function testParseInvalidSpaces()
  33. {
  34. $this->expectException(InvalidFileException::class);
  35. $this->expectExceptionMessage('Failed to parse dotenv file. Encountered unexpected whitespace at [bar baz].');
  36. (new Parser())->parse("FOO=bar baz\n");
  37. }
  38. public function testParseStrayEquals()
  39. {
  40. $this->expectException(InvalidFileException::class);
  41. $this->expectExceptionMessage('Failed to parse dotenv file. Encountered an unexpected equals at [=].');
  42. (new Parser())->parse("=\n");
  43. }
  44. public function testParseInvalidName()
  45. {
  46. $this->expectException(InvalidFileException::class);
  47. $this->expectExceptionMessage('Failed to parse dotenv file. Encountered an invalid name at [FOO_ASD!].');
  48. (new Parser())->parse('FOO_ASD!=BAZ');
  49. }
  50. /**
  51. * @param \Dotenv\Parser\Entry $entry
  52. * @param string $name
  53. * @param string $chars
  54. * @param int[] $vars
  55. *
  56. * @return void
  57. */
  58. private function checkPositiveEntry(Entry $entry, string $name, string $chars, array $vars = [])
  59. {
  60. self::assertInstanceOf(Entry::class, $entry);
  61. self::assertSame($name, $entry->getName());
  62. self::assertTrue($entry->getValue()->isDefined());
  63. $value = $entry->getValue()->get();
  64. self::assertInstanceOf(Value::class, $value);
  65. self::assertSame($chars, $value->getChars());
  66. self::assertSame($vars, $value->getVars());
  67. }
  68. /**
  69. * @param \Dotenv\Parser\Entry $entry
  70. * @param string $name
  71. *
  72. * @return void
  73. */
  74. private function checkEmptyEntry(Entry $entry, string $name)
  75. {
  76. self::assertInstanceOf(Entry::class, $entry);
  77. self::assertSame('FOO', $entry->getName());
  78. self::assertFalse($entry->getValue()->isDefined());
  79. }
  80. }