Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/ORM/BehaviorRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,27 @@ protected function _getMethods(Behavior $instance, string $class, string $alias)
return compact('methods', 'finders');
}

/**
* Set an object directly into the registry by name.
*
* @param string $name The name of the object to set in the registry.
* @param \Cake\ORM\Behavior $object instance to store in the registry
* @return $this
*/
public function set(string $name, object $object)
{
parent::set($name, $object);

$methods = $this->_getMethods($instance, $class, $alias);
$this->_methodMap += $methods['methods'];
$this->_finderMap += $methods['finders'];
$methods = $this->_getMethods($object, get_class($object), $name);
$this->_methodMap += $methods['methods'];
$this->_finderMap += $methods['finders'];

return $this;
}

/**
* Remove an object from the registry.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/TestCase/ORM/BehaviorRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Cake\TestSuite\TestCase;
use LogicException;
use RuntimeException;
use TestApp\Model\Behavior\SluggableBehavior;

/**
* Test case for BehaviorRegistry.
Expand Down Expand Up @@ -209,6 +210,17 @@ public function testLoadDuplicateFinderAliasing(): void
$this->assertTrue($this->Behaviors->hasFinder('renamed'));
}

/**
* Test set()
*/
public function testSet(): void
{
$this->Behaviors->set('Sluggable', new SluggableBehavior($this->Table, ['replacement' => '_']));

$this->assertEquals(['replacement' => '_'], $this->Behaviors->get('Sluggable')->getConfig());
$this->assertTrue($this->Behaviors->hasMethod('slugify'));
}

/**
* test hasMethod()
*/
Expand Down
Loading