Skip to content

Commit

Permalink
Start of a unit test suite and tweaks for Phergie_Driver_Streams and …
Browse files Browse the repository at this point in the history
…related classes
  • Loading branch information
elazar committed Aug 23, 2010
1 parent e1031d4 commit 24ffc23
Show file tree
Hide file tree
Showing 6 changed files with 546 additions and 49 deletions.
12 changes: 3 additions & 9 deletions Phergie/Driver/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,18 @@ class Phergie_Driver_Exception extends Phergie_Exception
*/
const ERR_NO_ACTIVE_CONNECTION = 1;

/**
* Error indicating that an operation was requested requiring an active
* connection where one had been set but not initiated
*/
const ERR_NO_INITIATED_CONNECTION = 2;

/**
* Error indicating that an attempt to initiate a connection failed
*/
const ERR_CONNECTION_ATTEMPT_FAILED = 3;
const ERR_CONNECTION_ATTEMPT_FAILED = 2;

/**
* Error indicating that an attempt to send data via a connection failed
*/
const ERR_CONNECTION_WRITE_FAILED = 4;
const ERR_CONNECTION_WRITE_FAILED = 3;

/**
* Error indicating that an attempt to read data via a connection failed
*/
const ERR_CONNECTION_READ_FAILED = 5;
const ERR_CONNECTION_READ_FAILED = 4;
}
11 changes: 0 additions & 11 deletions Phergie/Driver/Streams.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,6 @@ protected function send($command, $args = '')
$connection = $this->getConnection();
$encoding = $connection->getEncoding();

// Require an open socket connection to continue
if (empty($this->socket)) {
throw new Phergie_Driver_Exception(
'doConnect() must be called first',
Phergie_Driver_Exception::ERR_NO_INITIATED_CONNECTION
);
}

// Add the command
$buffer = $command;

