LexerTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\ExpressionLanguage\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\ExpressionLanguage\Lexer;
  13. use Symfony\Component\ExpressionLanguage\Token;
  14. use Symfony\Component\ExpressionLanguage\TokenStream;
  15. class LexerTest extends TestCase
  16. {
  17. /**
  18. * @var Lexer
  19. */
  20. private $lexer;
  21. protected function setUp()
  22. {
  23. $this->lexer = new Lexer();
  24. }
  25. /**
  26. * @dataProvider getTokenizeData
  27. */
  28. public function testTokenize($tokens, $expression)
  29. {
  30. $tokens[] = new Token('end of expression', null, \strlen($expression) + 1);
  31. $this->assertEquals(new TokenStream($tokens, $expression), $this->lexer->tokenize($expression));
  32. }
  33. /**
  34. * @expectedException \Symfony\Component\ExpressionLanguage\SyntaxError
  35. * @expectedExceptionMessage Unexpected character "'" around position 33 for expression `service(faulty.expression.example').dummyMethod()`.
  36. */
  37. public function testTokenizeThrowsErrorWithMessage()
  38. {
  39. $expression = "service(faulty.expression.example').dummyMethod()";
  40. $this->lexer->tokenize($expression);
  41. }
  42. /**
  43. * @expectedException \Symfony\Component\ExpressionLanguage\SyntaxError
  44. * @expectedExceptionMessage Unclosed "(" around position 7 for expression `service(unclosed.expression.dummyMethod()`.
  45. */
  46. public function testTokenizeThrowsErrorOnUnclosedBrace()
  47. {
  48. $expression = 'service(unclosed.expression.dummyMethod()';
  49. $this->lexer->tokenize($expression);
  50. }
  51. public function getTokenizeData()
  52. {
  53. return [
  54. [
  55. [new Token('name', 'a', 3)],
  56. ' a ',
  57. ],
  58. [
  59. [new Token('name', 'a', 1)],
  60. 'a',
  61. ],
  62. [
  63. [new Token('string', 'foo', 1)],
  64. '"foo"',
  65. ],
  66. [
  67. [new Token('number', '3', 1)],
  68. '3',
  69. ],
  70. [
  71. [new Token('operator', '+', 1)],
  72. '+',
  73. ],
  74. [
  75. [new Token('punctuation', '.', 1)],
  76. '.',
  77. ],
  78. [
  79. [
  80. new Token('punctuation', '(', 1),
  81. new Token('number', '3', 2),
  82. new Token('operator', '+', 4),
  83. new Token('number', '5', 6),
  84. new Token('punctuation', ')', 7),
  85. new Token('operator', '~', 9),
  86. new Token('name', 'foo', 11),
  87. new Token('punctuation', '(', 14),
  88. new Token('string', 'bar', 15),
  89. new Token('punctuation', ')', 20),
  90. new Token('punctuation', '.', 21),
  91. new Token('name', 'baz', 22),
  92. new Token('punctuation', '[', 25),
  93. new Token('number', '4', 26),
  94. new Token('punctuation', ']', 27),
  95. ],
  96. '(3 + 5) ~ foo("bar").baz[4]',
  97. ],
  98. [
  99. [new Token('operator', '..', 1)],
  100. '..',
  101. ],
  102. [
  103. [new Token('string', '#foo', 1)],
  104. "'#foo'",
  105. ],
  106. [
  107. [new Token('string', '#foo', 1)],
  108. '"#foo"',
  109. ],
  110. ];
  111. }
  112. }