1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\SqlParser\Tests\Utils; |
6
|
|
|
|
7
|
|
|
use PhpMyAdmin\SqlParser\Tests\TestCase; |
8
|
|
|
use PhpMyAdmin\SqlParser\Utils\CLI; |
9
|
|
|
use function dirname; |
10
|
|
|
use function exec; |
11
|
|
|
use const PHP_BINARY; |
12
|
|
|
|
13
|
|
|
class CLITest extends TestCase |
14
|
|
|
{ |
15
|
|
|
private function getCLI($getopt) |
16
|
|
|
{ |
17
|
|
|
$cli = $this->getMockBuilder('PhpMyAdmin\SqlParser\Utils\CLI')->setMethods(['getopt'])->getMock(); |
18
|
|
|
$cli->method('getopt')->willReturn($getopt); |
19
|
|
|
|
20
|
|
|
return $cli; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
private function getCLIStdIn($input, $getopt) |
24
|
|
|
{ |
25
|
|
|
$cli = $this->getMockBuilder('PhpMyAdmin\SqlParser\Utils\CLI')->setMethods(['getopt', 'readStdin'])->getMock(); |
26
|
|
|
$cli->method('getopt')->willReturn($getopt); |
27
|
|
|
$cli->method('readStdin')->willReturn($input); |
28
|
|
|
|
29
|
|
|
return $cli; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Test that getopt call works. |
34
|
|
|
* |
35
|
|
|
* We do mock it for other tests to return values we want. |
36
|
|
|
*/ |
37
|
|
|
public function testGetopt() |
38
|
|
|
{ |
39
|
|
|
$cli = new CLI(); |
40
|
|
|
$this->assertEquals( |
41
|
|
|
$cli->getopt('', []), |
42
|
|
|
[] |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param mixed $getopt |
48
|
|
|
* @param mixed $output |
49
|
|
|
* @param mixed $result |
50
|
|
|
* |
51
|
|
|
* @dataProvider highlightParams |
52
|
|
|
*/ |
53
|
|
|
public function testRunHighlight($getopt, $output, $result) |
54
|
|
|
{ |
55
|
|
|
$cli = $this->getCLI($getopt); |
56
|
|
|
$this->expectOutputString($output); |
57
|
|
|
$this->assertEquals($result, $cli->runHighlight()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function highlightParams() |
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
|
|
[ |
64
|
|
|
['q' => 'SELECT 1'], |
65
|
|
|
"\x1b[35mSELECT\n \x1b[92m1\x1b[0m\n", |
66
|
|
|
0, |
67
|
|
|
], |
68
|
|
|
[ |
69
|
|
|
['query' => 'SELECT 1'], |
70
|
|
|
"\x1b[35mSELECT\n \x1b[92m1\x1b[0m\n", |
71
|
|
|
0, |
72
|
|
|
], |
73
|
|
|
[ |
74
|
|
|
[ |
75
|
|
|
'q' => 'SELECT /* comment */ 1 /* other */', |
76
|
|
|
'f' => 'text', |
77
|
|
|
], |
78
|
|
|
"SELECT\n /* comment */ 1 /* other */\n", |
79
|
|
|
0, |
80
|
|
|
], |
81
|
|
|
[ |
82
|
|
|
[ |
83
|
|
|
'q' => 'SELECT 1', |
84
|
|
|
'f' => 'foo', |
85
|
|
|
], |
86
|
|
|
"ERROR: Invalid value for format!\n", |
87
|
|
|
1, |
88
|
|
|
], |
89
|
|
|
[ |
90
|
|
|
[ |
91
|
|
|
'q' => 'SELECT 1', |
92
|
|
|
'f' => 'html', |
93
|
|
|
], |
94
|
|
|
'<span class="sql-reserved">SELECT</span><br/>' . |
95
|
|
|
' <span class="sql-number">1</span>' . "\n", |
96
|
|
|
0, |
97
|
|
|
], |
98
|
|
|
[ |
99
|
|
|
['h' => true], |
100
|
|
|
'Usage: highlight-query --query SQL [--format html|cli|text] [--ansi]' . "\n" . |
101
|
|
|
' cat file.sql | highlight-query' . "\n", |
102
|
|
|
0, |
103
|
|
|
], |
104
|
|
|
[ |
105
|
|
|
[], |
106
|
|
|
'ERROR: Missing parameters!' . "\n" . |
107
|
|
|
'Usage: highlight-query --query SQL [--format html|cli|text] [--ansi]' . "\n" . |
108
|
|
|
' cat file.sql | highlight-query' . "\n", |
109
|
|
|
1, |
110
|
|
|
], |
111
|
|
|
[ |
112
|
|
|
false, |
113
|
|
|
'', |
114
|
|
|
1, |
115
|
|
|
], |
116
|
|
|
]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param mixed $input |
121
|
|
|
* @param mixed $getopt |
122
|
|
|
* @param mixed $output |
123
|
|
|
* @param mixed $result |
124
|
|
|
* |
125
|
|
|
* @dataProvider highlightParamsStdIn |
126
|
|
|
*/ |
127
|
|
|
public function testRunHighlightStdIn($input, $getopt, $output, $result) |
128
|
|
|
{ |
129
|
|
|
$cli = $this->getCLIStdIn($input, $getopt); |
130
|
|
|
$this->expectOutputString($output); |
131
|
|
|
$this->assertEquals($result, $cli->runHighlight()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function highlightParamsStdIn() |
135
|
|
|
{ |
136
|
|
|
return [ |
137
|
|
|
[ |
138
|
|
|
'SELECT 1', |
139
|
|
|
[], |
140
|
|
|
"\x1b[35mSELECT\n \x1b[92m1\x1b[0m\n", |
141
|
|
|
0, |
142
|
|
|
], |
143
|
|
|
[ |
144
|
|
|
'SELECT /* comment */ 1 /* other */', |
145
|
|
|
['f' => 'text'], |
146
|
|
|
"SELECT\n /* comment */ 1 /* other */\n", |
147
|
|
|
0, |
148
|
|
|
], |
149
|
|
|
[ |
150
|
|
|
'SELECT 1', |
151
|
|
|
['f' => 'foo'], |
152
|
|
|
"ERROR: Invalid value for format!\n", |
153
|
|
|
1, |
154
|
|
|
], |
155
|
|
|
[ |
156
|
|
|
'SELECT 1', |
157
|
|
|
['f' => 'html'], |
158
|
|
|
'<span class="sql-reserved">SELECT</span><br/>' . |
159
|
|
|
' <span class="sql-number">1</span>' . "\n", |
160
|
|
|
0, |
161
|
|
|
], |
162
|
|
|
[ |
163
|
|
|
'', |
164
|
|
|
['h' => true], |
165
|
|
|
'Usage: highlight-query --query SQL [--format html|cli|text] [--ansi]' . "\n" . |
166
|
|
|
' cat file.sql | highlight-query' . "\n", |
167
|
|
|
0, |
168
|
|
|
], |
169
|
|
|
[ |
170
|
|
|
'', |
171
|
|
|
[], |
172
|
|
|
'ERROR: Missing parameters!' . "\n" . |
173
|
|
|
'Usage: highlight-query --query SQL [--format html|cli|text] [--ansi]' . "\n" . |
174
|
|
|
' cat file.sql | highlight-query' . "\n", |
175
|
|
|
1, |
176
|
|
|
], |
177
|
|
|
[ |
178
|
|
|
'', |
179
|
|
|
false, |
180
|
|
|
'', |
181
|
|
|
1, |
182
|
|
|
], |
183
|
|
|
]; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param mixed $input |
188
|
|
|
* @param mixed $getopt |
189
|
|
|
* @param mixed $output |
190
|
|
|
* @param mixed $result |
191
|
|
|
* |
192
|
|
|
* @dataProvider lintParamsStdIn |
193
|
|
|
*/ |
194
|
|
|
public function testRunLintFromStdIn($input, $getopt, $output, $result) |
195
|
|
|
{ |
196
|
|
|
$cli = $this->getCLIStdIn($input, $getopt); |
197
|
|
|
$this->expectOutputString($output); |
198
|
|
|
$this->assertEquals($result, $cli->runLint()); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function lintParamsStdIn() |
202
|
|
|
{ |
203
|
|
|
return [ |
204
|
|
|
[ |
205
|
|
|
'SELECT 1', |
206
|
|
|
[], |
207
|
|
|
'', |
208
|
|
|
0, |
209
|
|
|
], |
210
|
|
|
[ |
211
|
|
|
'SELECT SELECT', |
212
|
|
|
[], |
213
|
|
|
'#1: An expression was expected. (near "SELECT" at position 7)' . "\n" . |
214
|
|
|
'#2: This type of clause was previously parsed. (near "SELECT" at position 7)' . "\n" . |
215
|
|
|
'#3: An expression was expected. (near "" at position 0)' . "\n", |
216
|
|
|
10, |
217
|
|
|
], |
218
|
|
|
[ |
219
|
|
|
'SELECT SELECT', |
220
|
|
|
['c' => 'MySql80000'], |
221
|
|
|
'#1: An expression was expected. (near "SELECT" at position 7)' . "\n" . |
222
|
|
|
'#2: This type of clause was previously parsed. (near "SELECT" at position 7)' . "\n" . |
223
|
|
|
'#3: An expression was expected. (near "" at position 0)' . "\n", |
224
|
|
|
10, |
225
|
|
|
], |
226
|
|
|
[ |
227
|
|
|
'', |
228
|
|
|
[], |
229
|
|
|
'ERROR: Missing parameters!' . "\n" . |
230
|
|
|
'Usage: lint-query --query SQL [--ansi]' . "\n" . |
231
|
|
|
' cat file.sql | lint-query' . "\n", |
232
|
|
|
1, |
233
|
|
|
], |
234
|
|
|
[ |
235
|
|
|
'', |
236
|
|
|
['h' => true], |
237
|
|
|
'Usage: lint-query --query SQL [--ansi]' . "\n" . |
238
|
|
|
' cat file.sql | lint-query' . "\n", |
239
|
|
|
0, |
240
|
|
|
], |
241
|
|
|
[ |
242
|
|
|
'', |
243
|
|
|
false, |
244
|
|
|
'', |
245
|
|
|
1, |
246
|
|
|
], |
247
|
|
|
]; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param mixed $getopt |
252
|
|
|
* @param mixed $output |
253
|
|
|
* @param mixed $result |
254
|
|
|
* |
255
|
|
|
* @dataProvider lintParams |
256
|
|
|
*/ |
257
|
|
|
public function testRunLint($getopt, $output, $result) |
258
|
|
|
{ |
259
|
|
|
$cli = $this->getCLI($getopt); |
260
|
|
|
$this->expectOutputString($output); |
261
|
|
|
$this->assertEquals($result, $cli->runLint()); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function lintParams() |
265
|
|
|
{ |
266
|
|
|
return [ |
267
|
|
|
[ |
268
|
|
|
['q' => 'SELECT 1'], |
269
|
|
|
'', |
270
|
|
|
0, |
271
|
|
|
], |
272
|
|
|
[ |
273
|
|
|
['query' => 'SELECT 1'], |
274
|
|
|
'', |
275
|
|
|
0, |
276
|
|
|
], |
277
|
|
|
[ |
278
|
|
|
['q' => 'SELECT SELECT'], |
279
|
|
|
'#1: An expression was expected. (near "SELECT" at position 7)' . "\n" . |
280
|
|
|
'#2: This type of clause was previously parsed. (near "SELECT" at position 7)' . "\n" . |
281
|
|
|
'#3: An expression was expected. (near "" at position 0)' . "\n", |
282
|
|
|
10, |
283
|
|
|
], |
284
|
|
|
[ |
285
|
|
|
[ |
286
|
|
|
'q' => 'SELECT SELECT', |
287
|
|
|
'c' => 'MySql80000', |
288
|
|
|
], |
289
|
|
|
'#1: An expression was expected. (near "SELECT" at position 7)' . "\n" . |
290
|
|
|
'#2: This type of clause was previously parsed. (near "SELECT" at position 7)' . "\n" . |
291
|
|
|
'#3: An expression was expected. (near "" at position 0)' . "\n", |
292
|
|
|
10, |
293
|
|
|
], |
294
|
|
|
[ |
295
|
|
|
['h' => true], |
296
|
|
|
'Usage: lint-query --query SQL [--ansi]' . "\n" . |
297
|
|
|
' cat file.sql | lint-query' . "\n", |
298
|
|
|
0, |
299
|
|
|
], |
300
|
|
|
[ |
301
|
|
|
[], |
302
|
|
|
'ERROR: Missing parameters!' . "\n" . |
303
|
|
|
'Usage: lint-query --query SQL [--ansi]' . "\n" . |
304
|
|
|
' cat file.sql | lint-query' . "\n", |
305
|
|
|
1, |
306
|
|
|
], |
307
|
|
|
[ |
308
|
|
|
false, |
309
|
|
|
'', |
310
|
|
|
1, |
311
|
|
|
], |
312
|
|
|
]; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @param mixed $getopt |
317
|
|
|
* @param mixed $output |
318
|
|
|
* @param mixed $result |
319
|
|
|
* |
320
|
|
|
* @dataProvider tokenizeParams |
321
|
|
|
*/ |
322
|
|
|
public function testRunTokenize($getopt, $output, $result) |
323
|
|
|
{ |
324
|
|
|
$cli = $this->getCLI($getopt); |
325
|
|
|
$this->expectOutputString($output); |
326
|
|
|
$this->assertEquals($result, $cli->runTokenize()); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
public function tokenizeParams() |
330
|
|
|
{ |
331
|
|
|
$result = "[TOKEN 0]\nType = 1\nFlags = 3\nValue = 'SELECT'\nToken = 'SELECT'\n\n" |
332
|
|
|
. "[TOKEN 1]\nType = 3\nFlags = 0\nValue = ' '\nToken = ' '\n\n" |
333
|
|
|
. "[TOKEN 2]\nType = 6\nFlags = 0\nValue = 1\nToken = '1'\n\n" |
334
|
|
|
. "[TOKEN 3]\nType = 9\nFlags = 0\nValue = NULL\nToken = NULL\n\n"; |
335
|
|
|
|
336
|
|
|
return [ |
337
|
|
|
[ |
338
|
|
|
['q' => 'SELECT 1'], |
339
|
|
|
$result, |
340
|
|
|
0, |
341
|
|
|
], |
342
|
|
|
[ |
343
|
|
|
['query' => 'SELECT 1'], |
344
|
|
|
$result, |
345
|
|
|
0, |
346
|
|
|
], |
347
|
|
|
[ |
348
|
|
|
['h' => true], |
349
|
|
|
'Usage: tokenize-query --query SQL [--ansi]' . "\n" . |
350
|
|
|
' cat file.sql | tokenize-query' . "\n", |
351
|
|
|
0, |
352
|
|
|
], |
353
|
|
|
[ |
354
|
|
|
[], |
355
|
|
|
'ERROR: Missing parameters!' . "\n" . |
356
|
|
|
'Usage: tokenize-query --query SQL [--ansi]' . "\n" . |
357
|
|
|
' cat file.sql | tokenize-query' . "\n", |
358
|
|
|
1, |
359
|
|
|
], |
360
|
|
|
[ |
361
|
|
|
false, |
362
|
|
|
'', |
363
|
|
|
1, |
364
|
|
|
], |
365
|
|
|
]; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* @param mixed $input |
370
|
|
|
* @param mixed $getopt |
371
|
|
|
* @param mixed $output |
372
|
|
|
* @param mixed $result |
373
|
|
|
* |
374
|
|
|
* @dataProvider tokenizeParamsStdIn |
375
|
|
|
*/ |
376
|
|
|
public function testRunTokenizeStdIn($input, $getopt, $output, $result) |
377
|
|
|
{ |
378
|
|
|
$cli = $this->getCLIStdIn($input, $getopt); |
379
|
|
|
$this->expectOutputString($output); |
380
|
|
|
$this->assertEquals($result, $cli->runTokenize()); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
public function tokenizeParamsStdIn() |
384
|
|
|
{ |
385
|
|
|
$result = "[TOKEN 0]\nType = 1\nFlags = 3\nValue = 'SELECT'\nToken = 'SELECT'\n\n" |
386
|
|
|
. "[TOKEN 1]\nType = 3\nFlags = 0\nValue = ' '\nToken = ' '\n\n" |
387
|
|
|
. "[TOKEN 2]\nType = 6\nFlags = 0\nValue = 1\nToken = '1'\n\n" |
388
|
|
|
. "[TOKEN 3]\nType = 9\nFlags = 0\nValue = NULL\nToken = NULL\n\n"; |
389
|
|
|
|
390
|
|
|
return [ |
391
|
|
|
[ |
392
|
|
|
'SELECT 1', |
393
|
|
|
[], |
394
|
|
|
$result, |
395
|
|
|
0, |
396
|
|
|
], |
397
|
|
|
[ |
398
|
|
|
'', |
399
|
|
|
['h' => true], |
400
|
|
|
'Usage: tokenize-query --query SQL [--ansi]' . "\n" . |
401
|
|
|
' cat file.sql | tokenize-query' . "\n", |
402
|
|
|
0, |
403
|
|
|
], |
404
|
|
|
[ |
405
|
|
|
'', |
406
|
|
|
[], |
407
|
|
|
'ERROR: Missing parameters!' . "\n" . |
408
|
|
|
'Usage: tokenize-query --query SQL [--ansi]' . "\n" . |
409
|
|
|
' cat file.sql | tokenize-query' . "\n", |
410
|
|
|
1, |
411
|
|
|
], |
412
|
|
|
[ |
413
|
|
|
'', |
414
|
|
|
false, |
415
|
|
|
'', |
416
|
|
|
1, |
417
|
|
|
], |
418
|
|
|
]; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* @param string $cmd |
423
|
|
|
* @param int $result |
424
|
|
|
* |
425
|
|
|
* @dataProvider stdinParams |
426
|
|
|
*/ |
427
|
|
|
public function testStdinPipe($cmd, $result) |
428
|
|
|
{ |
429
|
|
|
exec($cmd, $out, $ret); |
430
|
|
|
$this->assertSame($result, $ret); |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
public function stdinParams() |
434
|
|
|
{ |
435
|
|
|
$binPath = PHP_BINARY . ' ' . dirname(__DIR__, 2) . '/bin/'; |
436
|
|
|
|
437
|
|
|
return [ |
438
|
|
|
[ |
439
|
|
|
'echo "SELECT 1" | ' . $binPath . 'highlight-query', |
440
|
|
|
0, |
441
|
|
|
], |
442
|
|
|
[ |
443
|
|
|
'echo "invalid query" | ' . $binPath . 'highlight-query', |
444
|
|
|
0, |
445
|
|
|
], |
446
|
|
|
[ |
447
|
|
|
'echo "SELECT 1" | ' . $binPath . 'lint-query', |
448
|
|
|
0, |
449
|
|
|
], |
450
|
|
|
[ |
451
|
|
|
'echo "invalid query" | ' . $binPath . 'lint-query', |
452
|
|
|
10, |
453
|
|
|
], |
454
|
|
|
[ |
455
|
|
|
'echo "SELECT 1" | ' . $binPath . 'tokenize-query', |
456
|
|
|
0, |
457
|
|
|
], |
458
|
|
|
[ |
459
|
|
|
'echo "invalid query" | ' . $binPath . 'tokenize-query', |
460
|
|
|
0, |
461
|
|
|
], |
462
|
|
|
]; |
463
|
|
|
} |
464
|
|
|
} |
465
|
|
|
|