Browse Source

Throw an exception instead of an triggering error. Add event unload test to BehaviorRegistryTest

Michael Hoffmann 9 years ago
parent
commit
ed80cb61ed

+ 1 - 0
src/Console/HelperRegistry.php

@@ -60,6 +60,7 @@ class HelperRegistry extends ObjectRegistry
      * Throws an exception when a helper is missing.
      * Throws an exception when a helper is missing.
      *
      *
      * Part of the template method for Cake\Core\ObjectRegistry::load()
      * Part of the template method for Cake\Core\ObjectRegistry::load()
+     * and Cake\Core\ObjectRegistry::unload()
      *
      *
      * @param string $class The classname that is missing.
      * @param string $class The classname that is missing.
      * @param string $plugin The plugin the helper is missing in.
      * @param string $plugin The plugin the helper is missing in.

+ 1 - 0
src/Console/TaskRegistry.php

@@ -59,6 +59,7 @@ class TaskRegistry extends ObjectRegistry
      * Throws an exception when a task is missing.
      * Throws an exception when a task is missing.
      *
      *
      * Part of the template method for Cake\Core\ObjectRegistry::load()
      * Part of the template method for Cake\Core\ObjectRegistry::load()
+     * and Cake\Core\ObjectRegistry::unload()
      *
      *
      * @param string $class The classname that is missing.
      * @param string $class The classname that is missing.
      * @param string $plugin The plugin the task is missing in.
      * @param string $plugin The plugin the task is missing in.

+ 1 - 0
src/Controller/ComponentRegistry.php

@@ -88,6 +88,7 @@ class ComponentRegistry extends ObjectRegistry implements EventDispatcherInterfa
      * Throws an exception when a component is missing.
      * Throws an exception when a component is missing.
      *
      *
      * Part of the template method for Cake\Core\ObjectRegistry::load()
      * Part of the template method for Cake\Core\ObjectRegistry::load()
+     * and Cake\Core\ObjectRegistry::unload()
      *
      *
      * @param string $class The classname that is missing.
      * @param string $class The classname that is missing.
      * @param string $plugin The plugin the component is missing in.
      * @param string $plugin The plugin the component is missing in.

+ 3 - 12
src/Core/ObjectRegistry.php

