From 38c909002f9b6423da3b57492bf73dcde12a0aa9 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Sun, 14 Jan 2024 09:38:20 +0700 Subject: [PATCH] Execute `Query` without `->from()` (#760) --- tests/AbstractQueryBuilderTest.php | 53 ++++++++++++++++-------------- tests/Common/CommonQueryTest.php | 11 +++++++ 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/tests/AbstractQueryBuilderTest.php b/tests/AbstractQueryBuilderTest.php index 938974a47..a5a0169a7 100644 --- a/tests/AbstractQueryBuilderTest.php +++ b/tests/AbstractQueryBuilderTest.php @@ -246,10 +246,12 @@ public function testBuildCondition( [$sql, $params] = $db->getQueryBuilder()->build($query); - $replacedQuotes = DbHelper::replaceQuotes((string) $expected, $db->getDriverName()); - - $this->assertIsString($replacedQuotes); - $this->assertEquals('SELECT *' . (empty($expected) ? '' : ' WHERE ' . $replacedQuotes), $sql); + $this->assertEquals( + 'SELECT *' + . ($db->getDriverName() === 'oci' ? ' FROM DUAL' : '') + . (empty($expected) ? '' : ' WHERE ' . DbHelper::replaceQuotes($expected, $db->getDriverName())), + $sql + ); $this->assertEquals($expectedParams, $params); } @@ -349,9 +351,9 @@ public function testBuildFilterCondition(array $condition, string $expected, arr [$sql, $params] = $qb->build($query); $this->assertSame( - 'SELECT *' . ( - empty($expected) ? '' : ' WHERE ' . DbHelper::replaceQuotes($expected, $db->getDriverName()) - ), + 'SELECT *' + . ($db->getDriverName() === 'oci' ? ' FROM DUAL' : '') + . (empty($expected) ? '' : ' WHERE ' . DbHelper::replaceQuotes($expected, $db->getDriverName())), $sql, ); $this->assertSame($expectedParams, $params); @@ -472,10 +474,12 @@ public function testBuildLikeCondition( [$sql, $params] = $db->getQueryBuilder()->build($query); - $replacedQuotes = DbHelper::replaceQuotes($expected, $db->getDriverName()); - - $this->assertIsString($replacedQuotes); - $this->assertSame('SELECT *' . (empty($expected) ? '' : ' WHERE ' . $replacedQuotes), $sql); + $this->assertSame( + 'SELECT *' + . ($db->getDriverName() === 'oci' ? ' FROM DUAL' : '') + . (empty($expected) ? '' : ' WHERE ' . DbHelper::replaceQuotes($expected, $db->getDriverName())), + $sql + ); $this->assertSame($expectedParams, $params); } @@ -1306,12 +1310,13 @@ public function testBuildWithSelectOption(): void [$sql, $params] = $qb->build($query); - $this->assertSame( - <<getDriverName() === 'oci') { + $expected .= ' FROM DUAL'; + } + + $this->assertSame($expected, $sql); $this->assertSame([], $params); } @@ -1558,16 +1563,16 @@ public function testCreateIndex(string $sql, Closure $builder): void public function testCreateView(): void { $db = $this->getConnection(); - $qb = $db->getQueryBuilder(); + $expected = 'CREATE VIEW [[animal_view]] AS SELECT [[1]]'; + + if ($this->getDriverName() === 'oci') { + $expected .= ' FROM DUAL'; + } + $this->assertSame( - DbHelper::replaceQuotes( - <<getDriverName(), - ), + DbHelper::replaceQuotes($expected, $db->getDriverName()), $qb->createView('animal_view', (new query($db))->select('1')), ); } diff --git a/tests/Common/CommonQueryTest.php b/tests/Common/CommonQueryTest.php index a1a6bb1d0..9906d9109 100644 --- a/tests/Common/CommonQueryTest.php +++ b/tests/Common/CommonQueryTest.php @@ -81,4 +81,15 @@ public function testWithQueryRecursive() $db->close(); } + + public function testSelectWithoutFrom() + { + $db = $this->getConnection(); + + $query = (new Query($db))->select(new Expression('1')); + + $this->assertEquals(1, $query->scalar()); + + $db->close(); + } }