Added ability to create socket with configuration recommended from a protocol
This commit is contained in:
parent
9bc0cbce25
commit
3daaa99734
@ -1,5 +1,6 @@
|
||||
<?php
|
||||
namespace Ratchet;
|
||||
use Ratchet\Protocol\ProtocolInterface;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param int Specifies the protocol family to be used by the socket.
|
||||
|
29
tests/Ratchet/Tests/Mock/Protocol.php
Normal file
29
tests/Ratchet/Tests/Mock/Protocol.php
Normal 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() {
|
||||
}
|
||||
}
|
@ -3,10 +3,11 @@ namespace Ratchet\Tests\Mock;
|
||||
use Ratchet\Socket as RealSocket;
|
||||
|
||||
class Socket extends RealSocket {
|
||||
protected $_options = Array();
|
||||
protected $_arguments = Array();
|
||||
protected $_options = Array();
|
||||
|
||||
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() {
|
||||
|
@ -2,6 +2,7 @@
|
||||
namespace Ratchet\Tests;
|
||||
use Ratchet\Tests\Mock\Socket;
|
||||
use Ratchet\Socket as RealSocket;
|
||||
use Ratchet\Tests\Mock\Protocol;
|
||||
|
||||
/**
|
||||
* @covers Ratchet\Socket
|
||||
@ -48,4 +49,23 @@ class SocketTest extends \PHPUnit_Framework_TestCase {
|
||||
$this->setExpectedException('\\BadMethodCallException');
|
||||
$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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user