Passed
Pull Request — master (#385)
by
unknown
03:51
created

ExpressionTest::testParse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Tests\Components;
6
7
use PhpMyAdmin\SqlParser\Components\Expression;
8
use PhpMyAdmin\SqlParser\Parser;
9
use PhpMyAdmin\SqlParser\Tests\TestCase;
10
11
class ExpressionTest extends TestCase
12
{
13
    public function testParse(): void
14
    {
15
        $component = Expression::parse(new Parser(), $this->getTokensList('IF(film_id > 0, film_id, film_id)'));
16
        $this->assertEquals($component->expr, 'IF(film_id > 0, film_id, film_id)');
17
    }
18
19
    public function testParse2(): void
20
    {
21
        $component = Expression::parse(new Parser(), $this->getTokensList('col`test`'));
22
        $this->assertEquals($component->expr, 'col');
23
    }
24
25
    public function testParse3(): void
26
    {
27
        $component = Expression::parse(new Parser(), $this->getTokensList('col x'));
28
        $this->assertEquals($component->alias, 'x');
29
30
        $component = Expression::parse(new Parser(), $this->getTokensList('col y'));
31
        $this->assertEquals($component->alias, 'y');
32
33
        $component = Expression::parse(new Parser(), $this->getTokensList('x.col FROM (SELECT ev.col FROM ev) x'));
34
        $this->assertEquals($component->table, 'x');
35
36
        $component = Expression::parse(new Parser(), $this->getTokensList('y.col FROM (SELECT ev.col FROM ev) y'));
37
        $this->assertEquals($component->table, 'y');
38
    }
39
40
    /**
41
     * @dataProvider parseErrProvider
42
     */
43
    public function testParseErr(string $expr, string $error): void
44
    {
45
        $parser = new Parser();
46
        Expression::parse($parser, $this->getTokensList($expr));
47
        $errors = $this->getErrorsAsArray($parser);
48
        $this->assertEquals($errors[0][0], $error);
49
    }
50
51
    /**
52
     * @return string[][]
53
     */
54
    public function parseErrProvider(): array
55
    {
56
        return [
57
            /*
58
            [
59
                '(1))',
60
                'Unexpected closing bracket.',
61
            ],
62
            */
63
            [
64
                'tbl..col',
65
                'Unexpected dot.',
66
            ],
67
            [
68
                'id AS AS id2',
69
                'An alias was expected.',
70
            ],
71
            [
72
                'id`id2`\'id3\'',
73
                'An alias was previously found.',
74
            ],
75
            [
76
                '(id) id2 id3',
77
                'An alias was previously found.',
78
            ],
79
        ];
80
    }
81
82
    public function testBuild(): void
83
    {
84
        $component = [
85
            new Expression('1 + 2', 'three'),
86
            new Expression('1 + 3', 'four'),
87
        ];
88
        $this->assertEquals(
89
            Expression::build($component),
90
            '1 + 2 AS `three`, 1 + 3 AS `four`'
91
        );
92
    }
93
}
94