Skip to content

Commit

Permalink
Adding abstract association class to be used in table instances
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed May 10, 2013
1 parent 4789a69 commit 204029d
Show file tree
Hide file tree
Showing 2 changed files with 225 additions and 0 deletions.
106 changes: 106 additions & 0 deletions lib/Cake/ORM/Association.php
@@ -0,0 +1,106 @@
<?php
/**
* PHP Version 5.4
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\ORM;

abstract class Association {

protected $_name;

protected $_canBeJoined = false;

protected $_className;

protected $_foreignKey;

protected $_conditions = [];

protected $_dependent = false;

protected $_table;

public function __construct($name, array $options = []) {
$defaults = ['className', 'foreignKey', 'conditions', 'dependent'];
foreach ($defaults as $property) {
if (isset($options[$property])) {
$this->{'_' . $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 = []);

}
119 changes: 119 additions & 0 deletions lib/Cake/Test/TestCase/ORM/AssociationTest.php
@@ -0,0 +1,119 @@
<?php
/**
* PHP Version 5.4
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\ORM;

use Cake\ORM\Association;
use Cake\ORM\Table;

/**
* A Test double used to assert that default tables are created
*
**/
class TestTable extends Table {

}

/**
* Tests Association class
*
*/
class AssociationTest extends \Cake\TestSuite\TestCase {

public function setUp() {
$config = [
'className' => '\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());
}

}

0 comments on commit 204029d

Please sign in to comment.