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 classes
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 abstract class Phergie_Plugin_TestCase extends PHPUnit_Framework_TestCase
34 * @var Phergie_Event_Handler
39 * @var Phergie_Connection
41 protected $connection;
49 * @var Phergie_Plugin_Abstract
56 protected $config = array();
59 * Constructs a test case with the given name.
63 * @param string $dataName
65 public function __construct($name = NULL, array $data = array(), $dataName = '')
67 parent::__construct($name, $data, $dataName);
68 $this->connection = new Phergie_Connection();
69 $this->handler = new Phergie_Event_Handler();
73 * Assert that a given event type exists in the event handler
74 * @param string $event
75 * @param string $message
77 public function assertHasEvent($event, $message = null)
79 self::assertTrue($this->handler->hasEventOfType($event), $message);
83 * Assert that a given event type DOES NOT exist in the event handler
84 * @param string $event
85 * @param string $message
87 public function assertDoesNotHaveEvent($event, $message = null)
89 self::assertFalse($this->handler->hasEventOfType($event), $message);
93 * Assert that the emitter of the given command event was the given
96 * @param Phergie_Event_Command $event
97 * @param Phergie_Plugin_Abstract $plugin
98 * @param string $message
100 public function assertEventEmitter(Phergie_Event_Command $event,
101 Phergie_Plugin_Abstract $plugin,
104 $this->assertSame($plugin, $event->getPlugin(), $message);
108 * Gets the events added to the handler by the plugin
109 * @param string $type
110 * @return array | null
112 public function getResponseEvents($type = null)
114 if (is_string($type) && strlen($type) > 0) {
115 return $this->handler->getEventsOfType($type);
117 return $this->handler->getEvents();
121 * Sets the event for the test
122 * @param array $event
123 * @param array $eventArgs
125 public function setEvent(array $event, array $eventArgs = null)
127 $eventClass = 'Phergie_Event_Request';
128 if (is_array($event)) {
129 $eventClass = $event[0];
130 $eventType = $event[1];
132 throw new InvalidArgumentException("Invalid value for \$event");
134 $event = new $eventClass();
135 $event->setType($eventType);
136 $event->setArguments($eventArgs);
137 $this->plugin->setEvent($event);
138 $this->eventArgs = $eventArgs;
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
147 protected function setPlugin(Phergie_Plugin_Abstract $plugin)
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;
158 $plugin->setConfig($config);
163 * Overrides the runTest method to add additional annotations
164 * @return PHPUnit_Framework_TestResult
166 protected function runTest()
168 if (null === $this->plugin) {
169 throw new RuntimeException(
170 'Tests cannot be run before plugin is set'
174 // Clean the event handler... important!
175 $this->handler->clearEvents();
177 $info = $this->getAnnotations();
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'
186 $event = $info['method']['event'][0];
188 if (stristr($event, '::')) {
189 $event = explode('::', $event);
192 if (isset($info['method']['eventArg'])) {
193 $eventArgs = $info['method']['eventArg'];
195 if (null !== $event) {
196 $this->setEvent($event, $eventArgs);
199 $testResult = parent::runTest();
201 // Clean the event handler again... just incase this time.
202 $this->handler->clearEvents();