|
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 xx')); |
|
28
|
|
|
$this->assertEquals($component->alias, 'xx'); |
|
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('avg.col FROM (SELECT ev.col FROM ev)')); |
|
34
|
|
|
$this->assertEquals($component->table, 'avg'); |
|
35
|
|
|
$this->assertEquals($component->expr, 'avg.col'); |
|
36
|
|
|
|
|
37
|
|
|
$component = Expression::parse(new Parser(), $this->getTokensList('x.id FROM (SELECT a.id FROM a) x')); |
|
38
|
|
|
$this->assertEquals($component->table, 'x'); |
|
39
|
|
|
$this->assertEquals($component->expr, 'x.id'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @dataProvider parseErrProvider |
|
44
|
|
|
*/ |
|
45
|
|
|
public function testParseErr(string $expr, string $error): void |
|
46
|
|
|
{ |
|
47
|
|
|
$parser = new Parser(); |
|
48
|
|
|
Expression::parse($parser, $this->getTokensList($expr)); |
|
49
|
|
|
$errors = $this->getErrorsAsArray($parser); |
|
50
|
|
|
$this->assertEquals($errors[0][0], $error); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return string[][] |
|
55
|
|
|
*/ |
|
56
|
|
|
public function parseErrProvider(): array |
|
57
|
|
|
{ |
|
58
|
|
|
return [ |
|
59
|
|
|
/* |
|
60
|
|
|
[ |
|
61
|
|
|
'(1))', |
|
62
|
|
|
'Unexpected closing bracket.', |
|
63
|
|
|
], |
|
64
|
|
|
*/ |
|
65
|
|
|
[ |
|
66
|
|
|
'tbl..col', |
|
67
|
|
|
'Unexpected dot.', |
|
68
|
|
|
], |
|
69
|
|
|
[ |
|
70
|
|
|
'id AS AS id2', |
|
71
|
|
|
'An alias was expected.', |
|
72
|
|
|
], |
|
73
|
|
|
[ |
|
74
|
|
|
'id`id2`\'id3\'', |
|
75
|
|
|
'An alias was previously found.', |
|
76
|
|
|
], |
|
77
|
|
|
[ |
|
78
|
|
|
'(id) id2 id3', |
|
79
|
|
|
'An alias was previously found.', |
|
80
|
|
|
], |
|
81
|
|
|
]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testBuild(): void |
|
85
|
|
|
{ |
|
86
|
|
|
$component = [ |
|
87
|
|
|
new Expression('1 + 2', 'three'), |
|
88
|
|
|
new Expression('1 + 3', 'four'), |
|
89
|
|
|
]; |
|
90
|
|
|
$this->assertEquals( |
|
91
|
|
|
Expression::build($component), |
|
92
|
|
|
'1 + 2 AS `three`, 1 + 3 AS `four`' |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|