LexerTest.php 7.12 KB
Newer Older
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
<?php

namespace Doctrine\Tests\ORM\Query;

use Doctrine\ORM\Query\Lexer;

require_once __DIR__ . '/../../TestInit.php';

class LexerTest extends \Doctrine\Tests\OrmTestCase
{
    //private $_lexer;

    protected function setUp() {
    }

    public function testScannerRecognizesIdentifierWithLengthOfOneCharacter()
    {
        $lexer = new Lexer('u');
        
        $lexer->moveNext();
        $token = $lexer->lookahead;

        $this->assertEquals(Lexer::T_IDENTIFIER, $token['type']);
        $this->assertEquals('u', $token['value']);
    }

    public function testScannerRecognizesIdentifierConsistingOfLetters()
    {
        $lexer = new Lexer('someIdentifier');

        $lexer->moveNext();
        $token = $lexer->lookahead;
        $this->assertEquals(Lexer::T_IDENTIFIER, $token['type']);
        $this->assertEquals('someIdentifier', $token['value']);
    }

    public function testScannerRecognizesIdentifierIncludingDigits()
    {
        $lexer = new Lexer('s0m31d3nt1f13r');

        $lexer->moveNext();
        $token = $lexer->lookahead;
        $this->assertEquals(Lexer::T_IDENTIFIER, $token['type']);
        $this->assertEquals('s0m31d3nt1f13r', $token['value']);
    }

    public function testScannerRecognizesIdentifierIncludingUnderscore()
    {
        $lexer = new Lexer('some_identifier');
        $lexer->moveNext();
        $token = $lexer->lookahead;
        $this->assertEquals(Lexer::T_IDENTIFIER, $token['type']);
        $this->assertEquals('some_identifier', $token['value']);
    }

    public function testScannerRecognizesDecimalInteger()
    {
        $lexer = new Lexer('1234');
        $lexer->moveNext();
        $token = $lexer->lookahead;
        $this->assertEquals(Lexer::T_INTEGER, $token['type']);
        $this->assertEquals(1234, $token['value']);
    }

    public function testScannerRecognizesFloat()
    {
        $lexer = new Lexer('1.234');
        $lexer->moveNext();
        $token = $lexer->lookahead;
        $this->assertEquals(Lexer::T_FLOAT, $token['type']);
        $this->assertEquals(1.234, $token['value']);
    }

    public function testScannerRecognizesFloatWithExponent()
    {
        $lexer = new Lexer('1.2e3');
        $lexer->moveNext();
        $token = $lexer->lookahead;
        $this->assertEquals(Lexer::T_FLOAT, $token['type']);
        $this->assertEquals(1.2e3, $token['value']);
    }

    public function testScannerRecognizesFloatWithExponent2()
    {
        $lexer = new Lexer('0.2e3');
        $lexer->moveNext();
        $token = $lexer->lookahead;
        $this->assertEquals(Lexer::T_FLOAT, $token['type']);
        $this->assertEquals(.2e3, $token['value']);
    }

    public function testScannerRecognizesFloatWithNegativeExponent()
    {
        $lexer = new Lexer('7E-10');
        $lexer->moveNext();
        $token = $lexer->lookahead;
        $this->assertEquals(Lexer::T_FLOAT, $token['type']);
        $this->assertEquals(7E-10, $token['value']);
    }

    public function testScannerRecognizesFloatBig()
    {
103
        $lexer = new Lexer('123456789.01');
104 105 106
        $lexer->moveNext();
        $token = $lexer->lookahead;
        $this->assertEquals(Lexer::T_FLOAT, $token['type']);
107
        $this->assertEquals(1.2345678901e8, $token['value']);
108 109 110 111 112 113 114
    }

