Passed
Pull Request — master (#340)
by
unknown
12:20
created

CLITest::testRunHighlight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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