diff options
-rw-r--r-- | src/Utils/CLI.php | 9 | ||||
-rw-r--r-- | tests/Utils/CLITest.php | 92 |
2 files changed, 99 insertions, 2 deletions
diff --git a/src/Utils/CLI.php b/src/Utils/CLI.php index 306de04..0a34dea 100644 --- a/src/Utils/CLI.php +++ b/src/Utils/CLI.php @@ -37,10 +37,15 @@ class CLI echo "Usage: highlight-query --query SQL [--format html|cli|text]\n"; } + public function getopt($opt, $long) + { + return getopt($opt, $long); + } + public function parseHighlight() { $longopts = array('help', 'query:', 'format:'); - $params = getopt( + $params = $this->getopt( 'hq:f:', $longopts ); $this->mergeLongOpts($params, $longopts); @@ -84,7 +89,7 @@ class CLI public function parseLint() { $longopts = array('help', 'query:'); - $params = getopt( + $params = $this->getopt( 'hq:', $longopts ); $this->mergeLongOpts($params, $longopts); diff --git a/tests/Utils/CLITest.php b/tests/Utils/CLITest.php new file mode 100644 index 0000000..96c3f67 --- /dev/null +++ b/tests/Utils/CLITest.php @@ -0,0 +1,92 @@ +<?php + +namespace SqlParser\Tests\Utils; + +use SqlParser\Tests\TestCase; + +class CLITest extends TestCase +{ + private function getCLI($getopt) + { + $cli = $this->getMockBuilder('SqlParser\Utils\CLI')->setMethods(array('getopt'))->getMock(); + $cli->method('getopt')->willReturn($getopt); + return $cli; + } + + /** + * @dataProvider highlightParams + */ + public function testRunHighlight($getopt, $output, $result) + { + $cli = $this->getCLI($getopt); + $this->expectOutputString($output); + $this->assertEquals($result, $cli->runHighlight()); + } + + public function highlightParams() + { + return array( + array( + array('q' => 'SELECT 1'), + "\x1b[35mSELECT\n \x1b[92m1\x1b[0m\n", + 0, + ), + array( + array('q' => 'SELECT 1', 'f' => 'html'), + '<span class="sql-reserved">SELECT</span>' . "\n" . + ' <span class="sql-number">1</span>' . "\n", + 0, + ), + array( + array('h' => true), + 'Usage: highlight-query --query SQL [--format html|cli|text]' . "\n", + 0, + ), + array( + array(), + 'ERROR: Missing parameters!' . "\n" . + 'Usage: highlight-query --query SQL [--format html|cli|text]' . "\n", + 1, + ), + ); + } + + /** + * @dataProvider lintParams + */ + public function testRunLint($getopt, $output, $result) + { + $cli = $this->getCLI($getopt); + $this->expectOutputString($output); + $this->assertEquals($result, $cli->runLint()); + } + + public function lintParams() + { + return array( + array( + array('q' => 'SELECT 1'), + '', + 0, + ), + array( + array('q' => 'SELECT SELECT'), + '#1: An expression was expected. (near "SELECT" at position 7)' . "\n" . + '#2: This type of clause was previously parsed. (near "SELECT" at position 7)' . "\n" . + '#3: An expression was expected. (near "" at position 0)' . "\n", + 10, + ), + array( + array('h' => true), + 'Usage: lint-query --query SQL' . "\n", + 0, + ), + array( + array(), + 'ERROR: Missing parameters!' . "\n" . + 'Usage: lint-query --query SQL' . "\n", + 1, + ), + ); + } +} |