Moar cleaning

Added a few unit tests
Added MIT license
Changed Array's to array's
This commit is contained in:
Chris Boden 2011-11-01 11:01:15 -04:00
parent d4d67eba44
commit f9d609074c
11 changed files with 77 additions and 58 deletions

7
LICENSE Normal file
View File

@ -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.

View File

@ -9,7 +9,7 @@ interface ProtocolInterface extends SocketObserver {
function __construct(SocketObserver $application);
/**
* @return Array
* @return array
*/
static function getDefaultConfig();
}

View File

@ -40,7 +40,7 @@ class WebSocket implements ProtocolInterface {
}
/**
* @return Array
* @return array
*/
public static function getDefaultConfig() {
return array(

View File

@ -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;

View File

@ -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

View File

@ -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;

View File

@ -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)
)
);
}

View File

@ -25,9 +25,9 @@ class Hixie76Test extends \PHPUnit_Framework_TestCase {
}
public static function HandshakeProvider() {
return Array(
Array('', '')
, Array('', '')
return array(
array('', '')
, array('', '')
);
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace Ratchet\Tests\Protocol\WebSocket\Version;
use Ratchet\Protocol\WebSocket\Version\HyBi10;
/**
* @covers Ratchet\Protocol\WebSocket\Version\Hybi10
*/
class HyBi10Test extends \PHPUnit_Framework_TestCase {
protected $_version;
public function setUp() {
$this->_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=')
);
}
}

View File

@ -1,33 +0,0 @@
<?php
namespace Ratchet\Tests\Protocol\WebSocket\Version;
use Ratchet\Protocol\WebSocket\Version\Hybi10;
/**
* @covers Ratchet\Protocol\WebSocket\Version\Hybi10
*/
class Hybi10Test extends \PHPUnit_Framework_TestCase {
protected $_version;
public function setUp() {
$this->_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=')
);
}
}

View File

@ -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'));
}
}