Skip to content

Commit de9009d

Browse files
committed
Merge branch 'QA'
Signed-off-by: William Desportes <[email protected]>
2 parents 1f45fbc + 8a9b8fc commit de9009d

File tree

8 files changed

+422
-7
lines changed

8 files changed

+422
-7
lines changed

CONTRIBUTING.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ them and to run automated tests on the code.
2424

2525
## Coding standards
2626

27-
We do follow PSR-1 and PSR-2 coding standards.
27+
We do follow PSR-1 and PSR-2 coding standards.
2828

29-
You can use php-cs-fixer to fix the code to match our expectations:
29+
You can use phpcbf to fix the code to match our expectations:
3030

3131
```
32-
php-cs-fixer fix .
32+
./vendor/bin/phpcbf
3333
```
3434

3535
## Testsuite
@@ -38,7 +38,7 @@ Our code comes with quite comprehensive testsuite, it is automatically executed
3838
on every commit and pull request, you can also run it locally:
3939

4040
```
41-
./vendor/bin/phpunit -c phpunit.xml
41+
./vendor/bin/phpunit
4242
```
4343

4444
The testsuite relies on fixtures of parser states, in case you need to

src/Utils/CLI.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public function runTokenize()
214214
return 1;
215215
}
216216

217-
private function readStdin()
217+
public function readStdin()
218218
{
219219
stream_set_blocking(STDIN, false);
220220
$stdin = stream_get_contents(STDIN);

tests/Builder/CallStatementTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace PhpMyAdmin\SqlParser\Tests\Builder;
4+
5+
use PhpMyAdmin\SqlParser\Parser;
6+
use PhpMyAdmin\SqlParser\Tests\TestCase;
7+
8+
class CallStatementTest extends TestCase
9+
{
10+
public function testBuilder()
11+
{
12+
$query = 'CALL foo()';
13+
14+
$parser = new Parser($query);
15+
$stmt = $parser->statements[0];
16+
17+
$this->assertEquals($query, $stmt->build());
18+
}
19+
}

tests/Builder/SelectStatementTest.php

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

56+
public function testBuilderAliasOrder()
57+
{
58+
$parser = new Parser(
59+
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
60+
. 'RIGHT JOIN `student_course_booking` scb ON sgu.id = scb.user_id '
61+
. 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
62+
. 'ORDER BY scb.id LIMIT 0,300'
63+
);
64+
$stmt = $parser->statements[0];
65+
66+
$this->assertEquals(
67+
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` AS `sgu` '
68+
. 'RIGHT JOIN `student_course_booking` AS `scb` ON sgu.id = scb.user_id '
69+
. 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
70+
. 'ORDER BY scb.id ASC LIMIT 0, 300',
71+
$stmt->build()
72+
);
73+
}
74+
75+
public function testBuilderAliasOrderMultiple()
76+
{
77+
$parser = new Parser(
78+
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
79+
. 'RIGHT JOIN `student_course_booking` scb ON sgu.id = scb.user_id '
80+
. 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
81+
. 'ORDER BY scb.id DESC, scb.order LIMIT 0,300'
82+
);
83+
$stmt = $parser->statements[0];
84+
85+
$this->assertEquals(
86+
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` AS `sgu` '
87+
. 'RIGHT JOIN `student_course_booking` AS `scb` ON sgu.id = scb.user_id '
88+
. 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
89+
. 'ORDER BY scb.id DESC, scb.order ASC LIMIT 0, 300',
90+
$stmt->build()
91+
);
92+
}
93+
94+
public function testBuilderAliasOrderMultipleFunctions()
95+
{
96+
$parser = new Parser(
97+
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
98+
. 'RIGHT JOIN `student_course_booking` scb ON sgu.id = scb.user_id '
99+
. 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
100+
. 'ORDER BY scb.id DESC, YEAR(scb.dob) LIMIT 0,300'
101+
);
102+
$stmt = $parser->statements[0];
103+
104+
$this->assertEquals(
105+
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` AS `sgu` '
106+
. 'RIGHT JOIN `student_course_booking` AS `scb` ON sgu.id = scb.user_id '
107+
. 'WHERE `has_found_course` = \'1\' GROUP BY sgu.id '
108+
. 'ORDER BY scb.id DESC, YEAR(scb.dob) ASC LIMIT 0, 300',
109+
$stmt->build()
110+
);
111+
}
112+
113+
public function testBuilderAliasGroupByMultipleFunctions()
114+
{
115+
$parser = new Parser(
116+
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
117+
. 'RIGHT JOIN `student_course_booking` scb ON sgu.id = scb.user_id '
118+
. 'WHERE `has_found_course` = \'1\' '
119+
. 'GROUP BY scb.id, YEAR(scb.dob) LIMIT 0,300'
120+
);
121+
$stmt = $parser->statements[0];
122+
123+
$this->assertEquals(
124+
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` AS `sgu` '
125+
. 'RIGHT JOIN `student_course_booking` AS `scb` ON sgu.id = scb.user_id '
126+
. 'WHERE `has_found_course` = \'1\' '
127+
. 'GROUP BY scb.id, YEAR(scb.dob) LIMIT 0, 300',
128+
$stmt->build()
129+
);
130+
}
131+
132+
public function testBuilderAliasGroupByMultipleFunctionsOrderRemoved()
133+
{
134+
$parser = new Parser(
135+
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` sgu '
136+
. 'RIGHT JOIN `student_course_booking` scb ON sgu.id = scb.user_id '
137+
. 'WHERE `has_found_course` = \'1\' '
138+
. 'GROUP BY scb.id ASC, YEAR(scb.dob) DESC LIMIT 0,300'
139+
);
140+
$stmt = $parser->statements[0];
141+
142+
// The order is not kept, is this an expected behavior ?
143+
// Ref: 4af06d24b041e499fb0e75ab3a98caf9a91700ef
144+
// Issue: #154
145+
$this->assertEquals(
146+
'SELECT sgu.id, sgu.email_address FROM `sf_guard_user` AS `sgu` '
147+
. 'RIGHT JOIN `student_course_booking` AS `scb` ON sgu.id = scb.user_id '
148+
. 'WHERE `has_found_course` = \'1\' '
149+
. 'GROUP BY scb.id, YEAR(scb.dob) LIMIT 0, 300',
150+
$stmt->build()
151+
);
152+
}
153+
154+
public function testBuilderAliasOrderCase()
155+
{
156+
$parser = new Parser(
157+
'SELECT * FROM `world_borders` ORDER BY CASE '
158+
. 'WHEN REGION = 2 THEN 99 '
159+
. 'WHEN REGION > 3 THEN REGION+1 '
160+
. 'ELSE 100 END LIMIT 0,300'
161+
);
162+
$stmt = $parser->statements[0];
163+
164+
$this->assertEquals(
165+
'SELECT * FROM `world_borders` ORDER BY CASE '
166+
. 'WHEN REGION = 2 THEN 99 '
167+
. 'WHEN REGION > 3 THEN REGION+1 '
168+
. 'ELSE 100 END ASC LIMIT 0, 300',
169+
$stmt->build()
170+
);
171+
}
172+
173+
public function testBuilderAliasGroupByCase()
174+
{
175+
$parser = new Parser(
176+
'SELECT * FROM `world_borders` GROUP BY CASE '
177+
. 'WHEN REGION = 2 THEN 99 '
178+
. 'WHEN REGION > 3 THEN REGION+1 '
179+
. 'ELSE 100 END LIMIT 0,300'
180+
);
181+
$stmt = $parser->statements[0];
182+
183+
$this->assertEquals(
184+
'SELECT * FROM `world_borders` GROUP BY CASE '
185+
. 'WHEN REGION = 2 THEN 99 '
186+
. 'WHEN REGION > 3 THEN REGION+1 '
187+
. 'ELSE 100 END LIMIT 0, 300',
188+
$stmt->build()
189+
);
190+
}
191+
56192
public function testBuilderEndOptions()
57193
{
58194
/* Assertion 1 */
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace PhpMyAdmin\SqlParser\Tests\Builder;
4+
5+
use PhpMyAdmin\SqlParser\Parser;
6+
use PhpMyAdmin\SqlParser\Tests\TestCase;
7+
8+
class TruncateStatementTest extends TestCase
9+
{
10+
public function testBuilder()
11+
{
12+
$query = 'TRUNCATE TABLE mytable;';
13+
14+
$parser = new Parser($query);
15+
$stmt = $parser->statements[0];
16+
17+
$this->assertEquals($query, $stmt->build());
18+
}
19+
20+
public function testBuilderDbtable()
21+
{
22+
$query = 'TRUNCATE TABLE mydb.mytable;';
23+
24+
$parser = new Parser($query);
25+
$stmt = $parser->statements[0];
26+
27+
$this->assertEquals($query, $stmt->build());
28+
}
29+
30+
public function testBuilderDbtableBackQuotes()
31+
{
32+
$query = 'TRUNCATE TABLE `mydb`.`mytable`;';
33+
34+
$parser = new Parser($query);
35+
$stmt = $parser->statements[0];
36+
37+
$this->assertEquals($query, $stmt->build());
38+
}
39+
}

tests/Lexer/IsMethodsTest.php

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

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

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

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

114116
public function testIsSymbol()
@@ -119,7 +121,8 @@ public function testIsSymbol()
119121
$this->assertEquals(Token::FLAG_SYMBOL_VARIABLE, Context::isSymbol('@id'));
120122
$this->assertEquals(Token::FLAG_SYMBOL_BACKTICK, Context::isSymbol('`id`'));
121123

122-
$this->assertEquals(Context::isSymbol('id'), null);
124+
$this->assertNull(Context::isSymbol(''));
125+
$this->assertNull(Context::isSymbol('id'));
123126
}
124127

125128
public function testisSeparator()

0 commit comments

Comments
 (0)