Skip to content

Commit 3bb1ad3

Browse files
committed
Add tests for applying an order with magic finder.
1 parent 1bc729a commit 3bb1ad3

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

Cake/Test/TestCase/ORM/TableTest.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
use Cake\Core\Configure;
2020
use Cake\Database\ConnectionManager;
21+
use Cake\Database\Expression\OrderByExpression;
2122
use Cake\Database\Expression\QueryExpression;
2223
use Cake\ORM\Table;
2324
use Cake\ORM\TableRegistry;
@@ -2023,6 +2024,21 @@ public function testMagicFindFirstOr() {
20232024
$this->assertEquals($expected, $result->clause('where'));
20242025
}
20252026

2027+
/**
2028+
* Test setting order by clauses on magic finders.
2029+
*
2030+
* @return void
2031+
*/
2032+
public function testMagicFindFirstOrderBy() {
2033+
$table = TableRegistry::get('Users');
2034+
2035+
$result = $table->findByAuthorIdOrPublished(1, 'Y', ['id' => 'DESC']);
2036+
$this->assertInstanceOf('Cake\ORM\Query', $result);
2037+
$this->assertEquals(1, $result->clause('limit'));
2038+
$expected = new OrderByExpression(['id' => 'DESC']);
2039+
$this->assertEquals($expected, $result->clause('order'));
2040+
}
2041+
20262042

20272043
/**
20282044
* Test magic findAllByXX method.
@@ -2034,7 +2050,7 @@ public function testMagicFindAll() {
20342050

20352051
$result = $table->findAllByAuthorId(1);
20362052
$this->assertInstanceOf('Cake\ORM\Query', $result);
2037-
$this->assertEquals(null, $result->clause('limit'));
2053+
$this->assertNull($result->clause('limit'));
20382054
$expected = new QueryExpression(
20392055
['author_id' => 1],
20402056
['author_id' => 'integer'],
@@ -2053,7 +2069,7 @@ public function testMagicFindAllAnd() {
20532069

20542070
$result = $table->findAllByAuthorIdAndPublished(1, 'Y');
20552071
$this->assertInstanceOf('Cake\ORM\Query', $result);
2056-
$this->assertEquals(null, $result->clause('limit'));
2072+
$this->assertNull($result->clause('limit'));
20572073
$expected = new QueryExpression(
20582074
['author_id' => 1, 'published' => 'Y']
20592075
);
@@ -2070,12 +2086,28 @@ public function testMagicFindAllOr() {
20702086

20712087
$result = $table->findAllByAuthorIdOrPublished(1, 'Y');
20722088
$this->assertInstanceOf('Cake\ORM\Query', $result);
2073-
$this->assertEquals(null, $result->clause('limit'));
2089+
$this->assertNull($result->clause('limit'));
20742090
$expected = new QueryExpression();
20752091
$expected->add(
20762092
['or' => ['author_id' => 1, 'published' => 'Y']]
20772093
);
20782094
$this->assertEquals($expected, $result->clause('where'));
2095+
$this->assertNull($result->clause('order'));
2096+
}
2097+
2098+
/**
2099+
* Test setting order by clauses on magic finders.
2100+
*
2101+
* @return void
2102+
*/
2103+
public function testMagicFindAllOrderBy() {
2104+
$table = TableRegistry::get('Users');
2105+
2106+
$result = $table->findAllByAuthorIdOrPublished(1, 'Y', ['id' => 'DESC']);
2107+
$this->assertInstanceOf('Cake\ORM\Query', $result);
2108+
$this->assertNull($result->clause('limit'));
2109+
$expected = new OrderByExpression(['id' => 'DESC']);
2110+
$this->assertEquals($expected, $result->clause('order'));
20792111
}
20802112

20812113
}

0 commit comments

Comments
 (0)