Skip to content

Commit

Permalink
Add BehaviorRegistry::className().
Browse files Browse the repository at this point in the history
This helps in behavior class resolution since core behaviors are
under ORM/Behavior while in app they are under Model/Behavior.
  • Loading branch information
ADmad committed Dec 2, 2017
1 parent 1785901 commit 5e8cdc5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/ORM/BehaviorRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,11 @@ public function setTable(Table $table)
/**
* Resolve a behavior classname.
*
* Part of the template method for Cake\Core\ObjectRegistry::load()
*
* @param string $class Partial classname to resolve.
* @return string|false Either the correct classname or false.
* @since 3.5.7
*/
protected function _resolveClassName($class)
public static function className($class)
{
$result = App::className($class, 'Model/Behavior', 'Behavior');
if (!$result) {
Expand All @@ -99,6 +98,19 @@ protected function _resolveClassName($class)
return $result;
}

/**
* Resolve a behavior classname.
*
* Part of the template method for Cake\Core\ObjectRegistry::load()
*
* @param string $class Partial classname to resolve.
* @return string|false Either the correct classname or false.
*/
protected function _resolveClassName($class)
{
return static::className($class);
}

/**
* Throws an exception when a behavior is missing.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/TestCase/ORM/BehaviorRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ public function tearDown()
parent::tearDown();
}

/**
* Test classname resolution.
*
* @return void
*/
public function testClassName()
{
Plugin::load('TestPlugin');

$expected = 'Cake\ORM\Behavior\TranslateBehavior';
$result = BehaviorRegistry::className('Translate');
$this->assertSame($expected, $result);

$expected = 'TestPlugin\Model\Behavior\PersisterOneBehavior';
$result = BehaviorRegistry::className('TestPlugin.PersisterOne');
$this->assertSame($expected, $result);
}

/**
* Test loading behaviors.
*
Expand Down

0 comments on commit 5e8cdc5

Please sign in to comment.