Completed
Push — master ( d70e65...52ffc4 )
by Maurício
21s queued 15s
created

ExpressionTest::testBuild()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
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
use PHPUnit\Framework\Attributes\DataProvider;
11
12
class ExpressionTest extends TestCase
13
{
14
    public function testParse(): void
15
    {
16
        $component = Expression::parse(new Parser(), $this->getTokensList('IF(film_id > 0, film_id, film_id)'));
17
        $this->assertEquals($component->expr, 'IF(film_id > 0, film_id, film_id)');
18
    }
19
20
    public function testParse2(): void
21
    {
22
        $component = Expression::parse(new Parser(), $this->getTokensList('col`test`'));
23
        $this->assertEquals($component->expr, 'col');
24
    }
25
26
    public function testParse3(): void
27
    {
28
        $component = Expression::parse(new Parser(), $this->getTokensList('col xx'));
29
        $this->assertEquals($component->alias, 'xx');
30
31
        $component = Expression::parse(new Parser(), $this->getTokensList('col y'));
32
        $this->assertEquals($component->alias, 'y');
33
34
        $component = Expression::parse(new Parser(), $this->getTokensList('avg.col FROM (SELECT ev.col FROM ev)'));
35
        $this->assertEquals($component->table, 'avg');
36
        $this->assertEquals($component->expr, 'avg.col');
37
38
        $component = Expression::parse(new Parser(), $this->getTokensList('x.id FROM (SELECT a.id FROM a) x'));
39
        $this->assertEquals($component->table, 'x');
40
        $this->assertEquals($component->expr, 'x.id');
41
    }
42
43
    #[DataProvider('parseErrProvider')]
44
    public function testParseErr(string $expr, string $error): void
45
    {
46
        $parser = new Parser();
47
        Expression::parse($parser, $this->getTokensList($expr));
48
        $errors = $this->getErrorsAsArray($parser);
49
        $this->assertEquals($errors[0][0], $error);
50
    }
51
52
    /**
53
     * @return string[][]
54
     */
55
    public static function parseErrProvider(): array
56
    {
57
        return [
58
            /*
59
            [
60
                '(1))',
61
                'Unexpected closing bracket.',
62
            ],
63
            */
64
            [
65
                'tbl..col',
66
                'Unexpected dot.',
67
            ],
68
            [
69
                'id AS AS id2',
70
                'An alias was expected.',
71
            ],
72
            [
73
                'id`id2`\'id3\'',
74
                'An alias was previously found.',
75
            ],
76
            [
77
                '(id) id2 id3',
78
                'An alias was previously found.',
79
            ],
80
        ];
81
    }
82
83
    public function testBuildAll(): void
84
    {
85
        $component = [
86
            new Expression('1 + 2', 'three'),
87
            new Expression('1 + 3', 'four'),
88
        ];
89
        $this->assertEquals(
90
            Expression::buildAll($component),
91
            '1 + 2 AS `three`, 1 + 3 AS `four`'
92
        );
93
    }
94
95
    /**
96
     * @return string[][]
97
     */
98
    public static function mysqlCommandsProvider(): array
99
    {
100
        return [
101
            [
102
                '/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;',
103
                'SET  @OLD_CHARACTER_SET_CLIENT = @@CHARACTER_SET_CLIENT',
104
            ],
105
            [
106
                '/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;',
107
                'SET  @OLD_CHARACTER_SET_RESULTS = @@CHARACTER_SET_RESULTS',
108
            ],
109
            [
110
                '/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;',
111
                'SET  @OLD_COLLATION_CONNECTION = @@COLLATION_CONNECTION',
112
            ],
113
            [
114
                '/*!40101 SET NAMES utf8 */;',
115
                'SET NAMES utf8',
116
            ],
117
            [
118
                '/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;',
119
                'SET  @OLD_TIME_ZONE = @@TIME_ZONE',
120
            ],
121
            [
122
                "/*!40103 SET TIME_ZONE='+00:00' */;",
123
                "SET  TIME_ZONE = '+00:00'",
124
            ],
125
            [
126
                '/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;',
127
                'SET  @OLD_UNIQUE_CHECKS = @@UNIQUE_CHECKS, UNIQUE_CHECKS = 0',
128
            ],
129
            [
130
                '/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;',
131
                'SET  @OLD_FOREIGN_KEY_CHECKS = @@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS = 0',
132
            ],
133
            [
134
                "/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;",
135
                "SET  @OLD_SQL_MODE = @@SQL_MODE, SQL_MODE = 'NO_AUTO_VALUE_ON_ZERO'",
136
            ],
137
            [
138
                '/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;',
139
                'SET  @OLD_SQL_NOTES = @@SQL_NOTES, SQL_NOTES = 0',
140
            ],
141
        ];
142
    }
143
144
    #[DataProvider('mysqlCommandsProvider')]
145
    public function testMysqlCommands(string $expr, string $expected): void
146
    {
147
        $parser = new Parser($expr, true);
148
        $parser->parse();
149
        self::assertSame($expected, $parser->statements[0]->build());
150
    }
151
}
152