File structure set, Hiby10 token handshake complete and unit tested

This commit is contained in:
Chris Boden 2011-09-04 12:11:24 -04:00
commit 5320efe4e7
13 changed files with 261 additions and 0 deletions

0
README.md Normal file
View File

0
lib/Ratchet/Client.php Normal file
View File

View File

@ -0,0 +1,5 @@
<?php
namespace Ratchet;
class Exception extends \Exception {
}

View File

@ -0,0 +1,12 @@
<?php
namespace Ratchet\WebSocket;
class Adapter {
/**
* @param string
* @return Ratchet\Protocol\WebSocket\Version\VersionInterface
*/
public function getProtocol($header) {
}
}

View File

@ -0,0 +1,5 @@
<?php
namespace Ratchet\Protocol\WebSocket\Version;
class Hixi76 {
}

View File

@ -0,0 +1,10 @@
<?php
namespace Ratchet\Protocol\WebSocket\Version;
class Hybi10 implements VersionInterface {
const GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
public function sign($key) {
return base64_encode(sha1($key . static::GUID, 1));
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace Ratchet\Protocol\WebSocket\Version;
interface VersionInterface {
/**
* @param string
* @return string
*/
function sign($header);
}

8
lib/Ratchet/Server.php Normal file
View File

@ -0,0 +1,8 @@
<?php
namespace Ratchet;
class Server {
public function __construct($host, $port) {
}
}

17
lib/Ratchet/Socket.php Normal file
View File

@ -0,0 +1,17 @@
<?php
namespace Ratchet;
class Socket {
protected $_socket;
public function __construct() {
// $this->_socket = socket_open();
}
public function __call($method, $arguments) {
if (function_exists('socket_' . $method)) {
array_unshift($arguments, $this->_socket);
return call_user_func_array('socket_' . $method, $arguments);
}
}
}

17
phpunit.xml.dist Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
forceCoversAnnotation="true"
mapTestClassNameToCoveredClassName="true"
bootstrap="tests/bootstrap.php"
colors="true"
backupGlobals="false"
backupStaticAttributes="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Ratchet Test Suite">
<directory>./tests/Ratchet/</directory>
</testsuite>
</testsuites>
</phpunit>

View File

@ -0,0 +1,33 @@
<?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=')
);
}
}

139
tests/SplClassLoader.php Normal file
View File

@ -0,0 +1,139 @@
<?php
/**
* SplClassLoader implementation that implements the technical interoperability
* standards for PHP 5.3 namespaces and class names.
*
* http://groups.google.com/group/php-standards/web/final-proposal
*
* // Example which loads classes for the Doctrine Common package in the
* // Doctrine\Common namespace.
* $classLoader = new SplClassLoader('Doctrine\Common', '/path/to/doctrine');
* $classLoader->register();
*
* @author Jonathan H. Wage <jonwage@gmail.com>
* @author Roman S. Borschel <roman@code-factory.org>
* @author Matthew Weier O'Phinney <matthew@zend.com>
* @author Kris Wallsmith <kris.wallsmith@gmail.com>
* @author Fabien Potencier <fabien.potencier@symfony-project.org>
*/
class SplClassLoader
{
private $_fileExtension = '.php';
private $_namespace;
private $_includePath;
private $_namespaceSeparator = '\\';
/**
* Creates a new <tt>SplClassLoader</tt> that loads classes of the
* specified namespace.
*
* @param string $ns The namespace to use.
*/
public function __construct($ns = null, $includePath = null)
{
$this->_namespace = $ns;
$this->_includePath = $includePath;
}
/**
* Sets the namespace separator used by classes in the namespace of this class loader.
*
* @param string $sep The separator to use.
*/
public function setNamespaceSeparator($sep)
{
$this->_namespaceSeparator = $sep;
}
/**
* Gets the namespace seperator used by classes in the namespace of this class loader.
*
* @return void
*/
public function getNamespaceSeparator()
{
return $this->_namespaceSeparator;
}
/**
* Sets the base include path for all class files in the namespace of this class loader.
*
* @param string $includePath
*/
public function setIncludePath($includePath)
{
$this->_includePath = $includePath;
}
/**
* Gets the base include path for all class files in the namespace of this class loader.
*
* @return string $includePath
*/
public function getIncludePath()
{
return $this->_includePath;
}
/**
* Sets the file extension of class files in the namespace of this class loader.
*
* @param string $fileExtension
*/
public function setFileExtension($fileExtension)
{
$this->_fileExtension = $fileExtension;
}
/**
* Gets the file extension of class files in the namespace of this class loader.
*
* @return string $fileExtension
*/
public function getFileExtension()
{
return $this->_fileExtension;
}
/**
* Installs this class loader on the SPL autoload stack.
*/
public function register()
{
spl_autoload_register(array($this, 'loadClass'));
}
/**
* Uninstalls this class loader from the SPL autoloader stack.
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
}
/**
* Loads the given class or interface.
*
* @param string $className The name of the class to load.
* @return void
*/
public function loadClass($className)
{
if (null === $this->_namespace || $this->_namespace.$this->_namespaceSeparator === substr($className, 0, strlen($this->_namespace.$this->_namespaceSeparator))) {
$fileName = '';
$namespace = '';
if (false !== ($lastNsPos = strripos($className, $this->_namespaceSeparator))) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension;
$filePath = ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName;
if (is_file($filePath)) {
require $filePath;
}
}
}
}

5
tests/bootstrap.php Normal file
View File

@ -0,0 +1,5 @@
<?php
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'SplClassLoader.php');
$app = new SplClassLoader('Ratchet', dirname(__DIR__) . DIRECTORY_SEPARATOR . 'lib');
$app->register();