JSONPathLexerTest.php 6.64 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
<?php
namespace Flow\JSONPath\Test;

use Flow\JSONPath\JSONPathLexer;
use Flow\JSONPath\JSONPathToken;

require_once __DIR__ . "/../vendor/autoload.php";

class JSONPathLexerTest extends \PHPUnit_Framework_TestCase
{
    public function test_Index_Wildcard()
    {
        $tokens = (new JSONPathLexer('.*'))->parseExpression();
        $this->assertEquals(JSONPathToken::T_INDEX, $tokens[0]->type);
        $this->assertEquals("*", $tokens[0]->value);
    }

    public function test_Index_Simple()
    {
        $tokens = (new JSONPathLexer('.foo'))->parseExpression();
        $this->assertEquals(JSONPathToken::T_INDEX, $tokens[0]->type);
        $this->assertEquals("foo", $tokens[0]->value);
    }

    public function test_Index_Recursive()
    {
        $tokens = (new JSONPathLexer('..teams.*'))->parseExpression();

        $this->assertEquals(3, count($tokens));

        $this->assertEquals(JSONPathToken::T_RECURSIVE, $tokens[0]->type);
        $this->assertEquals(null, $tokens[0]->value);
        $this->assertEquals(JSONPathToken::T_INDEX, $tokens[1]->type);
        $this->assertEquals('teams', $tokens[1]->value);
        $this->assertEquals(JSONPathToken::T_INDEX, $tokens[2]->type);
        $this->assertEquals('*', $tokens[2]->value);
    }

    public function test_Index_Complex()
    {
        $tokens = (new JSONPathLexer('["\'b.^*_"]'))->parseExpression();
        $this->assertEquals(JSONPathToken::T_INDEX, $tokens[0]->type);
        $this->assertEquals("'b.^*_", $tokens[0]->value);
    }

    /**
     * @expectedException           Flow\JSONPath\JSONPathException
     * @expectedExceptionMessage    Unable to parse token hello* in expression: .hello*
     */
    public function test_Index_BadlyFormed()
    {
        $tokens = (new JSONPathLexer('.hello*'))->parseExpression();
    }

    public function test_Index_Integer()
    {
        $tokens = (new \Flow\JSONPath\JSONPathLexer('[0]'))->parseExpression();
        $this->assertEquals(JSONPathToken::T_INDEX, $tokens[0]->type);
        $this->assertEquals("0", $tokens[0]->value);
    }

    public function test_Index_IntegerAfterDotNotation()
    {
        $tokens = (new \Flow\JSONPath\JSONPathLexer('.books[0]'))->parseExpression();
        $this->assertEquals(JSONPathToken::T_INDEX, $tokens[0]->type);
        $this->assertEquals(JSONPathToken::T_INDEX, $tokens[1]->type);
        $this->assertEquals("books", $tokens[0]->value);
        $this->assertEquals("0", $tokens[1]->value);
    }

    public function test_Index_Word()
    {
        $tokens = (new \Flow\JSONPath\JSONPathLexer('["foo$-/\'"]'))->parseExpression();
        $this->assertEquals(JSONPathToken::T_INDEX, $tokens[0]->type);
        $this->assertEquals("foo$-/'", $tokens[0]->value);
    }

    public function test_Index_WordWithWhitespace()
    {
        $tokens = (new \Flow\JSONPath\JSONPathLexer('[   "foo$-/\'"     ]'))->parseExpression();
        $this->assertEquals(JSONPathToken::T_INDEX, $tokens[0]->type);
        $this->assertEquals("foo$-/'", $tokens[0]->value);
    }

    public function test_Slice_Simple()
    {
        $tokens = (new \Flow\JSONPath\JSONPathLexer('[0:1:2]'))->parseExpression();
        $this->assertEquals(JSONPathToken::T_SLICE, $tokens[0]->type);
        $this->assertEquals(['start' => 0, 'end' => 1, 'step' => 2], $tokens[0]->value);
    }

