diff --git a/src/Ratchet/Resource/AbstractConnectionDecorator.php b/src/Ratchet/Resource/AbstractConnectionDecorator.php new file mode 100644 index 0000000..3ae5472 --- /dev/null +++ b/src/Ratchet/Resource/AbstractConnectionDecorator.php @@ -0,0 +1,43 @@ +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); + } +} \ No newline at end of file diff --git a/src/Ratchet/Resource/Connection.php b/src/Ratchet/Resource/Connection.php index 4f1485c..def516b 100644 --- a/src/Ratchet/Resource/Connection.php +++ b/src/Ratchet/Resource/Connection.php @@ -1,6 +1,7 @@ _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... diff --git a/src/Ratchet/Resource/ConnectionInterface.php b/src/Ratchet/Resource/ConnectionInterface.php index e754de8..f53df88 100644 --- a/src/Ratchet/Resource/ConnectionInterface.php +++ b/src/Ratchet/Resource/ConnectionInterface.php @@ -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(); } \ No newline at end of file diff --git a/src/Ratchet/Resource/Socket/BSDSocket.php b/src/Ratchet/Resource/Socket/BSDSocket.php index 353054a..d66c371 100644 --- a/src/Ratchet/Resource/Socket/BSDSocket.php +++ b/src/Ratchet/Resource/Socket/BSDSocket.php @@ -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; diff --git a/tests/Ratchet/Tests/Mock/Connection.php b/tests/Ratchet/Tests/Mock/Connection.php index 7c321f3..47f4da1 100644 --- a/tests/Ratchet/Tests/Mock/Connection.php +++ b/tests/Ratchet/Tests/Mock/Connection.php @@ -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; + } } \ No newline at end of file diff --git a/tests/Ratchet/Tests/Mock/ConnectionDecorator.php b/tests/Ratchet/Tests/Mock/ConnectionDecorator.php new file mode 100644 index 0000000..60e0862 --- /dev/null +++ b/tests/Ratchet/Tests/Mock/ConnectionDecorator.php @@ -0,0 +1,22 @@ + '' + , 'end' => false + ); + + public function write($data) { + $this->last[__FUNCTION__] = $data; + + $this->getConnection()->write($data); + } + + public function end() { + $this->last[__FUNCTION__] = true; + + $this->getConnection()->end(); + } +} \ No newline at end of file diff --git a/tests/Ratchet/Tests/Resource/AbstractConnectionDecoratorTest.php b/tests/Ratchet/Tests/Resource/AbstractConnectionDecoratorTest.php new file mode 100644 index 0000000..46cf6d7 --- /dev/null +++ b/tests/Ratchet/Tests/Resource/AbstractConnectionDecoratorTest.php @@ -0,0 +1,109 @@ +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'); + } +} \ No newline at end of file