Skip to content

Commit 8548a7f

Browse files
committed
Cover some edge cases
Ref: #154 (See my comment for one test I added) Signed-off-by: William Desportes <[email protected]>
1 parent a92eaff commit 8548a7f

File tree

2 files changed

+141
-2
lines changed

2 files changed

+141
-2
lines changed

tests/Builder/SelectStatementTest.php

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,142 @@ public function testBuilderAlias()
5252
);
5353
}
5454

55+
public function testBuilderAliasOrder()
56+
{
57+
$parser = new Parser(
58+
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
59+
. 'RIGHT JOIN `student_course_booking` scb ON sgu.id = scb.user_id '
60+
. 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
61+
. 'ORDER BY scb.id LIMIT 0,300'
62+
);
63+
$stmt = $parser->statements[0];
64+
65+
$this->assertEquals(
66+
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` AS `sgu` '
67+
. 'RIGHT JOIN `student_course_booking` AS `scb` ON sgu.id = scb.user_id '
68+
. 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
69+
. 'ORDER BY scb.id ASC LIMIT 0, 300',
70+
$stmt->build()
71+
);
72+
}
73+
74+
public function testBuilderAliasOrderMultiple()
75+
{
76+
$parser = new Parser(
77+
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
78+
. 'RIGHT JOIN `student_course_booking` scb ON sgu.id = scb.user_id '
79+
. 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
80+
. 'ORDER BY scb.id DESC, scb.order LIMIT 0,300'
81+
);
82+
$stmt = $parser->statements[0];
83+
84+
$this->assertEquals(
85+
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` AS `sgu` '
86+
. 'RIGHT JOIN `student_course_booking` AS `scb` ON sgu.id = scb.user_id '
87+
. 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
88+
. 'ORDER BY scb.id DESC, scb.order ASC LIMIT 0, 300',
89+
$stmt->build()
90+
);
91+
}
92+
93+
public function testBuilderAliasOrderMultipleFunctions()
94+
{
95+
$parser = new Parser(
96+
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
97+
. 'RIGHT JOIN `student_course_booking` scb ON sgu.id = scb.user_id '
98+
. 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
99+
. 'ORDER BY scb.id DESC, YEAR(scb.dob) LIMIT 0,300'
100+
);
101+
$stmt = $parser->statements[0];
102+
103+
$this->assertEquals(
104+
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` AS `sgu` '
105+
. 'RIGHT JOIN `student_course_booking` AS `scb` ON sgu.id = scb.user_id '
106+
. 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
107+
. 'ORDER BY scb.id DESC, YEAR(scb.dob) ASC LIMIT 0, 300',
108+
$stmt->build()
109+
);
110+
}
111+
112+
public function testBuilderAliasGroupByMultipleFunctions()
113+
{
114+
$parser = new Parser(
115+
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
116+
. 'RIGHT JOIN `student_course_booking` scb ON sgu.id = scb.user_id '
117+
. 'WHERE `has_found_course` = \'1\' '
118+
. 'GROUP BY scb.id, YEAR(scb.dob) LIMIT 0,300'
119+
);
120+
$stmt = $parser->statements[0];
121+
122+
$this->assertEquals(
123+
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` AS `sgu` '
124+
. 'RIGHT JOIN `student_course_booking` AS `scb` ON sgu.id = scb.user_id '
125+
. 'WHERE `has_found_course` = \'1\' '
126+
. 'GROUP BY scb.id, YEAR(scb.dob) LIMIT 0, 300',
127+
$stmt->build()
128+
);
129+
}
130+
131+
public function testBuilderAliasGroupByMultipleFunctionsOrderRemoved()
132+
{
133+
$parser = new Parser(
134+
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
135+
. 'RIGHT JOIN `student_course_booking` scb ON sgu.id = scb.user_id '
136+
. 'WHERE `has_found_course` = \'1\' '
137+
. 'GROUP BY scb.id ASC, YEAR(scb.dob) DESC LIMIT 0,300'
138+
);
139+
$stmt = $parser->statements[0];
140+
141+
// The order is not kept, is this an expected behavior ?
142+
// Ref: 4af06d24b041e499fb0e75ab3a98caf9a91700ef
143+
// Issue: #154
144+
$this->assertEquals(
145+
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` AS `sgu` '
146+
. 'RIGHT JOIN `student_course_booking` AS `scb` ON sgu.id = scb.user_id '
147+
. 'WHERE `has_found_course` = \'1\' '
148+
. 'GROUP BY scb.id, YEAR(scb.dob) LIMIT 0, 300',
149+
$stmt->build()
150+
);
151+
}
152+
153+
public function testBuilderAliasOrderCase()
154+
{
155+
$parser = new Parser(
156+
'SELECT * FROM `world_borders` ORDER BY CASE '
157+
. 'WHEN REGION = 2 THEN 99 '
158+
. 'WHEN REGION > 3 THEN REGION+1 '
159+
. 'ELSE 100 END LIMIT 0,300'
160+
);
161+
$stmt = $parser->statements[0];
162+
163+
$this->assertEquals(
164+
'SELECT * FROM `world_borders` ORDER BY CASE '
165+
. 'WHEN REGION = 2 THEN 99 '
166+
. 'WHEN REGION > 3 THEN REGION+1 '
167+
. 'ELSE 100 END ASC LIMIT 0, 300',
168+
$stmt->build()
169+
);
170+
}
171+
172+
public function testBuilderAliasGroupByCase()
173+
{
174+
$parser = new Parser(
175+
'SELECT * FROM `world_borders` GROUP BY CASE '
176+
. 'WHEN REGION = 2 THEN 99 '
177+
. 'WHEN REGION > 3 THEN REGION+1 '
178+
. 'ELSE 100 END LIMIT 0,300'
179+
);
180+
$stmt = $parser->statements[0];
181+
182+
$this->assertEquals(
183+
'SELECT * FROM `world_borders` GROUP BY CASE '
184+
. 'WHEN REGION = 2 THEN 99 '
185+
. 'WHEN REGION > 3 THEN REGION+1 '
186+
. 'ELSE 100 END LIMIT 0, 300',
187+
$stmt->build()
188+
);
189+
}
190+
55191
public function testBuilderEndOptions()
56192
{
57193
/* Assertion 1 */

tests/Lexer/IsMethodsTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function testIsComment()
6868
$this->assertEquals(Token::FLAG_COMMENT_C, Context::isComment('/*comment */'));
6969
$this->assertEquals(Token::FLAG_COMMENT_SQL, Context::isComment('-- my comment'));
7070

71+
$this->assertNull(Context::isComment(''));
7172
$this->assertNull(Context::isComment('--not a comment'));
7273
}
7374

@@ -107,7 +108,8 @@ public function testIsString()
107108
$this->assertEquals(Token::FLAG_STRING_SINGLE_QUOTES, Context::isString("'foo bar'"));
108109
$this->assertEquals(Token::FLAG_STRING_DOUBLE_QUOTES, Context::isString('"foo bar"'));
109110

110-
$this->assertEquals(Context::isString('foo bar'), null);
111+
$this->assertNull(Context::isString(''));
112+
$this->assertNull(Context::isString('foo bar'));
111113
}
112114

113115
public function testIsSymbol()
@@ -118,7 +120,8 @@ public function testIsSymbol()
118120
$this->assertEquals(Token::FLAG_SYMBOL_VARIABLE, Context::isSymbol('@id'));
119121
$this->assertEquals(Token::FLAG_SYMBOL_BACKTICK, Context::isSymbol('`id`'));
120122

121-
$this->assertEquals(Context::isSymbol('id'), null);
123+
$this->assertNull(Context::isSymbol(''));
124+
$this->assertNull(Context::isSymbol('id'));
122125
}
123126

124127
public function testisSeparator()

0 commit comments

Comments
 (0)