Move common code into a function
[phpmyadmin.git] / test / PMA_SQL_parser_test.php
blobaa3299c52497ea070de8ae725e7de0af8b294e43
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * tests for correctness of SQL parser
6 * @package phpMyAdmin-test
7 */
9 /**
10 * Tests core.
12 require_once 'PHPUnit/Framework.php';
14 define('PHPMYADMIN', 1);
15 define('TESTSUITE', 1);
16 $GLOBALS['charset'] = 'utf-8';
18 function __($s) {
19 return $s;
22 /**
23 * Include to test.
25 require_once './libraries/sqlparser.lib.php';
27 /**
28 * Test for SQL parser
30 * @package phpMyAdmin-test
32 class PMA_SQL_parser_test extends PHPUnit_Framework_TestCase
34 private function assertParser($sql, $expected, $error = '')
36 $parsed_sql = PMA_SQP_parse($sql);
37 $this->assertEquals(PMA_SQP_getErrorString(), $error);
38 $this->assertEquals($parsed_sql, $expected);
41 public function testParse_1()
43 $this->assertParser('SELECT 1;', array (
44 'raw' => 'SELECT 1;',
45 0 =>
46 array (
47 'type' => 'alpha_reservedWord',
48 'data' => 'SELECT',
49 'pos' => 6,
50 'forbidden' => true,
52 1 =>
53 array (
54 'type' => 'digit_integer',
55 'data' => '1',
56 'pos' => 8,
58 2 =>
59 array (
60 'type' => 'punct_queryend',
61 'data' => ';',
62 'pos' => 0,
64 'len' => 3,
65 ));
68 public function testParse_2()
70 $this->assertParser('SELECT * from aaa;', array (
71 'raw' => 'SELECT * from aaa;',
72 0 =>
73 array (
74 'type' => 'alpha_reservedWord',
75 'data' => 'SELECT',
76 'pos' => 6,
77 'forbidden' => true,
79 1 =>
80 array (
81 'type' => 'punct',
82 'data' => '*',
83 'pos' => 0,
85 2 =>
86 array (
87 'type' => 'alpha_reservedWord',
88 'data' => 'from',
89 'pos' => 13,
90 'forbidden' => true,
92 3 =>
93 array (
94 'type' => 'alpha_identifier',
95 'data' => 'aaa',
96 'pos' => 17,
97 'forbidden' => false,
99 4 =>
100 array (
101 'type' => 'punct_queryend',
102 'data' => ';',
103 'pos' => 0,
105 'len' => 5,
109 public function testParse_3()
111 $this->assertParser('SELECT * from `aaa`;', array (
112 'raw' => 'SELECT * from `aaa`;',
113 0 =>
114 array (
115 'type' => 'alpha_reservedWord',
116 'data' => 'SELECT',
117 'pos' => 6,
118 'forbidden' => true,
120 1 =>
121 array (
122 'type' => 'punct',
123 'data' => '*',
124 'pos' => 0,
126 2 =>
127 array (
128 'type' => 'alpha_reservedWord',
129 'data' => 'from',
130 'pos' => 13,
131 'forbidden' => true,
133 3 =>
134 array (
135 'type' => 'quote_backtick',
136 'data' => '`aaa`',
137 'pos' => 0,
139 4 =>
140 array (
141 'type' => 'punct_queryend',
142 'data' => ';',
143 'pos' => 0,
145 'len' => 5,
149 public function testParse_4()
151 $this->assertParser('SELECT * from `aaa;', array (
152 'raw' => 'SELECT * from `aaa`;',
153 0 =>
154 array (
155 'type' => 'alpha_reservedWord',
156 'data' => 'SELECT',
157 'pos' => 6,
158 'forbidden' => true,
160 1 =>
161 array (
162 'type' => 'punct',
163 'data' => '*',
164 'pos' => 0,
166 2 =>
167 array (
168 'type' => 'alpha_reservedWord',
169 'data' => 'from',
170 'pos' => 13,
171 'forbidden' => true,
173 3 =>
174 array (
175 'type' => 'quote_backtick',
176 'data' => '`aaa`',
177 'pos' => 0,
179 4 =>
180 array (
181 'type' => 'punct_queryend',
182 'data' => ';',
183 'pos' => 0,
185 'len' => 5,
189 public function testParse_5()
191 $this->assertParser('SELECT * FROM `a_table` tbla INNER JOIN b_table` tblb ON tblb.id = tbla.id WHERE tblb.field1 != tbla.field1`;', array (
192 'raw' => 'SELECT * FROM `a_table` tbla INNER JOIN b_table` tblb ON tblb.id = tbla.id WHERE tblb.field1 != tbla.field1`;',
193 0 =>
194 array (
195 'type' => 'alpha_reservedWord',
196 'data' => 'SELECT',
197 'pos' => 6,
198 'forbidden' => true,
200 1 =>
201 array (
202 'type' => 'punct',
203 'data' => '*',
204 'pos' => 0,
206 2 =>
207 array (
208 'type' => 'alpha_reservedWord',
209 'data' => 'FROM',
210 'pos' => 13,
211 'forbidden' => true,
213 3 =>
214 array (
215 'type' => 'quote_backtick',
216 'data' => '`a_table`',
217 'pos' => 0,
219 4 =>
220 array (
221 'type' => 'alpha_identifier',
222 'data' => 'tbla',
223 'pos' => 28,
224 'forbidden' => false,
226 5 =>
227 array (
228 'type' => 'alpha_reservedWord',
229 'data' => 'INNER',
230 'pos' => 34,
231 'forbidden' => true,
233 6 =>
234 array (
235 'type' => 'alpha_reservedWord',
236 'data' => 'JOIN',
237 'pos' => 39,
238 'forbidden' => true,
240 7 =>
241 array (
242 'type' => 'alpha_identifier',
243 'data' => 'b_table',
244 'pos' => 47,
245 'forbidden' => false,
247 8 =>
248 array (
249 'type' => 'quote_backtick',
250 'data' => '` tblb ON tblb.id = tbla.id WHERE tblb.field1 != tbla.field1`',
251 'pos' => 0,
253 9 =>
254 array (
255 'type' => 'punct_queryend',
256 'data' => ';',
257 'pos' => 0,
259 'len' => 10,