Skip to content

Commit 45a1795

Browse files
committed
FIX Don't confuse end of column name for direction
1 parent 89fbf45 commit 45a1795

2 files changed

Lines changed: 52 additions & 8 deletions

File tree

src/ORM/Queries/SQLSelect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ public function addOrderBy($clauses = null, $direction = null)
407407
*/
408408
private function getDirectionFromString($value, $defaultDirection = null)
409409
{
410-
if (preg_match('/^(.*)(asc|desc)$/i', $value ?? '', $matches)) {
410+
if (preg_match('/^(.*) (asc|desc)$/i', $value ?? '', $matches)) {
411411
$column = trim($matches[1] ?? '');
412412
$direction = strtoupper($matches[2] ?? '');
413413
} else {

tests/php/ORM/SQLSelectTest.php

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,17 +197,61 @@ public function testCanSortBy()
197197
$this->assertTrue($query->canSortBy('Name'));
198198
}
199199

200+
public static function provideAddOrderBy(): array
201+
{
202+
return [
203+
'single basic clause' => [
204+
'orderByClauses' => [
205+
['Title'],
206+
],
207+
'expectedQuery' => 'SELECT ID, Title FROM Page ORDER BY Title ASC',
208+
],
209+
'single basic clause with order' => [
210+
'orderByClauses' => [
211+
['Title', 'desc'],
212+
],
213+
'expectedQuery' => 'SELECT ID, Title FROM Page ORDER BY Title DESC',
214+
],
215+
'single basic clause with order in first arg' => [
216+
'orderByClauses' => [
217+
['Title desc'],
218+
],
219+
'expectedQuery' => 'SELECT ID, Title FROM Page ORDER BY Title DESC',
220+
],
221+
'single clause column name ends with "asc"' => [
222+
'orderByClauses' => [
223+
['Flasc', 'DESC'],
224+
],
225+
'expectedQuery' => 'SELECT ID, Title FROM Page ORDER BY Flasc DESC',
226+
],
227+
'multiple clauses' => [
228+
'orderByClauses' => [
229+
['ID'],
230+
['Title', 'DESC'],
231+
],
232+
'expectedQuery' => 'SELECT ID, Title FROM Page ORDER BY ID ASC, Title DESC',
233+
],
234+
'custom sort columns' => [
235+
'orderByClauses' => [
236+
['(ID % 2) = 0', 'ASC'],
237+
['ID > 50', 'ASC'],
238+
],
239+
'expectedQuery' => 'SELECT ID, Title, (ID % 2) = 0 AS "_SortColumn0", ID > 50 AS "_SortColumn1" FROM Page ORDER BY "_SortColumn0" ASC, "_SortColumn1" ASC',
240+
],
241+
];
242+
}
243+
200244
/**
201-
* Test multiple order by SQL clauses.
245+
* @dataProvider provideAddOrderBy
202246
*/
203-
public function testAddOrderBy()
247+
public function testAddOrderBy(array $orderByClauses, string $expectedQuery): void
204248
{
205249
$query = new SQLSelect();
206-
$query->setSelect('ID', "Title")->setFrom('Page')->addOrderBy('(ID % 2) = 0', 'ASC')->addOrderBy('ID > 50', 'ASC');
207-
$this->assertSQLEquals(
208-
'SELECT ID, Title, (ID % 2) = 0 AS "_SortColumn0", ID > 50 AS "_SortColumn1" FROM Page ORDER BY "_SortColumn0" ASC, "_SortColumn1" ASC',
209-
$query->sql($parameters)
210-
);
250+
$query->setSelect('ID', "Title")->setFrom('Page');
251+
foreach ($orderByClauses as $clause) {
252+
$query->addOrderBy(...$clause);
253+
}
254+
$this->assertSQLEquals($expectedQuery, $query->sql($parameters));
211255
}
212256

213257
public function testSelectWithChainedFilterParameters()

0 commit comments

Comments
 (0)