Skip to content

Commit

Permalink
Remove getBehavior method and change with behaviors() method to retur…
Browse files Browse the repository at this point in the history
…n the behavior registry instead of an array of loaded behaviors.

This brings the behaviors API closer to the associations API.
  • Loading branch information
Walther Lalk committed May 15, 2014
1 parent 41cfd50 commit 9f59802
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 37 deletions.
26 changes: 5 additions & 21 deletions src/ORM/Table.php
Expand Up @@ -480,13 +480,11 @@ public function addBehavior($name, array $options = []) {
}

/**
* Removes a behavior.
*
* Removes a behavior from this table's behavior collection.
* Removes a behavior from this table's behavior registry.
*
* Example:
*
* Unload a behavior, with some settings.
* Remove a behavior from this table.
*
* {{{
* $this->removeBehavior('Tree');
Expand All @@ -502,15 +500,12 @@ public function removeBehavior($name) {
}

/**
* Get the list of Behaviors loaded.
*
* This method will return the *aliases* of the behaviors attached
* to this instance.
* Returns the behavior registry for this table.
*
* @return array
* @return \Cake\ORM\BehaviorRegistry
*/
public function behaviors() {
return $this->_behaviors->loaded();
return $this->_behaviors;
}

/**
Expand All @@ -523,17 +518,6 @@ public function hasBehavior($name) {
return $this->_behaviors->loaded($name);
}

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

/**
* Returns a association objected configured for the specified alias if any
*
Expand Down
20 changes: 4 additions & 16 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -1050,20 +1050,10 @@ public function testRemoveBehavior() {
*
* @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);
public function testBehaviors() {
$table = TableRegistry::get('article');
$result = $table->behaviors();
$this->assertInstanceOf('\Cake\ORM\BehaviorRegistry', $result);
}

/**
Expand Down Expand Up @@ -2255,10 +2245,8 @@ public function testMagicFindAllOr() {
*/
public function testBehaviorIntrospection() {
$table = TableRegistry::get('users');
$this->assertEquals([], $table->behaviors(), 'no loaded behaviors');

$table->addBehavior('Timestamp');
$this->assertEquals(['Timestamp'], $table->behaviors(), 'Should have loaded behavior');
$this->assertTrue($table->hasBehavior('Timestamp'), 'should be true on loaded behavior');
$this->assertFalse($table->hasBehavior('Tree'), 'should be false on unloaded behavior');
}
Expand Down

0 comments on commit 9f59802

Please sign in to comment.