* Support php 5.3 for sql parser lib.
This commit is contained in:
+200
-223
@@ -1,44 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\SqlParser\Tests\Utils;
|
||||
|
||||
use PhpMyAdmin\SqlParser\Tests\TestCase;
|
||||
use PhpMyAdmin\SqlParser\Utils\CLI;
|
||||
|
||||
use function dirname;
|
||||
use function exec;
|
||||
|
||||
use const PHP_BINARY;
|
||||
|
||||
class CLITest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @param mixed $getopt
|
||||
*
|
||||
* @return CLI
|
||||
*/
|
||||
private function getCLI($getopt)
|
||||
{
|
||||
$cli = $this->createPartialMock(CLI::class, ['getopt']);
|
||||
$cli = $this->getMockBuilder('PhpMyAdmin\SqlParser\Utils\CLI')->setMethods(array('getopt'))->getMock();
|
||||
$cli->method('getopt')->willReturn($getopt);
|
||||
|
||||
return $cli;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $input
|
||||
* @param mixed $getopt
|
||||
*
|
||||
* @return CLI
|
||||
*/
|
||||
private function getCLIStdIn($input, $getopt)
|
||||
{
|
||||
$cli = $this->createPartialMock(CLI::class, ['getopt', 'readStdin']);
|
||||
$cli = $this->getMockBuilder('PhpMyAdmin\SqlParser\Utils\CLI')->setMethods(array('getopt', 'readStdin'))->getMock();
|
||||
$cli->method('getopt')->willReturn($getopt);
|
||||
$cli->method('readStdin')->willReturn($input);
|
||||
|
||||
return $cli;
|
||||
}
|
||||
|
||||
@@ -47,428 +27,425 @@ class CLITest extends TestCase
|
||||
*
|
||||
* We do mock it for other tests to return values we want.
|
||||
*/
|
||||
public function testGetopt(): void
|
||||
public function testGetopt()
|
||||
{
|
||||
$cli = new CLI();
|
||||
$cli = new \PhpMyAdmin\SqlParser\Utils\CLI();
|
||||
$this->assertEquals(
|
||||
$cli->getopt('', []),
|
||||
[]
|
||||
$cli->getopt('', array()),
|
||||
array()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider highlightParams
|
||||
*
|
||||
* @param mixed $getopt
|
||||
* @param mixed $output
|
||||
* @param mixed $result
|
||||
*
|
||||
* @dataProvider highlightParamsProvider
|
||||
*/
|
||||
public function testRunHighlight($getopt, $output, $result): void
|
||||
public function testRunHighlight($getopt, $output, $result)
|
||||
{
|
||||
$cli = $this->getCLI($getopt);
|
||||
$this->expectOutputString($output);
|
||||
$this->assertEquals($result, $cli->runHighlight());
|
||||
}
|
||||
|
||||
public function highlightParamsProvider(): array
|
||||
public function highlightParams()
|
||||
{
|
||||
return [
|
||||
[
|
||||
['q' => 'SELECT 1'],
|
||||
return array(
|
||||
array(
|
||||
array('q' => 'SELECT 1'),
|
||||
"\x1b[35mSELECT\n \x1b[92m1\x1b[0m\n",
|
||||
0,
|
||||
],
|
||||
[
|
||||
['query' => 'SELECT 1'],
|
||||
0
|
||||
),
|
||||
array(
|
||||
array('query' => 'SELECT 1'),
|
||||
"\x1b[35mSELECT\n \x1b[92m1\x1b[0m\n",
|
||||
0,
|
||||
],
|
||||
[
|
||||
[
|
||||
0
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'q' => 'SELECT /* comment */ 1 /* other */',
|
||||
'f' => 'text',
|
||||
],
|
||||
),
|
||||
"SELECT\n /* comment */ 1 /* other */\n",
|
||||
0,
|
||||
],
|
||||
[
|
||||
[
|
||||
0
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'q' => 'SELECT 1',
|
||||
'f' => 'foo',
|
||||
],
|
||||
),
|
||||
"ERROR: Invalid value for format!\n",
|
||||
1,
|
||||
],
|
||||
[
|
||||
[
|
||||
1
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'q' => 'SELECT 1',
|
||||
'f' => 'html',
|
||||
],
|
||||
'<span class="sql-reserved">SELECT</span><br/>' .
|
||||
),
|
||||
'<span class="sql-reserved">SELECT</span>' . '<br/>' .
|
||||
' <span class="sql-number">1</span>' . "\n",
|
||||
0,
|
||||
],
|
||||
[
|
||||
['h' => true],
|
||||
0
|
||||
),
|
||||
array(
|
||||
array('h' => true),
|
||||
'Usage: highlight-query --query SQL [--format html|cli|text] [--ansi]' . "\n" .
|
||||
' cat file.sql | highlight-query' . "\n",
|
||||
0,
|
||||
],
|
||||
[
|
||||
[],
|
||||
0
|
||||
),
|
||||
array(
|
||||
array(),
|
||||
'ERROR: Missing parameters!' . "\n" .
|
||||
'Usage: highlight-query --query SQL [--format html|cli|text] [--ansi]' . "\n" .
|
||||
' cat file.sql | highlight-query' . "\n",
|
||||
1,
|
||||
],
|
||||
[
|
||||
),
|
||||
array(
|
||||
false,
|
||||
'',
|
||||
1,
|
||||
],
|
||||
];
|
||||
1
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @dataProvider highlightParamsStdIn
|
||||
*
|
||||
* @param mixed $input
|
||||
* @param mixed $getopt
|
||||
* @param mixed $output
|
||||
* @param mixed $result
|
||||
*
|
||||
* @dataProvider highlightParamsStdInProvider
|
||||
*/
|
||||
public function testRunHighlightStdIn($input, $getopt, $output, $result): void
|
||||
public function testRunHighlightStdIn($input, $getopt, $output, $result)
|
||||
{
|
||||
$cli = $this->getCLIStdIn($input, $getopt);
|
||||
$this->expectOutputString($output);
|
||||
$this->assertEquals($result, $cli->runHighlight());
|
||||
}
|
||||
|
||||
public function highlightParamsStdInProvider(): array
|
||||
public function highlightParamsStdIn()
|
||||
{
|
||||
return [
|
||||
[
|
||||
return array(
|
||||
array(
|
||||
'SELECT 1',
|
||||
[],
|
||||
array(),
|
||||
"\x1b[35mSELECT\n \x1b[92m1\x1b[0m\n",
|
||||
0,
|
||||
],
|
||||
[
|
||||
0
|
||||
),
|
||||
array(
|
||||
'SELECT /* comment */ 1 /* other */',
|
||||
['f' => 'text'],
|
||||
array(
|
||||
'f' => 'text',
|
||||
),
|
||||
"SELECT\n /* comment */ 1 /* other */\n",
|
||||
0,
|
||||
],
|
||||
[
|
||||
0
|
||||
),
|
||||
array(
|
||||
'SELECT 1',
|
||||
['f' => 'foo'],
|
||||
array(
|
||||
'f' => 'foo',
|
||||
),
|
||||
"ERROR: Invalid value for format!\n",
|
||||
1,
|
||||
],
|
||||
[
|
||||
1
|
||||
),
|
||||
array(
|
||||
'SELECT 1',
|
||||
['f' => 'html'],
|
||||
'<span class="sql-reserved">SELECT</span><br/>' .
|
||||
array(
|
||||
'f' => 'html',
|
||||
),
|
||||
'<span class="sql-reserved">SELECT</span>' . '<br/>' .
|
||||
' <span class="sql-number">1</span>' . "\n",
|
||||
0,
|
||||
],
|
||||
[
|
||||
0
|
||||
),
|
||||
array(
|
||||
'',
|
||||
['h' => true],
|
||||
array('h' => true),
|
||||
'Usage: highlight-query --query SQL [--format html|cli|text] [--ansi]' . "\n" .
|
||||
' cat file.sql | highlight-query' . "\n",
|
||||
0,
|
||||
],
|
||||
[
|
||||
0
|
||||
),
|
||||
array(
|
||||
'',
|
||||
[],
|
||||
array(),
|
||||
'ERROR: Missing parameters!' . "\n" .
|
||||
'Usage: highlight-query --query SQL [--format html|cli|text] [--ansi]' . "\n" .
|
||||
' cat file.sql | highlight-query' . "\n",
|
||||
1,
|
||||
],
|
||||
[
|
||||
),
|
||||
array(
|
||||
'',
|
||||
false,
|
||||
'',
|
||||
1,
|
||||
],
|
||||
];
|
||||
1
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider lintParamsStdIn
|
||||
*
|
||||
* @param mixed $input
|
||||
* @param mixed $getopt
|
||||
* @param mixed $output
|
||||
* @param mixed $result
|
||||
*
|
||||
* @dataProvider lintParamsStdInProvider
|
||||
*/
|
||||
public function testRunLintFromStdIn($input, $getopt, $output, $result): void
|
||||
public function testRunLintFromStdIn($input, $getopt, $output, $result)
|
||||
{
|
||||
$cli = $this->getCLIStdIn($input, $getopt);
|
||||
$this->expectOutputString($output);
|
||||
$this->assertEquals($result, $cli->runLint());
|
||||
}
|
||||
|
||||
public function lintParamsStdInProvider(): array
|
||||
public function lintParamsStdIn()
|
||||
{
|
||||
return [
|
||||
[
|
||||
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',
|
||||
['c' => 'MySql80000'],
|
||||
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 [--ansi]' . "\n" .
|
||||
' cat file.sql | lint-query' . "\n",
|
||||
1,
|
||||
],
|
||||
[
|
||||
),
|
||||
array(
|
||||
'',
|
||||
['h' => true],
|
||||
array('h' => true),
|
||||
'Usage: lint-query --query SQL [--ansi]' . "\n" .
|
||||
' cat file.sql | lint-query' . "\n",
|
||||
0,
|
||||
],
|
||||
[
|
||||
),
|
||||
array(
|
||||
'',
|
||||
false,
|
||||
'',
|
||||
1,
|
||||
],
|
||||
];
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider lintParams
|
||||
*
|
||||
* @param mixed $getopt
|
||||
* @param mixed $output
|
||||
* @param mixed $result
|
||||
*
|
||||
* @dataProvider lintParamsProvider
|
||||
*/
|
||||
public function testRunLint($getopt, $output, $result): void
|
||||
public function testRunLint($getopt, $output, $result)
|
||||
{
|
||||
$cli = $this->getCLI($getopt);
|
||||
$this->expectOutputString($output);
|
||||
$this->assertEquals($result, $cli->runLint());
|
||||
}
|
||||
|
||||
public function lintParamsProvider(): array
|
||||
public function lintParams()
|
||||
{
|
||||
return [
|
||||
[
|
||||
['q' => 'SELECT 1'],
|
||||
return array(
|
||||
array(
|
||||
array('q' => 'SELECT 1'),
|
||||
'',
|
||||
0,
|
||||
],
|
||||
[
|
||||
['query' => 'SELECT 1'],
|
||||
),
|
||||
array(
|
||||
array('query' => 'SELECT 1'),
|
||||
'',
|
||||
0,
|
||||
],
|
||||
[
|
||||
['q' => 'SELECT SELECT'],
|
||||
),
|
||||
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,
|
||||
],
|
||||
[
|
||||
[
|
||||
'q' => 'SELECT SELECT',
|
||||
'c' => 'MySql80000',
|
||||
],
|
||||
),
|
||||
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,
|
||||
],
|
||||
[
|
||||
['h' => true],
|
||||
),
|
||||
array(
|
||||
array('h' => true),
|
||||
'Usage: lint-query --query SQL [--ansi]' . "\n" .
|
||||
' cat file.sql | lint-query' . "\n",
|
||||
0,
|
||||
],
|
||||
[
|
||||
[],
|
||||
),
|
||||
array(
|
||||
array(),
|
||||
'ERROR: Missing parameters!' . "\n" .
|
||||
'Usage: lint-query --query SQL [--ansi]' . "\n" .
|
||||
' cat file.sql | lint-query' . "\n",
|
||||
1,
|
||||
],
|
||||
[
|
||||
),
|
||||
array(
|
||||
false,
|
||||
'',
|
||||
1,
|
||||
],
|
||||
];
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider tokenizeParams
|
||||
*
|
||||
* @param mixed $getopt
|
||||
* @param mixed $output
|
||||
* @param mixed $result
|
||||
*
|
||||
* @dataProvider tokenizeParamsProvider
|
||||
*/
|
||||
public function testRunTokenize($getopt, $output, $result): void
|
||||
public function testRunTokenize($getopt, $output, $result)
|
||||
{
|
||||
$cli = $this->getCLI($getopt);
|
||||
$this->expectOutputString($output);
|
||||
$this->assertEquals($result, $cli->runTokenize());
|
||||
}
|
||||
|
||||
public function tokenizeParamsProvider(): array
|
||||
public function tokenizeParams()
|
||||
{
|
||||
$result = "[TOKEN 0]\nType = 1\nFlags = 3\nValue = 'SELECT'\nToken = 'SELECT'\n\n"
|
||||
$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";
|
||||
. "[TOKEN 3]\nType = 9\nFlags = 0\nValue = NULL\nToken = NULL\n\n"
|
||||
);
|
||||
|
||||
return [
|
||||
[
|
||||
['q' => 'SELECT 1'],
|
||||
return array(
|
||||
array(
|
||||
array('q' => 'SELECT 1'),
|
||||
$result,
|
||||
0,
|
||||
],
|
||||
[
|
||||
['query' => 'SELECT 1'],
|
||||
),
|
||||
array(
|
||||
array('query' => 'SELECT 1'),
|
||||
$result,
|
||||
0,
|
||||
],
|
||||
[
|
||||
['h' => true],
|
||||
),
|
||||
array(
|
||||
array('h' => true),
|
||||
'Usage: tokenize-query --query SQL [--ansi]' . "\n" .
|
||||
' cat file.sql | tokenize-query' . "\n",
|
||||
0,
|
||||
],
|
||||
[
|
||||
[],
|
||||
),
|
||||
array(
|
||||
array(),
|
||||
'ERROR: Missing parameters!' . "\n" .
|
||||
'Usage: tokenize-query --query SQL [--ansi]' . "\n" .
|
||||
' cat file.sql | tokenize-query' . "\n",
|
||||
1,
|
||||
],
|
||||
[
|
||||
),
|
||||
array(
|
||||
false,
|
||||
'',
|
||||
1,
|
||||
],
|
||||
];
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider tokenizeParamsStdIn
|
||||
*
|
||||
* @param mixed $input
|
||||
* @param mixed $getopt
|
||||
* @param mixed $output
|
||||
* @param mixed $result
|
||||
*
|
||||
* @dataProvider tokenizeParamsStdInProvider
|
||||
*/
|
||||
public function testRunTokenizeStdIn($input, $getopt, $output, $result): void
|
||||
public function testRunTokenizeStdIn($input, $getopt, $output, $result)
|
||||
{
|
||||
$cli = $this->getCLIStdIn($input, $getopt);
|
||||
$this->expectOutputString($output);
|
||||
$this->assertEquals($result, $cli->runTokenize());
|
||||
}
|
||||
|
||||
public function tokenizeParamsStdInProvider(): array
|
||||
public function tokenizeParamsStdIn()
|
||||
{
|
||||
$result = "[TOKEN 0]\nType = 1\nFlags = 3\nValue = 'SELECT'\nToken = 'SELECT'\n\n"
|
||||
$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";
|
||||
. "[TOKEN 3]\nType = 9\nFlags = 0\nValue = NULL\nToken = NULL\n\n"
|
||||
);
|
||||
|
||||
return [
|
||||
[
|
||||
return array(
|
||||
array(
|
||||
'SELECT 1',
|
||||
[],
|
||||
array(),
|
||||
$result,
|
||||
0,
|
||||
],
|
||||
[
|
||||
),
|
||||
array(
|
||||
'',
|
||||
['h' => true],
|
||||
array('h' => true),
|
||||
'Usage: tokenize-query --query SQL [--ansi]' . "\n" .
|
||||
' cat file.sql | tokenize-query' . "\n",
|
||||
0,
|
||||
],
|
||||
[
|
||||
),
|
||||
array(
|
||||
'',
|
||||
[],
|
||||
array(),
|
||||
'ERROR: Missing parameters!' . "\n" .
|
||||
'Usage: tokenize-query --query SQL [--ansi]' . "\n" .
|
||||
' cat file.sql | tokenize-query' . "\n",
|
||||
1,
|
||||
],
|
||||
[
|
||||
),
|
||||
array(
|
||||
'',
|
||||
false,
|
||||
'',
|
||||
1,
|
||||
],
|
||||
];
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stdinParamsProvider
|
||||
* @dataProvider stdinParams
|
||||
*
|
||||
* @param string $cmd
|
||||
* @param int $result
|
||||
*/
|
||||
public function testStdinPipe(string $cmd, int $result): void
|
||||
public function testStdinPipe($cmd, $result)
|
||||
{
|
||||
exec($cmd, $out, $ret);
|
||||
exec ($cmd, $out, $ret);
|
||||
$this->assertSame($result, $ret);
|
||||
}
|
||||
|
||||
public function stdinParamsProvider(): array
|
||||
public function stdinParams()
|
||||
{
|
||||
$binPath = PHP_BINARY . ' ' . dirname(__DIR__, 2) . '/bin/';
|
||||
if (defined('PHP_BINARY')) {
|
||||
$binPath = PHP_BINARY . ' ' . realpath(dirname(__DIR__) . '/../') . '/bin/';
|
||||
} else {
|
||||
$binPath = 'php' . ' ' . realpath(dirname(__DIR__) . '/../') . '/bin/';
|
||||
}
|
||||
|
||||
return [
|
||||
[
|
||||
'echo "SELECT 1" | ' . $binPath . 'highlight-query',
|
||||
0,
|
||||
],
|
||||
[
|
||||
'echo "invalid query" | ' . $binPath . 'highlight-query',
|
||||
0,
|
||||
],
|
||||
[
|
||||
'echo "SELECT 1" | ' . $binPath . 'lint-query',
|
||||
0,
|
||||
],
|
||||
[
|
||||
'echo "invalid query" | ' . $binPath . 'lint-query',
|
||||
10,
|
||||
],
|
||||
[
|
||||
'echo "SELECT 1" | ' . $binPath . 'tokenize-query',
|
||||
0,
|
||||
],
|
||||
[
|
||||
'echo "invalid query" | ' . $binPath . 'tokenize-query',
|
||||
0,
|
||||
],
|
||||
];
|
||||
return array(
|
||||
array('echo "SELECT 1" | '. $binPath .'highlight-query', 0),
|
||||
array('echo "invalid query" | '. $binPath .'highlight-query', 0),
|
||||
array('echo "SELECT 1" | '. $binPath .'lint-query', 0),
|
||||
array('echo "invalid query" | '. $binPath .'lint-query', 10),
|
||||
array('echo "SELECT 1" | '. $binPath .'tokenize-query', 0),
|
||||
array('echo "invalid query" | '. $binPath .'tokenize-query', 0)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user