Skip to content

Commit b5d6ec4

Browse files
committed
18263 - Fix method and finder map population when using BehaviorRegistry::set()
1 parent 51bded6 commit b5d6ec4

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/ORM/BehaviorRegistry.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,27 @@ protected function _getMethods(Behavior $instance, string $class, string $alias)
203203
return compact('methods', 'finders');
204204
}
205205

206+
/**
207+
* Set an object directly into the registry by name.
208+
*
209+
* @param string $name The name of the object to set in the registry.
210+
* @param \Cake\ORM\Behavior $object instance to store in the registry
211+
* @return $this
212+
*/
213+
public function set(string $name, object $object)
214+
{
215+
parent::set($name, $object);
216+
217+
$methods = $this->_getMethods($instance, $class, $alias);
218+
$this->_methodMap += $methods['methods'];
219+
$this->_finderMap += $methods['finders'];
220+
$methods = $this->_getMethods($object, get_class($object), $name);
221+
$this->_methodMap += $methods['methods'];
222+
$this->_finderMap += $methods['finders'];
223+
224+
return $this;
225+
}
226+
206227
/**
207228
* Remove an object from the registry.
208229
*

tests/TestCase/ORM/BehaviorRegistryTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Cake\TestSuite\TestCase;
2525
use LogicException;
2626
use RuntimeException;
27+
use TestApp\Model\Behavior\SluggableBehavior;
2728

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

213+
/**
214+
* Test set()
215+
*/
216+
public function testSet(): void
217+
{
218+
$this->Behaviors->set('Sluggable', new SluggableBehavior($this->Table, ['replacement' => '_']));
219+
220+
$this->assertEquals(['replacement' => '_'], $this->Behaviors->get('Sluggable')->getConfig());
221+
$this->assertTrue($this->Behaviors->hasMethod('slugify'));
222+
}
223+
212224
/**
213225
* test hasMethod()
214226
*/

0 commit comments

Comments
 (0)