Skip to content

Commit 684d556

Browse files
committed
Adding a test case for using a custom finder for associations
1 parent b82a09f commit 684d556

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

tests/TestCase/ORM/QueryTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,25 @@ public function testFormatDeepDistantAssociationRecords() {
17701770
$this->assertEquals($expected, $query->toArray());
17711771
}
17721772

1773+
/**
1774+
* Tests that custom finders are applied to associations when using the proxies
1775+
*
1776+
* @return void
1777+
*/
1778+
public function testCustomFinderInBelongsTo() {
1779+
$table = TableRegistry::get('ArticlesTags');
1780+
$table->belongsTo('Articles', [
1781+
'className' => 'TestApp\Model\Table\ArticlesTable',
1782+
'finder' => 'published'
1783+
]);
1784+
$result = $table->find()->contain('Articles');
1785+
$this->assertCount(4, $result->extract('article')->filter()->toArray());
1786+
$table->Articles->updateAll(['published' => 'N'], ['1 = 1']);
1787+
1788+
$result = $table->find()->contain('Articles');
1789+
$this->assertCount(0, $result->extract('article')->filter()->toArray());
1790+
}
1791+
17731792
/**
17741793
* Tests that it is possible to attach more association when using a query
17751794
* builder for other associations

tests/test_app/TestApp/Model/Table/ArticlesTable.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ public function initialize(array $config) {
2525
$this->hasMany('ArticlesTags');
2626
}
2727

28+
/**
29+
* Find published
30+
*
31+
* @param Cake\ORM\Query $query The query
32+
* @return Cake\ORM\Query
33+
*/
34+
public function findPublished($query) {
35+
return $query->where(['published' => 'Y']);
36+
}
37+
2838
/**
2939
* Example public method
3040
*

0 commit comments

Comments
 (0)