Skip to content

Commit 82e6f86

Browse files
committed
Add tests for CLI interface
Signed-off-by: Michal Čihař <michal@cihar.com>
1 parent a684b1d commit 82e6f86

File tree

2 files changed

+99
-2
lines changed

2 files changed

+99
-2
lines changed

src/Utils/CLI.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,15 @@ public function usageHighlight()
3737
echo "Usage: highlight-query --query SQL [--format html|cli|text]\n";
3838
}
3939

40+
public function getopt($opt, $long)
41+
{
42+
return getopt($opt, $long);
43+
}
44+
4045
public function parseHighlight()
4146
{
4247
$longopts = array('help', 'query:', 'format:');
43-
$params = getopt(
48+
$params = $this->getopt(
4449
'hq:f:', $longopts
4550
);
4651
$this->mergeLongOpts($params, $longopts);
@@ -84,7 +89,7 @@ public function usageLint()
8489
public function parseLint()
8590
{
8691
$longopts = array('help', 'query:');
87-
$params = getopt(
92+
$params = $this->getopt(
8893
'hq:', $longopts
8994
);
9095
$this->mergeLongOpts($params, $longopts);

tests/Utils/CLITest.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace SqlParser\Tests\Utils;
4+
5+
use SqlParser\Tests\TestCase;
6+
7+
class CLITest extends TestCase
8+
{
9+
private function getCLI($getopt)
10+
{
11+
$cli = $this->getMockBuilder('SqlParser\Utils\CLI')->setMethods(array('getopt'))->getMock();
12+
$cli->method('getopt')->willReturn($getopt);
13+
return $cli;
14+
}
15+
16+
/**
17+
* @dataProvider highlightParams
18+
*/
19+
public function testRunHighlight($getopt, $output, $result)
20+
{
21+
$cli = $this->getCLI($getopt);
22+
$this->expectOutputString($output);
23+
$this->assertEquals($result, $cli->runHighlight());
24+
}
25+
26+
public function highlightParams()
27+
{
28+
return array(
29+
array(
30+
array('q' => 'SELECT 1'),
31+
"\x1b[35mSELECT\n \x1b[92m1\x1b[0m\n",
32+
0,
33+
),
34+
array(
35+
array('q' => 'SELECT 1', 'f' => 'html'),
36+
'<span class="sql-reserved">SELECT</span>' . "\n" .
37+
' <span class="sql-number">1</span>' . "\n",
38+
0,
39+
),
40+
array(
41+
array('h' => true),
42+
'Usage: highlight-query --query SQL [--format html|cli|text]' . "\n",
43+
0,
44+
),
45+
array(
46+
array(),
47+
'ERROR: Missing parameters!' . "\n" .
48+
'Usage: highlight-query --query SQL [--format html|cli|text]' . "\n",
49+
1,
50+
),
51+
);
52+
}
53+
54+
/**
55+
* @dataProvider lintParams
56+
*/
57+
public function testRunLint($getopt, $output, $result)
58+
{
59+
$cli = $this->getCLI($getopt);
60+
$this->expectOutputString($output);
61+
$this->assertEquals($result, $cli->runLint());
62+
}
63+
64+
public function lintParams()
65+
{
66+
return array(
67+
array(
68+
array('q' => 'SELECT 1'),
69+
'',
70+
0,
71+
),
72+
array(
73+
array('q' => 'SELECT SELECT'),
74+
'#1: An expression was expected. (near "SELECT" at position 7)' . "\n" .
75+
'#2: This type of clause was previously parsed. (near "SELECT" at position 7)' . "\n" .
76+
'#3: An expression was expected. (near "" at position 0)' . "\n",
77+
10,
78+
),
79+
array(
80+
array('h' => true),
81+
'Usage: lint-query --query SQL' . "\n",
82+
0,
83+
),
84+
array(
85+
array(),
86+
'ERROR: Missing parameters!' . "\n" .
87+
'Usage: lint-query --query SQL' . "\n",
88+
1,
89+
),
90+
);
91+
}
92+
}

0 commit comments

Comments
 (0)