Commit: 1263141227c41f40561af7f9526ede6b51e9a8bf

Author: gwoo | Date: 2010-01-28 21:23:23 -0800
adding some tests for Irc command
diff --git a/extensions/command/bot/Irc.php b/extensions/command/bot/Irc.php index 802d857..b771f09 100644 --- a/extensions/command/bot/Irc.php +++ b/extensions/command/bot/Irc.php @@ -66,7 +66,6 @@ class Irc extends \lithium\console\Command { $this->out('connected'); $this->_connect(); } - while($this->_running && !$this->socket->eof()) { $this->_process(); } @@ -75,8 +74,7 @@ class Irc extends \lithium\console\Command { public function __call($method, $params) { if ($method[0] === '_') { $value = empty($params) ? $this->{$method} : $params[0]; - $command = strtoupper(ltrim($method, '_')) . " {$value} \r\n"; - $this->out($command); + $command = strtoupper(ltrim($method, '_')) . " {$value}\r\n"; return $this->socket->write($command); } } @@ -107,8 +105,6 @@ class Irc extends \lithium\console\Command { $cmd = $params[2]; $msg = !empty($params[4]) ? $params[4] : null; - //$this->out($cmd); - switch ($cmd) { case 'PRIVMSG': $channel = $params[3]; diff --git a/tests/cases/extensions/command/bot/IrcTest.php b/tests/cases/extensions/command/bot/IrcTest.php index 91da070..801caa1 100644 --- a/tests/cases/extensions/command/bot/IrcTest.php +++ b/tests/cases/extensions/command/bot/IrcTest.php @@ -8,34 +8,60 @@ namespace li3_bot\tests\cases\extensions\command\bot; -use \li3_bot\extensions\command\bot\Irc; use \lithium\console\Request; use \lithium\console\Response; +use \li3_bot\tests\mocks\extensions\command\bot\MockIrc; +use li3_bot\tests\mocks\extensions\command\MockIrcSocket; -use \lithium\core\Libraries; class IrcTest extends \lithium\test\Unit { public function setUp() { - $this->irc = new Irc(array('init' => false)); + $this->irc = new MockIrc(array('init' => false, 'host' => 'localhost')); $this->irc->request = new Request(array('input' => fopen('php://temp', 'w+'))); $this->irc->response = new Response(array( 'output' => fopen('php://temp', 'w+'), 'error' => fopen('php://temp', 'w+') )); - - $this->working = LITHIUM_APP_PATH; - if (!empty($_SERVER['PWD'])) { - $this->working = $_SERVER['PWD']; - } + $this->irc->socket = new MockIrcSocket(); } public function tearDown() { unset($this->irc); } - + public function testRun() { - + $result = $this->irc->run(); + $resource = $this->irc->socket->resource(); + rewind($resource); + rewind($this->irc->response->output); + + $expected = "connected\n"; + $result = fgets($this->irc->response->output); + $this->assertEqual($expected, $result); + + $expected = "NICK li3_bot\r\nUSER li3_bot localhost botts :li3_bot\r\n"; + $result = fread($resource, 1024); + $this->assertEqual($expected, $result); + } + + public function testProcess() { + $this->irc->run(); + $resource = $this->irc->socket->resource(); + rewind($resource); + fwrite($resource, 'something'); + rewind($resource); + $result = $this->irc->process(); + + rewind($this->irc->response->output); + + $expected = "connected\n"; + $result = fgets($this->irc->response->output); + $this->assertEqual($expected, $result); + + $expected = "something"; + $result = fread($resource, 1024); + $this->assertEqual($expected, $result); } }