EntryParserTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. declare(strict_types=1);
  3. namespace Dotenv\Tests\Parser;
  4. use Dotenv\Parser\Entry;
  5. use Dotenv\Parser\EntryParser;
  6. use Dotenv\Parser\Value;
  7. use GrahamCampbell\ResultType\Result;
  8. use PHPUnit\Framework\TestCase;
  9. final class EntryParserTest extends TestCase
  10. {
  11. public function testBasicParse()
  12. {
  13. $result = EntryParser::parse('FOO=BAR');
  14. $this->checkPositiveResult($result, 'FOO', 'BAR');
  15. }
  16. public function testNullParse()
  17. {
  18. $result = EntryParser::parse('FOO');
  19. $this->checkEmptyResult($result, 'FOO');
  20. }
  21. public function testUnicodeNameParse()
  22. {
  23. $result = EntryParser::parse('FOOƱ=BAZ');
  24. $this->checkPositiveResult($result, 'FOOƱ', 'BAZ');
  25. }
  26. public function testQuotesParse()
  27. {
  28. $result = EntryParser::parse("FOO=\"BAR \n\"");
  29. $this->checkPositiveResult($result, 'FOO', "BAR \n");
  30. }
  31. public function testNewlineParse()
  32. {
  33. $result = EntryParser::parse('FOO="\n"');
  34. $this->checkPositiveResult($result, 'FOO', "\n");
  35. }
  36. public function testTabParseDouble()
  37. {
  38. $result = EntryParser::parse('FOO="\t"');
  39. $this->checkPositiveResult($result, 'FOO', "\t");
  40. }
  41. public function testTabParseSingle()
  42. {
  43. $result = EntryParser::parse('FOO=\'\t\'');
  44. $this->checkPositiveResult($result, 'FOO', '\t');
  45. }
  46. public function testNonEscapeParse1()
  47. {
  48. $result = EntryParser::parse('FOO=\n\v');
  49. $this->checkPositiveResult($result, 'FOO', '\n\v');
  50. }
  51. public function testNonEscapeParse2()
  52. {
  53. $result = EntryParser::parse('FOO=\q');
  54. $this->checkPositiveResult($result, 'FOO', '\q');
  55. }
  56. public function testBadEscapeParse()
  57. {
  58. $result = EntryParser::parse('FOO="\q"');
  59. $this->checkErrorResult($result, 'Encountered an unexpected escape sequence at ["\q"].');
  60. }
  61. public function testInlineVariable()
  62. {
  63. $result = EntryParser::parse('FOO=$BAR');
  64. $this->checkPositiveResult($result, 'FOO', '$BAR', [0]);
  65. }
  66. public function testInlineVariableOffset()
  67. {
  68. $result = EntryParser::parse('FOO=AAA$BAR');
  69. $this->checkPositiveResult($result, 'FOO', 'AAA$BAR', [3]);
  70. }
  71. public function testInlineVariables()
  72. {
  73. $result = EntryParser::parse('FOO="TEST $BAR $$BAZ"');
  74. $this->checkPositiveResult($result, 'FOO', 'TEST $BAR $$BAZ', [11, 10, 5]);
  75. }
  76. public function testNonInlineVariable()
  77. {
  78. $result = EntryParser::parse('FOO=\'TEST $BAR $$BAZ\'');
  79. $this->checkPositiveResult($result, 'FOO', 'TEST $BAR $$BAZ');
  80. self::assertTrue($result->success()->isDefined());
  81. }
  82. public function testWhitespaceParse()
  83. {
  84. $result = EntryParser::parse("FOO=\"\n\"");
  85. $this->checkPositiveResult($result, 'FOO', "\n");
  86. }
  87. public function testExportParse()
  88. {
  89. $result = EntryParser::parse('export FOO="bar baz"');
  90. $this->checkPositiveResult($result, 'FOO', 'bar baz');
  91. }
  92. public function testExportParseTab()
  93. {
  94. $result = EntryParser::parse("export\t\"FOO\"='bar baz'");
  95. $this->checkPositiveResult($result, 'FOO', 'bar baz');
  96. }
  97. public function testExportParseFail()
  98. {
  99. $result = EntryParser::parse('export "FOO="bar baz"');
  100. $this->checkErrorResult($result, 'Encountered an invalid name at ["FOO].');
  101. }
  102. public function testClosingSlashParse()
  103. {
  104. $result = EntryParser::parse('SPVAR5="test some escaped characters like a quote \\" or maybe a backslash \\\\" # not escaped');
  105. $this->checkPositiveResult($result, 'SPVAR5', 'test some escaped characters like a quote " or maybe a backslash \\');
  106. }
  107. public function testParseInvalidSpaces()
  108. {
  109. $result = EntryParser::parse('FOO=bar baz');
  110. $this->checkErrorResult($result, 'Encountered unexpected whitespace at [bar baz].');
  111. }
  112. public function testParseStrayEquals()
  113. {
  114. $result = EntryParser::parse('=');
  115. $this->checkErrorResult($result, 'Encountered an unexpected equals at [=].');
  116. }
  117. public function testParseInvalidName()
  118. {
  119. $result = EntryParser::parse('FOO_ASD!=BAZ');
  120. $this->checkErrorResult($result, 'Encountered an invalid name at [FOO_ASD!].');
  121. }
  122. public function testParserEscapingDouble()
  123. {
  124. $result = EntryParser::parse('FOO_BAD="iiiiviiiixiiiiviiii\\a"');
  125. $this->checkErrorResult($result, 'Encountered an unexpected escape sequence at ["iiiiviiiixiiiiviiii\a"].');
  126. }
  127. public function testParserEscapingSingle()
  128. {
  129. $result = EntryParser::parse('FOO_BAD=\'iiiiviiiixiiiiviiii\\a\'');
  130. $this->checkPositiveResult($result, 'FOO_BAD', 'iiiiviiiixiiiiviiii\\a');
  131. }
  132. public function testParserMissingClosingSingleQuote()
  133. {
  134. $result = EntryParser::parse('TEST=\'erert');
  135. $this->checkErrorResult($result, 'Encountered a missing closing quote at [\'erert].');
  136. }
  137. public function testParserMissingClosingDoubleQuote()
  138. {
  139. $result = EntryParser::parse('TEST="erert');
  140. $this->checkErrorResult($result, 'Encountered a missing closing quote at ["erert].');
  141. }
  142. public function testParserMissingClosingQuotes()
  143. {
  144. $result = EntryParser::parse("TEST=\"erert\nTEST='erert\n");
  145. $this->checkErrorResult($result, 'Encountered a missing closing quote at ["erert].');
  146. }
  147. public function testParserClosingQuoteWithEscape()
  148. {
  149. $result = EntryParser::parse('TEST="\\');
  150. $this->checkErrorResult($result, 'Encountered a missing closing quote at ["\\].');
  151. }
  152. /**
  153. * @param \GrahamCampbell\ResultType\Result<\Dotenv\Parser\Entry,string> $result
  154. * @param string $name
  155. * @param string $chars
  156. * @param int[] $vars
  157. *
  158. * @return void
  159. */
  160. private function checkPositiveResult(Result $result, string $name, string $chars, array $vars = [])
  161. {
  162. self::assertTrue($result->success()->isDefined());
  163. $entry = $result->success()->get();
  164. self::assertInstanceOf(Entry::class, $entry);
  165. self::assertSame($name, $entry->getName());
  166. self::assertTrue($entry->getValue()->isDefined());
  167. $value = $entry->getValue()->get();
  168. self::assertInstanceOf(Value::class, $value);
  169. self::assertSame($chars, $value->getChars());
  170. self::assertSame($vars, $value->getVars());
  171. }
  172. /**
  173. * @param \GrahamCampbell\ResultType\Result<\Dotenv\Parser\Entry,string> $result
  174. * @param string $name
  175. *
  176. * @return void
  177. */
  178. private function checkEmptyResult(Result $result, string $name)
  179. {
  180. self::assertTrue($result->success()->isDefined());
  181. $entry = $result->success()->get();
  182. self::assertInstanceOf(Entry::class, $entry);
  183. self::assertSame('FOO', $entry->getName());
  184. self::assertFalse($entry->getValue()->isDefined());
  185. }
  186. /**
  187. * @param \GrahamCampbell\ResultType\Result<\Dotenv\Parser\Entry,string> $result
  188. * @param string $error
  189. *
  190. * @return void
  191. */
  192. private function checkErrorResult(Result $result, string $error)
  193. {
  194. self::assertTrue($result->error()->isDefined());
  195. self::assertSame($error, $result->error()->get());
  196. }
  197. }