Skip to content

Commit

Permalink
Add test that covers callable usage with Query::order().
Browse files Browse the repository at this point in the history
  • Loading branch information
ndm2 committed Aug 23, 2018
1 parent 329b4b2 commit 9288415
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -16,6 +16,7 @@

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;
Expand Down Expand Up @@ -2001,6 +2002,73 @@ public function testSelectOrderByAssociativeArrayContainingExtraExpressions()
});
}

/**
* 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.
*
Expand Down

0 comments on commit 9288415

Please sign in to comment.