    public function test_Slice_NegativeIndex()
    {
        $tokens = (new \Flow\JSONPath\JSONPathLexer('[-1]'))->parseExpression();
        $this->assertEquals(JSONPathToken::T_SLICE, $tokens[0]->type);
        $this->assertEquals(['start' => -1, 'end' => null, 'step' => null], $tokens[0]->value);
    }

    public function test_Slice_AllNull()
    {
        $tokens = (new \Flow\JSONPath\JSONPathLexer('[:]'))->parseExpression();
        $this->assertEquals(JSONPathToken::T_SLICE, $tokens[0]->type);
        $this->assertEquals(['start' => null, 'end' => null, 'step' => null], $tokens[0]->value);
    }

    public function test_QueryResult_Simple()
    {
        $tokens = (new \Flow\JSONPath\JSONPathLexer('[(@.foo + 2)]'))->parseExpression();
        $this->assertEquals(JSONPathToken::T_QUERY_RESULT, $tokens[0]->type);
        $this->assertEquals('@.foo + 2', $tokens[0]->value);
    }

    public function test_QueryMatch_Simple()
    {
        $tokens = (new \Flow\JSONPath\JSONPathLexer('[?(@.foo < \'bar\')]'))->parseExpression();
        $this->assertEquals(JSONPathToken::T_QUERY_MATCH, $tokens[0]->type);
        $this->assertEquals('@.foo < \'bar\'', $tokens[0]->value);
    }

    public function test_QueryMatch_NotEqualTO()
    {
        $tokens = (new \Flow\JSONPath\JSONPathLexer('[?(@.foo != \'bar\')]'))->parseExpression();
        $this->assertEquals(JSONPathToken::T_QUERY_MATCH, $tokens[0]->type);
        $this->assertEquals('@.foo != \'bar\'', $tokens[0]->value);
    }

    public function test_QueryMatch_Brackets()
    {
        $tokens = (new \Flow\JSONPath\JSONPathLexer("[?(@['@language']='en')]"))->parseExpression();

        $this->assertEquals(JSONPathToken::T_QUERY_MATCH, $tokens[0]->type);
        $this->assertEquals("@['@language']='en'", $tokens[0]->value);

    }


    public function test_Recursive_Simple()
    {
        $tokens = (new \Flow\JSONPath\JSONPathLexer('..foo'))->parseExpression();
        $this->assertEquals(JSONPathToken::T_RECURSIVE, $tokens[0]->type);
        $this->assertEquals(JSONPathToken::T_INDEX, $tokens[1]->type);
        $this->assertEquals(null, $tokens[0]->value);
        $this->assertEquals('foo', $tokens[1]->value);
    }


    public function test_Recursive_Wildcard()
    {
        $tokens = (new \Flow\JSONPath\JSONPathLexer('..*'))->parseExpression();
        $this->assertEquals(JSONPathToken::T_RECURSIVE, $tokens[0]->type);
        $this->assertEquals(JSONPathToken::T_INDEX, $tokens[1]->type);
        $this->assertEquals(null, $tokens[0]->value);
        $this->assertEquals('*', $tokens[1]->value);
    }


    /**
     * @expectedException           Flow\JSONPath\JSONPathException
     * @expectedExceptionMessage    Unable to parse token ba^r in expression: ..ba^r
     */
    public function test_Recursive_BadlyFormed()
    {
        $tokens = (new JSONPathLexer('..ba^r'))->parseExpression();
    }


    /**
     */
    public function test_Indexes_Simple()
    {
        $tokens = (new JSONPathLexer('[1,2,3]'))->parseExpression();
        $this->assertEquals(JSONPathToken::T_INDEXES, $tokens[0]->type);
        $this->assertEquals([1,2,3], $tokens[0]->value);
    }
    /**
     */
    public function test_Indexes_Whitespace()
    {
        $tokens = (new JSONPathLexer('[ 1,2 , 3]'))->parseExpression();
        $this->assertEquals(JSONPathToken::T_INDEXES, $tokens[0]->type);
        $this->assertEquals([1,2,3], $tokens[0]->value);
    }





}