gnu-social/plugins/Irc/extlib/phergie/Tests/Phergie/Plugin/HandlerTest.php

737 lines
20 KiB
PHP
Raw Normal View History

2010-06-16 01:55:39 +01:00
<?php
/**
* Phergie
*
* PHP version 5
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.
* It is also available through the world-wide-web at this URL:
* http://phergie.org/license
*
* @category Phergie
* @package Phergie_Tests
* @author Phergie Development Team <team@phergie.org>
* @copyright 2008-2010 Phergie Development Team (http://phergie.org)
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie_Tests
*/
/**
* Unit test suite for Pherge_Plugin_Handler.
*
* @category Phergie
* @package Phergie_Tests
* @author Phergie Development Team <team@phergie.org>
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie_Tests
*/
class Phergie_Plugin_HandlerTest extends PHPUnit_Framework_TestCase
{
/**
* Plugin handler instance being tested
*
* @var Phergie_Plugin_Handler
*/
protected $handler;
2010-06-30 17:33:29 +01:00
/**
* Mock Phergie_Config instance passed to the plugin handler constructor
*
* @var Phergie_Config
*/
protected $config;
/**
* Mock Phergie_Event_Handler instance passed to the plugin handler
* constructor
*
* @var Phergie_Event_Handler
*/
protected $events;
/**
* Returns a mock plugin instance.
*
* @param string $name Optional short name for the mock plugin, defaults
* to 'TestPlugin'
* @param array $methods Optional list of methods to override
*
* @return Phergie_Plugin_Abstract
*/
protected function getMockPlugin($name = 'TestPlugin', array $methods = array())
{
$methods[] = 'getName';
$plugin = $this->getMock('Phergie_Plugin_Abstract', $methods);
$plugin
->expects($this->any())
->method('getName')
->will($this->returnValue($name));
return $plugin;
}
2010-06-16 01:55:39 +01:00
/**
* Sets up a new handler instance before each test.
*
* @return void
*/
public function setUp()
{
2010-06-30 17:33:29 +01:00
$this->config = $this->getMock('Phergie_Config');
$this->events = $this->getMock('Phergie_Event_Handler');
2010-06-16 01:55:39 +01:00
$this->handler = new Phergie_Plugin_Handler(
2010-06-30 17:33:29 +01:00
$this->config,
$this->events
2010-06-16 01:55:39 +01:00
);
}
/**
2010-06-30 17:33:29 +01:00
* Tests iterability of the plugin handler.
2010-06-16 01:55:39 +01:00
*
* @return void
*/
2010-06-30 17:33:29 +01:00
public function testImplementsIteratorAggregate()
2010-06-16 01:55:39 +01:00
{
2010-06-30 17:33:29 +01:00
$reflection = new ReflectionObject($this->handler);
$this->assertTrue(
$reflection->implementsInterface('IteratorAggregate'),
'Handler does not implement IteratorAggregate'
);
$this->assertType(
'Iterator',
$this->handler->getIterator(),
'getIterator() must return an iterator'
);
2010-06-16 01:55:39 +01:00
}
/**
2010-06-30 17:33:29 +01:00
* Tests countability of the plugin handler.
2010-06-16 01:55:39 +01:00
*
* @return void
*/
2010-06-30 17:33:29 +01:00
public function testImplementsCountable()
2010-06-16 01:55:39 +01:00
{
$reflection = new ReflectionObject($this->handler);
2010-06-30 17:33:29 +01:00
2010-06-16 01:55:39 +01:00
$this->assertTrue(
2010-06-30 17:33:29 +01:00
$reflection->implementsInterface('Countable'),
'Handler does not implement Countable'
2010-06-16 01:55:39 +01:00
);
$this->assertType(
2010-06-30 17:33:29 +01:00
'int',
count($this->handler),
'count() must return an integer'
2010-06-16 01:55:39 +01:00
);
}
/**
2010-06-30 17:33:29 +01:00
* Tests the plugin handler exposing added plugins as instance
* properties of the handler via isset().
2010-06-16 01:55:39 +01:00
*
* @return void
*/
2010-06-30 17:33:29 +01:00
public function testImplementsIsset()
{
$pluginName = 'TestPlugin';
$this->assertFalse(isset($this->handler->{$pluginName}));
$plugin = $this->getMockPlugin($pluginName);
$this->handler->addPlugin($plugin);
$this->assertTrue(isset($this->handler->{$pluginName}));
}
/**
* Tests the plugin handler exposing added plugins as instance
* properties of the handler.
*
* @depends testImplementsIsset
* @return void
*/
public function testImplementsGet()
{
$plugin = $this->getMockPlugin();
$this->handler->addPlugin($plugin);
$name = $plugin->getName();
$getPlugin = $this->handler->getPlugin($name);
$this->assertTrue(isset($this->handler->$name));
$get = $this->handler->$name;
$this->assertSame($getPlugin, $get);
}
/**
* Tests the plugin handler allowing for plugin removal via unset().
*
* @depends testImplementsGet
* @return void
*/
public function testImplementsUnset()
{
$plugin = $this->getMockPlugin();
$this->handler->addPlugin($plugin);
unset($this->handler->{$plugin->getName()});
$this->assertFalse($this->handler->hasPlugin($plugin->getName()));
}
/**
* Tests the plugin handler executing a callback on all contained
2010-08-05 00:02:24 +01:00
* plugins.
2010-06-30 17:33:29 +01:00
*
* @return void
*/
2010-08-05 00:02:24 +01:00
public function testImplementsCall()
2010-06-16 01:55:39 +01:00
{
2010-06-30 17:33:29 +01:00
foreach (range(1, 2) as $index) {
$plugin = $this->getMockPlugin('TestPlugin' . $index, array('callback'));
$plugin
->expects($this->once())
->method('callback');
$this->handler->addPlugin($plugin);
2010-06-16 01:55:39 +01:00
}
2010-06-30 17:33:29 +01:00
$this->assertTrue($this->handler->callback());
2010-06-16 01:55:39 +01:00
}
2010-06-30 17:33:29 +01:00
2010-06-16 01:55:39 +01:00
/**
2010-06-30 17:33:29 +01:00
* Tests a newly instantiated handler not having plugins associated with
* it.
2010-06-16 01:55:39 +01:00
*
2010-06-30 17:33:29 +01:00
* @depends testImplementsCountable
2010-06-16 01:55:39 +01:00
* @return void
*/
2010-06-30 17:33:29 +01:00
public function testEmptyHandlerHasNoPlugins()
{
$this->assertEquals(0, count($this->handler));
}
/**
* Tests a newly instantiated handler not having autoloading enabled by
* default.
*
* @return void
*/
public function testGetAutoloadDefaultsToNotAutoload()
2010-06-16 01:55:39 +01:00
{
$this->assertFalse($this->handler->getAutoload());
}
/**
2010-06-30 17:33:29 +01:00
* Tests setAutoload().
*
* @depends testGetAutoloadDefaultsToNotAutoload
* @return void
*/
public function testSetAutoload()
{
$this->assertSame(
$this->handler->setAutoload(true),
$this->handler,
'setAutoload() does not provide a fluent interface'
);
$this->assertTrue(
$this->handler->getAutoload(),
'setAutoload() had no effect on getAutoload()'
);
}
/**
* Tests addPath() providing a fluent interface.
2010-06-16 01:55:39 +01:00
*
* @return void
*/
public function testAddPathProvidesFluentInterface()
{
$handler = $this->handler->addPath(dirname(__FILE__));
$this->assertSame($this->handler, $handler);
}
/**
2010-06-30 17:33:29 +01:00
* Tests addPath() throwing an exception when it cannot read the
* directory.
2010-06-16 01:55:39 +01:00
*
* @return void
*/
public function testAddPathThrowsExceptionOnUnreadableDirectory()
{
try {
$this->handler->addPath('/an/unreadable/directory/path');
} catch(Phergie_Plugin_Exception $e) {
$this->assertEquals(
Phergie_Plugin_Exception::ERR_DIRECTORY_NOT_READABLE,
$e->getCode()
);
return;
}
2010-06-30 17:33:29 +01:00
$this->fail('An expected exception has not been raised');
2010-06-16 01:55:39 +01:00
}
/**
2010-06-30 17:33:29 +01:00
* Tests adding a path to the plugin handler.
2010-06-16 01:55:39 +01:00
*
* @return void
*/
public function testAddPath()
{
2010-06-30 17:33:29 +01:00
$pluginName = 'Mock';
2010-06-16 01:55:39 +01:00
try {
2010-06-30 17:33:29 +01:00
$this->handler->addPlugin($pluginName);
2010-06-16 01:55:39 +01:00
} catch(Phergie_Plugin_Exception $e) {
$this->assertEquals(
Phergie_Plugin_Exception::ERR_CLASS_NOT_FOUND,
$e->getCode()
);
}
2010-06-30 17:33:29 +01:00
if (!isset($e)) {
$this->fail('Plugin loaded, path was already present');
}
$this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
try {
$this->handler->addPlugin($pluginName);
} catch(Phergie_Plugin_Exception $e) {
$this->fail('Added path, plugin still not found');
}
2010-06-16 01:55:39 +01:00
}
/**
2010-06-30 17:33:29 +01:00
* Tests addPlugin() returning an added plugin instance.
2010-06-16 01:55:39 +01:00
*
* @return void
*/
2010-06-30 17:33:29 +01:00
public function testAddPluginByInstanceReturnsPluginInstance()
{
$plugin = $this->getMockPlugin();
$returnedPlugin = $this->handler->addPlugin($plugin);
2010-06-16 01:55:39 +01:00
$this->assertSame(
2010-06-30 17:33:29 +01:00
$returnedPlugin,
2010-06-16 01:55:39 +01:00
$plugin,
2010-06-30 17:33:29 +01:00
'addPlugin() does not return the instance passed to it'
2010-06-16 01:55:39 +01:00
);
}
/**
2010-06-30 17:33:29 +01:00
* Tests adding a plugin to the handler using the plugin's short name.
2010-06-16 01:55:39 +01:00
*
* @return void
*/
2010-06-30 17:33:29 +01:00
public function testAddPluginByShortName()
2010-06-16 01:55:39 +01:00
{
2010-06-30 17:33:29 +01:00
$pluginName = 'Mock';
2010-06-16 01:55:39 +01:00
$this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
2010-06-30 17:33:29 +01:00
$returnedPlugin = $this->handler->addPlugin($pluginName);
$this->assertTrue($this->handler->hasPlugin($pluginName));
2010-06-16 01:55:39 +01:00
$this->assertType(
'Phergie_Plugin_Mock',
2010-06-30 17:33:29 +01:00
$this->handler->getPlugin($pluginName)
2010-06-16 01:55:39 +01:00
);
2010-06-30 17:33:29 +01:00
2010-06-16 01:55:39 +01:00
$this->assertSame(
2010-06-30 17:33:29 +01:00
$this->handler->getPlugin($pluginName),
$returnedPlugin,
'Handler does not contain added plugin'
2010-06-16 01:55:39 +01:00
);
}
/**
2010-06-30 17:33:29 +01:00
* Tests adding a plugin instance to the handler.
2010-06-16 01:55:39 +01:00
*
* @return void
*/
2010-06-30 17:33:29 +01:00
public function testAddPluginByInstance()
2010-06-16 01:55:39 +01:00
{
2010-06-30 17:33:29 +01:00
$plugin = $this->getMockPlugin();
$returnedPlugin = $this->handler->addPlugin($plugin);
2010-06-16 01:55:39 +01:00
$this->assertTrue($this->handler->hasPlugin('TestPlugin'));
2010-06-30 17:33:29 +01:00
2010-06-16 01:55:39 +01:00
$this->assertSame(
2010-06-30 17:33:29 +01:00
$plugin,
$returnedPlugin,
'addPlugin() does not return added plugin instance'
2010-06-16 01:55:39 +01:00
);
2010-06-30 17:33:29 +01:00
2010-06-16 01:55:39 +01:00
$this->assertSame(
2010-06-30 17:33:29 +01:00
$plugin,
$this->handler->getPlugin('TestPlugin'),
'getPlugin() does not return added plugin instance'
2010-06-16 01:55:39 +01:00
);
}
/**
2010-06-30 17:33:29 +01:00
* Tests addPlugin() throwing an exception when the plugin class file
* can't be found.
2010-06-16 01:55:39 +01:00
*
* @return void
*/
2010-06-30 17:33:29 +01:00
public function testAddPluginThrowsExceptionWhenPluginFileNotFound()
2010-06-16 01:55:39 +01:00
{
try {
$this->handler->addPlugin('TestPlugin');
} catch(Phergie_Plugin_Exception $e) {
$this->assertEquals(
Phergie_Plugin_Exception::ERR_CLASS_NOT_FOUND,
$e->getCode()
);
return;
}
2010-06-30 17:33:29 +01:00
$this->fail('An expected exception has not been raised');
}
/**
* Recursively removes all files and subdirectories in a directory.
*
* @param string $path Directory path
* @return void
*/
private function removeDirectory($path)
{
if (file_exists($path)) {
$it = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($it as $entry) {
if ($it->isDot()) {
continue;
}
if ($entry->isDir()) {
rmdir($entry->getPathname());
} else {
unlink($entry->getPathname());
}
}
}
}
/**
* Tests addPlugin() throwing an exception when the plugin class file is
* found, but does not contain the plugin class as expected.
*
* @return void
*/
public function testAddPluginThrowsExceptionWhenPluginClassNotFound()
{
$path = sys_get_temp_dir() . '/Phergie/Plugin';
$this->removeDirectory(dirname($path));
mkdir($path, 0777, true);
touch($path . '/TestPlugin.php');
$this->handler->addPath($path, 'Phergie_Plugin_');
try {
$this->handler->addPlugin('TestPlugin');
} catch(Phergie_Plugin_Exception $e) { }
if (isset($e)) {
$this->assertEquals(
Phergie_Plugin_Exception::ERR_CLASS_NOT_FOUND,
$e->getCode()
);
} else {
$this->fail('An expected exception has not been raised');
}
$this->removeDirectory(dirname($path));
2010-06-16 01:55:39 +01:00
}
/**
2010-06-30 17:33:29 +01:00
* Tests addPlugin() throwing an exception when trying to instantiate a
* class that doesn't extend Phergie_Plugin_Abstract.
2010-06-16 01:55:39 +01:00
*
* @return void
*/
public function testAddPluginThrowsExceptionIfRequestingNonPlugin()
{
try {
$this->handler->addPlugin('Handler');
} catch(Phergie_Plugin_Exception $e) {
$this->assertEquals(
Phergie_Plugin_Exception::ERR_INCORRECT_BASE_CLASS,
$e->getCode()
);
return;
}
2010-06-30 17:33:29 +01:00
$this->fail('An expected exception has not been raised');
2010-06-16 01:55:39 +01:00
}
/**
2010-06-30 17:33:29 +01:00
* Tests addPlugin() throwing an exception when trying to instantiate a
2010-06-16 01:55:39 +01:00
* class that can't be instantiated.
*
* @return void
*/
public function testAddPluginThrowsExceptionIfPluginNotInstantiable()
{
$this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
try {
$this->handler->addPlugin('TestNonInstantiablePluginFromFile');
} catch(Phergie_Plugin_Exception $e) {
$this->assertEquals(
Phergie_Plugin_Exception::ERR_CLASS_NOT_INSTANTIABLE,
$e->getCode()
);
return;
}
2010-06-30 17:33:29 +01:00
$this->fail('An expected exception has not been raised');
2010-06-16 01:55:39 +01:00
}
/**
2010-06-30 17:33:29 +01:00
* Tests adding a plugin by its short name with arguments passed to the
* plugin constructor.
2010-06-16 01:55:39 +01:00
*
2010-06-30 17:33:29 +01:00
* @return void
2010-06-16 01:55:39 +01:00
*/
2010-06-30 17:33:29 +01:00
public function testAddPluginShortNamePassesArgsToConstructor()
2010-06-16 01:55:39 +01:00
{
2010-06-30 17:33:29 +01:00
$pluginName = 'Mock';
2010-06-16 01:55:39 +01:00
$this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
$arguments = array('a', 'b', 'c');
2010-06-30 17:33:29 +01:00
$plugin = $this->handler->addPlugin($pluginName, $arguments);
2010-06-16 01:55:39 +01:00
$this->assertAttributeSame(
$arguments,
2010-06-30 17:33:29 +01:00
'arguments',
2010-06-16 01:55:39 +01:00
$plugin,
2010-06-30 17:33:29 +01:00
'Arguments do not match'
2010-06-16 01:55:39 +01:00
);
}
/**
2010-06-30 17:33:29 +01:00
* Tests addPlugin() passing Phergie_Config to an instantiated plugin.
2010-06-16 01:55:39 +01:00
*
2010-06-30 17:33:29 +01:00
* @return void
2010-06-16 01:55:39 +01:00
*/
2010-06-30 17:33:29 +01:00
public function testAddPluginPassesConstructorArguments()
2010-06-16 01:55:39 +01:00
{
2010-06-30 17:33:29 +01:00
$pluginName = 'Mock';
2010-06-16 01:55:39 +01:00
$this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
2010-06-30 17:33:29 +01:00
$plugin = $this->handler->addPlugin($pluginName);
2010-06-16 01:55:39 +01:00
$this->assertSame(
2010-06-30 17:33:29 +01:00
$this->config,
2010-06-16 01:55:39 +01:00
$plugin->getConfig(),
2010-06-30 17:33:29 +01:00
'Phergie_Config instances do not match'
2010-06-16 01:55:39 +01:00
);
$this->assertSame(
2010-06-30 17:33:29 +01:00
$this->events,
2010-06-16 01:55:39 +01:00
$plugin->getEventHandler(),
2010-06-30 17:33:29 +01:00
'Phergie_Event_Handler instances do not match'
2010-06-16 01:55:39 +01:00
);
}
/**
2010-06-30 17:33:29 +01:00
* Tests addPlugin() calling onLoad() on an instantiated plugin.
2010-06-16 01:55:39 +01:00
*
* @return void
*/
2010-06-30 17:33:29 +01:00
public function testAddPluginCallsOnLoadOnInstantiatedPlugin()
2010-06-16 01:55:39 +01:00
{
2010-06-30 17:33:29 +01:00
$plugin = $this->getMockPlugin(null, array('onLoad'));
2010-06-16 01:55:39 +01:00
$plugin
2010-06-30 17:33:29 +01:00
->expects($this->once())
->method('onLoad');
2010-06-16 01:55:39 +01:00
$this->handler->addPlugin($plugin);
}
/**
2010-06-30 17:33:29 +01:00
* Tests addPlugin() returning the same plugin when called twice.
2010-06-16 01:55:39 +01:00
*
* @return void
*/
public function testAddPluginReturnsSamePluginWhenAskedTwice()
{
2010-06-30 17:33:29 +01:00
$pluginName = 'Mock';
2010-06-16 01:55:39 +01:00
$this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
2010-06-30 17:33:29 +01:00
$plugin1 = $this->handler->addPlugin($pluginName);
$plugin2 = $this->handler->addPlugin($pluginName);
2010-06-16 01:55:39 +01:00
$this->assertSame($plugin1, $plugin2);
}
/**
2010-06-30 17:33:29 +01:00
* Tests getPlugin() throwing an exception when trying to get an
* unloaded plugin with autoload disabled.
2010-06-16 01:55:39 +01:00
*
2010-06-30 17:33:29 +01:00
* @depends testGetAutoloadDefaultsToNotAutoload
2010-06-16 01:55:39 +01:00
* @return void
*/
public function testExceptionThrownWhenLoadingPluginWithoutAutoload()
{
$this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
try {
$this->handler->getPlugin('Mock');
} catch (Phergie_Plugin_Exception $expected) {
$this->assertEquals(
Phergie_Plugin_Exception::ERR_PLUGIN_NOT_LOADED,
$expected->getCode()
);
return;
}
2010-06-30 17:33:29 +01:00
$this->fail('An expected exception has not been raised');
}
/**
* Tests addPlugins() with a plugin short name and no plugin constructor
* arguments.
*
* @depends testAddPluginByShortName
* @depends testAddPluginByInstance
* @return void
*/
public function testAddPluginsWithoutArguments()
{
$prefix = 'Phergie_Plugin_';
$this->handler->addPath(dirname(__FILE__), $prefix);
$plugin = 'Mock';
$this->handler->addPlugins(array($plugin));
$returnedPlugin = $this->handler->getPlugin($plugin);
$this->assertContains(
get_class($returnedPlugin),
$prefix . $plugin,
'Short name plugin not of expected class'
);
}
/**
* Tests addPlugins() with a plugin short name and plugin constructor
* arguments.
*
* @depends testAddPluginByShortName
* @depends testAddPluginByInstance
* @return void
*/
public function testAddPluginsWithArguments()
{
$prefix = 'Phergie_Plugin_';
$this->handler->addPath(dirname(__FILE__), $prefix);
$arguments = array(1, 2, 3);
$plugin = array('Mock', $arguments);
$this->handler->addPlugins(array($plugin));
$returnedPlugin = $this->handler->getPlugin('Mock');
$this->assertEquals(
$arguments,
$returnedPlugin->getArguments(),
'Constructor arguments for instance plugin do not match'
);
}
/**
* Tests removePlugin() with a plugin instance.
*
* @depends testAddPluginByInstance
* @return void
*/
public function testRemovePluginByInstance()
{
$plugin = $this->getMockPlugin();
$this->handler->addPlugin($plugin);
$this->handler->removePlugin($plugin);
$this->assertFalse(
$this->handler->hasPlugin($plugin->getName()),
'Plugin was not removed'
);
}
/**
* Tests removePlugin() with a plugin short name.
*
* @depends testAddPluginByShortName
* @return void
*/
public function testRemovePluginByShortName()
{
$plugin = 'Mock';
$this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
$this->handler->addPlugin($plugin);
$this->handler->removePlugin($plugin);
$this->assertFalse(
$this->handler->hasPlugin($plugin),
'Plugin was not removed'
);
}
/**
* Tests getPlugin() when the plugin is not already loaded and
* autoloading is disabled.
*
* @depends testSetAutoload
* @return void
*/
public function testGetPluginWithAutoloadEnabled()
{
$this->handler->setAutoload(true);
$this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
$plugin = $this->handler->getPlugin('Mock');
$this->assertType(
'Phergie_Plugin_Mock',
$plugin,
'Retrieved plugin not of expected class'
);
}
/**
* Tests getPlugins().
*
* @depends testGetPluginWithAutoloadEnabled
* @return void
*/
public function testGetPlugins()
{
$plugin1 = $this->getMockPlugin('TestPlugin1');
$this->handler->addPlugin($plugin1);
$plugin2 = $this->getMockPlugin('TestPlugin2');
$this->handler->addPlugin($plugin2);
$expected = array(
'testplugin1' => $plugin1,
'testplugin2' => $plugin2,
);
$actual = $this->handler->getPlugins();
$this->assertEquals($expected, $actual);
$actual = $this->handler->getPlugins(array('testplugin1', 'testplugin2'));
$this->assertEquals($expected, $actual);
2010-06-16 01:55:39 +01:00
}
2010-08-05 00:02:24 +01:00
/**
* Tests the plugin receiving and using a predefined iterator instance.
*
* @depends testGetPlugins
* @return void
*/
public function testSetIterator()
{
$plugin = $this->getMockPlugin('TestPlugin');
$this->handler->addPlugin($plugin);
$plugins = $this->handler->getPlugins();
$iterator = new ArrayIterator($plugins);
$this->handler->setIterator($iterator);
$this->assertSame($this->handler->getIterator(), $iterator);
$iterated = array();
foreach ($this->handler as $plugin) {
$iterated[strtolower($plugin->getName())] = $plugin;
}
$this->assertEquals($iterated, $plugins);
}
2010-06-16 01:55:39 +01:00
}