From 2811cdbd487bc9930765571053c092ae49abb4c1 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Sun, 4 Sep 2011 18:26:57 -0400 Subject: [PATCH] Mock Socket, Interfaces, Test stubs --- lib/Ratchet/Socket.php | 9 +++++-- tests/Ratchet/Tests/Mock/Socket.php | 41 +++++++++++++++++++++++++++++ tests/Ratchet/Tests/ServerTest.php | 20 ++++++++++++++ tests/Ratchet/Tests/SocketTest.php | 29 ++++++++++++++++++++ tests/bootstrap.php | 3 +++ 5 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 tests/Ratchet/Tests/Mock/Socket.php create mode 100644 tests/Ratchet/Tests/ServerTest.php create mode 100644 tests/Ratchet/Tests/SocketTest.php diff --git a/lib/Ratchet/Socket.php b/lib/Ratchet/Socket.php index 92ed6d4..fdd4898 100644 --- a/lib/Ratchet/Socket.php +++ b/lib/Ratchet/Socket.php @@ -11,13 +11,18 @@ class Socket { ); public function __construct($domain = null, $type = null, $protocol = null) { - foreach (static::$_default as $key => $val) { + list($domain, $type, $protocol) = static::getConfig($domain, $type, $protocol); + $this->_socket = socket_create($domain, $type, $protocol); + } + + protected static function getConfig($domain = null, $type = null, $protocol = null) { + foreach (static::$_defaults as $key => $val) { if (null === $$key) { $$key = $val; } } - $this->_socket = socket_create($domain, $type, $protocol); + return Array($domain, $type, $protocol); } public function __call($method, $arguments) { diff --git a/tests/Ratchet/Tests/Mock/Socket.php b/tests/Ratchet/Tests/Mock/Socket.php new file mode 100644 index 0000000..5f7bbf9 --- /dev/null +++ b/tests/Ratchet/Tests/Mock/Socket.php @@ -0,0 +1,41 @@ +_options[$level][$optname]; + } + + public function listen($backlog) { + } + + public function recv($buf, $len, $flags) { + } + + public function set_option($level, $optname, $optval) { + if (!is_array($this->_options[$level])) { + $this->_options[$level] = Array(); + } + + $this->_options[$level][$optname] = $optval; + } + + public function write($buffer, $length = 0) { + } +} \ No newline at end of file diff --git a/tests/Ratchet/Tests/ServerTest.php b/tests/Ratchet/Tests/ServerTest.php new file mode 100644 index 0000000..78ab40e --- /dev/null +++ b/tests/Ratchet/Tests/ServerTest.php @@ -0,0 +1,20 @@ +_server = new Server(new Socket()); + } + + public function testServerHasServerInterface() { + $constraint = $this->isInstanceOf('\\Ratchet\\ServerInterface'); + $this->assertThat($this->_server, $constraint); + } +} \ No newline at end of file diff --git a/tests/Ratchet/Tests/SocketTest.php b/tests/Ratchet/Tests/SocketTest.php new file mode 100644 index 0000000..9538ddb --- /dev/null +++ b/tests/Ratchet/Tests/SocketTest.php @@ -0,0 +1,29 @@ +getMethod($name); + $method->setAccessible(true); + + return $method; + } + + public function setUp() { + $this->_socket = new Socket(); + } + + public function testGetConfigForConstruct() { + $ref_conf = static::getMethod('getConfig'); + $config = $ref_conf->invokeArgs($this->_socket, Array()); + + $this->assertEquals(array_values(Socket::$_defaults), $config); + } +} \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php index a8909cb..2f02d40 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,5 +1,8 @@ register(); + $app = new SplClassLoader('Ratchet', dirname(__DIR__) . DIRECTORY_SEPARATOR . 'lib'); $app->register(); \ No newline at end of file