]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Tests/Phergie/Plugin/HandlerTest.php
Added Phergie PHP IRC library
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Tests / Phergie / Plugin / HandlerTest.php
1 <?php
2 /**
3  * Phergie
4  *
5  * PHP version 5
6  *
7  * LICENSE
8  *
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
13  *
14  * @category  Phergie
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
20  */
21
22 /**
23  * Unit test suite for Pherge_Plugin_Handler.
24  *
25  * @category Phergie
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
30  */
31 class Phergie_Plugin_HandlerTest extends PHPUnit_Framework_TestCase
32 {
33     /**
34      * Plugin handler instance being tested
35      *
36      * @var Phergie_Plugin_Handler
37      */
38     protected $handler;
39
40     /**
41      * Sets up a new handler instance before each test.
42      *
43      * @return void
44      */
45     public function setUp()
46     {
47         $this->handler = new Phergie_Plugin_Handler(
48             new Phergie_Config(),
49             new Phergie_Event_Handler()
50         );
51     }
52
53     /**
54      * Destroys the handler instance after each test
55      *
56      * @return void
57      */
58     public function tearDown()
59     {
60         unset($this->handler);
61     }
62
63     /**
64      * Ensures that we can iterate over the handler
65      *
66      * @return void
67      */
68     public function testImplementsIterator()
69     {
70         $reflection = new ReflectionObject($this->handler);
71         $this->assertTrue(
72             $reflection->implementsInterface('IteratorAggregate')
73         );
74
75         $this->assertType(
76             'Iterator', $this->handler->getIterator(),
77             'getIterator() must actually return an Iterator'
78         );
79     }
80
81     /**
82      * Ensures a newly instantiated handler does not have plugins associated
83      * with it
84      *
85      * @depends testImplementsIterator
86      * @return void
87      */
88     public function testEmptyHandlerHasNoPlugins()
89     {
90         $count = 0;
91         foreach ($this->handler as $plugin) {
92             $count++;
93         }
94
95         $this->assertEquals(0, $count);
96     }
97     
98     /**
99      * Ensures a newly instantiated handler does not default to autoload
100      *
101      * @return void
102      */
103     public function testDefaultsToNotAutoload()
104     {
105         $this->assertFalse($this->handler->getAutoload());
106     }
107
108     /**
109      * addPath provides a fluent interface
110      *
111      * @return void
112      */
113     public function testAddPathProvidesFluentInterface()
114     {
115         $handler = $this->handler->addPath(dirname(__FILE__));
116         $this->assertSame($this->handler, $handler);
117     }
118
119     /**
120      * addPath throws an exception when it cannot read the directory
121      *
122      * @return void
123      */
124     public function testAddPathThrowsExceptionOnUnreadableDirectory()
125     {
126         try {
127             $this->handler->addPath('/an/unreadable/directory/path');
128         } catch(Phergie_Plugin_Exception $e) {
129             $this->assertEquals(
130                 Phergie_Plugin_Exception::ERR_DIRECTORY_NOT_READABLE,
131                 $e->getCode()
132             );
133             return;
134         }
135
136         $this->fail('An expected exception has not been raised.');
137     }
138
139     /**
140      * adds a path into the plugin handler and then ensures that files
141      * in that location can be found
142      *
143      * @return void
144      */
145     public function testAddPath()
146     {
147         $plugin_name = 'Mock';
148         try {
149             $this->handler->addPlugin($plugin_name);
150         } catch(Phergie_Plugin_Exception $e) {
151             $this->assertEquals(
152                 Phergie_Plugin_Exception::ERR_CLASS_NOT_FOUND,
153                 $e->getCode()
154             );
155             
156             $this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
157
158             try {
159                 $this->handler->addPlugin($plugin_name);
160             } catch(Phergie_Plugin_Exception $e) {
161                 $this->fail(
162                     'After adding the directory, the plugin was still '
163                     . 'not found.'
164                 );
165             }
166             
167             return;
168         }
169
170         $this->fail(
171             'Before adding the directory, an expected exception '
172             . 'was not raised'
173         );
174     }
175
176     /**
177      * addPlugin returns the plugin instance that was added
178      *
179      * @return void
180      */
181     public function testAddPluginByInstanceReturnsPluginInstance() {
182         $plugin = $this->getMock('Phergie_Plugin_Abstract');
183         $plugin
184             ->expects($this->any())
185             ->method('getName')
186             ->will($this->returnValue('TestPlugin'));
187
188         $returned_plugin = $this->handler->addPlugin($plugin);
189         $this->assertSame(
190             $returned_plugin,
191             $plugin,
192             'addPlugin returns the same instance that is passed to it'
193         );
194     }
195
196     /**
197      * Can add a plugin to the handler by shortname
198      *
199      * @return void
200      */
201     public function testAddPluginToHandlerByShortname()
202     {
203         $plugin_name = 'Mock';
204         $this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
205
206         $returned_plugin = $this->handler->addPlugin($plugin_name);
207         $this->assertTrue($this->handler->hasPlugin($plugin_name));
208         $this->assertType(
209             'Phergie_Plugin_Mock',
210             $this->handler->getPlugin($plugin_name)
211         );
212         $this->assertSame(
213             $this->handler->getPlugin($plugin_name),
214             $returned_plugin,
215             'Handler contains plugin when added by shortname.'
216         );
217     }
218
219
220     /**
221      * Can add a plugin to the handler by instance
222      *
223      * @return void
224      */
225     public function testAddPluginToHandlerByInstance()
226     {
227         $plugin = $this->getMock('Phergie_Plugin_Abstract');
228         $plugin
229             ->expects($this->any())
230             ->method('getName')
231             ->will($this->returnValue('TestPlugin'));
232
233         $returned_plugin = $this->handler->addPlugin($plugin);
234
235         $this->assertTrue($this->handler->hasPlugin('TestPlugin'));
236         $this->assertSame(
237             $plugin, $returned_plugin,
238             'addPlugin returns the same plugin'
239         );
240         $this->assertSame(
241             $plugin, $this->handler->getPlugin('TestPlugin'),
242             'getPlugin returns the same plugin'
243         );
244     }
245
246     /**
247      * addPlugin throws an exception when it can't find the plugin
248      *
249      * @return void
250      */
251     public function testAddPluginThrowsExceptionIfCannotFindPlugin()
252     {
253         try {
254             $this->handler->addPlugin('TestPlugin');
255         } catch(Phergie_Plugin_Exception $e) {
256             $this->assertEquals(
257                 Phergie_Plugin_Exception::ERR_CLASS_NOT_FOUND,
258                 $e->getCode()
259             );
260             return;
261         }
262
263         $this->fail('An expected exception has not been raised.');
264     }
265
266     /**
267      * addPlugin throws an exception when trying to instantiate a
268      * class that doesn't extend from Phergie_Plugin_Abstract
269      *
270      * @return void
271      */
272     public function testAddPluginThrowsExceptionIfRequestingNonPlugin()
273     {
274         try {
275             $this->handler->addPlugin('Handler');
276         } catch(Phergie_Plugin_Exception $e) {
277             $this->assertEquals(
278                 Phergie_Plugin_Exception::ERR_INCORRECT_BASE_CLASS,
279                 $e->getCode()
280             );
281             return;
282         }
283
284         $this->fail('An expected exception has not been raised.');
285     }
286
287     /**
288      * addPlugin throws an exception when trying to instantiate a
289      * class that can't be instantiated.
290      *
291      * @return void
292      */
293     public function testAddPluginThrowsExceptionIfPluginNotInstantiable()
294     {
295         $this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
296         try {
297             $this->handler->addPlugin('TestNonInstantiablePluginFromFile');
298         } catch(Phergie_Plugin_Exception $e) {
299             $this->assertEquals(
300                 Phergie_Plugin_Exception::ERR_CLASS_NOT_INSTANTIABLE,
301                 $e->getCode()
302             );
303             return;
304         }
305
306         $this->fail('An expected exception has not been raised.');
307     }
308
309     /**
310      * addPlugin with shortname and arguments passes args to constructor
311      *
312      * @return null
313      */
314     public function testAddPluginShortnamePassesArgsToConstructor()
315     {
316         $plugin_name = 'Mock';
317         $this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
318
319         $arguments = array('a', 'b', 'c');
320
321         $plugin = $this->handler->addPlugin($plugin_name, $arguments);
322         $this->assertAttributeSame(
323             $arguments,
324             'args',
325             $plugin,
326             'Arguments passed in to addPlugin match the arguments '
327             . 'the Mock plugin constructor received'
328         );
329     }
330
331     /**
332      * addPlugin passes Phergie_Config to instantiated plugin
333      *
334      * @return null
335      */
336     public function testAddPluginPassesPhergieConfigToInstantiatedPlugin()
337     {
338         $my_config = new Phergie_Config();
339         $my_config['my_option'] = 'my_value';
340
341         // create a new handler with this config
342         unset($this->handler);
343         $this->handler = new Phergie_Plugin_Handler(
344             $my_config,
345             new Phergie_Event_Handler()
346         );
347
348         $plugin_name = 'Mock';
349         $this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
350
351         $plugin = $this->handler->addPlugin($plugin_name);
352
353         $this->assertSame(
354             $my_config,
355             $plugin->getConfig(),
356             'addPlugin passes Phergie_Config to instantiated plugin'
357         );
358     }
359
360     /**
361      * addPlugin passes Phergie_Event_Handler to instantiated plugin
362      *
363      * @return null
364      */
365     public function testAddPluginPassesPhergieEventHandlerToInstantiatedPlugin()
366     {
367         $plugin = $this->getMock('Phergie_Plugin_Abstract');
368         $plugin
369             ->expects($this->any())
370             ->method('getName')
371             ->will($this->returnValue('TestPlugin'));
372
373         $my_event_handler = new Phergie_Event_Handler();
374         $my_event_handler->addEvent($plugin, 'ping');
375
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(),
380             $my_event_handler
381         );
382
383         $plugin_name = 'Mock';
384         $this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
385
386         $plugin = $this->handler->addPlugin($plugin_name);
387
388         $this->assertSame(
389             $my_event_handler,
390             $plugin->getEventHandler(),
391             'addPlugin passes Phergie_Event_Handler to instantiated plugin'
392         );
393     }
394
395     /**
396      * @todo addPlugin calls onLoad() to instantiated plugin
397      */
398
399     /**
400      * implements __isset
401      *
402      * @return void
403      */
404     public function testPluginHandlerImplementsIsset()
405     {
406         $plugin_name = 'TestPlugin';
407
408         $this->assertFalse(isset($this->handler->{$plugin_name}));
409
410         $plugin = $this->getMock('Phergie_Plugin_Abstract');
411         $plugin
412             ->expects($this->any())
413             ->method('getName')
414             ->will($this->returnValue($plugin_name));
415
416         $this->handler->addPlugin($plugin);
417
418         $this->assertTrue(isset($this->handler->{$plugin_name}));
419
420     }
421
422     /**
423      * addPlugin() returns the same plugin when requested twice
424      *
425      * @return void
426      */
427     public function testAddPluginReturnsSamePluginWhenAskedTwice()
428     {
429         $plugin_name = 'Mock';
430         $this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
431
432         $plugin1 = $this->handler->addPlugin($plugin_name);
433         $plugin2 = $this->handler->addPlugin($plugin_name);
434         $this->assertSame($plugin1, $plugin2);
435     }
436
437     
438     /**
439      * Tests an exception is thrown when trying to get a plugin
440      * that is not already loaded and autoload is off
441      *
442      * @depends testDefaultsToNotAutoload
443      * @return void
444      */
445     public function testExceptionThrownWhenLoadingPluginWithoutAutoload()
446     {
447         $this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
448
449         try {
450             $this->handler->getPlugin('Mock');
451         } catch (Phergie_Plugin_Exception $expected) {
452             $this->assertEquals(
453                 Phergie_Plugin_Exception::ERR_PLUGIN_NOT_LOADED,
454                 $expected->getCode()
455             );
456             return;
457         }
458
459         $this->fail('An expected exception has not been raised.');
460     }
461 }