@@ -14,6 +14,14 @@ private function getCLI($getopt)
14
14
return $ cli ;
15
15
}
16
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
+
17
25
/**
18
26
* Test that getopt call works.
19
27
*
@@ -101,6 +109,144 @@ public function highlightParams()
101
109
);
102
110
}
103
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
+ ' <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] ' . "\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] ' . "\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 ' . "\n" .
231
+ ' cat file.sql | lint-query ' . "\n" ,
232
+ 1 ,
233
+ ),
234
+ array (
235
+ '' ,
236
+ array ('h ' => true ),
237
+ 'Usage: lint-query --query SQL ' . "\n" .
238
+ ' cat file.sql | lint-query ' . "\n" ,
239
+ 0 ,
240
+ ),
241
+ array (
242
+ '' ,
243
+ false ,
244
+ '' ,
245
+ 1 ,
246
+ )
247
+ );
248
+ }
249
+
104
250
/**
105
251
* @dataProvider lintParams
106
252
*
@@ -135,6 +281,13 @@ public function lintParams()
135
281
'#3: An expression was expected. (near "" at position 0) ' . "\n" ,
136
282
10 ,
137
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
+ ),
138
291
array (
139
292
array ('h ' => true ),
140
293
'Usage: lint-query --query SQL ' . "\n" .
@@ -211,6 +364,61 @@ public function tokenizeParams()
211
364
);
212
365
}
213
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 ' . "\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 ' . "\n" .
410
+ ' cat file.sql | tokenize-query ' . "\n" ,
411
+ 1 ,
412
+ ),
413
+ array (
414
+ '' ,
415
+ false ,
416
+ '' ,
417
+ 1 ,
418
+ )
419
+ );
420
+ }
421
+
214
422
/**
215
423
* @dataProvider stdinParams
216
424
*
0 commit comments