Removed Aggregator idea
Server implements ArrayAggregator, stores ArrayIterator
Unit tests cleanup
This commit is contained in:
Chris Boden 2011-10-24 13:26:36 -04:00
parent eefbd2be41
commit 6ac2272114
9 changed files with 15 additions and 228 deletions

View File

@ -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;
}

View File

@ -1,69 +0,0 @@
<?php
namespace Ratchet\Server;
use Ratchet\Socket;
use Ratchet\Exception;
class Aggregator implements \IteratorAggregator {
/**
* @var Ratchet\Socket
*/
protected $_master;
/**
* @var SplObjectStorage
*/
protected $_sockets;
protected $_resources = array();
/**
* @param Ratchet\Socket
* @throws Ratchet\Exception
*/
public function __construct(Socket $master) {
$this->_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;
}
}

View File

@ -1,13 +0,0 @@
<?php
namespace Ratchet\Server;
use Ratchet\Socket;
class Client {
protected $_socket;
public function __construct(Socket $socket) {
$this->_socket = $socket;
}
}

View File

@ -1,7 +1,7 @@
<?php
namespace Ratchet;
interface ServerInterface {
interface ServerInterface extends \IteratorAggregate {
function attatchReceiver(ReceiverInterface $receiver);
function run($address = '127.0.0.1', $port = 1025);

View File

@ -3,8 +3,8 @@ namespace Ratchet\Tests\Mock;
use Ratchet\Socket as RealSocket;
class FakeSocket extends RealSocket {
protected $_arguments = Array();
protected $_options = Array();
protected $_arguments = array();
protected $_options = array();
public function __construct($domain = null, $type = null, $protocol = null) {
list($this->_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) {

View File

@ -1,42 +0,0 @@
<?php
namespace Ratchet\Tests\Mock;
use Ratchet\SocketAggregator as RealSocketAggregator;
class SocketAggregator extends RealSocketAggregator {
protected $_arguments = Array();
protected $_options = Array();
public function __construct($domain = null, $type = null, $protocol = null) {
list($this->_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) {
}
}

View File

@ -33,6 +33,9 @@ class ServerTest extends \PHPUnit_Framework_TestCase {
}
public function testBindToInvalidAddress() {
$this->markTestIncomplete();
return;
$app = new TestApp();
$this->_server->attatchReceiver($app);

View File

@ -1,94 +0,0 @@
<?php
namespace Ratchet\Tests;
use Ratchet\Tests\Mock\FakeSocket as Socket;
use Ratchet\Socket as RealSocket;
use Ratchet\Tests\Mock\Protocol;
/**
* @covers Ratchet\SocketAggregator
*/
class SocketAggregatorTest extends \PHPUnit_Framework_TestCase {
protected $_socket;
protected static function getMethod($name) {
$class = new \ReflectionClass('\\Ratchet\\Tests\\Mock\\Socket');
$method = $class->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'));
}
}

View File

@ -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);
}