From f17f59856e839008b3d78f6b24918f648952236d Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Mon, 5 Sep 2011 13:10:57 -0400 Subject: [PATCH] Started observer pattern, API updates, unit tests --- .gitignore | 3 ++- lib/Ratchet/ApplicationInterface.php | 10 ++++++++++ lib/Ratchet/Exception.php | 6 +++++- .../WebSocket/ApplicationInterface.php | 6 ------ lib/Ratchet/Protocol/WebSocket/Server.php | 3 +++ lib/Ratchet/Server.php | 15 ++++++++++++++- lib/Ratchet/ServerInterface.php | 2 ++ lib/Ratchet/Socket.php | 4 +++- tests/Ratchet/Tests/Mock/Application.php | 18 ++++++++++++++++++ tests/Ratchet/Tests/ServerTest.php | 13 +++++++++++++ tests/Ratchet/Tests/SocketTest.php | 14 +++++++++++++- 11 files changed, 83 insertions(+), 11 deletions(-) delete mode 100644 lib/Ratchet/Protocol/WebSocket/ApplicationInterface.php create mode 100644 tests/Ratchet/Tests/Mock/Application.php diff --git a/.gitignore b/.gitignore index eefacf1..7c2eb7e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ phpunit.xml -coverage \ No newline at end of file +coverage +sandbox \ No newline at end of file diff --git a/lib/Ratchet/ApplicationInterface.php b/lib/Ratchet/ApplicationInterface.php index cf8cfbd..1241c5f 100644 --- a/lib/Ratchet/ApplicationInterface.php +++ b/lib/Ratchet/ApplicationInterface.php @@ -2,4 +2,14 @@ namespace Ratchet; interface ApplicationInterface { + /** + * @return string + */ + function getName(); + + function onConnect(); + + function onMessage(); + + function onClose(); } \ No newline at end of file diff --git a/lib/Ratchet/Exception.php b/lib/Ratchet/Exception.php index 4c1da15..62e2fb1 100644 --- a/lib/Ratchet/Exception.php +++ b/lib/Ratchet/Exception.php @@ -4,6 +4,10 @@ namespace Ratchet; class Exception extends \Exception { public function __construct() { $int = socket_last_error(); - parent::__construct(socket_strerror($int), $int); + $msg = socket_strerror($int); + + // todo, replace {$msg: $int} to {$msg} + + parent::__construct($msg, $int); } } \ No newline at end of file diff --git a/lib/Ratchet/Protocol/WebSocket/ApplicationInterface.php b/lib/Ratchet/Protocol/WebSocket/ApplicationInterface.php deleted file mode 100644 index a046a46..0000000 --- a/lib/Ratchet/Protocol/WebSocket/ApplicationInterface.php +++ /dev/null @@ -1,6 +0,0 @@ -_master = $socket; } + public function attatchApplication(ApplicationInterface $app) { + $this->_app = $app; + } + public function run() { + if (!($this->_app instanceof ApplicationInterface)) { + throw new \RuntimeException("No application has been bound to the server"); + } + set_time_limit(0); ob_implicit_flush(); + + do { + + } while (true); // $this->_master->set_nonblock(); // declare(ticks = 1); } diff --git a/lib/Ratchet/ServerInterface.php b/lib/Ratchet/ServerInterface.php index 087944d..a1c2585 100644 --- a/lib/Ratchet/ServerInterface.php +++ b/lib/Ratchet/ServerInterface.php @@ -2,5 +2,7 @@ namespace Ratchet; interface ServerInterface { + function attatchApplication(ApplicationInterface $app); + function run(); } \ No newline at end of file diff --git a/lib/Ratchet/Socket.php b/lib/Ratchet/Socket.php index 8fdb733..76af23d 100644 --- a/lib/Ratchet/Socket.php +++ b/lib/Ratchet/Socket.php @@ -26,7 +26,9 @@ class Socket { public function __construct($domain = null, $type = null, $protocol = null) { 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(); } } diff --git a/tests/Ratchet/Tests/Mock/Application.php b/tests/Ratchet/Tests/Mock/Application.php new file mode 100644 index 0000000..43cb3c6 --- /dev/null +++ b/tests/Ratchet/Tests/Mock/Application.php @@ -0,0 +1,18 @@ +isInstanceOf('\\Ratchet\\ServerInterface'); $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); + } } \ No newline at end of file diff --git a/tests/Ratchet/Tests/SocketTest.php b/tests/Ratchet/Tests/SocketTest.php index 9538ddb..72ec3e9 100644 --- a/tests/Ratchet/Tests/SocketTest.php +++ b/tests/Ratchet/Tests/SocketTest.php @@ -1,6 +1,7 @@ _socket = new Socket(); } - public function testGetConfigForConstruct() { +/* + public function testWhatGoesInConstructComesOut() { + $this->assertTrue(false); + } +*/ + + public function testGetDefaultConfigForConstruct() { $ref_conf = static::getMethod('getConfig'); $config = $ref_conf->invokeArgs($this->_socket, Array()); $this->assertEquals(array_values(Socket::$_defaults), $config); } + + public function testInvalidConstructorArguments() { + $this->setExpectedException('\\Ratchet\\Exception'); + $socket = new RealSocket('invalid', 'param', 'derp'); + } } \ No newline at end of file