Browse Source

Add BehaviorRegistry::className().

This helps in behavior class resolution since core behaviors are
under ORM/Behavior while in app they are under Model/Behavior.
ADmad 8 years ago
parent
commit
5e8cdc51e2
2 changed files with 33 additions and 3 deletions
  1. 15 3
      src/ORM/BehaviorRegistry.php
  2. 18 0
      tests/TestCase/ORM/BehaviorRegistryTest.php

+ 15 - 3
src/ORM/BehaviorRegistry.php

@@ -84,12 +84,11 @@ class BehaviorRegistry extends ObjectRegistry implements EventDispatcherInterfac
     /**
     /**
      * Resolve a behavior classname.
      * Resolve a behavior classname.
      *
      *
-     * Part of the template method for Cake\Core\ObjectRegistry::load()
-     *
      * @param string $class Partial classname to resolve.
      * @param string $class Partial classname to resolve.
      * @return string|false Either the correct classname or false.
      * @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');
         $result = App::className($class, 'Model/Behavior', 'Behavior');
         if (!$result) {
         if (!$result) {
@@ -100,6 +99,19 @@ class BehaviorRegistry extends ObjectRegistry implements EventDispatcherInterfac
     }
     }
 
 
     /**
     /**
+     * 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.
      * Throws an exception when a behavior is missing.
      *
      *
      * Part of the template method for Cake\Core\ObjectRegistry::load()
      * Part of the template method for Cake\Core\ObjectRegistry::load()

+ 18 - 0
tests/TestCase/ORM/BehaviorRegistryTest.php

@@ -52,6 +52,24 @@ class BehaviorRegistryTest extends TestCase
     }
     }
 
 
     /**
     /**
+     * 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.
      * Test loading behaviors.
      *
      *
      * @return void
      * @return void