Started observer pattern, API updates, unit tests

This commit is contained in:
Chris Boden 2011-09-05 13:10:57 -04:00
parent 5ce4a7b837
commit f17f59856e
11 changed files with 83 additions and 11 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
phpunit.xml phpunit.xml
coverage coverage
sandbox

View File

@ -2,4 +2,14 @@
namespace Ratchet; namespace Ratchet;
interface ApplicationInterface { interface ApplicationInterface {
/**
* @return string
*/
function getName();
function onConnect();
function onMessage();
function onClose();
} }

View File

@ -4,6 +4,10 @@ namespace Ratchet;
class Exception extends \Exception { class Exception extends \Exception {
public function __construct() { public function __construct() {
$int = socket_last_error(); $int = socket_last_error();
parent::__construct(socket_strerror($int), $int); $msg = socket_strerror($int);
// todo, replace {$msg: $int} to {$msg}
parent::__construct($msg, $int);
} }
} }

View File

@ -1,6 +0,0 @@
<?php
namespace Ratchet\Protocol\WebSocket;
use Ratchet\ApplicationInterface as AI;
class ApplicationInterface extends AI {
}

View File

@ -2,6 +2,7 @@
namespace Ratchet\Protocol\WebSocket; namespace Ratchet\Protocol\WebSocket;
use Ratchet\ServerInterface; use Ratchet\ServerInterface;
use Ratchet\Protocol\ProtocolInterface; use Ratchet\Protocol\ProtocolInterface;
use Ratchet\ApplicationInterface;
class Server implements ProtocolInterface { class Server implements ProtocolInterface {
protected $_server = null; protected $_server = null;
@ -24,6 +25,8 @@ class Server implements ProtocolInterface {
); );
} }
public function attatchApplication(ApplicationInterface $app) {}
public function run() { public function run() {
} }
} }

View File

@ -3,15 +3,28 @@ namespace Ratchet;
use Ratchet\Protocol\ProtocolInterface; use Ratchet\Protocol\ProtocolInterface;
class Server implements ServerInterface { class Server implements ServerInterface {
protected $master = null; protected $_master = null;
protected $_app = null;
public function __construct(Socket $socket) { public function __construct(Socket $socket) {
$this->_master = $socket; $this->_master = $socket;
} }
public function attatchApplication(ApplicationInterface $app) {
$this->_app = $app;
}
public function run() { public function run() {
if (!($this->_app instanceof ApplicationInterface)) {
throw new \RuntimeException("No application has been bound to the server");
}
set_time_limit(0); set_time_limit(0);
ob_implicit_flush(); ob_implicit_flush();
do {
} while (true);
// $this->_master->set_nonblock(); // $this->_master->set_nonblock();
// declare(ticks = 1); // declare(ticks = 1);
} }

View File

@ -2,5 +2,7 @@
namespace Ratchet; namespace Ratchet;
interface ServerInterface { interface ServerInterface {
function attatchApplication(ApplicationInterface $app);
function run(); function run();
} }

View File

@ -26,7 +26,9 @@ class Socket {
public function __construct($domain = null, $type = null, $protocol = null) { public function __construct($domain = null, $type = null, $protocol = null) {
list($domain, $type, $protocol) = static::getConfig($domain, $type, $protocol); list($domain, $type, $protocol) = static::getConfig($domain, $type, $protocol);
if (false === ($this->_socket = socket_create($domain, $type, $protocol))) { $this->_socket = @socket_create($domain, $type, $protocol);
if (!is_resource($this->_socket)) {
throw new Exception(); throw new Exception();
} }
} }

View File

@ -0,0 +1,18 @@
<?php
namespace Ratchet\Tests\Mock;
use Ratchet\ApplicationInterface;
class Application implements ApplicationInterface {
public function getName() {
return 'mock_application';
}
public function onConnect() {
}
public function onMessage() {
}
public function onClose() {
}
}

View File

@ -2,6 +2,7 @@
namespace Ratchet\Tests; namespace Ratchet\Tests;
use Ratchet\Server; use Ratchet\Server;
use Ratchet\Tests\Mock\Socket; use Ratchet\Tests\Mock\Socket;
use Ratchet\Tests\Mock\Application as TestApp;
/** /**
* @covers Ratchet\Server * @covers Ratchet\Server
@ -17,4 +18,16 @@ class ServerTest extends \PHPUnit_Framework_TestCase {
$constraint = $this->isInstanceOf('\\Ratchet\\ServerInterface'); $constraint = $this->isInstanceOf('\\Ratchet\\ServerInterface');
$this->assertThat($this->_server, $constraint); $this->assertThat($this->_server, $constraint);
} }
public function testServerCanNotRunWithoutApplication() {
$this->setExpectedException('\\RuntimeException');
$this->_server->run();
}
public function testAttatchedApplicationIsSet() {
$app = new TestApp();
$this->_server->attatchApplication($app);
$this->assertAttributeEquals($app, '_app', $this->_server);
}
} }

View File

@ -1,6 +1,7 @@
<?php <?php
namespace Ratchet\Tests; namespace Ratchet\Tests;
use Ratchet\Tests\Mock\Socket; use Ratchet\Tests\Mock\Socket;
use Ratchet\Socket as RealSocket;
/** /**
* @covers Ratchet\Socket * @covers Ratchet\Socket
@ -20,10 +21,21 @@ class SocketTest extends \PHPUnit_Framework_TestCase {
$this->_socket = new Socket(); $this->_socket = new Socket();
} }
public function testGetConfigForConstruct() { /*
public function testWhatGoesInConstructComesOut() {
$this->assertTrue(false);
}
*/
public function testGetDefaultConfigForConstruct() {
$ref_conf = static::getMethod('getConfig'); $ref_conf = static::getMethod('getConfig');
$config = $ref_conf->invokeArgs($this->_socket, Array()); $config = $ref_conf->invokeArgs($this->_socket, Array());
$this->assertEquals(array_values(Socket::$_defaults), $config); $this->assertEquals(array_values(Socket::$_defaults), $config);
} }
public function testInvalidConstructorArguments() {
$this->setExpectedException('\\Ratchet\\Exception');
$socket = new RealSocket('invalid', 'param', 'derp');
}
} }