    public function testScannerRecognizesFloatContainingWhitespace()
    {
        $lexer = new Lexer('-   1.234e2');
        $lexer->moveNext();
        $token = $lexer->lookahead;
115
        $this->assertEquals(Lexer::T_MINUS, $token['type']);
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
        $this->assertEquals('-', $token['value']);

        $lexer->moveNext();
        $token = $lexer->lookahead;
        $this->assertEquals(Lexer::T_FLOAT, $token['type']);
        $this->assertNotEquals(-1.234e2, $token['value']);
        $this->assertEquals(1.234e2, $token['value']);
    }

    public function testScannerRecognizesStringContainingWhitespace()
    {
        $lexer = new Lexer("'This is a string.'");
        $lexer->moveNext();
        $token = $lexer->lookahead;
        $this->assertEquals(Lexer::T_STRING, $token['type']);
romanb's avatar
romanb committed
131
        $this->assertEquals("This is a string.", $token['value']);
132 133 134 135 136 137 138 139
    }

    public function testScannerRecognizesStringContainingSingleQuotes()
    {
        $lexer = new Lexer("'abc''defg'''");
        $lexer->moveNext();
        $token = $lexer->lookahead;
        $this->assertEquals(Lexer::T_STRING, $token['type']);
romanb's avatar
romanb committed
140
        $this->assertEquals("abc'defg'", $token['value']);
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 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    }

    public function testScannerRecognizesInputParameter()
    {
        $lexer = new Lexer('?1');
        $lexer->moveNext();
        $token = $lexer->lookahead;
        $this->assertEquals(Lexer::T_INPUT_PARAMETER, $token['type']);
        $this->assertEquals('?1', $token['value']);
    }

    public function testScannerRecognizesNamedInputParameter()
    {
        $lexer = new Lexer(':name');
        $lexer->moveNext();
        $token = $lexer->lookahead;
        $this->assertEquals(Lexer::T_INPUT_PARAMETER, $token['type']);
        $this->assertEquals(':name', $token['value']);
    }

    public function testScannerTokenizesASimpleQueryCorrectly()
    {
        $dql = "SELECT u FROM My\Namespace\User u WHERE u.name = 'Jack O''Neil'";
        $lexer = new Lexer($dql);

        $tokens = array(
            array(
                'value' => 'SELECT',
                'type'  => Lexer::T_SELECT,
                'position' => 0
            ),
            array(
                'value' => 'u',
                'type'  => Lexer::T_IDENTIFIER,
                'position' => 7
            ),
            array(
                'value' => 'FROM',
                'type'  => Lexer::T_FROM,
                'position' => 9
            ),
            array(
                'value' => 'My\Namespace\User',
                'type'  => Lexer::T_IDENTIFIER,
                'position' => 14
            ),
            array(
                'value' => 'u',
                'type'  => Lexer::T_IDENTIFIER,
                'position' => 32
            ),
            array(
                'value' => 'WHERE',
                'type'  => Lexer::T_WHERE,
                'position' => 34
            ),
            array(
                'value' => 'u',
                'type'  => Lexer::T_IDENTIFIER,
                'position' => 40
            ),
            array(
                'value' => '.',
204
                'type'  => Lexer::T_DOT,
205 206 207 208 209 210 211 212 213
                'position' => 41
            ),
            array(
                'value' => 'name',
                'type'  => Lexer::T_IDENTIFIER,
                'position' => 42
            ),
            array(
                'value' => '=',
214
                'type'  => Lexer::T_EQUALS,
215 216 217
                'position' => 47
            ),
            array(
romanb's avatar
romanb committed
218
                'value' => "Jack O'Neil",
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
                'type'  => Lexer::T_STRING,
                'position' => 49
            )
        );

        foreach ($tokens as $expected) {
            $lexer->moveNext();
            $actual = $lexer->lookahead;
            $this->assertEquals($expected['value'], $actual['value']);
            $this->assertEquals($expected['type'], $actual['type']);
            $this->assertEquals($expected['position'], $actual['position']);
        }

        $this->assertFalse($lexer->moveNext());
    }
}