Skip to content

Commit

Permalink
Fixing orderBy direction is not rendered when optional direction is o…
Browse files Browse the repository at this point in the history
…mitted (#165) (#172)
  • Loading branch information
kwhat authored Aug 11, 2022
1 parent 3029492 commit 89d1478
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
14 changes: 10 additions & 4 deletions src/AdvancedStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,19 @@ public function orderBy(string $column, string $direction = ''): self
protected function renderOrderBy(): string
{
$sql = '';
if ($direction = reset($this->orderBy)) {
if (($direction = reset($this->orderBy)) !== false) {
$column = key($this->orderBy);
$sql = " ORDER BY {$column} {$direction}";
$sql = " ORDER BY {$column}";
if (!empty($direction)) {
$sql .= " {$direction}";
}

while ($direction = next($this->orderBy)) {
while (($direction = next($this->orderBy)) !== false) {
$column = key($this->orderBy);
$sql .= ", {$column} {$direction}";
$sql .= ", {$column}";
if (!empty($direction)) {
$sql .= " {$direction}";
}
}
}

Expand Down
13 changes: 11 additions & 2 deletions tests/Statement/DeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,17 @@ public function testToStringWithOrderBy()
->orderBy('id', 'ASC')
->orderBy('name', 'DESC');

// FIXME This seems broken...
$this->assertStringEndsWith('test ORDER BY id ASC, name DESC', $this->subject->__toString());
$this->assertStringEndsWith(' ORDER BY id ASC, name DESC', $this->subject->__toString());
}

public function testToStringWithOrderByWithoutDirection()
{
$this->subject
->from('test')
->orderBy('id')
->orderBy('name');

$this->assertStringEndsWith(' ORDER BY id, name', $this->subject->__toString());
}

public function testToStringWithLimit()
Expand Down
12 changes: 11 additions & 1 deletion tests/Statement/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,17 @@ public function testToStringWithOrderBy()
->orderBy('id', 'ASC')
->orderBy('name', 'DESC');

$this->assertStringEndsWith('test ORDER BY id ASC, name DESC', $this->subject->__toString());
$this->assertStringEndsWith(' ORDER BY id ASC, name DESC', $this->subject->__toString());
}

public function testToStringWithOrderByWithoutDirection()
{
$this->subject
->from('test')
->orderBy('id')
->orderBy('name');

$this->assertStringEndsWith(' ORDER BY id, name', $this->subject->__toString());
}

public function testToStringWithLimit()
Expand Down
11 changes: 11 additions & 0 deletions tests/Statement/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ public function testToStringWithOrderBy()
$this->assertStringEndsWith(' ORDER BY id ASC, name DESC', $this->subject->__toString());
}

public function testToStringWithOrderByWithoutDirection()
{
$this->subject
->table('test')
->set('col', 'value')
->orderBy('id')
->orderBy('name');

$this->assertStringEndsWith(' ORDER BY id, name', $this->subject->__toString());
}

public function testToStringWithLimit()
{
$this->subject
Expand Down

0 comments on commit 89d1478

Please sign in to comment.