Skip to content
This repository has been archived by the owner on Mar 12, 2020. It is now read-only.

Commit

Permalink
Initial code to address #9
Browse files Browse the repository at this point in the history
BotTest::testPluginEmittedEvents() is failing for unknown reasons
  • Loading branch information
elazar committed Mar 31, 2015
1 parent 317286f commit 6f484fa
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 35 deletions.
34 changes: 17 additions & 17 deletions src/Bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ class Bot
protected $converter;

/**
* Queue for events generated by plugins to be sent to servers
* Event queue factory for creating connection-specific event queues
*
* @var \Phergie\Irc\Bot\React\EventQueueInterface
* @var \Phergie\Irc\Bot\React\EventQueueFactoryInterface
*/
protected $queue;
protected $queueFactory;

/**
* Sets the IRC client for the bot to use.
Expand Down Expand Up @@ -198,26 +198,26 @@ public function getConverter()
}

/**
* Sets the event queue for the bot to use.
* Sets the event queue factory for the bot to use.
*
* @param \Phergie\Irc\Bot\React\EventQueueInterface $queue
* @param \Phergie\Irc\Bot\React\EventQueueFactoryInterface $queueFactory
*/
public function setEventQueue(EventQueueInterface $queue)
public function setEventQueueFactory(EventQueueFactoryInterface $queueFactory)
{
$this->queue = $queue;
$this->queueFactory = $queueFactory;
}

