From f9d609074ca1ce011025576ae4897ca4d1e29b26 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Tue, 1 Nov 2011 11:01:15 -0400 Subject: [PATCH] Moar cleaning Added a few unit tests Added MIT license Changed Array's to array's --- LICENSE | 7 +++ lib/Ratchet/Protocol/ProtocolInterface.php | 2 +- lib/Ratchet/Protocol/WebSocket.php | 2 +- .../Protocol/WebSocket/Version/HyBi10.php | 11 ++-- lib/Ratchet/Socket.php | 4 +- tests/Ratchet/Tests/Mock/FakeSocket.php | 2 +- tests/Ratchet/Tests/Mock/Protocol.php | 6 +-- .../WebSocket/Version/Hixie76Test.php | 6 +-- .../Protocol/WebSocket/Version/HyBi10Test.php | 50 +++++++++++++++++++ .../Protocol/WebSocket/Version/Hybi10Test.php | 33 ------------ tests/Ratchet/Tests/SocketTest.php | 12 ++--- 11 files changed, 77 insertions(+), 58 deletions(-) create mode 100644 LICENSE create mode 100644 tests/Ratchet/Tests/Protocol/WebSocket/Version/HyBi10Test.php delete mode 100644 tests/Ratchet/Tests/Protocol/WebSocket/Version/Hybi10Test.php diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0423176 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright (c) 2011 Chris Boden + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/lib/Ratchet/Protocol/ProtocolInterface.php b/lib/Ratchet/Protocol/ProtocolInterface.php index 6604a20..f98c4b6 100644 --- a/lib/Ratchet/Protocol/ProtocolInterface.php +++ b/lib/Ratchet/Protocol/ProtocolInterface.php @@ -9,7 +9,7 @@ interface ProtocolInterface extends SocketObserver { function __construct(SocketObserver $application); /** - * @return Array + * @return array */ static function getDefaultConfig(); } \ No newline at end of file diff --git a/lib/Ratchet/Protocol/WebSocket.php b/lib/Ratchet/Protocol/WebSocket.php index 17711c1..d7bc906 100644 --- a/lib/Ratchet/Protocol/WebSocket.php +++ b/lib/Ratchet/Protocol/WebSocket.php @@ -40,7 +40,7 @@ class WebSocket implements ProtocolInterface { } /** - * @return Array + * @return array */ public static function getDefaultConfig() { return array( diff --git a/lib/Ratchet/Protocol/WebSocket/Version/HyBi10.php b/lib/Ratchet/Protocol/WebSocket/Version/HyBi10.php index b7b2e3a..6e86785 100644 --- a/lib/Ratchet/Protocol/WebSocket/Version/HyBi10.php +++ b/lib/Ratchet/Protocol/WebSocket/Version/HyBi10.php @@ -29,11 +29,9 @@ class HyBi10 implements VersionInterface { * @throws UnexpectedValueException */ public function unframe($message) { - $data = $message; - $payloadLength = ''; - $mask = ''; - $unmaskedPayload = ''; - $decodedData = array(); + $data = $message; + $mask = $payloadLength = $unmaskedPayload = ''; + $decodedData = array(); // estimate frame type: $firstByteBinary = sprintf('%08b', ord($data[0])); @@ -93,9 +91,6 @@ class HyBi10 implements VersionInterface { $unmaskedPayload .= $data[$i] ^ $mask[$j % 4]; } $decodedData['payload'] = $unmaskedPayload; - } else { - $payloadOffset = $payloadOffset - 4; - $decodedData['payload'] = substr($data, $payloadOffset); } return $decodedData; diff --git a/lib/Ratchet/Socket.php b/lib/Ratchet/Socket.php index d72be6e..0a4e5b1 100644 --- a/lib/Ratchet/Socket.php +++ b/lib/Ratchet/Socket.php @@ -12,7 +12,7 @@ class Socket implements SocketInterface { */ protected $_resource; - public static $_defaults = Array( + public static $_defaults = array( 'domain' => AF_INET , 'type' => SOCK_STREAM , 'protocol' => SOL_TCP @@ -164,7 +164,7 @@ class Socket implements SocketInterface { * Call all the socket_ functions (without passing the resource) through this * @see http://ca3.php.net/manual/en/ref.sockets.php * @param string - * @param Array + * @param array * @return mixed * @throws Exception * @throws \BadMethodCallException diff --git a/tests/Ratchet/Tests/Mock/FakeSocket.php b/tests/Ratchet/Tests/Mock/FakeSocket.php index 0c99752..67c08a2 100644 --- a/tests/Ratchet/Tests/Mock/FakeSocket.php +++ b/tests/Ratchet/Tests/Mock/FakeSocket.php @@ -31,7 +31,7 @@ class FakeSocket extends RealSocket { public function set_option($level, $optname, $optval) { if (!isset($this->_options[$level])) { - $this->_options[$level] = Array(); + $this->_options[$level] = array(); } $this->_options[$level][$optname] = $optval; diff --git a/tests/Ratchet/Tests/Mock/Protocol.php b/tests/Ratchet/Tests/Mock/Protocol.php index 38175b2..771db3c 100644 --- a/tests/Ratchet/Tests/Mock/Protocol.php +++ b/tests/Ratchet/Tests/Mock/Protocol.php @@ -10,12 +10,12 @@ class Protocol implements ProtocolInterface { } public static function getDefaultConfig() { - return Array( + return array( 'domain' => AF_INET , 'type' => SOCK_STREAM , 'protocol' => SOL_TCP - , 'options' => Array( - SOL_SOCKET => Array(SO_REUSEADDR => 1) + , 'options' => array( + SOL_SOCKET => array(SO_REUSEADDR => 1) ) ); } diff --git a/tests/Ratchet/Tests/Protocol/WebSocket/Version/Hixie76Test.php b/tests/Ratchet/Tests/Protocol/WebSocket/Version/Hixie76Test.php index b9dffbd..d4875ae 100644 --- a/tests/Ratchet/Tests/Protocol/WebSocket/Version/Hixie76Test.php +++ b/tests/Ratchet/Tests/Protocol/WebSocket/Version/Hixie76Test.php @@ -25,9 +25,9 @@ class Hixie76Test extends \PHPUnit_Framework_TestCase { } public static function HandshakeProvider() { - return Array( - Array('', '') - , Array('', '') + return array( + array('', '') + , array('', '') ); } } \ No newline at end of file diff --git a/tests/Ratchet/Tests/Protocol/WebSocket/Version/HyBi10Test.php b/tests/Ratchet/Tests/Protocol/WebSocket/Version/HyBi10Test.php new file mode 100644 index 0000000..9c04d77 --- /dev/null +++ b/tests/Ratchet/Tests/Protocol/WebSocket/Version/HyBi10Test.php @@ -0,0 +1,50 @@ +_version = new HyBi10(); + } + + public function testClassImplementsVersionInterface() { + $constraint = $this->isInstanceOf('\\Ratchet\\Protocol\\WebSocket\\Version\\VersionInterface'); + $this->assertThat($this->_version, $constraint); + } + + /** + * @dataProvider HandshakeProvider + */ + public function testKeySigningForHandshake($key, $accept) { + $this->assertEquals($accept, $this->_version->sign($key)); + } + + public static function HandshakeProvider() { + return array( + array('x3JJHMbDL1EzLkh9GBhXDw==', 'HSmrc0sMlYUkAGmm5OPpG2HaGWk=') + , array('dGhlIHNhbXBsZSBub25jZQ==', 's3pPLMBiTxaQ9kYGzzhZRbK+xOo=') + ); + } + + /** + * @dataProvider UnframeMessageProvider + */ + public function testUnframeMessage($message, $framed) { + $decoded = $this->_version->unframe(base64_decode($framed)); + $this->assertEquals($message, $decoded['payload']); + } + + public static function UnframeMessageProvider() { + return array( + array('Hello World!', 'gYydAIfa1WXrtvIg0LXvbOP7') + , array('!@#$%^&*()-=_+[]{}\|/.,<>`~', 'gZv+h96r38f9j9vZ+IHWrvOWoayF9oX6gtfRqfKXwOeg') + , array('ಠ_ಠ', 'gYfnSpu5B/g75gf4Ow==') + , array("The quick brown fox jumps over the lazy dog. All work and no play makes Chris a dull boy. I'm trying to get past 128 characters for a unit test here...", 'gf4Amahb14P8M7Kj2S6+4MN7tfHHLLmjzjSvo8IuuvPbe7j1zSn398A+9+/JIa6jzDSwrYh7lu/Ee6Ds2jD34sY/9+3He6fvySL37skwsvCIGL/xwSj34og/ou/Ee7Xs0XX3o+F8uqPcKa7qxjz398d7sObce6fi2y/3sppj9+DAOqXiyy+y8dt7sezae7aj3TW+94gvsvDce7/m2j75rYY=') + ); + } +} \ No newline at end of file diff --git a/tests/Ratchet/Tests/Protocol/WebSocket/Version/Hybi10Test.php b/tests/Ratchet/Tests/Protocol/WebSocket/Version/Hybi10Test.php deleted file mode 100644 index 2d4a8e1..0000000 --- a/tests/Ratchet/Tests/Protocol/WebSocket/Version/Hybi10Test.php +++ /dev/null @@ -1,33 +0,0 @@ -_version = new Hybi10(); - } - - public function testClassImplementsVersionInterface() { - $constraint = $this->isInstanceOf('\\Ratchet\\Protocol\\WebSocket\\Version\\VersionInterface'); - $this->assertThat($this->_version, $constraint); - } - - /** - * @dataProvider HandshakeProvider - */ - public function testKeySigningForHandshake($key, $accept) { - $this->assertEquals($accept, $this->_version->sign($key)); - } - - public static function HandshakeProvider() { - return Array( - Array('x3JJHMbDL1EzLkh9GBhXDw==', 'HSmrc0sMlYUkAGmm5OPpG2HaGWk=') - , Array('dGhlIHNhbXBsZSBub25jZQ==', 's3pPLMBiTxaQ9kYGzzhZRbK+xOo=') - ); - } -} \ No newline at end of file diff --git a/tests/Ratchet/Tests/SocketTest.php b/tests/Ratchet/Tests/SocketTest.php index 1f0c8e9..7fa946c 100644 --- a/tests/Ratchet/Tests/SocketTest.php +++ b/tests/Ratchet/Tests/SocketTest.php @@ -70,10 +70,10 @@ class SocketTest extends \PHPUnit_Framework_TestCase { } public function asArrayProvider() { - return Array( - Array(Array('hello' => 'world'), Array('hello' => 'world')) - , Array(null, null) - , Array(Array('hello' => 'world'), new \ArrayObject(Array('hello' => 'world'))) + return array( + array(array('hello' => 'world'), array('hello' => 'world')) + , array(null, null) + , array(array('hello' => 'world'), new \ArrayObject(array('hello' => 'world'))) ); } @@ -82,7 +82,7 @@ class SocketTest extends \PHPUnit_Framework_TestCase { */ public function testMethodMungforselectReturnsExpectedValues($output, $input) { $method = static::getMethod('mungForSelect'); - $return = $method->invokeArgs($this->_socket, Array($input)); + $return = $method->invokeArgs($this->_socket, array($input)); $this->assertEquals($return, $output); } @@ -90,6 +90,6 @@ class SocketTest extends \PHPUnit_Framework_TestCase { public function testMethodMungforselectRejectsNonTraversable() { $this->setExpectedException('\\InvalidArgumentException'); $method = static::getMethod('mungForSelect'); - $method->invokeArgs($this->_socket, Array('I am upset with PHP ATM')); + $method->invokeArgs($this->_socket, array('I am upset with PHP ATM')); } } \ No newline at end of file