From 9bdaaa87513470f911b26da7e3cd889a114ebebc Mon Sep 17 00:00:00 2001 From: Marc Morera Date: Wed, 11 Mar 2020 19:00:20 +0100 Subject: [PATCH] Improved server messages (#2) --- .circleci/config.yml | 5 ----- Connection/Connection.php | 24 ++++++++++++++++++++++++ Connection/WebsocketApp.php | 18 +++++++++--------- Console/ConnectToWebsocket.php | 1 - Console/WebsocketHeaderPrinter.php | 19 +++++++++++++++++++ Tests/RouteFunctionalTest.php | 9 ++++++++- Tests/WebsocketFunctionalTest.php | 1 + composer.json | 1 + 8 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 Connection/Connection.php diff --git a/.circleci/config.yml b/.circleci/config.yml index 01feef2..6f26072 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -10,11 +10,6 @@ jobs: steps: - checkout - - run: - name: Install PHPUnit - command: | - composer require phpunit/phpunit:7.5.17 --no-update - - run: name: Run tests / Symfony 5^0 command: | diff --git a/Connection/Connection.php b/Connection/Connection.php new file mode 100644 index 0000000..1354f22 --- /dev/null +++ b/Connection/Connection.php @@ -0,0 +1,24 @@ +outputPrinter) { (new ConsoleWebsocketMessage(sprintf( - 'Connection %s opened on route %s', - spl_object_hash($connection), + 'Connection %s - %s - Opened connection', + Connection::getConnectionHash($connection), $this->name ), '~', true))->print($this->outputPrinter); } @@ -105,8 +105,8 @@ public function onClose(ConnectionInterface $connection) if ($this->outputPrinter) { (new ConsoleWebsocketMessage(sprintf( - 'Connection %s closed from route %s', - spl_object_hash($connection), + 'Connection %s - %s - Closed connection', + Connection::getConnectionHash($connection), $this->name ), '~', true))->print($this->outputPrinter); } @@ -122,8 +122,8 @@ public function onError(ConnectionInterface $connection, Exception $exception) if ($this->outputPrinter) { (new ConsoleWebsocketMessage(sprintf( - 'Connection %s throw error on route %s', - spl_object_hash($connection), + 'Connection %s - %s - Error thrown', + Connection::getConnectionHash($connection), $this->name ), '~', true))->print($this->outputPrinter); } @@ -139,10 +139,10 @@ public function onMessage(ConnectionInterface $from, $message) if ($this->outputPrinter) { (new ConsoleWebsocketMessage(sprintf( - 'Message from connection %s on route %s - %s', - spl_object_hash($from), + 'Connection %s - %s - Messaged "%s"', + Connection::getConnectionHash($from), $this->name, - $message + trim($message, " \ \t\n\r\0\x0B") ), '~', true))->print($this->outputPrinter); } } diff --git a/Console/ConnectToWebsocket.php b/Console/ConnectToWebsocket.php index 9f83dcc..61aa2dc 100644 --- a/Console/ConnectToWebsocket.php +++ b/Console/ConnectToWebsocket.php @@ -86,7 +86,6 @@ protected function execute(InputInterface $input, OutputInterface $output) }); $stdio->on('data', function ($data) use ($connection, $stdio) { - echo 'LOL'; $connection->send($data); }); }) diff --git a/Console/WebsocketHeaderPrinter.php b/Console/WebsocketHeaderPrinter.php index aabba11..0e575d2 100644 --- a/Console/WebsocketHeaderPrinter.php +++ b/Console/WebsocketHeaderPrinter.php @@ -50,6 +50,10 @@ public static function print( $outputPrinter->printHeaderLine("Port: $port"); $outputPrinter->printHeaderLine("Environment: {$input->getOption('env')}"); $outputPrinter->printHeaderLine('Debug: '.($input->getOption('no-debug') ? 'disabled' : 'enabled')); + $outputPrinter->printHeaderLine('Routes: '.(!empty($input->getOption('route')) + ? implode(', ', static::getPlainRoutes($input)) + : 'disabled' + )); $outputPrinter->printHeaderLine('Exchanges subscribed: '.(!empty($input->getOption('exchange')) ? implode(', ', static::getPlainExchanges($input)) : 'disabled' @@ -58,6 +62,21 @@ public static function print( $outputPrinter->printLine(); } + /** + * @param InputInterface $input + * + * @return array + */ + public static function getPlainRoutes(InputInterface $input): array + { + $array = []; + foreach ($input->getOption('route') as $route) { + $array[] = trim($route); + } + + return $array; + } + /** * @param InputInterface $input * diff --git a/Tests/RouteFunctionalTest.php b/Tests/RouteFunctionalTest.php index 64c71f8..96b5acf 100644 --- a/Tests/RouteFunctionalTest.php +++ b/Tests/RouteFunctionalTest.php @@ -38,6 +38,9 @@ protected static function decorateConfiguration(array $configuration): array 'main' => [ 'path' => '/', ], + 'another' => [ + 'path' => '/another/', + ], ], ]; @@ -61,8 +64,12 @@ public function testRouteConnection() await($promise, $loop); sleep(1); - $this->assertContains('opened on route main', $websocketServer->getOutput()); + $this->assertContains('Opened connection', $websocketServer->getOutput()); $this->assertContains('TestEvent', $websocketServer->getOutput()); + $this->assertContains('Exchanges subscribed: events', $websocketServer->getOutput()); + $this->assertContains('Routes: main, another', $websocketServer->getOutput()); + $this->assertContains('Port: 8001', $websocketServer->getOutput()); + $this->assertContains('Host: localhost', $websocketServer->getOutput()); $this->assertContains('Opened connection', $conn1->getOutput()); $this->assertContains(TestEvent::class, $conn1->getOutput()); diff --git a/Tests/WebsocketFunctionalTest.php b/Tests/WebsocketFunctionalTest.php index 7187f3a..162bbbd 100644 --- a/Tests/WebsocketFunctionalTest.php +++ b/Tests/WebsocketFunctionalTest.php @@ -119,6 +119,7 @@ protected function createSocketServer(string $port): Process 'websocket:run', 'localhost:'.$port, '--route=main', + '--route=another', '--exchange=events', ]); } diff --git a/composer.json b/composer.json index 6006183..623b3ee 100644 --- a/composer.json +++ b/composer.json @@ -27,6 +27,7 @@ }, "require-dev": { + "phpunit/phpunit": "7.5.17", "drift/amqp-bundle": "0.1.*, >=0.1.2", "symfony/process": "^5.0" },