Skip to content

Commit 8a9b8fc

Browse files
committed
Add more tests for CLI stdin
Signed-off-by: William Desportes <[email protected]>
1 parent 8548a7f commit 8a9b8fc

File tree

3 files changed

+213
-1
lines changed

3 files changed

+213
-1
lines changed

src/Utils/CLI.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public function runTokenize()
218218
return 1;
219219
}
220220

221-
private function readStdin() {
221+
public function readStdin() {
222222
stream_set_blocking(STDIN, false);
223223
$stdin = stream_get_contents(STDIN);
224224
// restore-default block-mode setting

tests/Utils/CLITest.php

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ private function getCLI($getopt)
1414
return $cli;
1515
}
1616

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+
1725
/**
1826
* Test that getopt call works.
1927
*
@@ -101,6 +109,144 @@ public function highlightParams()
101109
);
102110
}
103111

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]' . "\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+
104250
/**
105251
* @dataProvider lintParams
106252
*
@@ -135,6 +281,13 @@ public function lintParams()
135281
'#3: An expression was expected. (near "" at position 0)' . "\n",
136282
10,
137283
),
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+
),
138291
array(
139292
array('h' => true),
140293
'Usage: lint-query --query SQL' . "\n" .
@@ -211,6 +364,61 @@ public function tokenizeParams()
211364
);
212365
}
213366

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+
214422
/**
215423
* @dataProvider stdinParams
216424
*

tests/Utils/ErrorTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,9 @@ public function testFormat()
3838
array('#1: error msg (near "token" at position 100)'),
3939
Error::format(array(array('error msg', 42, 'token', 100)))
4040
);
41+
$this->assertEquals(
42+
array('#1: error msg (near "token" at position 100)', '#2: error msg (near "token" at position 200)'),
43+
Error::format(array(array('error msg', 42, 'token', 100), array('error msg', 42, 'token', 200)))
44+
);
4145
}
4246
}

0 commit comments

Comments
 (0)