1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
|
<?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;
/**
* 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;
}
/**
* Sets up a new handler instance before each test.
*
* @return void
*/
public function setUp()
{
$this->config = $this->getMock('Phergie_Config');
$this->events = $this->getMock('Phergie_Event_Handler');
$this->handler = new Phergie_Plugin_Handler(
$this->config,
$this->events
);
}
/**
* Tests iterability of the plugin handler.
*
* @return void
*/
public function testImplementsIteratorAggregate()
{
$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'
);
}
/**
* Tests countability of the plugin handler.
*
* @return void
*/
public function testImplementsCountable()
{
$reflection = new ReflectionObject($this->handler);
$this->assertTrue(
$reflection->implementsInterface('Countable'),
'Handler does not implement Countable'
);
$this->assertType(
'int',
count($this->handler),
'count() must return an integer'
);
}
/**
* Tests the plugin handler exposing added plugins as instance
* properties of the handler via isset().
*
* @return void
*/
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
* plugins.
*
* @return void
*/
public function testImplementsCall()
{
foreach (range(1, 2) as $index) {
$plugin = $this->getMockPlugin('TestPlugin' . $index, array('callback'));
$plugin
->expects($this->once())
->method('callback');
$this->handler->addPlugin($plugin);
}
$this->assertTrue($this->handler->callback());
}
/**
* Tests a newly instantiated handler not having plugins associated with
* it.
*
* @depends testImplementsCountable
* @return void
*/
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()
{
$this->assertFalse($this->handler->getAutoload());
}
/**
* 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.
*
* @return void
*/
public function testAddPathProvidesFluentInterface()
{
$handler = $this->handler->addPath(dirname(__FILE__));
$this->assertSame($this->handler, $handler);
}
/**
* Tests addPath() throwing an exception when it cannot read the
* directory.
*
* @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;
}
$this->fail('An expected exception has not been raised');
}
/**
* Tests adding a path to the plugin handler.
*
* @return void
*/
public function testAddPath()
{
$pluginName = 'Mock';
try {
$this->handler->addPlugin($pluginName);
} catch(Phergie_Plugin_Exception $e) {
$this->assertEquals(
Phergie_Plugin_Exception::ERR_CLASS_NOT_FOUND,
$e->getCode()
);
}
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');
}
}
/**
* Tests addPlugin() returning an added plugin instance.
*
* @return void
*/
public function testAddPluginByInstanceReturnsPluginInstance()
{
$plugin = $this->getMockPlugin();
$returnedPlugin = $this->handler->addPlugin($plugin);
$this->assertSame(
$returnedPlugin,
$plugin,
'addPlugin() does not return the instance passed to it'
);
}
/**
* Tests adding a plugin to the handler using the plugin's short name.
*
* @return void
*/
public function testAddPluginByShortName()
{
$pluginName = 'Mock';
$this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
$returnedPlugin = $this->handler->addPlugin($pluginName);
$this->assertTrue($this->handler->hasPlugin($pluginName));
$this->assertType(
'Phergie_Plugin_Mock',
$this->handler->getPlugin($pluginName)
);
$this->assertSame(
$this->handler->getPlugin($pluginName),
$returnedPlugin,
'Handler does not contain added plugin'
);
}
/**
* Tests adding a plugin instance to the handler.
*
* @return void
*/
public function testAddPluginByInstance()
{
$plugin = $this->getMockPlugin();
$returnedPlugin = $this->handler->addPlugin($plugin);
$this->assertTrue($this->handler->hasPlugin('TestPlugin'));
$this->assertSame(
$plugin,
$returnedPlugin,
'addPlugin() does not return added plugin instance'
);
$this->assertSame(
$plugin,
$this->handler->getPlugin('TestPlugin'),
'getPlugin() does not return added plugin instance'
);
}
/**
* Tests addPlugin() throwing an exception when the plugin class file
* can't be found.
*
* @return void
*/
public function testAddPluginThrowsExceptionWhenPluginFileNotFound()
{
try {
$this->handler->addPlugin('TestPlugin');
} catch(Phergie_Plugin_Exception $e) {
$this->assertEquals(
Phergie_Plugin_Exception::ERR_CLASS_NOT_FOUND,
$e->getCode()
);
return;
}
$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));
}
/**
* Tests addPlugin() throwing an exception when trying to instantiate a
* class that doesn't extend Phergie_Plugin_Abstract.
*
* @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;
}
$this->fail('An expected exception has not been raised');
}
/**
* Tests addPlugin() throwing an exception when trying to instantiate a
* 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;
}
$this->fail('An expected exception has not been raised');
}
/**
* Tests adding a plugin by its short name with arguments passed to the
* plugin constructor.
*
* @return void
*/
public function testAddPluginShortNamePassesArgsToConstructor()
{
$pluginName = 'Mock';
$this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
$arguments = array('a', 'b', 'c');
$plugin = $this->handler->addPlugin($pluginName, $arguments);
$this->assertAttributeSame(
$arguments,
'arguments',
$plugin,
'Arguments do not match'
);
}
/**
* Tests addPlugin() passing Phergie_Config to an instantiated plugin.
*
* @return void
*/
public function testAddPluginPassesConstructorArguments()
{
$pluginName = 'Mock';
$this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
$plugin = $this->handler->addPlugin($pluginName);
$this->assertSame(
$this->config,
$plugin->getConfig(),
'Phergie_Config instances do not match'
);
$this->assertSame(
$this->events,
$plugin->getEventHandler(),
'Phergie_Event_Handler instances do not match'
);
}
/**
* Tests addPlugin() calling onLoad() on an instantiated plugin.
*
* @return void
*/
public function testAddPluginCallsOnLoadOnInstantiatedPlugin()
{
$plugin = $this->getMockPlugin(null, array('onLoad'));
$plugin
->expects($this->once())
->method('onLoad');
$this->handler->addPlugin($plugin);
}
/**
* Tests addPlugin() returning the same plugin when called twice.
*
* @return void
*/
public function testAddPluginReturnsSamePluginWhenAskedTwice()
{
$pluginName = 'Mock';
$this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_');
$plugin1 = $this->handler->addPlugin($pluginName);
$plugin2 = $this->handler->addPlugin($pluginName);
$this->assertSame($plugin1, $plugin2);
}
/**
* Tests getPlugin() throwing an exception when trying to get an
* unloaded plugin with autoload disabled.
*
* @depends testGetAutoloadDefaultsToNotAutoload
* @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;
}
$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);
}
/**
* 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);
}
}
|