9 * This source file is subject to the new BSD license that is bundled
10 * with this package in the file LICENSE.
11 * It is also available through the world-wide-web at this URL:
12 * http://phergie.org/license
15 * @package Phergie_Tests
16 * @author Phergie Development Team <team@phergie.org>
17 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18 * @license http://phergie.org/license New BSD License
19 * @link http://pear.phergie.org/package/Phergie_Tests
23 * Unit test suite for Pherge_Plugin_Handler.
26 * @package Phergie_Tests
27 * @author Phergie Development Team <team@phergie.org>
28 * @license http://phergie.org/license New BSD License
29 * @link http://pear.phergie.org/package/Phergie_Tests
31 class Phergie_Plugin_HandlerTest extends PHPUnit_Framework_TestCase
34 * Plugin handler instance being tested
36 * @var Phergie_Plugin_Handler
41 * Sets up a new handler instance before each test.
45 public function setUp()
47 $this->handler = new Phergie_Plugin_Handler(
49 new Phergie_Event_Handler()
54 * Destroys the handler instance after each test
58 public function tearDown()
60 unset($this->handler);
64 * Ensures that we can iterate over the handler
68 public function testImplementsIterator()
70 $reflection = new ReflectionObject($this->handler);
72 $reflection->implementsInterface('IteratorAggregate')
76 'Iterator', $this->handler->getIterator(),
77 'getIterator() must actually return an Iterator'
82 * Ensures a newly instantiated handler does not have plugins associated
85 * @depends testImplementsIterator
88 public function testEmptyHandlerHasNoPlugins()
91 foreach ($this->handler as $plugin) {
95 $this->assertEquals(0, $count);
99 * Ensures a newly instantiated handler does not default to autoload
103 public function testDefaultsToNotAutoload()
105 $this->assertFalse($this->handler->getAutoload());
109 * addPath provides a fluent interface
113 public function testAddPathProvidesFluentInterface()
115 $handler = $this->handler->addPath(dirname(__FILE__));
116 $this->assertSame($this->handler, $handler);
120 * addPath throws an exception when it cannot read the directory
124 public function testAddPathThrowsExceptionOnUnreadableDirectory()
127 $this->handler->addPath('/an/unreadable/directory/path');
128 } catch(Phergie_Plugin_Exception $e) {
130 Phergie_Plugin_Exception::ERR_DIRECTORY_NOT_READABLE,
136 $this->fail('An expected exception has not been raised.');
140 * adds a path into the plugin handler and then ensures that files
141 * in that location can be found
145 public function testAddPath()
147 $plugin_name = 'Mock';
149 $this->handler->addPlugin($plugin_name);
150 } catch(Phergie_Plugin_Exception $e) {
152 Phergie_Plugin_Exception::ERR_CLASS_NOT_FOUND,
156 $this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
159 $this->handler->addPlugin($plugin_name);
160 } catch(Phergie_Plugin_Exception $e) {
162 'After adding the directory, the plugin was still '
171 'Before adding the directory, an expected exception '
177 * addPlugin returns the plugin instance that was added
181 public function testAddPluginByInstanceReturnsPluginInstance() {
182 $plugin = $this->getMock('Phergie_Plugin_Abstract');
184 ->expects($this->any())
186 ->will($this->returnValue('TestPlugin'));
188 $returned_plugin = $this->handler->addPlugin($plugin);
192 'addPlugin returns the same instance that is passed to it'
197 * Can add a plugin to the handler by shortname
201 public function testAddPluginToHandlerByShortname()
203 $plugin_name = 'Mock';
204 $this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
206 $returned_plugin = $this->handler->addPlugin($plugin_name);
207 $this->assertTrue($this->handler->hasPlugin($plugin_name));
209 'Phergie_Plugin_Mock',
210 $this->handler->getPlugin($plugin_name)
213 $this->handler->getPlugin($plugin_name),
215 'Handler contains plugin when added by shortname.'
221 * Can add a plugin to the handler by instance
225 public function testAddPluginToHandlerByInstance()
227 $plugin = $this->getMock('Phergie_Plugin_Abstract');
229 ->expects($this->any())
231 ->will($this->returnValue('TestPlugin'));
233 $returned_plugin = $this->handler->addPlugin($plugin);
235 $this->assertTrue($this->handler->hasPlugin('TestPlugin'));
237 $plugin, $returned_plugin,
238 'addPlugin returns the same plugin'
241 $plugin, $this->handler->getPlugin('TestPlugin'),
242 'getPlugin returns the same plugin'
247 * addPlugin throws an exception when it can't find the plugin
251 public function testAddPluginThrowsExceptionIfCannotFindPlugin()
254 $this->handler->addPlugin('TestPlugin');
255 } catch(Phergie_Plugin_Exception $e) {
257 Phergie_Plugin_Exception::ERR_CLASS_NOT_FOUND,
263 $this->fail('An expected exception has not been raised.');
267 * addPlugin throws an exception when trying to instantiate a
268 * class that doesn't extend from Phergie_Plugin_Abstract
272 public function testAddPluginThrowsExceptionIfRequestingNonPlugin()
275 $this->handler->addPlugin('Handler');
276 } catch(Phergie_Plugin_Exception $e) {
278 Phergie_Plugin_Exception::ERR_INCORRECT_BASE_CLASS,
284 $this->fail('An expected exception has not been raised.');
288 * addPlugin throws an exception when trying to instantiate a
289 * class that can't be instantiated.
293 public function testAddPluginThrowsExceptionIfPluginNotInstantiable()
295 $this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
297 $this->handler->addPlugin('TestNonInstantiablePluginFromFile');
298 } catch(Phergie_Plugin_Exception $e) {
300 Phergie_Plugin_Exception::ERR_CLASS_NOT_INSTANTIABLE,
306 $this->fail('An expected exception has not been raised.');
310 * addPlugin with shortname and arguments passes args to constructor
314 public function testAddPluginShortnamePassesArgsToConstructor()
316 $plugin_name = 'Mock';
317 $this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
319 $arguments = array('a', 'b', 'c');
321 $plugin = $this->handler->addPlugin($plugin_name, $arguments);
322 $this->assertAttributeSame(
326 'Arguments passed in to addPlugin match the arguments '
327 . 'the Mock plugin constructor received'
332 * addPlugin passes Phergie_Config to instantiated plugin
336 public function testAddPluginPassesPhergieConfigToInstantiatedPlugin()
338 $my_config = new Phergie_Config();
339 $my_config['my_option'] = 'my_value';
341 // create a new handler with this config
342 unset($this->handler);
343 $this->handler = new Phergie_Plugin_Handler(
345 new Phergie_Event_Handler()
348 $plugin_name = 'Mock';
349 $this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
351 $plugin = $this->handler->addPlugin($plugin_name);
355 $plugin->getConfig(),
356 'addPlugin passes Phergie_Config to instantiated plugin'
361 * addPlugin passes Phergie_Event_Handler to instantiated plugin
365 public function testAddPluginPassesPhergieEventHandlerToInstantiatedPlugin()
367 $plugin = $this->getMock('Phergie_Plugin_Abstract');
369 ->expects($this->any())
371 ->will($this->returnValue('TestPlugin'));
373 $my_event_handler = new Phergie_Event_Handler();
374 $my_event_handler->addEvent($plugin, 'ping');
376 // create a new plugin handler with this event handler
377 unset($this->handler);
378 $this->handler = new Phergie_Plugin_Handler(
379 new Phergie_Config(),
383 $plugin_name = 'Mock';
384 $this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
386 $plugin = $this->handler->addPlugin($plugin_name);
390 $plugin->getEventHandler(),
391 'addPlugin passes Phergie_Event_Handler to instantiated plugin'
396 * @todo addPlugin calls onLoad() to instantiated plugin
404 public function testPluginHandlerImplementsIsset()
406 $plugin_name = 'TestPlugin';
408 $this->assertFalse(isset($this->handler->{$plugin_name}));
410 $plugin = $this->getMock('Phergie_Plugin_Abstract');
412 ->expects($this->any())
414 ->will($this->returnValue($plugin_name));
416 $this->handler->addPlugin($plugin);
418 $this->assertTrue(isset($this->handler->{$plugin_name}));
423 * addPlugin() returns the same plugin when requested twice
427 public function testAddPluginReturnsSamePluginWhenAskedTwice()
429 $plugin_name = 'Mock';
430 $this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
432 $plugin1 = $this->handler->addPlugin($plugin_name);
433 $plugin2 = $this->handler->addPlugin($plugin_name);
434 $this->assertSame($plugin1, $plugin2);
439 * Tests an exception is thrown when trying to get a plugin
440 * that is not already loaded and autoload is off
442 * @depends testDefaultsToNotAutoload
445 public function testExceptionThrownWhenLoadingPluginWithoutAutoload()
447 $this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
450 $this->handler->getPlugin('Mock');
451 } catch (Phergie_Plugin_Exception $expected) {
453 Phergie_Plugin_Exception::ERR_PLUGIN_NOT_LOADED,
459 $this->fail('An expected exception has not been raised.');