Added ability to create socket with configuration recommended from a protocol

This commit is contained in:
Chris Boden 2011-09-05 20:06:16 -04:00
parent 9bc0cbce25
commit 3daaa99734
4 changed files with 75 additions and 2 deletions

View File

@ -1,5 +1,6 @@
<?php <?php
namespace Ratchet; namespace Ratchet;
use Ratchet\Protocol\ProtocolInterface;
/** /**
* A wrapper for the PHP socket_ functions * A wrapper for the PHP socket_ functions
@ -33,6 +34,28 @@ class Socket {
} }
} }
/**
* @param Ratchet\Protocol\ProtocolInterface
* @return Ratchet\Socket
* @throws Ratchet\Exception
*/
public static function createFromConfig(ProtocolInterface $protocol) {
$config = $protocol::getDefaultConfig();
// todo - this is wrong when class is extended, I don't have internet, can't look up the fn - unit test fails
$class = __CLASS__;
$socket = new $class($config['domain'] ?: null, $config['type'] ?: null, $config['protocol'] ?: null);
if (is_array($config['options'])) {
foreach ($config['options'] as $level => $pair) {
foreach ($pair as $optname => $optval) {
$socket->set_option($level, $optname, $optval);
}
}
}
}
/** /**
* @internal * @internal
* @param int Specifies the protocol family to be used by the socket. * @param int Specifies the protocol family to be used by the socket.

View File

@ -0,0 +1,29 @@
<?php
namespace Ratchet\Tests\Mock;
use Ratchet\Protocol\ProtocolInterface;
class Protocol implements ProtocolInterface {
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 getName() {
return 'mock_protocol';
}
public function handleConnect() {
}
public function handleMessage() {
}
public function handleClose() {
}
}

View File

@ -3,10 +3,11 @@ namespace Ratchet\Tests\Mock;
use Ratchet\Socket as RealSocket; use Ratchet\Socket as RealSocket;
class Socket extends RealSocket { class Socket extends RealSocket {
protected $_options = Array(); protected $_arguments = Array();
protected $_options = Array();
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($this->_arguments['domain'], $this->_arguments['type'], $this->_arguments['protocol']) = static::getConfig($domain, $type, $protocol);
} }
public function accept() { public function accept() {

View File

@ -2,6 +2,7 @@
namespace Ratchet\Tests; namespace Ratchet\Tests;
use Ratchet\Tests\Mock\Socket; use Ratchet\Tests\Mock\Socket;
use Ratchet\Socket as RealSocket; use Ratchet\Socket as RealSocket;
use Ratchet\Tests\Mock\Protocol;
/** /**
* @covers Ratchet\Socket * @covers Ratchet\Socket
@ -48,4 +49,23 @@ class SocketTest extends \PHPUnit_Framework_TestCase {
$this->setExpectedException('\\BadMethodCallException'); $this->setExpectedException('\\BadMethodCallException');
$this->_socket->fake_method(); $this->_socket->fake_method();
} }
public function testConstructionFromProtocolInterfaceConfig() {
$protocol = new Protocol();
$socket = Socket::createFromConfig($protocol);
$constraint = $this->isInstanceOf('\\Ratchet\\Socket');
$this->assertThat($socket, $constraint);
}
public function testCreationFromConfigOutputMatchesInput() {
$protocol = new Protocol();
$socket = Socket::createFromConfig($protocol);
$config = $protocol::getDefaultConfig();
// chnage this to array_filter late
unset($config['options']);
$this->assertAttributeEquals($config, '_arguments', $socket);
}
} }