@@ -285,7 +285,7 @@ abstract class ObjectRegistry
     {
     {
         list(, $name) = pluginSplit($objectName);
         list(, $name) = pluginSplit($objectName);
 
 
-        // Just call unload if the object was loaded before */
+        // Just call unload if the object was loaded before
         if (array_key_exists($objectName, $this->_loaded)) {
         if (array_key_exists($objectName, $this->_loaded)) {
             $this->unload($objectName);
             $this->unload($objectName);
         }
         }
@@ -306,17 +306,8 @@ abstract class ObjectRegistry
     public function unload($objectName)
     public function unload($objectName)
     {
     {
         if (empty($this->_loaded[$objectName])) {
         if (empty($this->_loaded[$objectName])) {
-            $additionalInfo = '';
-            if (strpos($objectName, '.') !== false) {
-                $additionalInfo = ' Remember to omit plugin prefixes.';
-            }
-            trigger_error(sprintf(
-                'Object "%s" was not loaded before.%s',
-                $objectName,
-                $additionalInfo
-            ), E_USER_WARNING);
-
-            return;
+            list($plugin, $objectName) = pluginSplit($objectName);
+            $this->_throwMissingClassError($objectName, $plugin);
         }
         }
 
 
         $object = $this->_loaded[$objectName];
         $object = $this->_loaded[$objectName];

+ 1 - 0
src/ORM/BehaviorRegistry.php

@@ -100,6 +100,7 @@ class BehaviorRegistry extends ObjectRegistry implements EventDispatcherInterfac
      * 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()
+     * and Cake\Core\ObjectRegistry::unload()
      *
      *
      * @param string $class The classname that is missing.
      * @param string $class The classname that is missing.
      * @param string $plugin The plugin the behavior is missing in.
      * @param string $plugin The plugin the behavior is missing in.

+ 1 - 0
src/View/HelperRegistry.php

@@ -115,6 +115,7 @@ class HelperRegistry extends ObjectRegistry implements EventDispatcherInterface
      * Throws an exception when a helper is missing.
      * Throws an exception when a helper is missing.
      *
      *
      * Part of the template method for Cake\Core\ObjectRegistry::load()
      * Part of the template method for Cake\Core\ObjectRegistry::load()
+     * and Cake\Core\ObjectRegistry::unload()
      *
      *
      * @param string $class The classname that is missing.
      * @param string $class The classname that is missing.
      * @param string $plugin The plugin the helper is missing in.
      * @param string $plugin The plugin the helper is missing in.

+ 2 - 20
tests/TestCase/Controller/ComponentRegistryTest.php

@@ -211,11 +211,8 @@ class ComponentRegistryTest extends TestCase
     /**
     /**
      * Test that unloading a none existing component triggers an error.
      * Test that unloading a none existing component triggers an error.
      *
      *
-     * This should produce an "Object "Foo" was not loaded before." error
-     * which gets thrown as a \PHPUnit\Framework\Error\Error Exception by PHPUnit.
-     *
-     * @expectedException \PHPUnit\Framework\Error\Error
-     * @expectedExceptionMessage Object "Foo" was not loaded before.
+     * @expectedException \Cake\Controller\Exception\MissingComponentException
+     * @expectedExceptionMessage Component class FooComponent could not be found.
      * @return void
      * @return void
      */
      */
     public function testUnloadUnknown()
     public function testUnloadUnknown()
@@ -224,21 +221,6 @@ class ComponentRegistryTest extends TestCase
     }
     }
 
 
     /**
     /**
-     * Test that unloading a none existing plugin component triggers an error.
-     *
-     * This should produce an "Object "Plugin.Foo" was not loaded before. Remember to omit plugin prefixes." error
-     * which gets thrown as a \PHPUnit\Framework\Error\Error Exception by PHPUnit.
-     *
-     * @expectedException \PHPUnit\Framework\Error\Error
-     * @expectedExceptionMessage Object "Plugin.Foo" was not loaded before. Remember to omit plugin prefixes.
-     * @return void
-     */
-    public function testUnloadUnknownPluginComponent()
-    {
-        $this->Components->unload('Plugin.Foo');
-    }
-
-    /**
      * Test set.
      * Test set.
      *
      *
      * @return void
      * @return void

+ 11 - 15
tests/TestCase/ORM/BehaviorRegistryTest.php

@@ -365,31 +365,27 @@ class BehaviorRegistryTest extends TestCase
     /**
     /**
      * Test that unloading a none existing behavior triggers an error.
      * Test that unloading a none existing behavior triggers an error.
      *
      *
-     * This should produce an "Behavior "Foo" was not loaded before." error
-     * which gets thrown as a \PHPUnit\Framework\Error\Error Exception by PHPUnit.
-     *
-     * @expectedException \PHPUnit\Framework\Error\Error
-     * @expectedExceptionMessage Object "Foo" was not loaded before.
      * @return void
      * @return void
      */
      */
-    public function testUnloadUnknown()
+    public function testUnload()
     {
     {
-        $this->Behaviors->unload('Foo');
+        $this->Behaviors->load('Sluggable');
+        $this->Behaviors->unload('Sluggable');
+
+        $this->assertEmpty($this->Behaviors->loaded());
+        $this->assertCount(0, $this->EventManager->listeners('Model.beforeFind'));
     }
     }
 
 
     /**
     /**
-     * Test that unloading a none existing plugin behavior triggers an error.
-     *
-     * This should produce an "Behavior "Plugin.Foo" was not loaded before. Remember to omit plugin prefixes." error
-     * which gets thrown as a \PHPUnit\Framework\Error\Error Exception by PHPUnit.
+     * Test that unloading a none existing behavior triggers an error.
      *
      *
-     * @expectedException \PHPUnit\Framework\Error\Error
-     * @expectedExceptionMessage Object "Plugin.Foo" was not loaded before. Remember to omit plugin prefixes.
+     * @expectedException \Cake\ORM\Exception\MissingBehaviorException
+     * @expectedExceptionMessage Behavior class FooBehavior could not be found.
      * @return void
      * @return void
      */
      */
-    public function testUnloadUnknownPluginBehavior()
+    public function testUnloadUnknown()
     {
     {
-        $this->Behaviors->unload('Plugin.Foo');
+        $this->Behaviors->unload('Foo');
     }
     }
 
 
     /**
     /**

+ 2 - 20
tests/TestCase/View/HelperRegistryTest.php

@@ -276,11 +276,8 @@ class HelperRegistryTest extends TestCase
     /**
     /**
      * Test that unloading a none existing helper triggers an error.
      * Test that unloading a none existing helper triggers an error.
      *
      *
-     * This should produce an "Object "Foo" was not loaded before." error
-     * which gets thrown as a \PHPUnit\Framework\Error\Error Exception by PHPUnit.
-     *
-     * @expectedException \PHPUnit\Framework\Error\Error
-     * @expectedExceptionMessage Object "Foo" was not loaded before.
+     * @expectedException \Cake\View\Exception\MissingHelperException
+     * @expectedExceptionMessage Helper class FooHelper could not be found.
      * @return void
      * @return void
      */
      */
     public function testUnloadUnknown()
     public function testUnloadUnknown()
@@ -289,21 +286,6 @@ class HelperRegistryTest extends TestCase
     }
     }
 
 
     /**
     /**
-     * Test that unloading a none existing plugin helper triggers an error.
-     *
-     * This should produce an "Object "Plugin.Foo" was not loaded before. Remember to omit plugin prefixes." error
-     * which gets thrown as a \PHPUnit\Framework\Error\Error Exception by PHPUnit.
-     *
-     * @expectedException \PHPUnit\Framework\Error\Error
-     * @expectedExceptionMessage Object "Plugin.Foo" was not loaded before. Remember to omit plugin prefixes.
-     * @return void
-     */
-    public function testUnloadUnknownPluginHelper()
-    {
-        $this->Helpers->unload('Plugin.Foo');
-    }
-
-    /**
      * Loading a helper with no config should "just work"
      * Loading a helper with no config should "just work"
      *
      *
      * The addToAssertionCount call is to record that no exception was thrown
      * The addToAssertionCount call is to record that no exception was thrown