summaryrefslogtreecommitdiffstats
path: root/tests/Utils
diff options
context:
space:
mode:
authorWilliam Desportes <williamdes@wdes.fr>2019-12-31 16:22:51 +0100
committerWilliam Desportes <williamdes@wdes.fr>2019-12-31 16:33:33 +0100
commitde9009d256336851fa7ef8c7166aeedf26be988b (patch)
treeafc85bdb92ea962ac4689c55d5f313f87af0ca0b /tests/Utils
parent1f45fbcc794c7a8e10b78b6d024c1dc48a42eaf9 (diff)
parent8a9b8fcd3357fbf42756519bcea7ef3ca668aaf5 (diff)
downloadsql-parser-de9009d256336851fa7ef8c7166aeedf26be988b.zip
sql-parser-de9009d256336851fa7ef8c7166aeedf26be988b.tar.gz
sql-parser-de9009d256336851fa7ef8c7166aeedf26be988b.tar.bz2
Merge branch 'QA'
Signed-off-by: William Desportes <williamdes@wdes.fr>
Diffstat (limited to 'tests/Utils')
-rw-r--r--tests/Utils/CLITest.php211
-rw-r--r--tests/Utils/ErrorTest.php7
2 files changed, 218 insertions, 0 deletions
diff --git a/tests/Utils/CLITest.php b/tests/Utils/CLITest.php
index 7e53aca..2f7102f 100644
--- a/tests/Utils/CLITest.php
+++ b/tests/Utils/CLITest.php
@@ -16,6 +16,14 @@ class CLITest extends TestCase
return $cli;
}
+ private function getCLIStdIn($input, $getopt)
+ {
+ $cli = $this->getMockBuilder('PhpMyAdmin\SqlParser\Utils\CLI')->setMethods(['getopt', 'readStdin'])->getMock();
+ $cli->method('getopt')->willReturn($getopt);
+ $cli->method('readStdin')->willReturn($input);
+ return $cli;
+ }
+
/**
* Test that getopt call works.
*
@@ -103,6 +111,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 [
+ [
+ 'SELECT 1',
+ [],
+ "\x1b[35mSELECT\n \x1b[92m1\x1b[0m\n",
+ 0,
+ ],
+ [
+ 'SELECT /* comment */ 1 /* other */',
+ [
+ 'f' => 'text',
+ ],
+ "SELECT\n /* comment */ 1 /* other */\n",
+ 0,
+ ],
+ [
+ 'SELECT 1',
+ [
+ 'f' => 'foo',
+ ],
+ "ERROR: Invalid value for format!\n",
+ 1,
+ ],
+ [
+ 'SELECT 1',
+ [
+ 'f' => 'html',
+ ],
+ '<span class="sql-reserved">SELECT</span>' . '<br/>' .
+ '&nbsp;&nbsp;&nbsp;&nbsp;<span class="sql-number">1</span>' . "\n",
+ 0,
+ ],
+ [
+ '',
+ ['h' => true],
+ 'Usage: highlight-query --query SQL [--format html|cli|text]' . "\n" .
+ ' cat file.sql | highlight-query' . "\n",
+ 0,
+ ],
+ [
+ '',
+ [],
+ 'ERROR: Missing parameters!' . "\n" .
+ 'Usage: highlight-query --query SQL [--format html|cli|text]' . "\n" .
+ ' cat file.sql | highlight-query' . "\n",
+ 1,
+ ],
+ [
+ '',
+ false,
+ '',
+ 1,
+ ],
+ ];
+ }
+
+ /**
+ * @param mixed $input
+ * @param mixed $getopt
+ * @param mixed $output
+ * @param mixed $result
+ *
+ * @dataProvider lintParamsStdIn
+ */
+ public function testRunLintFromStdIn($input, $getopt, $output, $result)
+ {
+ $cli = $this->getCLIStdIn($input, $getopt);
+ $this->expectOutputString($output);
+ $this->assertEquals($result, $cli->runLint());
+ }
+
+ public function lintParamsStdIn()
+ {
+ return [
+ [
+ 'SELECT 1',
+ [],
+ '',
+ 0,
+ ],
+ [
+ '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,
+ ],
+ [
+ '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,
+ ],
+ [
+ '',
+ [],
+ 'ERROR: Missing parameters!' . "\n" .
+ 'Usage: lint-query --query SQL' . "\n" .
+ ' cat file.sql | lint-query' . "\n",
+ 1,
+ ],
+ [
+ '',
+ ['h' => true],
+ 'Usage: lint-query --query SQL' . "\n" .
+ ' cat file.sql | lint-query' . "\n",
+ 0,
+ ],
+ [
+ '',
+ false,
+ '',
+ 1,
+ ],
+ ];
+ }
+
/**
* @param mixed $getopt
* @param mixed $output
@@ -138,6 +284,16 @@ class CLITest extends TestCase
10,
],
[
+ [
+ '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,
+ ],
+ [
['h' => true],
'Usage: lint-query --query SQL' . "\n" .
' cat file.sql | lint-query' . "\n",
@@ -212,6 +368,61 @@ class CLITest extends TestCase
}
/**
+ * @param mixed $input
+ * @param mixed $getopt
+ * @param mixed $output
+ * @param mixed $result
+ *
+ * @dataProvider tokenizeParamsStdIn
+ */
+ 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 [
+ [
+ 'SELECT 1',
+ [],
+ $result,
+ 0,
+ ],
+ [
+ '',
+ ['h' => true],
+ 'Usage: tokenize-query --query SQL' . "\n" .
+ ' cat file.sql | tokenize-query' . "\n",
+ 0,
+ ],
+ [
+ '',
+ [],
+ 'ERROR: Missing parameters!' . "\n" .
+ 'Usage: tokenize-query --query SQL' . "\n" .
+ ' cat file.sql | tokenize-query' . "\n",
+ 1,
+ ],
+ [
+ '',
+ false,
+ '',
+ 1,
+ ],
+ ];
+ }
+
+ /**
* @param string $cmd
* @param int $result
*
diff --git a/tests/Utils/ErrorTest.php b/tests/Utils/ErrorTest.php
index 7e1326f..eec1130 100644
--- a/tests/Utils/ErrorTest.php
+++ b/tests/Utils/ErrorTest.php
@@ -39,5 +39,12 @@ class ErrorTest extends TestCase
['#1: error msg (near "token" at position 100)'],
Error::format([['error msg', 42, 'token', 100]])
);
+ $this->assertEquals(
+ [
+ '#1: error msg (near "token" at position 100)',
+ '#2: error msg (near "token" at position 200)',
+ ],
+ Error::format([['error msg', 42, 'token', 100], ['error msg', 42, 'token', 200]])
+ );
}
}