JSONPathLexerTest.php 6.6 KB

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