summaryrefslogtreecommitdiff
path: root/plugins/Irc/extlib/phergie/Tests/Phergie/Plugin/TestCase.php
blob: 36b81d6fae307450491c4bb80a7954c7983bbfbc (plain)
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
<?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 classes
 *
 * @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
 */
abstract class Phergie_Plugin_TestCase extends PHPUnit_Framework_TestCase
{
    /**
     * @var Phergie_Event_Handler
     */
    protected $handler;

    /**
     * @var Phergie_Connection
     */
    protected $connection;

    /**
     * @var array
     */
    protected $eventArgs;

    /**
     * @var Phergie_Plugin_Abstract
     */
    protected $plugin;

    /**
     * @var array
     */
    protected $config = array();

    /**
     * Constructs a test case with the given name.
     *
     * @param  string $name
     * @param  array  $data
     * @param  string $dataName
     */
    public function __construct($name = NULL, array $data = array(), $dataName = '')
    {
        parent::__construct($name, $data, $dataName);
        $this->connection = new Phergie_Connection();
        $this->handler    = new Phergie_Event_Handler();
    }

    /**
     * Assert that a given event type exists in the event handler
     * @param string $event
     * @param string $message
     */
    public function assertHasEvent($event, $message = null)
    {
        self::assertTrue($this->handler->hasEventOfType($event), $message);
    }

    /**
     * Assert that a given event type DOES NOT exist in the event handler
     * @param string $event
     * @param string $message
     */
    public function assertDoesNotHaveEvent($event, $message = null)
    {
        self::assertFalse($this->handler->hasEventOfType($event), $message);
    }

    /**
     * Assert that the emitter of the given command event was the given
     * plugin
     *
     * @param Phergie_Event_Command   $event
     * @param Phergie_Plugin_Abstract $plugin
     * @param string                  $message
     */
    public function assertEventEmitter(Phergie_Event_Command $event,
                                       Phergie_Plugin_Abstract $plugin,
                                       $message = null)
    {
        $this->assertSame($plugin, $event->getPlugin(), $message);
    }

    /**
     * Gets the events added to the handler by the plugin
     * @param string $type
     * @return array | null
     */
    public function getResponseEvents($type = null)
    {
        if (is_string($type) && strlen($type) > 0) {
            return $this->handler->getEventsOfType($type);
        }
        return $this->handler->getEvents();
    }

    /**
     * Sets the event for the test
     * @param array $event
     * @param array $eventArgs
     */
    public function setEvent(array $event, array $eventArgs = null)
    {
        $eventClass = 'Phergie_Event_Request';
        if (is_array($event)) {
            $eventClass = $event[0];
            $eventType  = $event[1];
        } else {
            throw new InvalidArgumentException("Invalid value for \$event");
        }
        $event = new $eventClass();
        $event->setType($eventType);
        $event->setArguments($eventArgs);
        $this->plugin->setEvent($event);
        $this->eventArgs = $eventArgs;
    }

    /**
     * Sets the plugin to be tested
     * If a plugin requries config for testing, an array placed in
     * $this->config will be parsed into a Phergie_Config object and
     * attached to the plugin
     */
    protected function setPlugin(Phergie_Plugin_Abstract $plugin)
    {
        $this->plugin = $plugin;
        $this->plugin->setEventHandler($this->handler);
        $this->plugin->setConnection($this->connection);
        $this->connection->setNick('test');
        if (!empty($this->config)) {
            $config = new Phergie_Config();
            foreach ($this->config as $configKey => $configValue) {
                $config[$configKey] = $configValue;
            }
            $plugin->setConfig($config);
        }
    }

    /**
     * Overrides the runTest method to add additional annotations
     * @return PHPUnit_Framework_TestResult
     */
    protected function runTest()
    {
        if (null === $this->plugin) {
            throw new RuntimeException(
                    'Tests cannot be run before plugin is set'
            );
        }
        
        // Clean the event handler... important!
        $this->handler->clearEvents();

        $info      = $this->getAnnotations();
        $event     = null;
        $eventArgs = array();
        if (isset($info['method']['event']) && isset($info['method']['event'][0])) {
            if (!is_string($info['method']['event'][0])) {
                throw new InvalidArgumentException(
                        'Only one event may be specified'
                );
            }
            $event = $info['method']['event'][0];

            if (stristr($event, '::')) {
                $event = explode('::', $event);
            }
        }
        if (isset($info['method']['eventArg'])) {
            $eventArgs = $info['method']['eventArg'];
        }
        if (null !== $event) {
            $this->setEvent($event, $eventArgs);
        }

        $testResult = parent::runTest();

        // Clean the event handler again... just incase this time.
        $this->handler->clearEvents();

        return $testResult;
    }

}