Skip to content

Commit

Permalink
Added PHP8 support (#7)
Browse files Browse the repository at this point in the history
- Fixed event configuration in testing
  • Loading branch information
mmoreram authored Oct 24, 2021
1 parent 90f046f commit acb9fde
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 79 deletions.
20 changes: 18 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,26 @@ jobs:
command: |
composer update -n --prefer-dist --no-suggest
rm -Rf var/*
php vendor/bin/phpunit
sh .circleci/start.sh
test-php80:
docker:
- image: circleci/php:8.0-cli
- image: rabbitmq

working_directory: ~/project
steps:
- checkout

- run:
name: Run tests / Symfony 5^0
command: |
composer update -n --prefer-dist --no-suggest
rm -Rf var/*
sh .circleci/start.sh
workflows:
version: 2
test:
jobs:
- test-php74
- test-php74
- test-php80
4 changes: 4 additions & 0 deletions .circleci/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

while ! nc -z localhost 5672; do sleep 1; done
php vendor/bin/phpunit
2 changes: 1 addition & 1 deletion .php_cs.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"php":"7.4.3","version":"2.16.1","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":{"allow_single_line_closure":true},"class_definition":{"single_line":true},"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":true,"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":true,"single_import_per_statement":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"binary_operator_spaces":true,"blank_line_after_opening_tag":true,"blank_line_before_statement":{"statements":["return"]},"cast_spaces":true,"class_attributes_separation":{"elements":["method"]},"concat_space":true,"declare_equal_normalize":true,"function_typehint_space":true,"include":true,"increment_style":true,"lowercase_cast":true,"lowercase_static_reference":true,"magic_constant_casing":true,"magic_method_casing":true,"native_function_casing":true,"native_function_type_declaration_casing":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_blank_lines_after_phpdoc":true,"no_empty_comment":true,"no_empty_phpdoc":true,"no_empty_statement":true,"no_extra_blank_lines":{"tokens":["curly_brace_block","extra","parenthesis_brace_block","square_brace_block","throw","use"]},"no_leading_import_slash":true,"no_leading_namespace_whitespace":true,"no_mixed_echo_print":true,"no_multiline_whitespace_around_double_arrow":true,"no_short_bool_cast":true,"no_singleline_whitespace_before_semicolons":true,"no_spaces_around_offset":true,"no_trailing_comma_in_list_call":true,"no_trailing_comma_in_singleline_array":true,"no_unneeded_control_parentheses":true,"no_unneeded_curly_braces":true,"no_unneeded_final_method":true,"no_unused_imports":true,"no_whitespace_before_comma_in_array":true,"no_whitespace_in_blank_line":true,"normalize_index_brace":true,"object_operator_without_whitespace":true,"ordered_imports":true,"php_unit_fqcn_annotation":true,"phpdoc_align":{"tags":["method","param","property","return","throws","type","var"]},"phpdoc_annotation_without_dot":true,"phpdoc_indent":true,"phpdoc_inline_tag":true,"phpdoc_no_access":true,"phpdoc_no_alias_tag":true,"phpdoc_no_package":true,"phpdoc_no_useless_inheritdoc":true,"phpdoc_return_self_reference":true,"phpdoc_scalar":true,"phpdoc_separation":true,"phpdoc_single_line_var_spacing":true,"phpdoc_summary":true,"phpdoc_to_comment":true,"phpdoc_trim":true,"phpdoc_trim_consecutive_blank_line_separation":true,"phpdoc_types":true,"phpdoc_types_order":{"null_adjustment":"always_last","sort_algorithm":"none"},"phpdoc_var_without_name":true,"return_type_declaration":true,"semicolon_after_instruction":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_line_comment_style":{"comment_types":["hash"]},"single_line_throw":true,"single_quote":true,"single_trait_insert_per_statement":true,"space_after_semicolon":{"remove_in_empty_for_expressions":true},"standardize_increment":true,"standardize_not_equals":true,"ternary_operator_spaces":true,"trailing_comma_in_multiline_array":true,"trim_array_spaces":true,"unary_operator_spaces":true,"whitespace_after_comma_in_array":true,"yoda_style":true},"hashes":{"Connection\/WebsocketApps.php":2317183475,"Connection\/Connections.php":84316399,"Connection\/WebsocketServer.php":3342442655,"Connection\/WebsocketApp.php":2545711206,"DependencyInjection\/WebsocketConfiguration.php":2953772139,"DependencyInjection\/CompilerPass\/WebsocketCompilerPass.php":1543538877,"DependencyInjection\/WebsocketExtension.php":1899471205,"WebsocketBundle.php":1472971440,"Event\/WebsocketConnectionClosed.php":2222190510,"Event\/WebsocketMessageReceived.php":3197828197,"Event\/WebsocketConnectionError.php":3011051078,"Event\/WebsocketConnectionOpened.php":695964194,"Event\/WebsocketEvent.php":2053024357,"Tests\/RouteFunctionalTest.php":2473681876,"Tests\/WebsocketFunctionalTest.php":3469799083,"Tests\/WebsocketEventSubscriber.php":2248726942,"Tests\/TestEvent.php":4131591015,"Console\/WebsocketHeaderPrinter.php":3710664822,"Console\/ConnectToWebsocket.php":2680932720,"Console\/RunWebsocket.php":3757557141,"Console\/ConsoleWebsocketMessage.php":2669861452}}
{"php":"7.4.15","version":"2.16.1","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":{"allow_single_line_closure":true},"class_definition":{"single_line":true},"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":true,"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":true,"single_import_per_statement":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"binary_operator_spaces":true,"blank_line_after_opening_tag":true,"blank_line_before_statement":{"statements":["return"]},"cast_spaces":true,"class_attributes_separation":{"elements":["method"]},"concat_space":true,"declare_equal_normalize":true,"function_typehint_space":true,"include":true,"increment_style":true,"lowercase_cast":true,"lowercase_static_reference":true,"magic_constant_casing":true,"magic_method_casing":true,"native_function_casing":true,"native_function_type_declaration_casing":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_blank_lines_after_phpdoc":true,"no_empty_comment":true,"no_empty_phpdoc":true,"no_empty_statement":true,"no_extra_blank_lines":{"tokens":["curly_brace_block","extra","parenthesis_brace_block","square_brace_block","throw","use"]},"no_leading_import_slash":true,"no_leading_namespace_whitespace":true,"no_mixed_echo_print":true,"no_multiline_whitespace_around_double_arrow":true,"no_short_bool_cast":true,"no_singleline_whitespace_before_semicolons":true,"no_spaces_around_offset":true,"no_trailing_comma_in_list_call":true,"no_trailing_comma_in_singleline_array":true,"no_unneeded_control_parentheses":true,"no_unneeded_curly_braces":true,"no_unneeded_final_method":true,"no_unused_imports":true,"no_whitespace_before_comma_in_array":true,"no_whitespace_in_blank_line":true,"normalize_index_brace":true,"object_operator_without_whitespace":true,"ordered_imports":true,"php_unit_fqcn_annotation":true,"phpdoc_align":{"tags":["method","param","property","return","throws","type","var"]},"phpdoc_annotation_without_dot":true,"phpdoc_indent":true,"phpdoc_inline_tag":true,"phpdoc_no_access":true,"phpdoc_no_alias_tag":true,"phpdoc_no_package":true,"phpdoc_no_useless_inheritdoc":true,"phpdoc_return_self_reference":true,"phpdoc_scalar":true,"phpdoc_separation":true,"phpdoc_single_line_var_spacing":true,"phpdoc_summary":true,"phpdoc_to_comment":true,"phpdoc_trim":true,"phpdoc_trim_consecutive_blank_line_separation":true,"phpdoc_types":true,"phpdoc_types_order":{"null_adjustment":"always_last","sort_algorithm":"none"},"phpdoc_var_without_name":true,"return_type_declaration":true,"semicolon_after_instruction":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_line_comment_style":{"comment_types":["hash"]},"single_line_throw":true,"single_quote":true,"single_trait_insert_per_statement":true,"space_after_semicolon":{"remove_in_empty_for_expressions":true},"standardize_increment":true,"standardize_not_equals":true,"ternary_operator_spaces":true,"trailing_comma_in_multiline_array":true,"trim_array_spaces":true,"unary_operator_spaces":true,"whitespace_after_comma_in_array":true,"yoda_style":true},"hashes":{"Connection\/WebsocketApps.php":4157201624,"Connection\/Connections.php":1889335883,"Connection\/WebsocketServer.php":103965827,"Connection\/WebsocketApp.php":3711384802,"Connection\/Connection.php":1455972305,"Tests\/RouteFunctionalTest.php":3650438628,"Tests\/WebsocketFunctionalTest.php":2593048220,"Tests\/WebsocketEventSubscriber.php":956909013,"Tests\/TestEvent.php":4131591015,"DependencyInjection\/WebsocketConfiguration.php":2953772139,"DependencyInjection\/CompilerPass\/WebsocketCompilerPass.php":1543538877,"DependencyInjection\/WebsocketExtension.php":1899471205,"WebsocketBundle.php":1472971440,"Console\/ConsoleWebsocketMessage.php":4073404464,"Console\/WebsocketHeaderPrinter.php":3577760015,"Console\/ConnectToWebsocket.php":1389340276,"Console\/RunWebsocket.php":2245222042,"Event\/WebsocketConnectionClosed.php":2222190510,"Event\/WebsocketMessageReceived.php":3197828197,"Event\/WebsocketConnectionError.php":3371276194,"Event\/WebsocketConnectionOpened.php":695964194,"Event\/WebsocketEvent.php":2053024357}}
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"defects":{"Drift\\Websocket\\Tests\\RouteFunctionalTest::testRouteConnection":3},"times":{"Drift\\Websocket\\Tests\\RouteFunctionalTest::testRouteConnection":7.141}}
20 changes: 16 additions & 4 deletions Connection/Connection.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
<?php

/*
* This file is part of the DriftPHP Project
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <[email protected]>
*/

declare(strict_types=1);

namespace Drift\Websocket\Connection;

use Ratchet\ConnectionInterface;

/**
* Class Connection
* Class Connection.
*/
class Connection
{
/**
* Get connection hash
* Get connection hash.
*
* @param ConnectionInterface $connection
*
* @return string
*/
public static function getConnectionHash(ConnectionInterface $connection) : string
public static function getConnectionHash(ConnectionInterface $connection): string
{
return substr(md5(spl_object_hash($connection)), 0, 13);
}
}
}
5 changes: 1 addition & 4 deletions Connection/Connections.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
*/
class Connections
{
/**
* @var SplObjectStorage
*/
private $connections;
private SplObjectStorage $connections;

/**
* Connections constructor.
Expand Down
34 changes: 15 additions & 19 deletions Connection/WebsocketApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,11 @@
*/
class WebsocketApp implements MessageComponentInterface
{
/**
* @var string
*/
private $name;

/**
* @var Connections
*/
private $connections;

/**
* @var InlineEventBus
*/
private $eventBus;

/**
* @var OutputPrinter
*/
private $outputPrinter;
private string $name;
private Connections $connections;
private InlineEventBus $eventBus;
private OutputPrinter $outputPrinter;
private bool $broadcast = false;

/**
* WebsocketsApp constructor.
Expand All @@ -76,6 +62,11 @@ public function setOutputPrinter(OutputPrinter $outputPrinter): void
$this->outputPrinter = $outputPrinter;
}

public function broadcast(): void
{
$this->broadcast = true;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -136,6 +127,7 @@ public function onMessage(ConnectionInterface $from, $message)
{
$event = new WebsocketMessageReceived($this->name, $this->connections, $from, $message);
$this->eventBus->dispatch($event);
$message = trim($message, " \ \t\n\r\0\x0B");

if ($this->outputPrinter) {
(new ConsoleWebsocketMessage(sprintf(
Expand All @@ -144,6 +136,10 @@ public function onMessage(ConnectionInterface $from, $message)
$this->name,
trim($message, " \ \t\n\r\0\x0B")
), '~', true))->print($this->outputPrinter);

if ($this->broadcast) {
$this->connections->broadcast($message, $from);
}
}
}
}
5 changes: 1 addition & 4 deletions Connection/WebsocketApps.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
*/
class WebsocketApps
{
/**
* @var array
*/
private $apps = [];
private array $apps = [];

/**
* Add app.
Expand Down
17 changes: 8 additions & 9 deletions Connection/WebsocketServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,8 @@
*/
class WebsocketServer
{
/**
* @var LoopInterface
*/
private $loop;

/**
* @var WebsocketApps
*/
private $apps;
private LoopInterface $loop;
private WebsocketApps $apps;

/**
* WebsocketServer constructor.
Expand All @@ -53,13 +46,15 @@ public function __construct(LoopInterface $loop, WebsocketApps $apps)
* @param int $port
* @param OutputPrinter $outputPrinter
* @param string[] $connectionsName
* @param bool $broadcast
* @param string $address
*/
public function createServer(
string $httpHost,
int $port,
array $connectionsName,
OutputPrinter $outputPrinter,
bool $broadcast,
string $address = '0.0.0.0'
) {
$server = new App($httpHost, $port, $address, $this->loop);
Expand All @@ -69,6 +64,10 @@ public function createServer(

foreach ($apps as list($app, $configuration)) {
$app->setOutputPrinter($outputPrinter);
if ($broadcast) {
$app->broadcast();
}

$server->route(
$configuration['path'],
$app,
Expand Down
10 changes: 2 additions & 8 deletions Console/ConnectToWebsocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
Expand All @@ -35,11 +34,7 @@ class ConnectToWebsocket extends Command
* @var string
*/
protected static $defaultName = 'websocket:connect';

/**
* @var LoopInterface
*/
private $loop;
private LoopInterface $loop;

/**
* RunWebsocket constructor.
Expand All @@ -60,8 +55,7 @@ protected function configure()
{
$this
->setDescription('Run the websocket')
->addArgument('path', InputArgument::REQUIRED, 'The server will start listening to this address')
->addOption('route', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Routes to listen');
->addArgument('path', InputArgument::REQUIRED, 'The server will start listening to this address');
}

/**
Expand Down
6 changes: 4 additions & 2 deletions Console/RunWebsocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ protected function configure()
null,
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
'Exchanges to listen'
);
)
->addOption('broadcast', null, InputOption::VALUE_NONE, 'The server broadcasts messages to all connections');
}

/**
Expand Down Expand Up @@ -113,7 +114,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$host,
(int) $port,
$input->getOption('route'),
$outputPrinter
$outputPrinter,
\boolval($input->getOption('broadcast')),
);

$this->eventBusSubscriber->subscribeToExchanges(
Expand Down
2 changes: 1 addition & 1 deletion Event/WebsocketConnectionError.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace Drift\Websocket\Event;

use App\Connections;
use Drift\Websocket\Connection\Connections;
use Exception;
use Ratchet\ConnectionInterface;

Expand Down
23 changes: 12 additions & 11 deletions Tests/RouteFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,25 @@ public function testRouteConnection()
$websocketServer = $this->createSocketServer('8001');
sleep(1);
$conn1 = $this->connectToSocket('8001');
sleep(2);
$conn2 = $this->connectToSocket('8001');
sleep(1);
sleep(2);

$promise = $this->get('drit.event_bus.public')->dispatch(new TestEvent());
await($promise, $loop);
sleep(1);

$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->assertStringContainsString('Opened connection', $websocketServer->getOutput());
$this->assertStringContainsString('TestEvent', $websocketServer->getOutput());
$this->assertStringContainsString('Exchanges subscribed: events', $websocketServer->getOutput());
$this->assertStringContainsString('Routes: main, another', $websocketServer->getOutput());
$this->assertStringContainsString('Port: 8001', $websocketServer->getOutput());
$this->assertStringContainsString('Host: localhost', $websocketServer->getOutput());

$this->assertContains('Opened connection', $conn1->getOutput());
$this->assertContains(TestEvent::class, $conn1->getOutput());
$this->assertContains('Opened connection', $conn2->getOutput());
$this->assertContains(TestEvent::class, $conn2->getOutput());
$this->assertStringContainsString('Opened connection', $conn1->getOutput());
$this->assertStringContainsString(TestEvent::class, $conn1->getOutput());
$this->assertStringContainsString('Opened connection', $conn2->getOutput());
$this->assertStringContainsString(TestEvent::class, $conn2->getOutput());

$websocketServer->stop();
$conn1->stop();
Expand Down
5 changes: 1 addition & 4 deletions Tests/WebsocketEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
*/
class WebsocketEventSubscriber implements EventSubscriberInterface
{
/**
* @var Connections
*/
private $connections;
private Connections $connections;

/**
* WebsocketEventSubscriber constructor.
Expand Down
5 changes: 4 additions & 1 deletion Tests/WebsocketFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ protected static function getKernel(): KernelInterface
'exchanges' => [
'events' => 'events',
],
'router' => [
'_all' => 'events',
],
'async_adapter' => [
'adapter' => 'amqp',
'amqp' => [
Expand Down Expand Up @@ -135,7 +138,7 @@ protected function connectToSocket(string $port): Process
{
return static::runAsyncCommand([
'websocket:connect',
'ws://localhost:'.$port,
'ws://localhost:'.$port.'/',
]);
}

Expand Down
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
],

"require": {
"php": "^7.3",
"php": "^7.4 | ^8.0",

"symfony/console": "^5.0.0",
"symfony/framework-bundle": "^5.0.0",
"clue/stdio-react": "^2.0",
"symfony/console": "^5",
"symfony/framework-bundle": "^5",
"clue/stdio-react": "^2",

"drift/http-kernel": "0.1.*, >=0.1.7",
"drift/event-bus-bundle": "0.1.*, >=0.1.4",
"drift/http-kernel": "^0.1.7",
"drift/event-bus-bundle": "^0.1.4",
"drift/event-loop-utils": "0.1.*",
"mmoreram/base-bundle": "^2.1.2",

Expand All @@ -27,8 +27,8 @@
},

"require-dev": {
"phpunit/phpunit": "7.5.17",
"drift/amqp-bundle": "0.1.*, >=0.1.2",
"phpunit/phpunit": "^9",
"drift/amqp-bundle": "^0.1.2",
"symfony/process": "^5.0"
},

Expand Down
Loading

0 comments on commit acb9fde

Please sign in to comment.