From 89d14787b4443e5bfe70954e40729c1715a56ae7 Mon Sep 17 00:00:00 2001 From: Alex Barker Date: Wed, 10 Aug 2022 17:46:28 -0700 Subject: [PATCH] Fixing orderBy direction is not rendered when optional direction is omitted (#165) (#172) --- src/AdvancedStatement.php | 14 ++++++++++---- tests/Statement/DeleteTest.php | 13 +++++++++++-- tests/Statement/SelectTest.php | 12 +++++++++++- tests/Statement/UpdateTest.php | 11 +++++++++++ 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/AdvancedStatement.php b/src/AdvancedStatement.php index debdcc3..d0ba23f 100644 --- a/src/AdvancedStatement.php +++ b/src/AdvancedStatement.php @@ -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}"; + } } } diff --git a/tests/Statement/DeleteTest.php b/tests/Statement/DeleteTest.php index 2e3f5d2..c9c04e8 100644 --- a/tests/Statement/DeleteTest.php +++ b/tests/Statement/DeleteTest.php @@ -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() diff --git a/tests/Statement/SelectTest.php b/tests/Statement/SelectTest.php index f8a4d75..58d273c 100644 --- a/tests/Statement/SelectTest.php +++ b/tests/Statement/SelectTest.php @@ -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() diff --git a/tests/Statement/UpdateTest.php b/tests/Statement/UpdateTest.php index 1fe4b1e..b9a5491 100644 --- a/tests/Statement/UpdateTest.php +++ b/tests/Statement/UpdateTest.php @@ -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