summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorWilliam Desportes <williamdes@wdes.fr>2019-12-31 14:05:32 +0100
committerWilliam Desportes <williamdes@wdes.fr>2019-12-31 15:19:23 +0100
commit8548a7fd567fe6e4cffa09232b05f85001d54d3b (patch)
tree4d906b5a6278255cc8f720b16c9fb0d610eeffef /tests
parenta92eaff0caad863a3f83affa1652646c0a2363f1 (diff)
downloadsql-parser-8548a7fd567fe6e4cffa09232b05f85001d54d3b.zip
sql-parser-8548a7fd567fe6e4cffa09232b05f85001d54d3b.tar.gz
sql-parser-8548a7fd567fe6e4cffa09232b05f85001d54d3b.tar.bz2
Cover some edge cases
Ref: #154 (See my comment for one test I added) Signed-off-by: William Desportes <williamdes@wdes.fr>
Diffstat (limited to 'tests')
-rw-r--r--tests/Builder/SelectStatementTest.php136
-rw-r--r--tests/Lexer/IsMethodsTest.php7
2 files changed, 141 insertions, 2 deletions
diff --git a/tests/Builder/SelectStatementTest.php b/tests/Builder/SelectStatementTest.php
index 376ba07..6abee03 100644
--- a/tests/Builder/SelectStatementTest.php
+++ b/tests/Builder/SelectStatementTest.php
@@ -52,6 +52,142 @@ class SelectStatementTest extends TestCase
);
}
+ public function testBuilderAliasOrder()
+ {
+ $parser = new Parser(
+ 'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
+ . 'RIGHT JOIN `student_course_booking` scb ON sgu.id = scb.user_id '
+ . 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
+ . 'ORDER BY scb.id LIMIT 0,300'
+ );
+ $stmt = $parser->statements[0];
+
+ $this->assertEquals(
+ 'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` AS `sgu` '
+ . 'RIGHT JOIN `student_course_booking` AS `scb` ON sgu.id = scb.user_id '
+ . 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
+ . 'ORDER BY scb.id ASC LIMIT 0, 300',
+ $stmt->build()
+ );
+ }
+
+ public function testBuilderAliasOrderMultiple()
+ {
+ $parser = new Parser(
+ 'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
+ . 'RIGHT JOIN `student_course_booking` scb ON sgu.id = scb.user_id '
+ . 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
+ . 'ORDER BY scb.id DESC, scb.order LIMIT 0,300'
+ );
+ $stmt = $parser->statements[0];
+
+ $this->assertEquals(
+ 'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` AS `sgu` '
+ . 'RIGHT JOIN `student_course_booking` AS `scb` ON sgu.id = scb.user_id '
+ . 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
+ . 'ORDER BY scb.id DESC, scb.order ASC LIMIT 0, 300',
+ $stmt->build()
+ );
+ }
+
+ public function testBuilderAliasOrderMultipleFunctions()
+ {
+ $parser = new Parser(
+ 'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
+ . 'RIGHT JOIN `student_course_booking` scb ON sgu.id = scb.user_id '
+ . 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
+ . 'ORDER BY scb.id DESC, YEAR(scb.dob) LIMIT 0,300'
+ );
+ $stmt = $parser->statements[0];
+
+ $this->assertEquals(
+ 'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` AS `sgu` '
+ . 'RIGHT JOIN `student_course_booking` AS `scb` ON sgu.id = scb.user_id '
+ . 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
+ . 'ORDER BY scb.id DESC, YEAR(scb.dob) ASC LIMIT 0, 300',
+ $stmt->build()
+ );
+ }
+
+ public function testBuilderAliasGroupByMultipleFunctions()
+ {
+ $parser = new Parser(
+ 'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
+ . 'RIGHT JOIN `student_course_booking` scb ON sgu.id = scb.user_id '
+ . 'WHERE `has_found_course` = \'1\' '
+ . 'GROUP BY scb.id, YEAR(scb.dob) LIMIT 0,300'
+ );
+ $stmt = $parser->statements[0];
+
+ $this->assertEquals(
+ 'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` AS `sgu` '
+ . 'RIGHT JOIN `student_course_booking` AS `scb` ON sgu.id = scb.user_id '
+ . 'WHERE `has_found_course` = \'1\' '
+ . 'GROUP BY scb.id, YEAR(scb.dob) LIMIT 0, 300',
+ $stmt->build()
+ );
+ }
+
+ public function testBuilderAliasGroupByMultipleFunctionsOrderRemoved()
+ {
+ $parser = new Parser(
+ 'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
+ . 'RIGHT JOIN `student_course_booking` scb ON sgu.id = scb.user_id '
+ . 'WHERE `has_found_course` = \'1\' '
+ . 'GROUP BY scb.id ASC, YEAR(scb.dob) DESC LIMIT 0,300'
+ );
+ $stmt = $parser->statements[0];
+
+ // The order is not kept, is this an expected behavior ?
+ // Ref: 4af06d24b041e499fb0e75ab3a98caf9a91700ef
+ // Issue: #154
+ $this->assertEquals(
+ 'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` AS `sgu` '
+ . 'RIGHT JOIN `student_course_booking` AS `scb` ON sgu.id = scb.user_id '
+ . 'WHERE `has_found_course` = \'1\' '
+ . 'GROUP BY scb.id, YEAR(scb.dob) LIMIT 0, 300',
+ $stmt->build()
+ );
+ }
+
+ public function testBuilderAliasOrderCase()
+ {
+ $parser = new Parser(
+ 'SELECT * FROM `world_borders` ORDER BY CASE '
+ . 'WHEN REGION = 2 THEN 99 '
+ . 'WHEN REGION > 3 THEN REGION+1 '
+ . 'ELSE 100 END LIMIT 0,300'
+ );
+ $stmt = $parser->statements[0];
+
+ $this->assertEquals(
+ 'SELECT * FROM `world_borders` ORDER BY CASE '
+ . 'WHEN REGION = 2 THEN 99 '
+ . 'WHEN REGION > 3 THEN REGION+1 '
+ . 'ELSE 100 END ASC LIMIT 0, 300',
+ $stmt->build()
+ );
+ }
+
+ public function testBuilderAliasGroupByCase()
+ {
+ $parser = new Parser(
+ 'SELECT * FROM `world_borders` GROUP BY CASE '
+ . 'WHEN REGION = 2 THEN 99 '
+ . 'WHEN REGION > 3 THEN REGION+1 '
+ . 'ELSE 100 END LIMIT 0,300'
+ );
+ $stmt = $parser->statements[0];
+
+ $this->assertEquals(
+ 'SELECT * FROM `world_borders` GROUP BY CASE '
+ . 'WHEN REGION = 2 THEN 99 '
+ . 'WHEN REGION > 3 THEN REGION+1 '
+ . 'ELSE 100 END LIMIT 0, 300',
+ $stmt->build()
+ );
+ }
+
public function testBuilderEndOptions()
{
/* Assertion 1 */
diff --git a/tests/Lexer/IsMethodsTest.php b/tests/Lexer/IsMethodsTest.php
index 56552c7..3a7ef9b 100644
--- a/tests/Lexer/IsMethodsTest.php
+++ b/tests/Lexer/IsMethodsTest.php
@@ -68,6 +68,7 @@ class IsMethodsTest extends TestCase
$this->assertEquals(Token::FLAG_COMMENT_C, Context::isComment('/*comment */'));
$this->assertEquals(Token::FLAG_COMMENT_SQL, Context::isComment('-- my comment'));
+ $this->assertNull(Context::isComment(''));
$this->assertNull(Context::isComment('--not a comment'));
}
@@ -107,7 +108,8 @@ class IsMethodsTest extends TestCase
$this->assertEquals(Token::FLAG_STRING_SINGLE_QUOTES, Context::isString("'foo bar'"));
$this->assertEquals(Token::FLAG_STRING_DOUBLE_QUOTES, Context::isString('"foo bar"'));
- $this->assertEquals(Context::isString('foo bar'), null);
+ $this->assertNull(Context::isString(''));
+ $this->assertNull(Context::isString('foo bar'));
}
public function testIsSymbol()
@@ -118,7 +120,8 @@ class IsMethodsTest extends TestCase
$this->assertEquals(Token::FLAG_SYMBOL_VARIABLE, Context::isSymbol('@id'));
$this->assertEquals(Token::FLAG_SYMBOL_BACKTICK, Context::isSymbol('`id`'));
- $this->assertEquals(Context::isSymbol('id'), null);
+ $this->assertNull(Context::isSymbol(''));
+ $this->assertNull(Context::isSymbol('id'));
}
public function testisSeparator()