Browse Source

Refactor ObjectRegistry unload

Michael Hoffmann 9 years ago
parent
commit
cacc214d1b

+ 16 - 1
src/Core/ObjectRegistry.php

@@ -284,7 +284,11 @@ abstract class ObjectRegistry
     public function set($objectName, $object)
     {
         list(, $name) = pluginSplit($objectName);
-        $this->unload($objectName);
+
+        // Just call unload if the object was loaded before */
+        if (array_key_exists($objectName, $this->_loaded)) {
+            $this->unload($objectName);
+        }
         if ($this instanceof EventDispatcherInterface && $object instanceof EventListenerInterface) {
             $this->eventManager()->on($object);
         }
@@ -302,8 +306,19 @@ abstract class ObjectRegistry
     public function unload($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;
         }
+
         $object = $this->_loaded[$objectName];
         if ($this instanceof EventDispatcherInterface && $object instanceof EventListenerInterface) {
             $this->eventManager()->off($object);

+ 30 - 0
tests/TestCase/Controller/ComponentRegistryTest.php

@@ -209,6 +209,36 @@ class ComponentRegistryTest extends TestCase
     }
 
     /**
+     * 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.
+     * @return void
+     */
+    public function testUnloadUnknown()
+    {
+        $this->Components->unload('Foo');
+    }
+
+    /**
+     * 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.
      *
      * @return void

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

@@ -363,6 +363,36 @@ class BehaviorRegistryTest extends TestCase
     }
 
     /**
+     * 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
+     */
+    public function testUnloadUnknown()
+    {
+        $this->Behaviors->unload('Foo');
+    }
+
+    /**
+     * 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.
+     *
+     * @expectedException \PHPUnit\Framework\Error\Error
+     * @expectedExceptionMessage Object "Plugin.Foo" was not loaded before. Remember to omit plugin prefixes.
+     * @return void
+     */
+    public function testUnloadUnknownPluginBehavior()
+    {
+        $this->Behaviors->unload('Plugin.Foo');
+    }
+
+    /**
      * Test setTable() method.
      *
      * @return void

+ 30 - 0
tests/TestCase/View/HelperRegistryTest.php

@@ -274,6 +274,36 @@ class HelperRegistryTest extends TestCase
     }
 
     /**
+     * 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.
+     * @return void
+     */
+    public function testUnloadUnknown()
+    {
+        $this->Helpers->unload('Foo');
+    }
+
+    /**
+     * 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"
      *
      * The addToAssertionCount call is to record that no exception was thrown