/** * 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; }
/** * Modifies the event handler to include an expectation of an event NOT * being added by the plugin being tested. Note that this must be called * BEFORE executing plugin code that may initiate the event. * * @param string $type Event type * @param array $args Optional enumerated array of event arguments * * @return void */ protected function assertDoesNotEmitEvent($type, array $args = array()) { // Ugly hack to get around an issue in PHPUnit // @link http://github.com/sebastianbergmann/phpunit-mock-objects/issues/issue/5#issue/5/comment/343524 $callback = create_function('$plugin, $type, $args', 'if (get_class($plugin) == "' . $this->pluginClass . '" && $type == "' . $type . '" && $args == "' . var_export($args, true) . '") { trigger_error("Instance of ' . $this->pluginClass . ' unexpectedly emitted event of type ' . $type . '", E_USER_ERROR); }'); $this->events->expects($this->any())->method('addEvent')->will($this->returnCallback($callback)); }
/** * Tests that the handler supports iteration of the events it contains. * * @return void * @depends testAddEventWithValidData */ public function testImplementsGetIterator() { $reflector = new ReflectionClass('Phergie_Event_Handler'); $this->assertTrue($reflector->implementsInterface('IteratorAggregate')); $this->addMockEvent(); $events = $this->events->getEvents(); $expected = array_shift($events); foreach ($this->events as $actual) { $this->assertSame($expected, $actual); } }
/** * Sends resulting outgoing events from earlier processing in handleEvents(). * * @param Phergie_Connection $connection Active connection * * @return void */ protected function processEvents(Phergie_Connection $connection) { $this->plugins->preDispatch(); if (count($this->events)) { foreach ($this->events as $event) { $this->ui->onCommand($event, $connection); $method = 'do' . ucfirst(strtolower($event->getType())); call_user_func_array(array($this->driver, $method), $event->getArguments()); } } $this->plugins->postDispatch(); if ($this->events->hasEventOfType(Phergie_Event_Request::TYPE_QUIT)) { $this->ui->onQuit($connection); $this->connections->removeConnection($connection); } $this->events->clearEvents(); }
/** * addPlugin passes Phergie_Event_Handler to instantiated plugin * * @return null */ public function testAddPluginPassesPhergieEventHandlerToInstantiatedPlugin() { $plugin = $this->getMock('Phergie_Plugin_Abstract'); $plugin->expects($this->any())->method('getName')->will($this->returnValue('TestPlugin')); $my_event_handler = new Phergie_Event_Handler(); $my_event_handler->addEvent($plugin, 'ping'); // create a new plugin handler with this event handler unset($this->handler); $this->handler = new Phergie_Plugin_Handler(new Phergie_Config(), $my_event_handler); $plugin_name = 'Mock'; $this->handler->addPath(dirname(__FILE__), 'Phergie_Plugin_'); $plugin = $this->handler->addPlugin($plugin_name); $this->assertSame($my_event_handler, $plugin->getEventHandler(), 'addPlugin passes Phergie_Event_Handler to instantiated plugin'); }