Skip to content

Commit

Permalink
Adding a test case for using a custom finder for associations
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Aug 23, 2014
1 parent b82a09f commit 684d556
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
19 changes: 19 additions & 0 deletions tests/TestCase/ORM/QueryTest.php
Expand Up @@ -1770,6 +1770,25 @@ public function testFormatDeepDistantAssociationRecords() {
$this->assertEquals($expected, $query->toArray());
}

/**
* Tests that custom finders are applied to associations when using the proxies
*
* @return void
*/
public function testCustomFinderInBelongsTo() {
$table = TableRegistry::get('ArticlesTags');
$table->belongsTo('Articles', [
'className' => 'TestApp\Model\Table\ArticlesTable',
'finder' => 'published'
]);
$result = $table->find()->contain('Articles');
$this->assertCount(4, $result->extract('article')->filter()->toArray());
$table->Articles->updateAll(['published' => 'N'], ['1 = 1']);

$result = $table->find()->contain('Articles');
$this->assertCount(0, $result->extract('article')->filter()->toArray());
}

/**
* Tests that it is possible to attach more association when using a query
* builder for other associations
Expand Down
10 changes: 10 additions & 0 deletions tests/test_app/TestApp/Model/Table/ArticlesTable.php
Expand Up @@ -25,6 +25,16 @@ public function initialize(array $config) {
$this->hasMany('ArticlesTags');
}

/**
* Find published
*
* @param Cake\ORM\Query $query The query
* @return Cake\ORM\Query
*/
public function findPublished($query) {
return $query->where(['published' => 'Y']);
}

/**
* Example public method
*
Expand Down

0 comments on commit 684d556

Please sign in to comment.