From 1dcb8faad06fc48e5aaa30420ca331dc22efeb87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Wed, 5 Sep 2012 16:29:41 +0200 Subject: [PATCH] Use provider rather than defining own methods with test --- test/libraries/PMA_SQL_parser_test.php | 409 ++++++++++++------------- 1 file changed, 203 insertions(+), 206 deletions(-) diff --git a/test/libraries/PMA_SQL_parser_test.php b/test/libraries/PMA_SQL_parser_test.php index f0409aee42..65418d9f75 100644 --- a/test/libraries/PMA_SQL_parser_test.php +++ b/test/libraries/PMA_SQL_parser_test.php @@ -13,7 +13,18 @@ require_once 'libraries/sqlparser.lib.php'; class PMA_SQL_parser_test extends PHPUnit_Framework_TestCase { - private function assertParser($sql, $expected, $error = '') + /** + * Testing of SQL parser. + * + * @param string $sql SQL query to parse + * @param array $expected Expected parse result + * @param string $error Expected error message + * + * @return void + * + * @dataProvider parserData + */ + public function testParser($sql, $expected, $error = '') { PMA_SQP_resetError(); $parsed_sql = PMA_SQP_parse($sql); @@ -21,217 +32,203 @@ class PMA_SQL_parser_test extends PHPUnit_Framework_TestCase $this->assertEquals($expected, $parsed_sql); } - public function testParse_1() - { - $this->assertParser( - 'SELECT 1;', - array( - 'raw' => 'SELECT 1;', - 0 => array( - 'type' => 'alpha_reservedWord', - 'data' => 'SELECT', - 'pos' => 6, - 'forbidden' => true, - ), - 1 => array( - 'type' => 'digit_integer', - 'data' => '1', - 'pos' => 8, - ), - 2 => array( - 'type' => 'punct_queryend', - 'data' => ';', - 'pos' => 0, - ), - 'len' => 3, - ) - ); - } - - public function testParse_2() - { - $this->assertParser( - 'SELECT * from aaa;', - array( - 'raw' => 'SELECT * from aaa;', - 0 => array( - 'type' => 'alpha_reservedWord', - 'data' => 'SELECT', - 'pos' => 6, - 'forbidden' => true, - ), - 1 => array( - 'type' => 'punct', - 'data' => '*', - 'pos' => 0, - ), - 2 => array( - 'type' => 'alpha_reservedWord', - 'data' => 'from', - 'pos' => 13, - 'forbidden' => true, - ), - 3 => array( - 'type' => 'alpha_identifier', - 'data' => 'aaa', - 'pos' => 17, - 'forbidden' => false, - ), - 4 => array( - 'type' => 'punct_queryend', - 'data' => ';', - 'pos' => 0, - ), - 'len' => 5, - ) - ); - } - - public function testParse_3() - { - $this->assertParser( - 'SELECT * from `aaa`;', - array( - 'raw' => 'SELECT * from `aaa`;', - 0 => array( - 'type' => 'alpha_reservedWord', - 'data' => 'SELECT', - 'pos' => 6, - 'forbidden' => true, - ), - 1 => array( - 'type' => 'punct', - 'data' => '*', - 'pos' => 0, - ), - 2 => array( - 'type' => 'alpha_reservedWord', - 'data' => 'from', - 'pos' => 13, - 'forbidden' => true, - ), - 3 => array( - 'type' => 'quote_backtick', - 'data' => '`aaa`', - 'pos' => 0, - ), - 4 => array( - 'type' => 'punct_queryend', - 'data' => ';', - 'pos' => 0, - ), - 'len' => 5, - ) - ); - } - /** + * Data provider for parser testing * - * @group medium + * @return array with test data */ - public function testParse_4() + public function parserData() { - $GLOBALS['is_ajax_request'] = true; - $this->assertParser( - 'SELECT * from `aaa;', + return array( array( - 'raw' => 'SELECT * from `aaa`;', - 0 => array ( - 'type' => 'alpha_reservedWord', - 'data' => 'SELECT', - 'pos' => 6, - 'forbidden' => true, - ), - 1 => array( - 'type' => 'punct', - 'data' => '*', - 'pos' => 0, - ), - 2 => array( - 'type' => 'alpha_reservedWord', - 'data' => 'from', - 'pos' => 13, - 'forbidden' => true, - ), - 3 => array( - 'type' => 'quote_backtick', - 'data' => '`aaa`', - 'pos' => 0, - ), - 4 => array( - 'type' => 'punct_queryend', - 'data' => ';', - 'pos' => 0, - ), - 'len' => 5, - ) - ); - } - - public function testParse_5() - { - $this->assertParser( - 'SELECT * FROM `a_table` tbla INNER JOIN b_table` tblb ON tblb.id = tbla.id WHERE tblb.field1 != tbla.field1`;', + 'SELECT 1;', + array( + 'raw' => 'SELECT 1;', + 0 => array( + 'type' => 'alpha_reservedWord', + 'data' => 'SELECT', + 'pos' => 6, + 'forbidden' => true, + ), + 1 => array( + 'type' => 'digit_integer', + 'data' => '1', + 'pos' => 8, + ), + 2 => array( + 'type' => 'punct_queryend', + 'data' => ';', + 'pos' => 0, + ), + 'len' => 3, + ) + ), array( - 'raw' => 'SELECT * FROM `a_table` tbla INNER JOIN b_table` tblb ON tblb.id = tbla.id WHERE tblb.field1 != tbla.field1`;', - 0 => array( - 'type' => 'alpha_reservedWord', - 'data' => 'SELECT', - 'pos' => 6, - 'forbidden' => true, - ), - 1 => array( - 'type' => 'punct', - 'data' => '*', - 'pos' => 0, - ), - 2 => array( - 'type' => 'alpha_reservedWord', - 'data' => 'FROM', - 'pos' => 13, - 'forbidden' => true, - ), - 3 => array( - 'type' => 'quote_backtick', - 'data' => '`a_table`', - 'pos' => 0, - ), - 4 => array( - 'type' => 'alpha_identifier', - 'data' => 'tbla', - 'pos' => 28, - 'forbidden' => false, - ), - 5 => array( - 'type' => 'alpha_reservedWord', - 'data' => 'INNER', - 'pos' => 34, - 'forbidden' => true, - ), - 6 => array( - 'type' => 'alpha_reservedWord', - 'data' => 'JOIN', - 'pos' => 39, - 'forbidden' => true, - ), - 7 => array( - 'type' => 'alpha_identifier', - 'data' => 'b_table', - 'pos' => 47, - 'forbidden' => false, - ), - 8 => array( - 'type' => 'quote_backtick', - 'data' => '` tblb ON tblb.id = tbla.id WHERE tblb.field1 != tbla.field1`', - 'pos' => 0, - ), - 9 => array( - 'type' => 'punct_queryend', - 'data' => ';', - 'pos' => 0, - ), - 'len' => 10, - ) + 'SELECT * from aaa;', + array( + 'raw' => 'SELECT * from aaa;', + 0 => array( + 'type' => 'alpha_reservedWord', + 'data' => 'SELECT', + 'pos' => 6, + 'forbidden' => true, + ), + 1 => array( + 'type' => 'punct', + 'data' => '*', + 'pos' => 0, + ), + 2 => array( + 'type' => 'alpha_reservedWord', + 'data' => 'from', + 'pos' => 13, + 'forbidden' => true, + ), + 3 => array( + 'type' => 'alpha_identifier', + 'data' => 'aaa', + 'pos' => 17, + 'forbidden' => false, + ), + 4 => array( + 'type' => 'punct_queryend', + 'data' => ';', + 'pos' => 0, + ), + 'len' => 5, + ) + ), + array( + 'SELECT * from `aaa`;', + array( + 'raw' => 'SELECT * from `aaa`;', + 0 => array( + 'type' => 'alpha_reservedWord', + 'data' => 'SELECT', + 'pos' => 6, + 'forbidden' => true, + ), + 1 => array( + 'type' => 'punct', + 'data' => '*', + 'pos' => 0, + ), + 2 => array( + 'type' => 'alpha_reservedWord', + 'data' => 'from', + 'pos' => 13, + 'forbidden' => true, + ), + 3 => array( + 'type' => 'quote_backtick', + 'data' => '`aaa`', + 'pos' => 0, + ), + 4 => array( + 'type' => 'punct_queryend', + 'data' => ';', + 'pos' => 0, + ), + 'len' => 5, + ) + ), + array( + 'SELECT * from `aaa;', + array( + 'raw' => 'SELECT * from `aaa`;', + 0 => array ( + 'type' => 'alpha_reservedWord', + 'data' => 'SELECT', + 'pos' => 6, + 'forbidden' => true, + ), + 1 => array( + 'type' => 'punct', + 'data' => '*', + 'pos' => 0, + ), + 2 => array( + 'type' => 'alpha_reservedWord', + 'data' => 'from', + 'pos' => 13, + 'forbidden' => true, + ), + 3 => array( + 'type' => 'quote_backtick', + 'data' => '`aaa`', + 'pos' => 0, + ), + 4 => array( + 'type' => 'punct_queryend', + 'data' => ';', + 'pos' => 0, + ), + 'len' => 5, + ) + ), + array( + 'SELECT * FROM `a_table` tbla INNER JOIN b_table` tblb ON tblb.id = tbla.id WHERE tblb.field1 != tbla.field1`;', + array( + 'raw' => 'SELECT * FROM `a_table` tbla INNER JOIN b_table` tblb ON tblb.id = tbla.id WHERE tblb.field1 != tbla.field1`;', + 0 => array( + 'type' => 'alpha_reservedWord', + 'data' => 'SELECT', + 'pos' => 6, + 'forbidden' => true, + ), + 1 => array( + 'type' => 'punct', + 'data' => '*', + 'pos' => 0, + ), + 2 => array( + 'type' => 'alpha_reservedWord', + 'data' => 'FROM', + 'pos' => 13, + 'forbidden' => true, + ), + 3 => array( + 'type' => 'quote_backtick', + 'data' => '`a_table`', + 'pos' => 0, + ), + 4 => array( + 'type' => 'alpha_identifier', + 'data' => 'tbla', + 'pos' => 28, + 'forbidden' => false, + ), + 5 => array( + 'type' => 'alpha_reservedWord', + 'data' => 'INNER', + 'pos' => 34, + 'forbidden' => true, + ), + 6 => array( + 'type' => 'alpha_reservedWord', + 'data' => 'JOIN', + 'pos' => 39, + 'forbidden' => true, + ), + 7 => array( + 'type' => 'alpha_identifier', + 'data' => 'b_table', + 'pos' => 47, + 'forbidden' => false, + ), + 8 => array( + 'type' => 'quote_backtick', + 'data' => '` tblb ON tblb.id = tbla.id WHERE tblb.field1 != tbla.field1`', + 'pos' => 0, + ), + 9 => array( + 'type' => 'punct_queryend', + 'data' => ';', + 'pos' => 0, + ), + 'len' => 10, + ) + ), ); } }