From a169756b06d3b1afe30873ad3ed12a5e5a799de5 Mon Sep 17 00:00:00 2001 From: "Nek (Maxime Veber)" Date: Sun, 11 Feb 2018 16:33:42 +0100 Subject: [PATCH] Add getter for event loop Now the user can close the connection but can also kill the loop if desired. --- src/Core/AbstractConnection.php | 8 ++++++++ tests/Woketo/Client/ConnectionTest.php | 17 ++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Core/AbstractConnection.php b/src/Core/AbstractConnection.php index 9143968..301bce7 100644 --- a/src/Core/AbstractConnection.php +++ b/src/Core/AbstractConnection.php @@ -164,6 +164,14 @@ public function close() $this->messageProcessor->close($this->stream); } + /** + * @return LoopInterface + */ + public function getLoop(): LoopInterface + { + return $this->loop; + } + /** * @param string|Frame $frame * @param int $opCode diff --git a/tests/Woketo/Client/ConnectionTest.php b/tests/Woketo/Client/ConnectionTest.php index c6d3fe3..00eb004 100644 --- a/tests/Woketo/Client/ConnectionTest.php +++ b/tests/Woketo/Client/ConnectionTest.php @@ -9,6 +9,7 @@ use Nekland\Woketo\Rfc6455\MessageProcessor; use Prophecy\Argument; use React\EventLoop\LoopInterface; +use React\EventLoop\StreamSelectLoop; use React\Promise\Promise; use React\Socket\ConnectionInterface; @@ -62,11 +63,25 @@ public function testItSendsMessagesWithMessageProcessor() $messageProcessor->write('hello', $socket, Argument::any())->shouldBeCalled(); - $promise = new Promise(function (callable $resolve, callable $reject) use ($socket) { $resolve($socket->reveal()); }); $connection = new Connection(new Url('ws://localhost:9000'), $promise, $messageProcessor->reveal(), $messageHandler->reveal(), $loop->reveal()); $connection->write('hello'); } + + public function testItGivesTheLoopBack() + { + $socket = $this->prophesize(ConnectionInterface::class); + $messageProcessor = $this->prophesize(MessageProcessor::class); + $messageHandler = $this->prophesize(MessageHandlerInterface::class); + $promise = new Promise(function (callable $resolve, callable $reject) use ($socket) { + $resolve($socket->reveal()); + }); + + $loop = new StreamSelectLoop(); + $connection = new Connection (new Url('ws://localhost:9000'), $promise, $messageProcessor->reveal(), $messageHandler->reveal(), $loop); + + $this->assertSame($loop, $connection->getLoop()); + } }