]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Tests/Phergie/Plugin/IteratorTest.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Tests / Phergie / Plugin / IteratorTest.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_Iterator.
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_IteratorTest extends PHPUnit_Framework_TestCase
32 {
33     /**
34      * Iterator instance being tested
35      *
36      * @var Phergie_Plugin_Iterator
37      */
38     protected $iterator;
39
40     /**
41      * List of mock plugin instances to be iterated
42      *
43      * @var array
44      */
45     protected $plugins;
46
47     /**
48      * Initializes the iterator instance being tested.
49      *
50      * @return void
51      */
52     public function setUp()
53     {
54         $this->plugins = array();
55         foreach (range(0, 4) as $index) {
56             $plugin = $this->getMock('Phergie_Plugin_Abstract');
57             $plugin
58                 ->expects($this->any())
59                 ->method('getName')
60                 ->will($this->returnValue($index));
61             $this->plugins[] = $plugin;
62         }
63
64         $this->iterator = new Phergie_Plugin_Iterator(
65             new ArrayIterator($this->plugins)
66         );
67     }
68
69     /**
70      * Tests that all plugins are iterated when no filters are applied.
71      */
72     public function testIteratesAllPluginsWithNoFilters()
73     {
74         $expected = range(0, 4);
75         $actual = array();
76         foreach ($this->iterator as $plugin) {
77             $actual[] = $plugin->getName();
78         }
79         $this->assertEquals($expected, $actual);
80     }
81
82     /**
83      * Tests that appropriate plugins are iterated when plugin name filters
84      * are applied.
85      */
86     public function testIteratesPluginsWithNameFilters()
87     {
88         // Test acceptance of strings and fluent interface implementation
89         $returned = $this->iterator->addPluginFilter('0');
90         $this->assertSame($this->iterator, $returned);
91
92         // Test acceptance of arrays
93         $this->iterator->addPluginFilter(array('1', '3'));
94
95         // Test application of filters to iteration
96         $expected = array('2', '4');
97         $actual = array();
98         foreach ($this->iterator as $plugin) {
99             $actual[] = $plugin->getName();
100         }
101         $this->assertEquals($expected, $actual);
102     }
103
104     /**
105      * Tests that appropriate plugins are iterated when method name filters
106      * are applied.
107      *
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
112      * works.
113      */
114     public function testIteratesPluginsWithMethodFilters()
115     {
116         // Tests acceptance of strings and fluent interface implementation
117         $returned = $this->iterator->addMethodFilter('getName');
118         $this->assertSame($this->iterator, $returned);
119
120         // Test acceptance of arrays
121         $this->iterator->addMethodFilter(array('getName', 'getName'));
122
123         // Test application of filters to iteration
124         $expected = array();
125         $actual = array();
126         foreach ($this->iterator as $plugin) {
127             $actual[] = $plugin->getName();
128         }
129         $this->assertEquals($expected, $actual);
130     }
131
132     /**
133      * Tests that all plugins are iterated after filters are cleared.
134      *
135      * @depends testIteratesPluginsWithNameFilters
136      * @depends testIteratesPluginsWithMethodFilters
137      */
138     public function testIteratesPluginsAfterClearingFilters()
139     {
140         $this->iterator->addPluginFilter('0');
141         $this->iterator->addMethodFilter('method1');
142         $this->iterator->clearFilters();
143
144         $expected = range(0, 4);
145         $actual = array();
146         foreach ($this->iterator as $plugin) {
147             $actual[] = $plugin->getName();
148         }
149         $this->assertEquals($expected, $actual);
150     }
151 }