diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Utils/CLITest.php | 208 | ||||
-rw-r--r-- | tests/Utils/ErrorTest.php | 4 |
2 files changed, 212 insertions, 0 deletions
diff --git a/tests/Utils/CLITest.php b/tests/Utils/CLITest.php index 3722c5f..4caa70d 100644 --- a/tests/Utils/CLITest.php +++ b/tests/Utils/CLITest.php @@ -14,6 +14,14 @@ class CLITest extends TestCase return $cli; } + private function getCLIStdIn($input, $getopt) + { + $cli = $this->getMockBuilder('PhpMyAdmin\SqlParser\Utils\CLI')->setMethods(array('getopt', 'readStdin'))->getMock(); + $cli->method('getopt')->willReturn($getopt); + $cli->method('readStdin')->willReturn($input); + return $cli; + } + /** * Test that getopt call works. * @@ -101,6 +109,144 @@ class CLITest extends TestCase ); } + + /** + * @dataProvider highlightParamsStdIn + * + * @param mixed $input + * @param mixed $getopt + * @param mixed $output + * @param mixed $result + */ + public function testRunHighlightStdIn($input, $getopt, $output, $result) + { + $cli = $this->getCLIStdIn($input, $getopt); + $this->expectOutputString($output); + $this->assertEquals($result, $cli->runHighlight()); + } + + public function highlightParamsStdIn() + { + return array( + array( + 'SELECT 1', + array(), + "\x1b[35mSELECT\n \x1b[92m1\x1b[0m\n", + 0 + ), + array( + 'SELECT /* comment */ 1 /* other */', + array( + 'f' => 'text', + ), + "SELECT\n /* comment */ 1 /* other */\n", + 0 + ), + array( + 'SELECT 1', + array( + 'f' => 'foo', + ), + "ERROR: Invalid value for format!\n", + 1 + ), + array( + 'SELECT 1', + array( + 'f' => 'html', + ), + '<span class="sql-reserved">SELECT</span>' . '<br/>' . + ' <span class="sql-number">1</span>' . "\n", + 0 + ), + array( + '', + array('h' => true), + 'Usage: highlight-query --query SQL [--format html|cli|text]' . "\n" . + ' cat file.sql | highlight-query' . "\n", + 0 + ), + array( + '', + array(), + 'ERROR: Missing parameters!' . "\n" . + 'Usage: highlight-query --query SQL [--format html|cli|text]' . "\n" . + ' cat file.sql | highlight-query' . "\n", + 1, + ), + array( + '', + false, + '', + 1 + ) + ); + } + + /** + * @dataProvider lintParamsStdIn + * + * @param mixed $input + * @param mixed $getopt + * @param mixed $output + * @param mixed $result + */ + public function testRunLintFromStdIn($input, $getopt, $output, $result) + { + $cli = $this->getCLIStdIn($input, $getopt); + $this->expectOutputString($output); + $this->assertEquals($result, $cli->runLint()); + } + + public function lintParamsStdIn() + { + return array( + array( + 'SELECT 1', + array(), + '', + 0, + ), + array( + 'SELECT SELECT', + array(), + '#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( + 'SELECT SELECT', + array('c' => 'MySql80000'), + '#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(), + 'ERROR: Missing parameters!' . "\n" . + 'Usage: lint-query --query SQL' . "\n" . + ' cat file.sql | lint-query' . "\n", + 1, + ), + array( + '', + array('h' => true), + 'Usage: lint-query --query SQL' . "\n" . + ' cat file.sql | lint-query' . "\n", + 0, + ), + array( + '', + false, + '', + 1, + ) + ); + } + /** * @dataProvider lintParams * @@ -136,6 +282,13 @@ class CLITest extends TestCase 10, ), array( + array('q' => 'SELECT SELECT', 'c' => 'MySql80000'), + '#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" . ' cat file.sql | lint-query' . "\n", @@ -212,6 +365,61 @@ class CLITest extends TestCase } /** + * @dataProvider tokenizeParamsStdIn + * + * @param mixed $input + * @param mixed $getopt + * @param mixed $output + * @param mixed $result + */ + public function testRunTokenizeStdIn($input, $getopt, $output, $result) + { + $cli = $this->getCLIStdIn($input, $getopt); + $this->expectOutputString($output); + $this->assertEquals($result, $cli->runTokenize()); + } + + public function tokenizeParamsStdIn() + { + $result = ( + "[TOKEN 0]\nType = 1\nFlags = 3\nValue = 'SELECT'\nToken = 'SELECT'\n\n" + . "[TOKEN 1]\nType = 3\nFlags = 0\nValue = ' '\nToken = ' '\n\n" + . "[TOKEN 2]\nType = 6\nFlags = 0\nValue = 1\nToken = '1'\n\n" + . "[TOKEN 3]\nType = 9\nFlags = 0\nValue = NULL\nToken = NULL\n\n" + ); + + return array( + array( + 'SELECT 1', + array(), + $result, + 0, + ), + array( + '', + array('h' => true), + 'Usage: tokenize-query --query SQL' . "\n" . + ' cat file.sql | tokenize-query' . "\n", + 0, + ), + array( + '', + array(), + 'ERROR: Missing parameters!' . "\n" . + 'Usage: tokenize-query --query SQL' . "\n" . + ' cat file.sql | tokenize-query' . "\n", + 1, + ), + array( + '', + false, + '', + 1, + ) + ); + } + + /** * @dataProvider stdinParams * * @param string $cmd diff --git a/tests/Utils/ErrorTest.php b/tests/Utils/ErrorTest.php index 2d3da43..be8e17d 100644 --- a/tests/Utils/ErrorTest.php +++ b/tests/Utils/ErrorTest.php @@ -38,5 +38,9 @@ class ErrorTest extends TestCase array('#1: error msg (near "token" at position 100)'), Error::format(array(array('error msg', 42, 'token', 100))) ); + $this->assertEquals( + array('#1: error msg (near "token" at position 100)', '#2: error msg (near "token" at position 200)'), + Error::format(array(array('error msg', 42, 'token', 100), array('error msg', 42, 'token', 200))) + ); } } |