summaryrefslogtreecommitdiffstats
path: root/tests/Components/ExpressionTest.php
blob: 11d10e4dd99c12fb777880b9a9ed02ee7e52c4ee (plain)
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
<?php

namespace SqlParser\Tests\Components;

use SqlParser\Parser;
use SqlParser\Components\Expression;
use SqlParser\Tests\TestCase;

class ExpressionTest extends TestCase
{
    public function testParse()
    {
        $component = Expression::parse(new Parser(), $this->getTokensList('IF(film_id > 0, film_id, film_id)'));
        $this->assertEquals($component->expr, 'IF(film_id > 0, film_id, film_id)');
    }

    public function testParse2()
    {
        $component = Expression::parse(new Parser(), $this->getTokensList('col`test`'));
    }

    /**
     * @dataProvider testParseErrProvider
     */
    public function testParseErr($expr, $error)
    {
        $parser = new Parser();
        Expression::parse($parser, $this->getTokensList($expr));
        $errors = $this->getErrorsAsArray($parser);
        $this->assertEquals($errors[0][0], $error);
    }

    public function testParseErrProvider()
    {
        return array(
            /*
            array(
                '(1))',
                'Unexpected closing bracket.',
            ),
            */
            array(
                'tbl..col',
                'Unexpected dot.',
            ),
            array(
                'id AS AS id2',
                'An alias was expected.',
            ),
            array(
                'id`id2`\'id3\'',
                'An alias was previously found.',
            ),
            array(
                '(id) id2 id3',
                'An alias was previously found.',
            ),
        );
    }

    public function testBuild()
    {
        $component = array(
            new Expression('1 + 2', 'three'),
            new Expression('1 + 3', 'four'),
        );
        $this->assertEquals(
            Expression::build($component),
            '1 + 2 AS `three`, 1 + 3 AS `four`'
        );
    }
}