Browse Source

Using th edefault finder method in Association

Jose Lorenzo Rodriguez 11 years ago
parent
commit
94fef9ff4c
2 changed files with 45 additions and 1 deletions
  1. 2 1
      src/ORM/Association.php
  2. 43 0
      tests/TestCase/ORM/AssociationTest.php

+ 2 - 1
src/ORM/Association.php

@@ -515,7 +515,8 @@ abstract class Association {
  * @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());

+ 43 - 0
tests/TestCase/ORM/AssociationTest.php

@@ -24,6 +24,14 @@ use Cake\ORM\TableRegistry;
  */
 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 @@ class AssociationTest extends \Cake\TestSuite\TestCase {
 		$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()
+		);
+	}
+
 }