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_Iterator.
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_IteratorTest extends PHPUnit_Framework_TestCase
34 * Iterator instance being tested
36 * @var Phergie_Plugin_Iterator
41 * List of mock plugin instances to be iterated
48 * Initializes the iterator instance being tested.
52 public function setUp()
54 $this->plugins = array();
55 foreach (range(0, 4) as $index) {
56 $plugin = $this->getMock('Phergie_Plugin_Abstract');
58 ->expects($this->any())
60 ->will($this->returnValue($index));
61 $this->plugins[] = $plugin;
64 $this->iterator = new Phergie_Plugin_Iterator(
65 new ArrayIterator($this->plugins)
70 * Tests that all plugins are iterated when no filters are applied.
72 public function testIteratesAllPluginsWithNoFilters()
74 $expected = range(0, 4);
76 foreach ($this->iterator as $plugin) {
77 $actual[] = $plugin->getName();
79 $this->assertEquals($expected, $actual);
83 * Tests that appropriate plugins are iterated when plugin name filters
86 public function testIteratesPluginsWithNameFilters()
88 // Test acceptance of strings and fluent interface implementation
89 $returned = $this->iterator->addPluginFilter('0');
90 $this->assertSame($this->iterator, $returned);
92 // Test acceptance of arrays
93 $this->iterator->addPluginFilter(array('1', '3'));
95 // Test application of filters to iteration
96 $expected = array('2', '4');
98 foreach ($this->iterator as $plugin) {
99 $actual[] = $plugin->getName();
101 $this->assertEquals($expected, $actual);
105 * Tests that appropriate plugins are iterated when method name filters
108 * The same method name is used in all cases here because mocked methods
109 * of mock objects do not appear to be detected by method_exists() or
110 * ReflectionClass, so filtering by a method defined in the base plugin
111 * class seems the easiest way to test that method filtering really
114 public function testIteratesPluginsWithMethodFilters()
116 // Tests acceptance of strings and fluent interface implementation
117 $returned = $this->iterator->addMethodFilter('getName');
118 $this->assertSame($this->iterator, $returned);
120 // Test acceptance of arrays
121 $this->iterator->addMethodFilter(array('getName', 'getName'));
123 // Test application of filters to iteration
126 foreach ($this->iterator as $plugin) {
127 $actual[] = $plugin->getName();
129 $this->assertEquals($expected, $actual);
133 * Tests that all plugins are iterated after filters are cleared.
135 * @depends testIteratesPluginsWithNameFilters
136 * @depends testIteratesPluginsWithMethodFilters
138 public function testIteratesPluginsAfterClearingFilters()
140 $this->iterator->addPluginFilter('0');
141 $this->iterator->addMethodFilter('method1');
142 $this->iterator->clearFilters();
144 $expected = range(0, 4);
146 foreach ($this->iterator as $plugin) {
147 $actual[] = $plugin->getName();
149 $this->assertEquals($expected, $actual);