Expand Down Expand Up @@ -361,9 +353,6 @@ public function getEvent()
*/
public function doConnect()
{
// Listen for input indefinitely
set_time_limit(0);

// Get connection information
$connection = $this->getConnection();
$hostname = $connection->getHost();
Expand Down
29 changes: 29 additions & 0 deletions Tests/Phergie/BotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ class Phergie_BotTest extends Phergie_TestCase
*/
private $bot;

/**
* Mock driver
*
* @var Phergie_Driver_Abstract
*/
private $driver;

/**
* Instantiates the class to test.
*
Expand All @@ -47,6 +54,28 @@ public function setUp()
$this->bot = new Phergie_Bot;
}

/**
* Returns a mock driver instance.
*
* @return Phergie_Driver_Abstract
*/
private function getMockDriver()
{
if (empty($this->driver)) {
if (!class_exists('Phergie_Driver_Mock', false)) {
$this->driver = $this->getMock(
'Phergie_Driver_Abstract',
array(),
array(),
'Phergie_Driver_Mock'
);
} else {
$this->driver = new Phergie_Driver_Mock;
}
}
return $this->driver;
}

/**
* Tests that a default configuration object is used if none is set.
*
Expand Down
265 changes: 265 additions & 0 deletions Tests/Phergie/Driver/StreamsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
<?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 Phergie_Driver_Streams.
*
* @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
*/
class Phergie_Driver_StreamsTest extends Phergie_TestCase
{
/**
* Instance of the class to test
*
* @var Phergie_Driver_Streams
*/
private $driver;

/**
* Fake daemon process acting as an IRC server to test the driver
*
* @var Phergie_FakeDaemon
*/
private $server;

/**
* Instantiates the class to test and its mock dependencies.
*
* @return void
*/
public function setUp()
{
$this->server = new Phergie_FakeDaemon;
$this->driver = new Phergie_Driver_Streams;
}

/**
* Returns a mock connection.
*
* @return Phergie_Connection
*/
protected function getMockConnection()
{
$options = array(
'host' => '0.0.0.0',
'port' => $this->server->getPort(),
'username' => 'username',
'realname' => 'realname',
'transport' => 'tcp'
);
$connection = parent::getMockConnection();
foreach ($options as $key => $value) {
$connection
->expects($this->any())
->method('get' . ucfirst($key))
->will($this->returnValue($value));
}
return $connection;
}

/**
* Tests that a default socket timeout is used if none is set.
*
* @return void
*/
public function testGetTimeout()
{
$this->assertSame(0.1, $this->driver->getTimeout());
}

/**
* Tests that a custom socket timeout can be set.
*
* @return void
*/
public function testSetTimeout()
{
$timeout = 0.2;
$this->driver->setTimeout($timeout);
$this->assertequals($timeout, $this->driver->getTimeout());
}

/**
* Tests that attempting to retrieve a connection before one is set
* results in an exception.
*
* @return void
*/
public function testGetConnection()
{
try {
$this->driver->getConnection();
$this->fail('Expected exception was not thrown');
} catch (Phergie_Driver_Exception $e) {
if ($e->getCode() != Phergie_Driver_Exception::ERR_NO_ACTIVE_CONNECTION) {
$this->fail('Unexpected exception code: ' . $e->getCode());
}
}
}

/**
* Tests that a new connection can be set and retrieved.
*
* @return void
*/
public function testSetConnectionWithNewConnection()
{
$connection = $this->getMockConnection();
$this->driver->setConnection($connection);
$this->assertSame($connection, $this->driver->getConnection());
}

/**
* Tests connecting to a server without a password.
*
* @return void
* @depends testSetConnectionWithNewConnection
*/
public function testDoConnectWithoutPassword()
{
$this->server->get();
$this->server->run();

$connection = $this->getMockConnection();
$this->driver->setConnection($connection);
$this->driver->doConnect();
$this->driver->doQuit();
$this->server->close();

$expected =
sprintf(
'USER %s %s %s %s',
$connection->getUsername(),
$connection->getHost(),
$connection->getHost(),
':' . $connection->getRealname()
) . "\r\n"
. 'NICK :' . $connection->getNick() . "\r\n"
. 'QUIT' . "\r\n";

$this->assertSame($expected, $this->server->getInput());
}

/**
* Tests connecting to a server with a password.
*
* @return void
* @depends testSetConnectionWithNewConnection
*/
public function testDoConnectWithPassword()
{
$this->server->get();
$this->server->run();

$connection = $this->getMockConnection();
$connection
->expects($this->any())
->method('getPassword')
->will($this->returnValue('password'));

$this->driver->setConnection($connection);
$this->driver->doConnect();
$this->driver->doQuit();
$this->server->close();

$expected =
'PASS :' . $connection->getPassword() . "\r\n"
. sprintf(
'USER %s %s %s %s',
$connection->getUsername(),
$connection->getHost(),
$connection->getHost(),
':' . $connection->getRealname()
) . "\r\n"
. 'NICK :' . $connection->getNick() . "\r\n"
. 'QUIT' . "\r\n";

$this->assertSame($expected, $this->server->getInput());
}

/**
* Tests that an exception is thrown if a socket error occurs when
* attempting to connect to a server.
*
* @return void
*/
public function testDoConnectHandlesSocketException()
{
$timeout = ini_get('default_socket_timeout');
ini_set('default_socket_timeout', 1);

$this->driver->setConnection($this->getMockConnection());

try {
$this->driver->doConnect();
$this->fail('Expected exception was not thrown');
} catch (Phergie_Driver_Exception $e) {
if ($e->getCode() != Phergie_Driver_Exception::ERR_CONNECTION_ATTEMPT_FAILED) {
$this->fail('Unexpected exception code: ' . $e->getCode());
}
}

ini_set('default_socket_timeout', $timeout);
}

/**
* Tests that a connection can be made the active connection following
* its initial establishment.
*
* @return void
* @depends testSetConnectionWithNewConnection
* @depends testDoConnectWithoutPassword
*/
public function testSetConnectionWithExistingConnection()
{
$this->server->get();
$this->server->run();

$connection = $this->getMockConnection();
$this->driver->setConnection($connection);
$this->driver->doConnect();
$this->driver->setConnection($connection);
$this->driver->doQuit();
$this->server->close();
}

/**
* Tests that an attempt to send a command without an active connection
* results in an exception.
*
* @return void
*/
public function testSendWithoutConnectionThrowsException()
{
try {
$this->driver->doQuit();
$this->fail('Expected exception not thrown');
} catch (Phergie_Driver_Exception $e) {
if ($e->getCode() != Phergie_Driver_Exception::ERR_NO_ACTIVE_CONNECTION) {
$this->fail('Unexpected exception code: ' . $e->getCode());
}
}
}
}
Loading

0 comments on commit 24ffc23

Please sign in to comment.