Skip to content

Commit

Permalink
Add methods for unloading and retrieving behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
Walther Lalk committed May 14, 2014
1 parent c4b66b4 commit 80edbe4
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/ORM/Table.php
Expand Up @@ -479,6 +479,28 @@ public function addBehavior($name, array $options = []) {
$this->_behaviors->load($name, $options);
}

/**
* Removes a behavior.
*
* Removes a behavior from this table's behavior collection.
*
* Example:
*
* Unload a behavior, with some settings.
*
* {{{
* $this->removeBehavior('Tree');
* }}}
*
* @param string $name The alias that the behavior was added with.
*
* @return void
* @see \Cake\ORM\Behavior
*/
public function removeBehavior($name) {
$this->_behaviors->unload($name);
}

/**
* Get the list of Behaviors loaded.
*
Expand All @@ -502,6 +524,17 @@ public function hasBehavior($name) {
}

/**
* Returns a behavior instance with the given alias.
*
* @param string $name The behavior alias to check.
*
* @return \Cake\ORM\Behavior or null
*/
public function getBehavior($name) {
return $this->_behaviors->{$name};
}

/**
* Returns a association objected configured for the specified alias if any
*
* @param string $name the alias used for the association
Expand Down
39 changes: 39 additions & 0 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -1027,6 +1027,45 @@ public function testAddBehavior() {
$table->addBehavior('Sluggable');
}

/**
* Test removing a behavior from a table.
*
* @return void
*/
public function testRemoveBehavior() {
$mock = $this->getMock('Cake\ORM\BehaviorRegistry', [], [], '', false);
$mock->expects($this->once())
->method('unload')
->with('Sluggable');

$table = new Table([
'table' => 'articles',
'behaviors' => $mock
]);
$table->removeBehavior('Sluggable');
}

/**
* Test getting a behavior instance from a table.
*
* @return void
*/
public function testGetBehavior() {
$returnValue = 'MockSlugInstance';
$mock = $this->getMock('Cake\ORM\BehaviorRegistry', [], [], '', false);
$mock->expects($this->once())
->method('__get')
->with('Sluggable')
->will($this->returnValue($returnValue));

$table = new Table([
'table' => 'articles',
'behaviors' => $mock
]);
$result = $table->getBehavior('Sluggable');
$this->assertSame($returnValue, $result);
}

/**
* Ensure exceptions are raised on missing behaviors.
*
Expand Down

0 comments on commit 80edbe4

Please sign in to comment.