|
|
@@ -16,6 +16,7 @@ namespace Cake\Test\TestCase\Database;
|
|
|
|
|
|
use Cake\Database\ExpressionInterface;
|
|
|
use Cake\Database\Expression\IdentifierExpression;
|
|
|
+use Cake\Database\Expression\QueryExpression;
|
|
|
use Cake\Database\Query;
|
|
|
use Cake\Database\StatementInterface;
|
|
|
use Cake\Database\Statement\StatementDecorator;
|
|
|
@@ -2002,6 +2003,73 @@ class QueryTest extends TestCase
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * Tests that order() works with closures.
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testSelectOrderByClosure()
|
|
|
+ {
|
|
|
+ $query = new Query($this->connection);
|
|
|
+ $query
|
|
|
+ ->select('*')
|
|
|
+ ->from('articles')
|
|
|
+ ->order(function ($exp, $q) use ($query) {
|
|
|
+ $this->assertInstanceOf(QueryExpression::class, $exp);
|
|
|
+ $this->assertSame($query, $q);
|
|
|
+
|
|
|
+ return ['id' => 'ASC'];
|
|
|
+ });
|
|
|
+
|
|
|
+ $this->assertQuotedQuery(
|
|
|
+ 'SELECT \* FROM <articles> ORDER BY <id> ASC',
|
|
|
+ $query->sql(),
|
|
|
+ !$this->autoQuote
|
|
|
+ );
|
|
|
+
|
|
|
+ $query = new Query($this->connection);
|
|
|
+ $query
|
|
|
+ ->select('*')
|
|
|
+ ->from('articles')
|
|
|
+ ->order(function ($exp) {
|
|
|
+ return [$exp->add(['id % 2 = 0']), 'title' => 'ASC'];
|
|
|
+ });
|
|
|
+
|
|
|
+ $this->assertQuotedQuery(
|
|
|
+ 'SELECT \* FROM <articles> ORDER BY id % 2 = 0, <title> ASC',
|
|
|
+ $query->sql(),
|
|
|
+ !$this->autoQuote
|
|
|
+ );
|
|
|
+
|
|
|
+ $query = new Query($this->connection);
|
|
|
+ $query
|
|
|
+ ->select('*')
|
|
|
+ ->from('articles')
|
|
|
+ ->order(function ($exp) {
|
|
|
+ return $exp->add('a + b');
|
|
|
+ });
|
|
|
+
|
|
|
+ $this->assertQuotedQuery(
|
|
|
+ 'SELECT \* FROM <articles> ORDER BY a \+ b',
|
|
|
+ $query->sql(),
|
|
|
+ !$this->autoQuote
|
|
|
+ );
|
|
|
+
|
|
|
+ $query = new Query($this->connection);
|
|
|
+ $query
|
|
|
+ ->select('*')
|
|
|
+ ->from('articles')
|
|
|
+ ->order(function ($exp, $q) {
|
|
|
+ return $q->func()->sum('a');
|
|
|
+ });
|
|
|
+
|
|
|
+ $this->assertQuotedQuery(
|
|
|
+ 'SELECT \* FROM <articles> ORDER BY SUM\(a\)',
|
|
|
+ $query->sql(),
|
|
|
+ !$this->autoQuote
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* Test orderAsc() and its input types.
|
|
|
*
|
|
|
* @return void
|