From 94fef9ff4c541dde30da6d613ed5e60e24dbebf3 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Fri, 22 Aug 2014 11:58:55 +0200 Subject: [PATCH] Using th edefault finder method in Association --- src/ORM/Association.php | 3 +- tests/TestCase/ORM/AssociationTest.php | 43 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/ORM/Association.php b/src/ORM/Association.php index 688bc4ab080..11e255b0c01 100644 --- a/src/ORM/Association.php +++ b/src/ORM/Association.php @@ -515,7 +515,8 @@ public function defaultRowValue($row, $joined) { * @see \Cake\ORM\Table::find() * @return \Cake\ORM\Query */ - public function find($type = 'all', array $options = []) { + public function find($type = null, array $options = []) { + $type = $type ?: $this->finder(); return $this->target() ->find($type, $options) ->where($this->conditions()); diff --git a/tests/TestCase/ORM/AssociationTest.php b/tests/TestCase/ORM/AssociationTest.php index fab87c0a1eb..4005e0b82e3 100644 --- a/tests/TestCase/ORM/AssociationTest.php +++ b/tests/TestCase/ORM/AssociationTest.php @@ -24,6 +24,14 @@ */ class TestTable extends Table { + public function initialize(array $config = []) { + $this->schema(['id' => ['type' => 'integer']]); + } + + public function findPublished($query) { + return $query->applyOptions(['this' => 'worked']); + } + } /** @@ -197,4 +205,39 @@ public function testInvalidStrategy() { $this->assertEquals('subquery', $this->association->strategy()); } + public function testFinderMethod() { + $this->assertEquals('all', $this->association->finder()); + $this->assertEquals('published', $this->association->finder('published')); + $this->assertEquals('published', $this->association->finder()); + } + + public function testFinderInConstructor() { + $config = [ + 'className' => '\Cake\Test\TestCase\ORM\TestTable', + 'foreignKey' => 'a_key', + 'conditions' => ['field' => 'value'], + 'dependent' => true, + 'sourceTable' => $this->source, + 'joinType' => 'INNER', + 'finder' => 'published' + ]; + $assoc = $this->getMock( + '\Cake\ORM\Association', + [ + '_options', 'attachTo', '_joinCondition', 'cascadeDelete', 'isOwningSide', + 'saveAssociated', 'eagerLoader', 'type' + ], + ['Foo', $config] + ); + $this->assertEquals('published', $assoc->finder()); + } + + public function testCustomFinderIsUsed() { + $this->association->finder('published'); + $this->assertEquals( + ['this' => 'worked'], + $this->association->find()->getOptions() + ); + } + }