|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpMyAdmin\SqlParser\Tests\Components; |
|
4
|
|
|
|
|
5
|
|
|
use PhpMyAdmin\SqlParser\Components\Expression; |
|
6
|
|
|
use PhpMyAdmin\SqlParser\Parser; |
|
7
|
|
|
use PhpMyAdmin\SqlParser\Tests\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
class ExpressionTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
public function testParse() |
|
12
|
|
|
{ |
|
13
|
|
|
$component = Expression::parse(new Parser(), $this->getTokensList('IF(film_id > 0, film_id, film_id)')); |
|
14
|
|
|
$this->assertEquals($component->expr, 'IF(film_id > 0, film_id, film_id)'); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function testParse2() |
|
18
|
|
|
{ |
|
19
|
|
|
$component = Expression::parse(new Parser(), $this->getTokensList('col`test`')); |
|
20
|
|
|
$this->assertEquals($component->expr, 'col'); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @dataProvider parseErrProvider |
|
25
|
|
|
* |
|
26
|
|
|
* @param mixed $expr |
|
27
|
|
|
* @param mixed $error |
|
28
|
|
|
*/ |
|
29
|
|
|
public function testParseErr($expr, $error) |
|
30
|
|
|
{ |
|
31
|
|
|
$parser = new Parser(); |
|
32
|
|
|
Expression::parse($parser, $this->getTokensList($expr)); |
|
33
|
|
|
$errors = $this->getErrorsAsArray($parser); |
|
34
|
|
|
$this->assertEquals($errors[0][0], $error); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function parseErrProvider() |
|
38
|
|
|
{ |
|
39
|
|
|
return array( |
|
40
|
|
|
/* |
|
41
|
|
|
array( |
|
42
|
|
|
'(1))', |
|
43
|
|
|
'Unexpected closing bracket.', |
|
44
|
|
|
), |
|
45
|
|
|
*/ |
|
46
|
|
|
array( |
|
47
|
|
|
'tbl..col', |
|
48
|
|
|
'Unexpected dot.', |
|
49
|
|
|
), |
|
50
|
|
|
array( |
|
51
|
|
|
'id AS AS id2', |
|
52
|
|
|
'An alias was expected.', |
|
53
|
|
|
), |
|
54
|
|
|
array( |
|
55
|
|
|
'id`id2`\'id3\'', |
|
56
|
|
|
'An alias was previously found.', |
|
57
|
|
|
), |
|
58
|
|
|
array( |
|
59
|
|
|
'(id) id2 id3', |
|
60
|
|
|
'An alias was previously found.', |
|
61
|
|
|
) |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testBuild() |
|
66
|
|
|
{ |
|
67
|
|
|
$component = array( |
|
68
|
|
|
new Expression('1 + 2', 'three'), |
|
69
|
|
|
new Expression('1 + 3', 'four') |
|
70
|
|
|
); |
|
71
|
|
|
$this->assertEquals( |
|
72
|
|
|
Expression::build($component), |
|
73
|
|
|
'1 + 2 AS `three`, 1 + 3 AS `four`' |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|