Unit Testing

This commit is contained in:
Chris Boden 2011-10-24 14:05:54 -04:00
parent 6ac2272114
commit 5b99af0f82
4 changed files with 99 additions and 1 deletions

View File

@ -3,14 +3,26 @@ namespace Ratchet\Logging;
use Monolog\Logger;
class MonologAdapter extends Logger implements LoggerInterface {
/**
* Maps to Monolog\Logger::addInfo
* @param string
*/
function note($msg) {
$this->addInfo($msg);
}
/**
* Maps to Monolog\Logger::addWarning
* @param string
*/
function warning($msg) {
$this->addWarning($msg);
}
/**
* Maps to Monolog\Logger::addError
* @param string
*/
function error($msg) {
$this->addError($msg);
}

View File

@ -0,0 +1,30 @@
<?php
namespace Ratchet\Tests\Logging;
use Ratchet\Logging\NullLogger;
/**
* @covers Ratchet\Logging\NullLogger
*/
class NullLoggerTest extends \PHPUnit_Framework_TestCase {
protected $_log;
public function setUp() {
$this->_log = new NullLogger;
}
public function testInterface() {
$this->assertInstanceOf('\\Ratchet\\Logging\\LoggerInterface', $this->_log);
}
public function testNoteDoesNothing() {
$this->assertNull($this->_log->note('hi'));
}
public function testWarningDoesNothing() {
$this->assertNull($this->_log->warning('hi'));
}
public function testErrorDoesNothing() {
$this->assertNull($this->_log->error('hi'));
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace Ratchet\Tests\Mock;
use Ratchet\Logging\LoggerInterface;
class ArrayLogger implements LoggerInterface {
public $last_msg = '';
public function note($msg) {
$this->last_msg = $msg;
}
public function warning($msg) {
$this->last_msg = $msg;
}
public function error($msg) {
$this->last_msg = $msg;
}
}

View File

@ -3,15 +3,26 @@ namespace Ratchet\Tests;
use Ratchet\Server;
use Ratchet\Tests\Mock\FakeSocket as Socket;
use Ratchet\Tests\Mock\Application as TestApp;
use Ratchet\Tests\Mock\ArrayLogger;
/**
* @covers Ratchet\Server
*/
class ServerTest extends \PHPUnit_Framework_TestCase {
protected $_catalyst;
protected $_server;
public function setUp() {
$this->_server = new Server(new Socket());
$this->_catalyst = new Socket;
$this->_server = new Server($this->_catalyst);
}
protected function getPrivateProperty($class, $name) {
$reflectedClass = new \ReflectionClass($class);
$property = $reflectedClass->getProperty($name);
$property->setAccessible(true);
return $property->getValue($class);
}
public function testServerHasServerInterface() {
@ -19,6 +30,32 @@ class ServerTest extends \PHPUnit_Framework_TestCase {
$this->assertThat($this->_server, $constraint);
}
public function testNullLoggerIsSetInConstruct() {
$this->assertInstanceOf('\\Ratchet\\Logging\\LoggerInterface', $this->getPrivateProperty($this->_server, '_log'));
}
public function testPassedLoggerIsSetInConstruct() {
$logger = new ArrayLogger;
$server = new Server(new Socket(), $logger);
$this->assertSame($logger, $this->getPrivateProperty($server, '_log'));
}
public function testLoggerIsSetInMethod() {
$logger = new ArrayLogger;
$this->_server->setLogger($logger);
$this->assertSame($logger, $this->getPrivateProperty($this->_server, '_log'));
}
public function testGetMasterReturnsCatalyst() {
$this->assertSame($this->_catalyst, $this->_server->getMaster());
}
public function testIteration() {
$this->assertInstanceOf('\\Iterator', $this->_server->getIterator());
}
public function testServerCanNotRunWithoutApplication() {
$this->setExpectedException('\\RuntimeException');
$this->_server->run();