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