Skip to content

Commit

Permalink
Add more tests around PluginApplication methods.
Browse files Browse the repository at this point in the history
Fix mistakes as `__DIR__` is not going to work.
  • Loading branch information
markstory committed Feb 21, 2018
1 parent 6b4b114 commit c6a7aea
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Core/PluginApp.php
Expand Up @@ -231,9 +231,9 @@ protected function checkHook($hook)
*/
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;
}
}

Expand All @@ -242,7 +242,7 @@ public function routes($routes)
*/
public function bootstrap()
{
$bootstrap = __DIR__ . 'config' . DS . 'bootstrap.php';
$bootstrap = $this->getConfigPath() . DS . 'bootstrap.php';
if (file_exists($bootstrap)) {
require_once $bootstrap;
}
Expand Down
76 changes: 76 additions & 0 deletions 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;
Expand All @@ -25,6 +30,12 @@ public function setUp()
$this->path = dirname(dirname(__DIR__));
}

public function tearDown()
{
parent::tearDown();
Plugin::unload();
}

/**
* Integration test for a simple controller.
*
Expand Down Expand Up @@ -74,4 +85,69 @@ public function testAddPluginValid()
$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 changes: 16 additions & 0 deletions tests/test_app/Plugin/TestPlugin/src/Plugin.php
Expand Up @@ -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;
}
}

0 comments on commit c6a7aea

Please sign in to comment.