/**
* Returns the event queue in use by the bot.
* Returns the event queue factory in use by the bot.
*
* @return \Phergie\Irc\Bot\React\EventQueueInterface
* @return \Phergie\Irc\Bot\React\EventQueueFactoryInterface
*/
public function getEventQueue()
public function getEventQueueFactory()
{
if (!$this->queue) {
$this->queue = new EventQueue;
if (!$this->queueFactory) {
$this->queueFactory = new EventQueueFactory;
}
return $this->queue;
return $this->queueFactory;
}

/**
Expand Down Expand Up @@ -258,8 +258,8 @@ protected function setDependencyOverrides(array $config)
$this->setConverter($config['converter']);
}

if (isset($config['eventQueue'])) {
$this->setEventQueue($config['eventQueue']);
if (isset($config['eventQueueFactory'])) {
$this->setEventQueueFactory($config['eventQueueFactory']);
}
}

Expand Down Expand Up @@ -472,7 +472,7 @@ public function processClientEvent($event, array $message, ConnectionInterface $
$converted->setConnection($connection);

$client = $this->getClient();
$queue = $this->getEventQueue();
$queue = $this->getEventQueueFactory()->getEventQueue($connection);
$params = array($converted, $queue);
$subtype = $this->getEventSubtype($converted);
$client->emit($event . '.each', $params);
Expand All @@ -493,7 +493,7 @@ public function processClientEvent($event, array $message, ConnectionInterface $
public function processOutgoingEvents(ConnectionInterface $connection, WriteStream $write)
{
$client = $this->getClient();
$queue = $this->getEventQueue();
$queue = $this->getEventQueueFactory()->getEventQueue($connection);

$client->emit('irc.sending.all', array($queue));
while ($extracted = $queue->extract()) {
Expand Down
50 changes: 50 additions & 0 deletions src/EventQueueFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Phergie (http://phergie.org)
* @copyright Copyright (c) 2008-2014 Phergie Development Team (http://phergie.org)
* @license http://phergie.org/license Simplified BSD License
* @package Phergie\Irc\Bot\React
*/

namespace Phergie\Irc\Bot\React;

use Phergie\Irc\ConnectionInterface;

/**
* Default implementation of a factory to maintain connection-specific event
* queue objects.
*
* @category Phergie
* @package Phergie\Irc\Bot\React
*/
class EventQueueFactory implements EventQueueFactoryInterface
{
/**
* Stores event queues keyed by connection
*
* @var \SplObjectStorage
*/
protected $queues;

/**
* Initializes storage for event queues.
*/
public function __construct()
{
$this->queues = new \SplObjectStorage;
}

/**
* Returns the event queue for a specified connection.
*
* @param \Phergie\Irc\ConnectionInterface $connection
* @return \Phergie\Irc\Bot\React\EventQueueInterface
*/
public function getEventQueue(ConnectionInterface $connection)
{
if (!isset($this->queues[$connection])) {
$this->queues[$connection] = new EventQueue;
}
return $this->queues[$connection];
}
}
28 changes: 28 additions & 0 deletions src/EventQueueFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Phergie (http://phergie.org)
* @copyright Copyright (c) 2008-2014 Phergie Development Team (http://phergie.org)
* @license http://phergie.org/license Simplified BSD License
* @package Phergie\Irc\Bot\React
*/

namespace Phergie\Irc\Bot\React;

use Phergie\Irc\ConnectionInterface;

/**
* Interface for a factory to maintain connection-specific event queue objects.
*
* @category Phergie
* @package Phergie\Irc\Bot\React
*/
interface EventQueueFactoryInterface
{
/**
* Returns the event queue for a specified connection.
*
* @param \Phergie\Irc\ConnectionInterface $connection
* @return \Phergie\Irc\Bot\React\EventQueueInterface
*/
public function getEventQueue(ConnectionInterface $connection);
}
55 changes: 37 additions & 18 deletions tests/BotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Phergie\Irc\Bot\React\AbstractPlugin;
use Phergie\Irc\Bot\React\Bot;
use Phergie\Irc\Bot\React\EventQueue;
use Phergie\Irc\Bot\React\EventQueueFactory;

/**
* Tests for Bot class.
Expand Down Expand Up @@ -145,23 +146,23 @@ public function testGetConverter()
}

/**
* Tests setEventQueue().
* Tests setEventQueueFactory().
*/
public function testSetEventQueue()
public function testSetEventQueueFactory()
{
$queue = $this->getMockEventQueue();
$this->bot->setEventQueue($queue);
$this->assertSame($queue, $this->bot->getEventQueue());
$queue = $this->getMockEventQueueFactory();
$this->bot->setEventQueueFactory($queue);
$this->assertSame($queue, $this->bot->getEventQueueFactory());
}

/**
* Tests getEventQueue().
* Tests getEventQueueFactory().
*/
public function testGetEventQueue()
public function testGetEventQueueFactory()
{
$this->assertInstanceOf(
'\Phergie\Irc\Bot\React\EventQueueInterface',
$this->bot->getEventQueue()
'\Phergie\Irc\Bot\React\EventQueueFactoryInterface',
$this->bot->getEventQueueFactory()
);
}

Expand Down Expand Up @@ -442,16 +443,18 @@ public function testEventCallbacks(EventInterface $eventObject, $eventType, $eve
Phake::when($parser)->parse($message)->thenReturn($message);
$this->bot->setParser($parser);

$queue = $this->getMockEventQueue();
$this->bot->setEventQueue($queue);

$client = new \Phergie\Irc\Client\React\Client;
$this->bot->setClient($client);

$write = $params[] = $this->getMockWriteStream();
$connection = $params[] = $this->getMockConnection();
$logger = $params[] = $this->getMockLogger();

$queue = $this->getMockEventQueue();
$queueFactory = $this->getMockEventQueueFactory();
Phake::when($queueFactory)->getEventQueue($connection)->thenReturn($queue);
$this->bot->setEventQueueFactory($queueFactory);

$test = $this;
$allCalled = false;
$typeCalled = false;
Expand Down Expand Up @@ -485,7 +488,6 @@ public function testTickEvent()

$queue = $this->getMockEventQueue();
Phake::when($queue)->extract()->thenReturn($eventObject)->thenReturn(false);
$this->bot->setEventQueue($queue);

$client = new \Phergie\Irc\Client\React\Client;
$this->bot->setClient($client);
Expand Down Expand Up @@ -522,6 +524,11 @@ function($otherEvent, $otherQueue)

$write = $params[] = $this->getMockWriteStream();
$connection = $params[] = $this->getMockConnection();

$queueFactory = $this->getMockEventQueueFactory();
Phake::when($queueFactory)->getEventQueue($connection)->thenReturn($queue);
$this->bot->setEventQueueFactory($queueFactory);

$client->emit('irc.tick', $params);

Phake::verify($eventObject)->setConnection($connection);
Expand Down Expand Up @@ -564,8 +571,10 @@ public function testPluginEmittedEvents($event, $class, $method)
$connection = $this->getMockConnection();
$connections = array($connection);

$queue = new EventQueue;
$this->bot->setEventQueue($queue);
$queue = $this->getMockEventQueue();
$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');
Expand Down Expand Up @@ -622,14 +631,14 @@ public function testOverrideDependencies()
$logger = $this->getMockLogger();
$parser = $this->getMockParser();
$converter = $this->getMockConverter();
$eventQueue = $this->getMockEventQueue();
$eventQueueFactory = $this->getMockEventQueueFactory();

$config = array(
'client' => $client,
'logger' => $logger,
'parser' => $parser,
'converter' => $converter,
'eventQueue' => $eventQueue,
'eventQueueFactory' => $eventQueueFactory,
'plugins' => array(),
'connections' => array($this->getMockConnection()),
);
Expand All @@ -641,7 +650,7 @@ public function testOverrideDependencies()
$this->assertSame($logger, $this->bot->getLogger());
$this->assertSame($parser, $this->bot->getParser());
$this->assertSame($converter, $this->bot->getConverter());
$this->assertSame($eventQueue, $this->bot->getEventQueue());
$this->assertSame($eventQueueFactory, $this->bot->getEventQueueFactory());
}

/*** SUPPORTING METHODS ***/
Expand Down Expand Up @@ -696,6 +705,16 @@ protected function getMockEventQueue()
return Phake::mock('\Phergie\Irc\Bot\React\EventQueueInterface');
}

/**
* Returns a mock event queue factory.
*
* @return \Phergie\Irc\Bot\React\EventQueueFactoryInterface
*/
protected function getMockEventQueueFactory()
{
return Phake::mock('\Phergie\Irc\Bot\React\EventQueueFactoryInterface');
}

/**
* Returns a mock plugin.
*
Expand Down

0 comments on commit 6f484fa

Please sign in to comment.