Fixing some errors in the new plugin loader and adding some tests

This commit is contained in:
Jose Lorenzo Rodriguez 2011-04-26 00:56:19 -04:30
parent 8053436b35
commit 4f1a6baf87
6 changed files with 183 additions and 13 deletions

View file

@ -45,13 +45,13 @@ class CakePlugin {
if (is_array($plugin)) { if (is_array($plugin)) {
foreach ($plugin as $name => $conf) { foreach ($plugin as $name => $conf) {
list($name, $conf) = (is_numeric($name)) ? array($conf, $config) : array($name, $conf); list($name, $conf) = (is_numeric($name)) ? array($conf, $config) : array($name, $conf);
self::load($name, $config); self::load($name, $conf);
} }
return; return;
} }
$config += array('bootstrap' => false, 'routes' => false); $config += array('bootstrap' => false, 'routes' => false);
$underscored = Inflector::underscore($plugin); $underscored = Inflector::underscore($plugin);
if (empty($config[$path])) { if (empty($config['path'])) {
foreach (App::path('plugins') as $path) { foreach (App::path('plugins') as $path) {
if (is_dir($path . $underscored)) { if (is_dir($path . $underscored)) {
self::$_plugins[$plugin] = $config + array('path' => $path . $underscored . DS); self::$_plugins[$plugin] = $config + array('path' => $path . $underscored . DS);
@ -104,16 +104,14 @@ class CakePlugin {
} }
$path = self::path($plugin); $path = self::path($plugin);
if ($config['bootstrap'] === true && is_file($path . 'config' . DS . 'bootstrap.php')) { if ($config['bootstrap'] === true) {
return include($path . 'config' . DS . 'bootstrap.php'); return include($path . 'config' . DS . 'bootstrap.php');
} }
$bootstrap = (array)$config['bootstrap']; $bootstrap = (array)$config['bootstrap'];
foreach ($bootstrap as $file) { foreach ($bootstrap as $file) {
if (is_file($path . 'config' . DS . $file . '.php')) {
include $path . 'config' . DS . $file . '.php'; include $path . 'config' . DS . $file . '.php';
} }
}
return true; return true;
} }
@ -129,11 +127,7 @@ class CakePlugin {
if ($config['routes'] === false) { if ($config['routes'] === false) {
return false; return false;
} }
$path = include self::path($plugin) . 'config' . DS . 'routes.php'; return (bool) include self::path($plugin) . 'config' . DS . 'routes.php';
if (is_file($path)) {
include $path;
}
return true;
} }
/** /**
@ -141,7 +135,7 @@ class CakePlugin {
* *
* @return array list of plugins that have been loaded * @return array list of plugins that have been loaded
*/ */
public static function enabled() { public static function loaded() {
return array_keys(self::$_plugins); return array_keys(self::$_plugins);
} }
@ -155,7 +149,7 @@ class CakePlugin {
if (is_null($plugin)) { if (is_null($plugin)) {
self::$_plugins = array(); self::$_plugins = array();
} else { } else {
unset($_plugins[$plugin]); unset(self::$_plugins[$plugin]);
} }
} }
} }

View file

