ConnectionInterface updates
Added write/end methods to Connections Created decorator class implementing ConnectionInterface
This commit is contained in:
parent
8404f911a3
commit
dbc40b821d
43
src/Ratchet/Resource/AbstractConnectionDecorator.php
Normal file
43
src/Ratchet/Resource/AbstractConnectionDecorator.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
namespace Ratchet\Resource;
|
||||
|
||||
abstract class AbstractConnectionDecorator implements ConnectionInterface {
|
||||
/**
|
||||
* @var ConnectionInterface
|
||||
*/
|
||||
protected $wrappedConn;
|
||||
|
||||
public function __construct(ConnectionInterface $conn) {
|
||||
$this->wrappedConn = $conn;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ConnectionInterface
|
||||
*/
|
||||
protected function getConnection() {
|
||||
return $this->wrappedConn;
|
||||
}
|
||||
|
||||
public function __set($name, $value) {
|
||||
$this->wrappedConn->$name = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo trigger_error() instead - Have it the same as if called from a POPO
|
||||
*/
|
||||
public function __get($name) {
|
||||
if (!$this->__isset($name)) {
|
||||
throw new \InvalidArgumentException("Attribute '{$name}' not found in Connection {$this->getID()}");
|
||||
}
|
||||
|
||||
return $this->wrappedConn->$name;
|
||||
}
|
||||
|
||||
public function __isset($name) {
|
||||
return isset($this->wrappedConn->$name);
|
||||
}
|
||||
|
||||
public function __unset($name) {
|
||||
unset($this->wrappedConn->$name);
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace Ratchet\Resource;
|
||||
use Ratchet\Resource\Socket\SocketInterface;
|
||||
use Ratchet\Resource\Socket\BSDSocketException;
|
||||
|
||||
/**
|
||||
* A proxy object representing a connection to the application
|
||||
@ -16,6 +17,25 @@ class Connection implements ConnectionInterface {
|
||||
$this->_socket = $socket;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function write($data) {
|
||||
return $this->_socket->deliver($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function end() {
|
||||
try {
|
||||
$this->_socket->shutdown();
|
||||
} catch (BSDSocketException $e) {
|
||||
}
|
||||
|
||||
$this->_socket->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* This is here because I couldn't figure out a better/easier way to tie a connection and socket together for the server and commands
|
||||
* Anyway, if you're here, it's not recommended you use this/directly interact with the socket in your App...
|
||||
|
@ -4,4 +4,14 @@ namespace Ratchet\Resource;
|
||||
const VERSION = 'Ratchet/0.1';
|
||||
|
||||
interface ConnectionInterface {
|
||||
/**
|
||||
* Send data to the connection
|
||||
* @param string
|
||||
*/
|
||||
function write($data);
|
||||
|
||||
/**
|
||||
* End the connection
|
||||
*/
|
||||
function end();
|
||||
}
|
@ -157,7 +157,7 @@ class BSDSocket implements SocketInterface {
|
||||
$num = socket_select($read, $write, $except, $tv_sec, $tv_usec);
|
||||
|
||||
if (false === $num) {
|
||||
throw new BSDException($this);
|
||||
throw new BSDSocketException($this);
|
||||
}
|
||||
|
||||
return $num;
|
||||
|
@ -3,5 +3,18 @@ namespace Ratchet\Tests\Mock;
|
||||
use Ratchet\Resource\ConnectionInterface;
|
||||
|
||||
class Connection implements ConnectionInterface {
|
||||
public $last = array(
|
||||
'write' => ''
|
||||
, 'end' => false
|
||||
);
|
||||
|
||||
public $remoteAddress = '127.0.0.1';
|
||||
|
||||
public function write($data) {
|
||||
$this->last[__FUNCTION__] = $data;
|
||||
}
|
||||
|
||||
public function end() {
|
||||
$this->last[__FUNCTION__] = true;
|
||||
}
|
||||
}
|
22
tests/Ratchet/Tests/Mock/ConnectionDecorator.php
Normal file
22
tests/Ratchet/Tests/Mock/ConnectionDecorator.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
namespace Ratchet\Tests\Mock;
|
||||
use Ratchet\Resource\AbstractConnectionDecorator;
|
||||
|
||||
class ConnectionDecorator extends AbstractConnectionDecorator {
|
||||
public $last = array(
|
||||
'write' => ''
|
||||
, 'end' => false
|
||||
);
|
||||
|
||||
public function write($data) {
|
||||
$this->last[__FUNCTION__] = $data;
|
||||
|
||||
$this->getConnection()->write($data);
|
||||
}
|
||||
|
||||
public function end() {
|
||||
$this->last[__FUNCTION__] = true;
|
||||
|
||||
$this->getConnection()->end();
|
||||
}
|
||||
}
|
109
tests/Ratchet/Tests/Resource/AbstractConnectionDecoratorTest.php
Normal file
109
tests/Ratchet/Tests/Resource/AbstractConnectionDecoratorTest.php
Normal file
@ -0,0 +1,109 @@
|
||||
<?php
|
||||
namespace Ratchet\Tests\Resource;
|
||||
use Ratchet\Tests\Mock\ConnectionDecorator;
|
||||
use Ratchet\Tests\Mock\Connection;
|
||||
|
||||
/**
|
||||
* @covers Ratchet\Resource\AbstractConnectionDecorator
|
||||
*/
|
||||
class AbstractConnectionDecoratorTest extends \PHPUnit_Framework_TestCase {
|
||||
protected $mock;
|
||||
protected $l1;
|
||||
protected $l2;
|
||||
|
||||
public function setUp() {
|
||||
$this->mock = new Connection;
|
||||
$this->l1 = new ConnectionDecorator($this->mock);
|
||||
$this->l2 = new ConnectionDecorator($this->l1);
|
||||
}
|
||||
|
||||
public function testGet() {
|
||||
$var = 'hello';
|
||||
$val = 'world';
|
||||
|
||||
$this->mock->$var = $val;
|
||||
|
||||
$this->assertEquals($val, $this->l1->$var);
|
||||
$this->assertEquals($val, $this->l2->$var);
|
||||
}
|
||||
|
||||
public function testSet() {
|
||||
$var = 'Chris';
|
||||
$val = 'Boden';
|
||||
|
||||
$this->l1->$var = $val;
|
||||
|
||||
$this->assertEquals($val, $this->mock->$var);
|
||||
}
|
||||
|
||||
public function testSetLevel2() {
|
||||
$var = 'Try';
|
||||
$val = 'Again';
|
||||
|
||||
$this->l2->$var = $val;
|
||||
|
||||
$this->assertEquals($val, $this->mock->$var);
|
||||
}
|
||||
|
||||
public function testIsSetTrue() {
|
||||
$var = 'PHP';
|
||||
$val = 'Ratchet';
|
||||
|
||||
$this->mock->$var = $val;
|
||||
|
||||
$this->assertTrue(isset($this->l1->$var));
|
||||
$this->assertTrue(isset($this->l2->$var));
|
||||
}
|
||||
|
||||
public function testIsSetFalse() {
|
||||
$var = 'herp';
|
||||
$val = 'derp';
|
||||
|
||||
$this->assertFalse(isset($this->l1->$var));
|
||||
$this->assertFalse(isset($this->l2->$var));
|
||||
}
|
||||
|
||||
public function testUnset() {
|
||||
$var = 'Flying';
|
||||
$val = 'Monkey';
|
||||
|
||||
$this->mock->$var = $val;
|
||||
unset($this->l1->$var);
|
||||
|
||||
$this->assertFalse(isset($this->mock->$var));
|
||||
}
|
||||
|
||||
public function testUnsetLevel2() {
|
||||
$var = 'Flying';
|
||||
$val = 'Monkey';
|
||||
|
||||
$this->mock->$var = $val;
|
||||
unset($this->l2->$var);
|
||||
|
||||
$this->assertFalse(isset($this->mock->$var));
|
||||
}
|
||||
|
||||
public function testGetConnection() {
|
||||
$class = new \ReflectionClass('\\Ratchet\\Resource\\AbstractConnectionDecorator');
|
||||
$method = $class->getMethod('getConnection');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$conn = $method->invokeArgs($this->l1, array());
|
||||
|
||||
$this->assertSame($this->mock, $conn);
|
||||
}
|
||||
|
||||
public function testGetConnectionLevel2() {
|
||||
$class = new \ReflectionClass('\\Ratchet\\Resource\\AbstractConnectionDecorator');
|
||||
$method = $class->getMethod('getConnection');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$conn = $method->invokeArgs($this->l2, array());
|
||||
|
||||
$this->assertSame($this->l1, $conn);
|
||||
}
|
||||
|
||||
public function testWarningGettingNothing() {
|
||||
$this->markTestSkipped('Functionality not in class yet');
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user