]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Tests/Phergie/Plugin/TestCase.php
Added Phergie PHP IRC library
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Tests / Phergie / Plugin / TestCase.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 classes
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 abstract class Phergie_Plugin_TestCase extends PHPUnit_Framework_TestCase
32 {
33     /**
34      * @var Phergie_Event_Handler
35      */
36     protected $handler;
37
38     /**
39      * @var Phergie_Connection
40      */
41     protected $connection;
42
43     /**
44      * @var array
45      */
46     protected $eventArgs;
47
48     /**
49      * @var Phergie_Plugin_Abstract
50      */
51     protected $plugin;
52
53     /**
54      * @var array
55      */
56     protected $config = array();
57
58     /**
59      * Constructs a test case with the given name.
60      *
61      * @param  string $name
62      * @param  array  $data
63      * @param  string $dataName
64      */
65     public function __construct($name = NULL, array $data = array(), $dataName = '')
66     {
67         parent::__construct($name, $data, $dataName);
68         $this->connection = new Phergie_Connection();
69         $this->handler    = new Phergie_Event_Handler();
70     }
71
72     /**
73      * Assert that a given event type exists in the event handler
74      * @param string $event
75      * @param string $message
76      */
77     public function assertHasEvent($event, $message = null)
78     {
79         self::assertTrue($this->handler->hasEventOfType($event), $message);
80     }
81
82     /**
83      * Assert that a given event type DOES NOT exist in the event handler
84      * @param string $event
85      * @param string $message
86      */
87     public function assertDoesNotHaveEvent($event, $message = null)
88     {
89         self::assertFalse($this->handler->hasEventOfType($event), $message);
90     }
91
92     /**
93      * Assert that the emitter of the given command event was the given
94      * plugin
95      *
96      * @param Phergie_Event_Command   $event
97      * @param Phergie_Plugin_Abstract $plugin
98      * @param string                  $message
99      */
100     public function assertEventEmitter(Phergie_Event_Command $event,
101                                        Phergie_Plugin_Abstract $plugin,
102                                        $message = null)
103     {
104         $this->assertSame($plugin, $event->getPlugin(), $message);
105     }
106
107     /**
108      * Gets the events added to the handler by the plugin
109      * @param string $type
110      * @return array | null
111      */
112     public function getResponseEvents($type = null)
113     {
114         if (is_string($type) && strlen($type) > 0) {
115             return $this->handler->getEventsOfType($type);
116         }
117         return $this->handler->getEvents();
118     }
119
120     /**
121      * Sets the event for the test
122      * @param array $event
123      * @param array $eventArgs
124      */
125     public function setEvent(array $event, array $eventArgs = null)
126     {
127         $eventClass = 'Phergie_Event_Request';
128         if (is_array($event)) {
129             $eventClass = $event[0];
130             $eventType  = $event[1];
131         } else {
132             throw new InvalidArgumentException("Invalid value for \$event");
133         }
134         $event = new $eventClass();
135         $event->setType($eventType);
136         $event->setArguments($eventArgs);
137         $this->plugin->setEvent($event);
138         $this->eventArgs = $eventArgs;
139     }
140
141     /**
142      * Sets the plugin to be tested
143      * If a plugin requries config for testing, an array placed in
144      * $this->config will be parsed into a Phergie_Config object and
145      * attached to the plugin
146      */
147     protected function setPlugin(Phergie_Plugin_Abstract $plugin)
148     {
149         $this->plugin = $plugin;
150         $this->plugin->setEventHandler($this->handler);
151         $this->plugin->setConnection($this->connection);
152         $this->connection->setNick('test');
153         if (!empty($this->config)) {
154             $config = new Phergie_Config();
155             foreach ($this->config as $configKey => $configValue) {
156                 $config[$configKey] = $configValue;
157             }
158             $plugin->setConfig($config);
159         }
160     }
161
162     /**
163      * Overrides the runTest method to add additional annotations
164      * @return PHPUnit_Framework_TestResult
165      */
166     protected function runTest()
167     {
168         if (null === $this->plugin) {
169             throw new RuntimeException(
170                     'Tests cannot be run before plugin is set'
171             );
172         }
173         
174         // Clean the event handler... important!
175         $this->handler->clearEvents();
176
177         $info      = $this->getAnnotations();
178         $event     = null;
179         $eventArgs = array();
180         if (isset($info['method']['event']) && isset($info['method']['event'][0])) {
181             if (!is_string($info['method']['event'][0])) {
182                 throw new InvalidArgumentException(
183                         'Only one event may be specified'
184                 );
185             }
186             $event = $info['method']['event'][0];
187
188             if (stristr($event, '::')) {
189                 $event = explode('::', $event);
190             }
191         }
192         if (isset($info['method']['eventArg'])) {
193             $eventArgs = $info['method']['eventArg'];
194         }
195         if (null !== $event) {
196             $this->setEvent($event, $eventArgs);
197         }
198
199         $testResult = parent::runTest();
200
201         // Clean the event handler again... just incase this time.
202         $this->handler->clearEvents();
203
204         return $testResult;
205     }
206
207 }