From 6ac22721142c73f9fa936abe19e9f8b52a3c2825 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Mon, 24 Oct 2011 13:26:36 -0400 Subject: [PATCH] Cleanup Removed Aggregator idea Server implements ArrayAggregator, stores ArrayIterator Unit tests cleanup --- lib/Ratchet/Server.php | 10 +- lib/Ratchet/Server/Aggregator.php | 69 -------------- lib/Ratchet/Server/Client.php | 13 --- lib/Ratchet/ServerInterface.php | 2 +- tests/Ratchet/Tests/Mock/FakeSocket.php | 6 +- tests/Ratchet/Tests/Mock/SocketAggregator.php | 42 --------- tests/Ratchet/Tests/ServerTest.php | 3 + tests/Ratchet/Tests/SocketAggregatorTest.php | 94 ------------------- tests/Ratchet/Tests/SocketTest.php | 4 +- 9 files changed, 15 insertions(+), 228 deletions(-) delete mode 100644 lib/Ratchet/Server/Aggregator.php delete mode 100644 lib/Ratchet/Server/Client.php delete mode 100644 tests/Ratchet/Tests/Mock/SocketAggregator.php delete mode 100644 tests/Ratchet/Tests/SocketAggregatorTest.php diff --git a/lib/Ratchet/Server.php b/lib/Ratchet/Server.php index 00b865f..4e760a5 100644 --- a/lib/Ratchet/Server.php +++ b/lib/Ratchet/Server.php @@ -24,9 +24,9 @@ class Server implements ServerInterface { protected $_resources = array(); /** - * @var array of Ratchet\Server\Client + * @var ArrayIterator of Resouces (Socket type) as keys, Ratchet\Socket as values */ - protected $_connections = array(); + protected $_connections; /** * @type Logging\LoggerInterface; @@ -47,6 +47,8 @@ class Server implements ServerInterface { $logger = new NullLogger; } $this->_log = $logger; + + $this->_connections = new \ArrayIterator(array()); } /** @@ -81,9 +83,9 @@ class Server implements ServerInterface { } /** - * @return array of Sockets + * @return ArrayIterator of Sockets */ - public function getClients() { + public function getIterator() { return $this->_connections; } diff --git a/lib/Ratchet/Server/Aggregator.php b/lib/Ratchet/Server/Aggregator.php deleted file mode 100644 index 1396701..0000000 --- a/lib/Ratchet/Server/Aggregator.php +++ /dev/null @@ -1,69 +0,0 @@ -_sockets = new \SplObjectStorage; - - $this->_master = $master; - $this->insert($this->_master); - } - - /** - * @return Socket - */ - public function getMaster() { - return $this->_master; - } - - /** - * @param resource - * @return Socket - */ - public function getClientByResource($resource) { - if ($this->_sockets->contains($resource)) { - return $this->_sockets[$resource]; - } - - throw new Exception("Resource not found"); - } - - protected function insert(Socket $socket) { - $resource = $socket->getResource(); - - $this->_sockets[$socket] = $resource; - $this->_resources[] = $resource; - } - - /** - * @return SplObjectStorage - */ - public function getIterator() { - return $this->_sockets; - } - - /** - * @return array - */ - public function asArray() { - return $this->_resources; - } -} \ No newline at end of file diff --git a/lib/Ratchet/Server/Client.php b/lib/Ratchet/Server/Client.php deleted file mode 100644 index 2bfcc6e..0000000 --- a/lib/Ratchet/Server/Client.php +++ /dev/null @@ -1,13 +0,0 @@ -_socket = $socket; - } - - -} \ No newline at end of file diff --git a/lib/Ratchet/ServerInterface.php b/lib/Ratchet/ServerInterface.php index b39d3ee..5b76821 100644 --- a/lib/Ratchet/ServerInterface.php +++ b/lib/Ratchet/ServerInterface.php @@ -1,7 +1,7 @@ _arguments['domain'], $this->_arguments['type'], $this->_arguments['protocol']) = static::getConfig($domain, $type, $protocol); @@ -23,7 +23,7 @@ class FakeSocket extends RealSocket { return $this->_options[$level][$optname]; } - public function listen($backlog) { + public function listen($backlog = 0) { } public function recv($buf, $len, $flags) { diff --git a/tests/Ratchet/Tests/Mock/SocketAggregator.php b/tests/Ratchet/Tests/Mock/SocketAggregator.php deleted file mode 100644 index c9488f1..0000000 --- a/tests/Ratchet/Tests/Mock/SocketAggregator.php +++ /dev/null @@ -1,42 +0,0 @@ -_arguments['domain'], $this->_arguments['type'], $this->_arguments['protocol']) = static::getConfig($domain, $type, $protocol); - } - - public function accept() { - } - - public function bind($address, $port) { - } - - public function close() { - } - - public function get_option($level, $optname) { - return $this->_options[$level][$optname]; - } - - public function listen($backlog) { - } - - public function recv($buf, $len, $flags) { - } - - public function set_option($level, $optname, $optval) { - if (!isset($this->_options[$level])) { - $this->_options[$level] = Array(); - } - - $this->_options[$level][$optname] = $optval; - } - - public function write($buffer, $length = 0) { - } -} \ No newline at end of file diff --git a/tests/Ratchet/Tests/ServerTest.php b/tests/Ratchet/Tests/ServerTest.php index d1fe6a5..c57d112 100644 --- a/tests/Ratchet/Tests/ServerTest.php +++ b/tests/Ratchet/Tests/ServerTest.php @@ -33,6 +33,9 @@ class ServerTest extends \PHPUnit_Framework_TestCase { } public function testBindToInvalidAddress() { + $this->markTestIncomplete(); + return; + $app = new TestApp(); $this->_server->attatchReceiver($app); diff --git a/tests/Ratchet/Tests/SocketAggregatorTest.php b/tests/Ratchet/Tests/SocketAggregatorTest.php deleted file mode 100644 index 2f3fce0..0000000 --- a/tests/Ratchet/Tests/SocketAggregatorTest.php +++ /dev/null @@ -1,94 +0,0 @@ -getMethod($name); - $method->setAccessible(true); - - return $method; - } - - public function setUp() { - $this->_socket = new Socket(); - } - -/* - 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'); - } - - public function testConstructAndCallByOpenAndClose() { - $socket = new RealSocket(); - $socket->close(); - } - - public function testInvalidSocketCall() { - $this->setExpectedException('\\BadMethodCallException'); - $this->_socket->fake_method(); - } - - public function testConstructionFromProtocolInterfaceConfig() { - $protocol = new Protocol(); - $socket = Socket::createFromConfig($protocol); - - $this->assertInstanceOf('\\Ratchet\\Socket', $socket); - } - - public function testCreationFromConfigOutputMatchesInput() { - $protocol = new Protocol(); - $socket = Socket::createFromConfig($protocol); - $config = $protocol::getDefaultConfig(); - - // change this to array_filter late - unset($config['options']); - - $this->assertAttributeEquals($config, '_arguments', $socket); - } - - public function asArrayProvider() { - return Array( - Array(Array('hello' => 'world'), Array('hello' => 'world')) - , Array(null, null) - , Array(Array('hello' => 'world'), new \ArrayObject(Array('hello' => 'world'))) - ); - } - - /** - * @dataProvider asArrayProvider - */ - public function testMethodMungforselectReturnsExpectedValues($output, $input) { - $method = static::getMethod('mungForSelect'); - $return = $method->invokeArgs($this->_socket, Array($input)); - - $this->assertEquals($return, $output); - } - - public function testMethodMungforselectRejectsNonTraversable() { - $this->setExpectedException('\\InvalidArgumentException'); - $method = static::getMethod('mungForSelect'); - $method->invokeArgs($this->_socket, Array('I am upset with PHP ATM')); - } -} \ No newline at end of file diff --git a/tests/Ratchet/Tests/SocketTest.php b/tests/Ratchet/Tests/SocketTest.php index cce6619..2994e1e 100644 --- a/tests/Ratchet/Tests/SocketTest.php +++ b/tests/Ratchet/Tests/SocketTest.php @@ -11,7 +11,7 @@ class SocketTest extends \PHPUnit_Framework_TestCase { protected $_socket; protected static function getMethod($name) { - $class = new \ReflectionClass('\\Ratchet\\Tests\\Mock\\Socket'); + $class = new \ReflectionClass('\\Ratchet\\Tests\\Mock\\FakeSocket'); $method = $class->getMethod($name); $method->setAccessible(true); @@ -30,7 +30,7 @@ class SocketTest extends \PHPUnit_Framework_TestCase { public function testGetDefaultConfigForConstruct() { $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); }