diff --git a/lib/Cake/ORM/Association.php b/lib/Cake/ORM/Association.php new file mode 100644 index 00000000000..be9ae095ebb --- /dev/null +++ b/lib/Cake/ORM/Association.php @@ -0,0 +1,106 @@ +{'_' . $property} = $options[$property]; + } + } + + $this->_name = $name; + $this->_options($options); + + if (empty($this->_className)) { + $this->_className = $this->_name; + } + } + + public function name($name = null) { + if ($name !== null) { + $this->_name = $name; + } + return $this->_name; + } + + public function repository(Table $table = null) { + if ($table === null && $this->_table) { + return $this->_table; + } + + if ($table !== null) { + return $this->_table = $table; + } + + if ($table === null && $this->_className !== null) { + $this->_table = Table::build( + $this->_name, + ['className' => $this->_className] + ); + } + return $this->_table; + } + + public function conditions($conditions = null) { + if ($conditions !== null) { + $this->_conditions = $conditions; + } + return $this->_conditions; + } + + public function foreignKey($key = null) { + if ($key !== null) { + $this->_foreignKey = $key; + } + return $this->_foreignKey; + } + + public function dependent($dependent = null) { + if ($dependent !== null) { + $this->_dependent = $dependent; + } + return $this->_dependent; + } + + public function canBeJoined() { + return $this->_canBeJoined; + } + + protected function _options(array $options) { + } + + public abstract function attachTo(Query $query, array $options = []); + +} diff --git a/lib/Cake/Test/TestCase/ORM/AssociationTest.php b/lib/Cake/Test/TestCase/ORM/AssociationTest.php new file mode 100644 index 00000000000..e9b800bebfe --- /dev/null +++ b/lib/Cake/Test/TestCase/ORM/AssociationTest.php @@ -0,0 +1,119 @@ + '\Cake\Test\TestCase\ORM\TestTable', + 'foreignKey' => 'a_key', + 'conditions' => ['field' => 'value'], + 'dependent' => true + ]; + $this->association = $this->getMock( + '\Cake\ORM\Association', + ['_options', 'attachTo'], + ['Foo', $config] + ); + } + +/** + * Tests that _options acts as a callback where subclasses can add their own + * initialization code based on the passed configuration array + * + * @return void + */ + public function testOptionsIsCalled() { + $options = ['foo' => 'bar']; + $this->association->expects($this->once())->method('_options')->with($options); + $this->association->__construct('Name', $options); + } + +/** + * Tests that name() returns the correct configure association name + * + * @return void + */ + public function testName() { + $this->assertEquals('Foo', $this->association->name()); + $this->association->name('Bar'); + $this->assertEquals('Bar', $this->association->name()); + } + +/** + * Tests that name() returns the correct configured value + * + * @return void + */ + public function testForeignKey() { + $this->assertEquals('a_key', $this->association->foreignKey()); + $this->association->foreignKey('another_key'); + $this->assertEquals('another_key', $this->association->foreignKey()); + } + +/** + * Tests that conditions() returns the correct configured value + * + * @return void + */ + public function testConditions() { + $this->assertEquals(['field' => 'value'], $this->association->conditions()); + $conds = ['another_key' => 'another value']; + $this->association->conditions($conds); + $this->assertEquals($conds, $this->association->conditions()); + } + +/** + * Tests that canBeJoined() returns the correct configured value + * + * @return void + */ + public function testCanBeJoined() { + $this->assertFalse($this->association->canBeJoined()); + } + +/** + * Tests that repository() returns the correct Table object + * + * @return void + */ + public function testRepository() { + $table = $this->association->repository(); + $this->assertInstanceOf(__NAMESPACE__ . '\TestTable', $table); + + $other = new Table; + $this->association->repository($other); + $this->assertSame($other, $this->association->repository()); + } + +}