diff --git a/src/LiveReloadMessager.php b/src/LiveReloadMessager.php index ef16a33..f6c2762 100644 --- a/src/LiveReloadMessager.php +++ b/src/LiveReloadMessager.php @@ -50,7 +50,7 @@ public function onOpen(ConnectionInterface $connection) { $this->clients->attach($connection); - echo 'New connection to the LiveReload server' . "\n"; + $this->output('New connection to the LiveReload server'); } /** @@ -64,12 +64,12 @@ public function onOpen(ConnectionInterface $connection) public function onMessage(ConnectionInterface $connection, $message) { $targetCount = count($this->clients) - 1; - echo sprintf( + $this->output(sprintf( 'Sending message "%s" to %d other connection%s', $message, $targetCount, $targetCount == 1 ? '' : 's' - ) . "\n"; + )); foreach ($this->clients as $client) { if ($connection !== $client) { @@ -89,7 +89,7 @@ public function onClose(ConnectionInterface $connection) { $this->clients->detach($connection); - echo 'Connection has disconnected' . "\n"; + $this->output('Connection has disconnected'); } /** @@ -102,8 +102,19 @@ public function onClose(ConnectionInterface $connection) */ public function onError(ConnectionInterface $connection, \Exception $exception) { - echo sprintf('An error has occurred: `%s`', $exception->getMessage()) . "\n"; + $this->output(sprintf('An error has occurred: `%s`', $exception->getMessage())); $connection->close(); } + + /** + * Outputs a message. + * + * @param string $message The message to output + * @return void + */ + protected function output($message) + { + echo $message . "\n"; + } } diff --git a/tests/TestCase/Robo/Task/LiveReloadMessagerTest.php b/tests/TestCase/Robo/Task/LiveReloadMessagerTest.php new file mode 100644 index 0000000..f4395ba --- /dev/null +++ b/tests/TestCase/Robo/Task/LiveReloadMessagerTest.php @@ -0,0 +1,119 @@ +getMockBuilder(LiveReloadMessager::class) + ->setMethods(['output']) + ->getMock(); + + $this->sock = $this + ->getMockBuilder('\\React\\Socket\\ConnectionInterface') + ->getMock(); + + $messager + ->expects($this->once()) + ->method('output') + ->with('New connection to the LiveReload server'); + + $messager->onOpen(new IoConnection($this->sock)); + } + + /** + * Test the LiveReloadMessager::onMessage method + * + * @return void + */ + public function testOnMessage() + { + $messager = $this + ->getMockBuilder(LiveReloadMessager::class) + ->setMethods(['output']) + ->getMock(); + + $this->sock = $this + ->getMockBuilder('\\React\\Socket\\ConnectionInterface') + ->getMock(); + + $messager + ->expects($this->at(2)) + ->method('output') + ->with('Sending message "Hello" to 1 other connection'); + + $connection = $this + ->getMockBuilder('\\Ratchet\\Server\\IoConnection') + ->setConstructorArgs([$this->sock]) + ->setMethods(['send']) + ->getMock(); + + $connection + ->expects($this->once()) + ->method('send') + ->with('Hello'); + + $messager->onOpen(new IoConnection($this->sock)); + $messager->onOpen($connection); + $messager->onMessage(new IoConnection($this->sock), 'Hello'); + } + + /** + * Test the LiveReloadMessager::onClose method + * + * @return void + */ + public function testOnClose() + { + $messager = $this + ->getMockBuilder(LiveReloadMessager::class) + ->setMethods(['output']) + ->getMock(); + + $this->sock = $this + ->getMockBuilder('\\React\\Socket\\ConnectionInterface') + ->getMock(); + + $messager + ->expects($this->once()) + ->method('output') + ->with('Connection has disconnected'); + + $messager->onClose(new IoConnection($this->sock)); + } + + /** + * Test the LiveReloadMessager::onError method + * + * @return void + */ + public function testOnError() + { + $messager = $this + ->getMockBuilder(LiveReloadMessager::class) + ->setMethods(['output']) + ->getMock(); + + $this->sock = $this + ->getMockBuilder('\\React\\Socket\\ConnectionInterface') + ->getMock(); + + $messager + ->expects($this->once()) + ->method('output') + ->with('An error has occurred: `An error`'); + + $messager->onError(new IoConnection($this->sock), new \Exception('An error')); + } +}