@ -0,0 +1,168 @@
<?php
App::uses('CakePlugin', 'Core');
/**
* CakePluginTest class
*
*/
class CakePluginTest extends CakeTestCase {
/**
* Sets the plugins folder for this test
*
* @return void
*/
public function setUp() {
App::build(array(
'plugins' => array(CAKE_TESTS . 'test_app' . DS . 'plugins' . DS)
), true);
}
/**
* Reverts the changes done to the environment while testing
*
* @return void
*/
public function tearDown() {
App::build();
CakePlugin::unload();
Configure::delete('CakePluginTest');
}
/**
* Tests loading a single plugin
*
* @return void
*/
public function testLoadSingle() {
CakePlugin::load('TestPlugin');
$expected = array('TestPlugin');
$this->assertEquals($expected, CakePlugin::loaded());
}
/**
* Tests unloading plugins
*
* @return void
*/
public function testUnload() {
CakePlugin::load('TestPlugin');
$expected = array('TestPlugin');
$this->assertEquals($expected, CakePlugin::loaded());
CakePlugin::unload('TestPlugin');
$this->assertEquals(array(), CakePlugin::loaded());
CakePlugin::load('TestPlugin');
$expected = array('TestPlugin');
$this->assertEquals($expected, CakePlugin::loaded());
CakePlugin::unload('TestFakePlugin');
$this->assertEquals($expected, CakePlugin::loaded());
}
/**
* Tests loading a plugin and its bootstrap file
*
* @return void
*/
public function testLoadSingleWithBootstrap() {
CakePlugin::load('TestPlugin', array('bootstrap' => true));
$expected = array('TestPlugin');
$this->assertEquals($expected, CakePlugin::loaded());
$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
}
/**
* Tests loading a plugin with bootstrap file and routes file
*
* @return void
*/
public function testLoadSingleWithBootstrapAndRoutes() {
CakePlugin::load('TestPlugin', array('bootstrap' => true, 'routes' => true));
$expected = array('TestPlugin');
$this->assertEquals($expected, CakePlugin::loaded());
$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
$this->assertEquals('loaded plugin routes', Configure::read('CakePluginTest.test_plugin.routes'));
}
/**
* Tests loading multiple plugins at once
*
* @return void
*/
public function testLoadMultiple() {
CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
$expected = array('TestPlugin', 'TestPluginTwo');
$this->assertEquals($expected, CakePlugin::loaded());
}
/**
* Tests loading multiple plugins and their bootstrap files
*
* @return void
*/
public function testLoadMultipleWithDefaults() {
CakePlugin::load(array('TestPlugin', 'TestPluginTwo'), array('bootstrap' => true, 'routes' => false));
$expected = array('TestPlugin', 'TestPluginTwo');
$this->assertEquals($expected, CakePlugin::loaded());
$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
$this->assertEquals('loaded plugin two bootstrap', Configure::read('CakePluginTest.test_plugin_two.bootstrap'));
}
/**
* Tests loading multiple plugins with default loading params and some overrides
*
* @return void
*/
public function testLoadMultipleWithDefaultsAndOverride() {
CakePlugin::load(
array('TestPlugin', 'TestPluginTwo' => array('routes' => false)),
array('bootstrap' => true, 'routes' => true)
);
$expected = array('TestPlugin', 'TestPluginTwo');
$this->assertEquals($expected, CakePlugin::loaded());
$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
$this->assertEquals(null, Configure::read('CakePluginTest.test_plugin_two.bootstrap'));
}
/**
* Tests that it is possible to load multiple bootstrap files at once
*
* @return void
*/
public function testMultipleBootstrapFiles() {
CakePlugin::load('TestPlugin', array('bootstrap' => array('bootstrap', 'custom_config')));
$expected = array('TestPlugin');
$this->assertEquals($expected, CakePlugin::loaded());
$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
}
public function testCallbackBootstrap() {
CakePlugin::load('TestPlugin', array('bootstrap' => array($this, 'pluginBootstrap')));
$expected = array('TestPlugin');
$this->assertEquals($expected, CakePlugin::loaded());
$this->assertEquals('called plugin bootstrap callback', Configure::read('CakePluginTest.test_plugin.bootstrap'));
}
/**
* Tests that loading a missing routes file throws a warning
*
* @return void
* @expectedException PHPUNIT_FRAMEWORK_ERROR_WARNING
*/
public function testLoadMultipleWithDefaultsMissingFile() {
CakePlugin::load(array('TestPlugin', 'TestPluginTwo'), array('bootstrap' => true, 'routes' => true));
}
/**
* Auxiliary function to test plugin bootstrap callbacks
*
* @return void
*/
public function pluginBootstrap() {
Configure::write('CakePluginTest.test_plugin.bootstrap', 'called plugin bootstrap callback');
}
}

View file

@ -0,0 +1,2 @@
<?php
Configure::write('CakePluginTest.test_plugin.bootstrap', 'loaded plugin bootstrap');

View file

@ -0,0 +1,2 @@
<?php
Configure::write('CakePluginTest.test_plugin.custom', 'loaded plugin custom config');

View file

@ -0,0 +1,2 @@
<?php
Configure::write('CakePluginTest.test_plugin.routes', 'loaded plugin routes');

View file

@ -0,0 +1,2 @@
<?php
Configure::write('CakePluginTest.test_plugin_two.bootstrap', 'loaded plugin two bootstrap');