Skip to content

Commit

Permalink
Implement simple methods in Associations class.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Dec 18, 2013
1 parent 06db4e5 commit 766f1db
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
33 changes: 31 additions & 2 deletions Cake/ORM/Associations.php
@@ -1,7 +1,5 @@
<?php
/**
* PHP Version 5.4
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand All @@ -16,6 +14,9 @@
*/
namespace Cake\ORM;

use Cake\ORM\Association;
use Cake\ORM\Entity;

/**
* A container/collection for association classes.
*
Expand All @@ -24,13 +25,41 @@
*/
class Associations {

protected $_items = [];

/**
* Add an association to the collection
*
* @param string $alias The association alias
* @param Association The association to add.
* @return void
*/
public function add($alias, Association $association) {
$this->_items[strtolower($alias)] = $association;
}

/**
* Fetch an attached association by name.
*
* @param string $alias The association alias to get.
* @return Association|null Either the association or null.
*/
public function get($alias) {
$alias = strtolower($alias);
if (isset($this->_items[$alias])) {
return $this->_items[$alias];
}
return null;
}

/**
* Check for an attached association by name.
*
* @param string $alias The association alias to get.
* @return boolean Whether or not the association exists.
*/
public function has($alias) {
return isset($this->_items[strtolower($alias)]);
}

public function drop($alias) {
Expand Down
57 changes: 57 additions & 0 deletions Cake/Test/TestCase/ORM/AssociationsTest.php
@@ -0,0 +1,57 @@
<?php
/**
* 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\BelongsTo;
use Cake\ORM\Associations;
use Cake\TestSuite\TestCase;

/**
* Associations test case.
*/
class AssociationsTest extends TestCase {

/**
* setup
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->associations = new Associations();
}

/**
* Test the simple add/has and get methods.
*
* @return void
*/
public function testAddHasAndGet() {
$this->assertFalse($this->associations->has('users'));
$this->assertFalse($this->associations->has('Users'));

$this->assertNull($this->associations->get('users'));
$this->assertNull($this->associations->get('Users'));

$belongsTo = new BelongsTo([]);
$this->assertNull($this->associations->add('Users', $belongsTo));
$this->assertTrue($this->associations->has('users'));
$this->assertTrue($this->associations->has('Users'));

$this->assertSame($belongsTo, $this->associations->get('users'));
$this->assertSame($belongsTo, $this->associations->get('Users'));
}

}

0 comments on commit 766f1db

Please sign in to comment.