Interface Cleanup

Separated Observable interface from Decorator interface, also separated config method to its own interface
Cleaned up unit tests to reflect interface changes
This commit is contained in:
Chris Boden 2011-11-13 13:37:42 -05:00
parent 47b7110dc1
commit c6a91692f7
10 changed files with 34 additions and 90 deletions

View File

@ -0,0 +1,12 @@
<?php
namespace Ratchet\Application;
use Ratchet\ObserverInterface;
interface ApplicationInterface extends ObserverInterface {
/**
* Decorator pattern
* @param Ratchet\ObserverInterface Application to wrap in protocol
* @throws UnexpectedValueException
*/
public function __construct(ApplicationInterface $app = null);
}

View File

@ -0,0 +1,12 @@
<?php
namespace Ratchet\Application;
/**
* @todo Does this belong in root dir of application
*/
interface ConfiguratorInterface {
/**
* @return array
*/
static function getDefaultConfig();
}

View File

@ -1,15 +0,0 @@
<?php
namespace Ratchet\Application;
use Ratchet\ObserverInterface;
interface ProtocolInterface extends ObserverInterface {
/**
* @param Ratchet\ObserverInterface Application to wrap in protocol
*/
// function __construct(ObserverInterface $application);
/**
* @return array
*/
static function getDefaultConfig();
}

View File

@ -1,6 +1,6 @@
<?php
namespace Ratchet\Application\Server;
use Ratchet\ObserverInterface;
use Ratchet\Application\ApplicationInterface;
use Ratchet\SocketInterface;
use Ratchet\Resource\Command\CommandInterface;
@ -10,7 +10,7 @@ use Ratchet\Resource\Command\CommandInterface;
* @todo Currently passing Socket object down the decorated chain - should be sending reference to it instead; Receivers do not interact with the Socket directly, they do so through the Command pattern
* @todo With all these options for the server I should probably use a DIC
*/
class App implements ObserverInterface {
class App implements ApplicationInterface {
/**
* The master socket, receives all connections
* @var Socket
@ -36,7 +36,7 @@ class App implements ObserverInterface {
/**
* @param Ratchet\ObserverInterface
*/
public function __construct(ObserverInterface $application = null) {
public function __construct(ApplicationInterface $application = null) {
if (null === $application) {
throw new \UnexpectedValueException("Server requires an application to run off of");
}

View File

@ -3,12 +3,12 @@ namespace Ratchet\Application\WebSocket;
use Ratchet\Application\WebSocket\Client;
use Ratchet\Application\WebSocket\VersionInterface;
use Ratchet\SocketInterface;
use Ratchet\ObserverInterface;
use Ratchet\Application\ApplicationInterface;
use Ratchet\Application\ConfiguratorInterface;
use Ratchet\Resource\Command\Factory;
use Ratchet\Resource\Command\CommandInterface;
use Ratchet\Resource\Command\Action\SendMessage;
use Ratchet\Application\WebSocket\Util\HTTP;
use Ratchet\Application\ProtocolInterface;
/**
* The adapter to handle WebSocket requests/responses
@ -20,7 +20,7 @@ use Ratchet\Application\ProtocolInterface;
* @todo Consider chaning this class to a State Pattern. If a ObserverInterface is passed in __construct, do what is there now. If it's an AppInterface change behaviour of socket interaction (onOpen, handshake, etc)
* @todo Change namespace to Ratchet\Application\WebSocket\Adapter
*/
class App implements ProtocolInterface {
class App implements ApplicationInterface, ConfiguratorInterface {
/**
* Lookup for connected clients
* @var SplObjectStorage
@ -46,7 +46,7 @@ class App implements ProtocolInterface {
, 'Hixie76' => null
);
public function __construct(ObserverInterface $app = null) {
public function __construct(ApplicationInterface $app = null) {
if (null === $app) {
throw new \UnexpectedValueException("WebSocket requires an application to run off of");
}

View File

@ -9,13 +9,6 @@ namespace Ratchet;
* @todo Does this belong in \Ratchet\Server\?
*/
interface ObserverInterface {
/**
* Decorator pattern
* @param ObserverInterface If nothing is passed it's the end of the line
* @throws UnexpectedValueException
*/
public function __construct(ObserverInterface $app = null);
/**
* When a new connection is opened it will be passed to this method
* @param SocketInterface The socket/connection that just connected to your application

View File

@ -1,11 +1,11 @@
<?php
namespace Ratchet\Tests\Mock;
use Ratchet\ObserverInterface;
use Ratchet\Application\ApplicationInterface;
use Ratchet\Tests\Mock\Socket as MockSocket;
use Ratchet\SocketInterface;
class Application implements ObserverInterface {
public function __construct(ObserverInterface $app = null) {
class Application implements ApplicationInterface {
public function __construct(ApplicationInterface $app = null) {
}
public function onOpen(SocketInterface $conn) {

View File

@ -1,34 +0,0 @@
<?php
namespace Ratchet\Tests\Mock;
use Ratchet\ObserverInterface;
use Ratchet\Application\ProtocolInterface;
use Ratchet\Server;
use Ratchet\SocketInterface;
class Protocol implements ProtocolInterface {
public function __construct(ObserverInterface $app = null) {
}
public static function getDefaultConfig() {
return array(
'domain' => AF_INET
, 'type' => SOCK_STREAM
, 'protocol' => SOL_TCP
, 'options' => array(
SOL_SOCKET => array(SO_REUSEADDR => 1)
)
);
}
public function onOpen(SocketInterface $conn) {
}
public function onRecv(SocketInterface $from, $msg) {
}
public function onClose(SocketInterface $conn) {
}
public function onError(SocketInterface $conn, \Exception $e) {
}
}

View File

@ -14,11 +14,6 @@ class WebSocketTest extends \PHPUnit_Framework_TestCase {
$this->_ws = new WebSocket(new Application);
}
public function testServerImplementsServerInterface() {
$constraint = $this->isInstanceOf('\\Ratchet\\ObserverInterface');
$this->assertThat($this->_ws, $constraint);
}
public function testGetConfigReturnsArray() {
$this->assertInternalType('array', $this->_ws->getDefaultConfig());
}

View File

@ -2,7 +2,6 @@
namespace Ratchet\Tests;
use Ratchet\Tests\Mock\FakeSocket as Socket;
use Ratchet\Socket as RealSocket;
use Ratchet\Tests\Mock\Protocol;
use Ratchet\Tests\Mock\Application as TestApp;
/**
@ -45,24 +44,6 @@ class SocketTest extends \PHPUnit_Framework_TestCase {
$this->_socket->fake_method();
}
public function testConstructionFromProtocolInterfaceConfig() {
$protocol = new Protocol(new TestApp);
$socket = Socket::createFromConfig($protocol);
$this->assertInstanceOf('\\Ratchet\\Socket', $socket);
}
public function testCreationFromConfigOutputMatchesInput() {
$protocol = new Protocol(new TestApp);
$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'))