From 3daaa9973420aecc95302e541e6ab9ced451f977 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Mon, 5 Sep 2011 20:06:16 -0400 Subject: [PATCH] Added ability to create socket with configuration recommended from a protocol --- lib/Ratchet/Socket.php | 23 +++++++++++++++++++++ tests/Ratchet/Tests/Mock/Protocol.php | 29 +++++++++++++++++++++++++++ tests/Ratchet/Tests/Mock/Socket.php | 5 +++-- tests/Ratchet/Tests/SocketTest.php | 20 ++++++++++++++++++ 4 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 tests/Ratchet/Tests/Mock/Protocol.php diff --git a/lib/Ratchet/Socket.php b/lib/Ratchet/Socket.php index 2d493b8..1265a4b 100644 --- a/lib/Ratchet/Socket.php +++ b/lib/Ratchet/Socket.php @@ -1,5 +1,6 @@ $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. diff --git a/tests/Ratchet/Tests/Mock/Protocol.php b/tests/Ratchet/Tests/Mock/Protocol.php new file mode 100644 index 0000000..636ee0b --- /dev/null +++ b/tests/Ratchet/Tests/Mock/Protocol.php @@ -0,0 +1,29 @@ + 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() { + } +} \ No newline at end of file diff --git a/tests/Ratchet/Tests/Mock/Socket.php b/tests/Ratchet/Tests/Mock/Socket.php index 5f7bbf9..76c2649 100644 --- a/tests/Ratchet/Tests/Mock/Socket.php +++ b/tests/Ratchet/Tests/Mock/Socket.php @@ -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() { diff --git a/tests/Ratchet/Tests/SocketTest.php b/tests/Ratchet/Tests/SocketTest.php index 41d1ed1..fd6cad7 100644 --- a/tests/Ratchet/Tests/SocketTest.php +++ b/tests/Ratchet/Tests/SocketTest.php @@ -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); + } } \ No newline at end of file