/**
* Tests that plugins can emit events.
*
* @param string $event Name of the plugin-emitted event
* @param string $class Class of the emitted event
* @param string $method Method invoked to queue the event
* @param array $params Parameters passed to the method invoked to queue
* the event
* @dataProvider dataProviderPluginEmittedEvents
*/
public function testPluginEmittedEvents($event, $class, $method)
{
$message = array('foo' => 'bar');
$write = $this->getMockWriteStream();
$logger = $this->getMockLogger();
$connection = $this->getMockConnection();
$connections = array($connection);
$queue = new EventQueue();
$queueFactory = $this->getMockEventQueueFactory();
Phake::when($queueFactory)->getEventQueue($connection)->thenReturn($queue);
$this->bot->setEventQueueFactory($queueFactory);
$eventObject = Phake::mock('\\Phergie\\Irc\\Event\\UserEvent');
$eventParams = array('#channel', 'message');
Phake::when($eventObject)->getCommand()->thenReturn('PRIVMSG');
Phake::when($eventObject)->getParams()->thenReturn($eventParams);
$converter = $this->getMockConverter();
Phake::when($converter)->convert($message)->thenReturn($eventObject);
$this->bot->setConverter($converter);
$plugin = $this->getMockTestPlugin();
Phake::when($plugin)->getSubscribedEvents()->thenReturn(array('irc.received.privmsg' => 'handleEvent'));
$callback = function ($eventObject, $queue) use($method, $eventParams) {
call_user_func_array(array($queue, $method), $eventParams);
};
Phake::when($plugin)->handleEvent($eventObject, $queue)->thenReturnCallback($callback);
$config = array('plugins' => array($plugin), 'connections' => $connections);
$this->bot->setConfig($config);
$client = Phake::partialMock('\\Phergie\\Irc\\Client\\React\\Client');
Phake::when($client)->run($connections)->thenReturn(null);
$this->bot->setClient($client);
$this->bot->run();
$client->emit('irc.received', array($message, $write, $connection, $logger));
Phake::verify($client)->emit('irc.sending.all', Phake::capture($allParams));
$this->assertSame($queue, $allParams[0]);
Phake::verify($client)->emit('irc.sending.each', Phake::capture($eachParams));
$this->assertInstanceOf($class, $eachParams[0]);
$this->assertSame($queue, $eachParams[1]);
Phake::verify($client)->emit('irc.sending.' . $event, $eachParams);
call_user_func_array(array(Phake::verify($write), $method), $eventParams);
}