Skip to content

Commit 204029d

Browse files
committed
Adding abstract association class to be used in table instances
1 parent 4789a69 commit 204029d

File tree

2 files changed

+225
-0
lines changed

2 files changed

+225
-0
lines changed

lib/Cake/ORM/Association.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
/**
3+
* PHP Version 5.4
4+
*
5+
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6+
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7+
*
8+
* Licensed under The MIT License
9+
* For full copyright and license information, please see the LICENSE.txt
10+
* Redistributions of files must retain the above copyright notice.
11+
*
12+
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13+
* @link http://cakephp.org CakePHP(tm) Project
14+
* @since CakePHP(tm) v 3.0.0
15+
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
16+
*/
17+
namespace Cake\ORM;
18+
19+
abstract class Association {
20+
21+
protected $_name;
22+
23+
protected $_canBeJoined = false;
24+
25+
protected $_className;
26+
27+
protected $_foreignKey;
28+
29+
protected $_conditions = [];
30+
31+
protected $_dependent = false;
32+
33+
protected $_table;
34+
35+
public function __construct($name, array $options = []) {
36+
$defaults = ['className', 'foreignKey', 'conditions', 'dependent'];
37+
foreach ($defaults as $property) {
38+
if (isset($options[$property])) {
39+
$this->{'_' . $property} = $options[$property];
40+
}
41+
}
42+
43+
$this->_name = $name;
44+
$this->_options($options);
45+
46+
if (empty($this->_className)) {
47+
$this->_className = $this->_name;
48+
}
49+
}
50+
51+
public function name($name = null) {
52+
if ($name !== null) {
53+
$this->_name = $name;
54+
}
55+
return $this->_name;
56+
}
57+
58+
public function repository(Table $table = null) {
59+
if ($table === null && $this->_table) {
60+
return $this->_table;
61+
}
62+
63+
if ($table !== null) {
64+
return $this->_table = $table;
65+
}
66+
67+
if ($table === null && $this->_className !== null) {
68+
$this->_table = Table::build(
69+
$this->_name,
70+
['className' => $this->_className]
71+
);
72+
}
73+
return $this->_table;
74+
}
75+
76+
public function conditions($conditions = null) {
77+
if ($conditions !== null) {
78+
$this->_conditions = $conditions;
79+
}
80+
return $this->_conditions;
81+
}
82+
83+
public function foreignKey($key = null) {
84+
if ($key !== null) {
85+
$this->_foreignKey = $key;
86+
}
87+
return $this->_foreignKey;
88+
}
89+
90+
public function dependent($dependent = null) {
91+
if ($dependent !== null) {
92+
$this->_dependent = $dependent;
93+
}
94+
return $this->_dependent;
95+
}
96+
97+
public function canBeJoined() {
98+
return $this->_canBeJoined;
99+
}
100+
101+
protected function _options(array $options) {
102+
}
103+
104+
public abstract function attachTo(Query $query, array $options = []);
105+
106+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
/**
3+
* PHP Version 5.4
4+
*
5+
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6+
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7+
*
8+
* Licensed under The MIT License
9+
* For full copyright and license information, please see the LICENSE.txt
10+
* Redistributions of files must retain the above copyright notice.
11+
*
12+
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13+
* @link http://cakephp.org CakePHP(tm) Project
14+
* @since CakePHP(tm) v 3.0.0
15+
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
16+
*/
17+
namespace Cake\Test\TestCase\ORM;
18+
19+
use Cake\ORM\Association;
20+
use Cake\ORM\Table;
21+
22+
/**
23+
* A Test double used to assert that default tables are created
24+
*
25+
**/
26+
class TestTable extends Table {
27+
28+
}
29+
30+
/**
31+
* Tests Association class
32+
*
33+
*/
34+
class AssociationTest extends \Cake\TestSuite\TestCase {
35+
36+
public function setUp() {
37+
$config = [
38+
'className' => '\Cake\Test\TestCase\ORM\TestTable',
39+
'foreignKey' => 'a_key',
40+
'conditions' => ['field' => 'value'],
41+
'dependent' => true
42+
];
43+
$this->association = $this->getMock(
44+
'\Cake\ORM\Association',
45+
['_options', 'attachTo'],
46+
['Foo', $config]
47+
);
48+
}
49+
50+
/**
51+
* Tests that _options acts as a callback where subclasses can add their own
52+
* initialization code based on the passed configuration array
53+
*
54+
* @return void
55+
*/
56+
public function testOptionsIsCalled() {
57+
$options = ['foo' => 'bar'];
58+
$this->association->expects($this->once())->method('_options')->with($options);
59+
$this->association->__construct('Name', $options);
60+
}
61+
62+
/**
63+
* Tests that name() returns the correct configure association name
64+
*
65+
* @return void
66+
*/
67+
public function testName() {
68+
$this->assertEquals('Foo', $this->association->name());
69+
$this->association->name('Bar');
70+
$this->assertEquals('Bar', $this->association->name());
71+
}
72+
73+
/**
74+
* Tests that name() returns the correct configured value
75+
*
76+
* @return void
77+
*/
78+
public function testForeignKey() {
79+
$this->assertEquals('a_key', $this->association->foreignKey());
80+
$this->association->foreignKey('another_key');
81+
$this->assertEquals('another_key', $this->association->foreignKey());
82+
}
83+
84+
/**
85+
* Tests that conditions() returns the correct configured value
86+
*
87+
* @return void
88+
*/
89+
public function testConditions() {
90+
$this->assertEquals(['field' => 'value'], $this->association->conditions());
91+
$conds = ['another_key' => 'another value'];
92+
$this->association->conditions($conds);
93+
$this->assertEquals($conds, $this->association->conditions());
94+
}
95+
96+
/**
97+
* Tests that canBeJoined() returns the correct configured value
98+
*
99+
* @return void
100+
*/
101+
public function testCanBeJoined() {
102+
$this->assertFalse($this->association->canBeJoined());
103+
}
104+
105+
/**
106+
* Tests that repository() returns the correct Table object
107+
*
108+
* @return void
109+
*/
110+
public function testRepository() {
111+
$table = $this->association->repository();
112+
$this->assertInstanceOf(__NAMESPACE__ . '\TestTable', $table);
113+
114+
$other = new Table;
115+
$this->association->repository($other);
116+
$this->assertSame($other, $this->association->repository());
117+
}
118+
119+
}

0 commit comments

Comments
 (0)