Browse Source

Merge pull request #13468 from ravage84/add-plugin-convenience

Add a convenience method to load plugins
Mark Story 5 years ago
parent
commit
d6c2e33a97
2 changed files with 55 additions and 0 deletions
  1. 22 0
      src/Http/BaseApplication.php
  2. 33 0
      tests/TestCase/Http/BaseApplicationTest.php

+ 22 - 0
src/Http/BaseApplication.php

@@ -12,11 +12,13 @@
  * @since         3.3.0
  * @license       https://opensource.org/licenses/mit-license.php MIT License
  */
+
 namespace Cake\Http;
 
 use Cake\Core\App;
 use Cake\Core\BasePlugin;
 use Cake\Core\ConsoleApplicationInterface;
+use Cake\Core\Exception\MissingPluginException;
 use Cake\Core\HttpApplicationInterface;
 use Cake\Core\Plugin;
 use Cake\Core\PluginApplicationInterface;
@@ -109,6 +111,26 @@ abstract class BaseApplication implements
     }
 
     /**
+     * Add an optional plugin
+     *
+     * If it isn't available, ignore it.
+     *
+     * @param string|\Cake\Core\PluginInterface $name The plugin name or plugin object.
+     * @param array $config The configuration data for the plugin if using a string for $name
+     * @return $this
+     */
+    public function addOptionalPlugin($name, array $config = [])
+    {
+        try {
+            $this->addPlugin($name, $config);
+        } catch (MissingPluginException $e) {
+            // Do not halt if the plugin is missing
+        }
+
+        return $this;
+    }
+
+    /**
      * Get the plugin collection in use.
      *
      * @return \Cake\Core\PluginCollection

+ 33 - 0
tests/TestCase/Http/BaseApplicationTest.php

@@ -31,6 +31,8 @@ use TestPlugin\Plugin as TestPlugin;
 
 /**
  * Base application test.
+ *
+ * @coversDefaultClass \Cake\Http\BaseApplication
  */
 class BaseApplicationTest extends TestCase
 {
@@ -261,4 +263,35 @@ class BaseApplicationTest extends TestCase
             $this->assertContains('route class', $e->getMessage());
         }
     }
+
+    /**
+     * Tests that loading a non existing plugin through addOptionalPlugin() does not throw an exception
+     *
+     * @return void
+     * @covers ::addOptionalPlugin
+     */
+    public function testAddOptionalPluginLoadingNonExistingPlugin()
+    {
+        $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
+        $pluginCountBefore = count($app->getPlugins());
+        $nonExistingPlugin = 'NonExistingPlugin';
+        $app->addOptionalPlugin($nonExistingPlugin);
+        $pluginCountAfter = count($app->getPlugins());
+        $this->assertSame($pluginCountBefore, $pluginCountAfter);
+    }
+
+    /**
+     * Tests that loading an existing plugin through addOptionalPlugin() works
+     *
+     * @return void
+     * @covers ::addOptionalPlugin
+     */
+    public function testAddOptionalPluginLoadingNonExistingPluginValid()
+    {
+        $app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
+        $app->addOptionalPlugin(TestPlugin::class);
+
+        $this->assertCount(1, $app->getPlugins());
+        $this->assertTrue($app->getPlugins()->has('TestPlugin'));
+    }
 }