JSONPathLexerTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace Flow\JSONPath\Test;
  3. use Flow\JSONPath\JSONPathLexer;
  4. use Flow\JSONPath\JSONPathToken;
  5. use PHPUnit\Framework\TestCase;
  6. require_once __DIR__ . "/../vendor/autoload.php";
  7. class JSONPathLexerTest extends TestCase
  8. {
  9. public function test_Index_Wildcard()
  10. {
  11. $tokens = (new JSONPathLexer('.*'))->parseExpression();
  12. $this->assertEquals(JSONPathToken::T_INDEX, $tokens[0]->type);
  13. $this->assertEquals("*", $tokens[0]->value);
  14. }
  15. public function test_Index_Simple()
  16. {
  17. $tokens = (new JSONPathLexer('.foo'))->parseExpression();
  18. $this->assertEquals(JSONPathToken::T_INDEX, $tokens[0]->type);
  19. $this->assertEquals("foo", $tokens[0]->value);
  20. }
  21. public function test_Index_Recursive()
  22. {
  23. $tokens = (new JSONPathLexer('..teams.*'))->parseExpression();
  24. $this->assertEquals(3, count($tokens));
  25. $this->assertEquals(JSONPathToken::T_RECURSIVE, $tokens[0]->type);
  26. $this->assertEquals(null, $tokens[0]->value);
  27. $this->assertEquals(JSONPathToken::T_INDEX, $tokens[1]->type);
  28. $this->assertEquals('teams', $tokens[1]->value);
  29. $this->assertEquals(JSONPathToken::T_INDEX, $tokens[2]->type);
  30. $this->assertEquals('*', $tokens[2]->value);
  31. }
  32. public function test_Index_Complex()
  33. {
  34. $tokens = (new JSONPathLexer('["\'b.^*_"]'))->parseExpression();
  35. $this->assertEquals(JSONPathToken::T_INDEX, $tokens[0]->type);
  36. $this->assertEquals("'b.^*_", $tokens[0]->value);
  37. }
  38. /**
  39. * @expectedException Flow\JSONPath\JSONPathException
  40. * @expectedExceptionMessage Unable to parse token hello* in expression: .hello*
  41. */
  42. public function test_Index_BadlyFormed()
  43. {
  44. $tokens = (new JSONPathLexer('.hello*'))->parseExpression();
  45. }
  46. public function test_Index_Integer()
  47. {
  48. $tokens = (new \Flow\JSONPath\JSONPathLexer('[0]'))->parseExpression();
  49. $this->assertEquals(JSONPathToken::T_INDEX, $tokens[0]->type);
  50. $this->assertEquals("0", $tokens[0]->value);
  51. }
  52. public function test_Index_IntegerAfterDotNotation()
  53. {
  54. $tokens = (new \Flow\JSONPath\JSONPathLexer('.books[0]'))->parseExpression();
  55. $this->assertEquals(JSONPathToken::T_INDEX, $tokens[0]->type);
  56. $this->assertEquals(JSONPathToken::T_INDEX, $tokens[1]->type);
  57. $this->assertEquals("books", $tokens[0]->value);
  58. $this->assertEquals("0", $tokens[1]->value);
  59. }
  60. public function test_Index_Word()
  61. {
  62. $tokens = (new \Flow\JSONPath\JSONPathLexer('["foo$-/\'"]'))->parseExpression();
  63. $this->assertEquals(JSONPathToken::T_INDEX, $tokens[0]->type);
  64. $this->assertEquals("foo$-/'", $tokens[0]->value);
  65. }
  66. public function test_Index_WordWithWhitespace()
  67. {
  68. $tokens = (new \Flow\JSONPath\JSONPathLexer('[ "foo$-/\'" ]'))->parseExpression();
  69. $this->assertEquals(JSONPathToken::T_INDEX, $tokens[0]->type);
  70. $this->assertEquals("foo$-/'", $tokens[0]->value);
  71. }
  72. public function test_Slice_Simple()
  73. {
  74. $tokens = (new \Flow\JSONPath\JSONPathLexer('[0:1:2]'))->parseExpression();
  75. $this->assertEquals(JSONPathToken::T_SLICE, $tokens[0]->type);
  76. $this->assertEquals(['start' => 0, 'end' => 1, 'step' => 2], $tokens[0]->value);
  77. }
  78. public function test_Index_NegativeIndex()
  79. {
  80. $tokens = (new \Flow\JSONPath\JSONPathLexer('[-1]'))->parseExpression();
  81. $this->assertEquals(JSONPathToken::T_INDEX, $tokens[0]->type);
  82. $this->assertEquals(['start' => -1, 'end' => null, 'step' => null], $tokens[0]->value);
  83. }
  84. public function test_Slice_AllNull()
  85. {
  86. $tokens = (new \Flow\JSONPath\JSONPathLexer('[:]'))->parseExpression();
  87. $this->assertEquals(JSONPathToken::T_SLICE, $tokens[0]->type);
  88. $this->assertEquals(['start' => null, 'end' => null, 'step' => null], $tokens[0]->value);
  89. }
  90. public function test_QueryResult_Simple()
  91. {
  92. $tokens = (new \Flow\JSONPath\JSONPathLexer('[(@.foo + 2)]'))->parseExpression();
  93. $this->assertEquals(JSONPathToken::T_QUERY_RESULT, $tokens[0]->type);
  94. $this->assertEquals('@.foo + 2', $tokens[0]->value);
  95. }
  96. public function test_QueryMatch_Simple()
  97. {
  98. $tokens = (new \Flow\JSONPath\JSONPathLexer('[?(@.foo < \'bar\')]'))->parseExpression();
  99. $this->assertEquals(JSONPathToken::T_QUERY_MATCH, $tokens[0]->type);
  100. $this->assertEquals('@.foo < \'bar\'', $tokens[0]->value);
  101. }
  102. public function test_QueryMatch_NotEqualTO()
  103. {
  104. $tokens = (new \Flow\JSONPath\JSONPathLexer('[?(@.foo != \'bar\')]'))->parseExpression();
  105. $this->assertEquals(JSONPathToken::T_QUERY_MATCH, $tokens[0]->type);
  106. $this->assertEquals('@.foo != \'bar\'', $tokens[0]->value);
  107. }
  108. public function test_QueryMatch_Brackets()
  109. {
  110. $tokens = (new \Flow\JSONPath\JSONPathLexer("[?(@['@language']='en')]"))->parseExpression();
  111. $this->assertEquals(JSONPathToken::T_QUERY_MATCH, $tokens[0]->type);
  112. $this->assertEquals("@['@language']='en'", $tokens[0]->value);
  113. }
  114. public function test_Recursive_Simple()
  115. {
  116. $tokens = (new \Flow\JSONPath\JSONPathLexer('..foo'))->parseExpression();
  117. $this->assertEquals(JSONPathToken::T_RECURSIVE, $tokens[0]->type);
  118. $this->assertEquals(JSONPathToken::T_INDEX, $tokens[1]->type);
  119. $this->assertEquals(null, $tokens[0]->value);
  120. $this->assertEquals('foo', $tokens[1]->value);
  121. }
  122. public function test_Recursive_Wildcard()
  123. {
  124. $tokens = (new \Flow\JSONPath\JSONPathLexer('..*'))->parseExpression();
  125. $this->assertEquals(JSONPathToken::T_RECURSIVE, $tokens[0]->type);
  126. $this->assertEquals(JSONPathToken::T_INDEX, $tokens[1]->type);
  127. $this->assertEquals(null, $tokens[0]->value);
  128. $this->assertEquals('*', $tokens[1]->value);
  129. }
  130. /**
  131. * @expectedException Flow\JSONPath\JSONPathException
  132. * @expectedExceptionMessage Unable to parse token ba^r in expression: ..ba^r
  133. */
  134. public function test_Recursive_BadlyFormed()
  135. {
  136. $tokens = (new JSONPathLexer('..ba^r'))->parseExpression();
  137. }
  138. /**
  139. */
  140. public function test_Indexes_Simple()
  141. {
  142. $tokens = (new JSONPathLexer('[1,2,3]'))->parseExpression();
  143. $this->assertEquals(JSONPathToken::T_INDEXES, $tokens[0]->type);
  144. $this->assertEquals([1,2,3], $tokens[0]->value);
  145. }
  146. /**
  147. */
  148. public function test_Indexes_Whitespace()
  149. {
  150. $tokens = (new JSONPathLexer('[ 1,2 , 3]'))->parseExpression();
  151. $this->assertEquals(JSONPathToken::T_INDEXES, $tokens[0]->type);
  152. $this->assertEquals([1,2,3], $tokens[0]->value);
  153. }
  154. public function test_Root_Expression()
  155. {
  156. $tokens = (new JSONPathLexer('$'));
  157. }
  158. }