Browse Source

Add more tests around PluginApplication methods.

Fix mistakes as `__DIR__` is not going to work.
Mark Story 8 years ago
parent
commit
c6a7aea958

+ 4 - 4
src/Core/PluginApp.php

@@ -231,9 +231,9 @@ class PluginApp implements PluginInterface
      */
     public function routes($routes)
     {
-        $routes = __DIR__ . 'config' . DS . 'routes.php';
-        if (file_exists($routes)) {
-            require_once $routes;
+        $path = $this->getConfigPath() . DS . 'routes.php';
+        if (file_exists($path)) {
+            require_once $path;
         }
     }
 
@@ -242,7 +242,7 @@ class PluginApp implements PluginInterface
      */
     public function bootstrap()
     {
-        $bootstrap = __DIR__ . 'config' . DS . 'bootstrap.php';
+        $bootstrap = $this->getConfigPath() . DS . 'bootstrap.php';
         if (file_exists($bootstrap)) {
             require_once $bootstrap;
         }

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

@@ -1,9 +1,14 @@
 <?php
 namespace Cake\Test\TestCase;
 
+use Cake\Core\Configure;
+use Cake\Core\Plugin;
 use Cake\Http\BaseApplication;
+use Cake\Http\MiddlewareQueue;
 use Cake\Http\Response;
 use Cake\Http\ServerRequestFactory;
+use Cake\Routing\RouteBuilder;
+use Cake\Routing\RouteCollection;
 use Cake\TestSuite\TestCase;
 use InvalidArgumentException;
 use TestPlugin\Plugin as TestPlugin;
@@ -25,6 +30,12 @@ class BaseApplicationTest extends TestCase
         $this->path = dirname(dirname(__DIR__));
     }
 
+    public function tearDown()
+    {
+        parent::tearDown();
+        Plugin::unload();
+    }
+
     /**
      * Integration test for a simple controller.
      *
@@ -74,4 +85,69 @@ class BaseApplicationTest extends TestCase
         $this->assertCount(1, $app->getPlugins());
         $this->assertTrue($app->getPlugins()->has('TestPlugin'));
     }
+
+    public function testPluginEvents()
+    {
+        $app = $this->getMockForAbstractClass(
+            BaseApplication::class,
+            [$this->path]
+        );
+        $start = $app->getEventManager();
+        $this->assertCount(0, $start->listeners('TestPlugin.load'));
+
+        $app->addPlugin(TestPlugin::class);
+        $this->assertNull($app->pluginEvents());
+
+        $after = $app->getEventManager();
+        $this->assertSame($after, $start);
+        $this->assertCount(1, $after->listeners('TestPlugin.load'));
+    }
+
+    public function testPluginMiddleware()
+    {
+        $start = new MiddlewareQueue();
+        $app = $this->getMockForAbstractClass(
+            BaseApplication::class,
+            [$this->path]
+        );
+        $app->addPlugin(TestPlugin::class);
+
+        $after = $app->pluginMiddleware($start);
+        $this->assertSame($start, $after);
+        $this->assertCount(1, $after);
+    }
+
+    public function testPluginRoutes()
+    {
+        $collection = new RouteCollection();
+        $routes = new RouteBuilder($collection, '/');
+        $app = $this->getMockForAbstractClass(
+            BaseApplication::class,
+            [$this->path]
+        );
+        $app->addPlugin(TestPlugin::class);
+
+        $result = $app->pluginRoutes($routes);
+        $this->assertSame($routes, $result);
+        $url = [
+            'plugin' => 'TestPlugin',
+            'controller' => 'TestPlugin',
+            'action' => 'index',
+            '_method' => 'GET'
+        ];
+        $this->assertNotEmpty($collection->match($url, []));
+    }
+
+    public function testPluginBootstrap()
+    {
+        $app = $this->getMockForAbstractClass(
+            BaseApplication::class,
+            [$this->path]
+        );
+        $app->addPlugin(TestPlugin::class);
+
+        $this->assertFalse(Configure::check('PluginTest.test_plugin.bootstrap'));
+        $this->assertNull($app->pluginBootstrap());
+        $this->assertTrue(Configure::check('PluginTest.test_plugin.bootstrap'));
+    }
 }

+ 16 - 0
tests/test_app/Plugin/TestPlugin/src/Plugin.php

@@ -14,8 +14,24 @@
 namespace TestPlugin;
 
 use Cake\Core\PluginApp;
+use Cake\Event\EventManagerInterface;
 
 class Plugin extends PluginApp
 {
+    public function events(EventManagerInterface $events)
+    {
+        $events->on('TestPlugin.load', function () {
+        });
 
+        return $events;
+    }
+
+    public function middleware($middleware)
+    {
+        $middleware->add(function ($req, $res, $next) {
+            return $next($req, $res);
+        });
+
+        return $middleware;
+    }
 }