Skip to content

Commit

Permalink
Fix tests (#18)
Browse files Browse the repository at this point in the history
* fix tests

* fix linting

* Fix code styling
  • Loading branch information
joedixon authored Nov 20, 2023
1 parent b07fa67 commit 283e8f6
Show file tree
Hide file tree
Showing 30 changed files with 473 additions and 638 deletions.
4 changes: 2 additions & 2 deletions src/Channels/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ public function broadcast(Application $app, array $payload, Connection $except =
{
collect($this->connections())
->each(function ($connection) use ($payload, $except) {
if ($except && $except->id() === $connection->id()) {
if ($except && $except->identifier() === $connection->identifier()) {
return;
}

if (isset($payload['except']) && $payload['except'] === $connection->id()) {
if (isset($payload['except']) && $payload['except'] === $connection->identifier()) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ClientEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static function whisper(Connection $connection, array $payload): void
{
Event::dispatch(
$connection->app(),
$payload + ['except' => $connection->id()],
$payload + ['except' => $connection->identifier()],
$connection
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function disconnect(): void

App::make(ConnectionManager::class)
->for($this->app())
->disconnect($this->id());
->disconnect($this->identifier());

$this->terminate();
}
Expand Down
1 change: 0 additions & 1 deletion src/Contracts/ConnectionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Closure;
use Laravel\Reverb\Application;
use Laravel\Reverb\Managers\Connections;

interface ConnectionManager
{
Expand Down
5 changes: 5 additions & 0 deletions src/Contracts/ServerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,9 @@ abstract public function buildConnectionManager(): ConnectionManager;
* Build the channel manager for the server.
*/
abstract public function buildChannelManager(): ChannelManager;

/**
* Build the channel manager for the server.
*/
abstract public function buildChannelConnectionManager(): ChannelConnectionManager;
}
2 changes: 1 addition & 1 deletion src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function dispatchSynchronously(Application $app, array $payload, C

foreach ($channels as $channel) {
unset($payload['channels']);
$channel = app(ChannelManager::class)->find($channel);
$channel = app(ChannelManager::class)->for($app)->find($channel);
$payload['channel'] = $channel->name();

$channel->broadcast($app, $payload, $connection);
Expand Down
6 changes: 4 additions & 2 deletions src/Http/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ protected function handleRequest(string $message, Connection $connection): void
/**
* Create a Psr7 request from the incoming message.
*/
protected function createRequest(string $message, Connection $connection): RequestInterface
protected function createRequest(string $message, Connection $connection): ?RequestInterface
{
try {
return Request::from($message, $connection);
$request = Request::from($message, $connection);
} catch (OverflowException $e) {
$this->close($connection, 413, 'Payload too large.');
}

return $request;
}
}
2 changes: 2 additions & 0 deletions src/Jobs/PruneStaleConnections.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public function handle(ConnectionManager $connections): void
collect($connections->for($application)->all())
->each(function ($connection) {
if (! $connection->isStale()) {
dump('Connection is not stale', $connection->id());

return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Managers/ArrayConnectionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function all(): array
*/
public function save(Connection $connection): void
{
$this->connections[$this->application->id()][$connection->id()] = $connection;
$this->connections[$this->application->id()][$connection->identifier()] = $connection;
}

/**
Expand Down
37 changes: 0 additions & 37 deletions src/Managers/Connections.php

This file was deleted.

32 changes: 16 additions & 16 deletions src/Servers/ApiGateway/ApiGatewayProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Laravel\Reverb\Contracts\ApplicationProvider;
use Laravel\Reverb\Contracts\ConnectionManager as ConnectionManagerInterface;
use Laravel\Reverb\Contracts\ChannelConnectionManager;
use Laravel\Reverb\Contracts\ChannelManager;
use Laravel\Reverb\Contracts\ConnectionManager;
use Laravel\Reverb\Contracts\ServerProvider;
use Laravel\Reverb\Event;
use Laravel\Reverb\Jobs\PingInactiveConnections;
use Laravel\Reverb\Jobs\PruneStaleConnections;
use Laravel\Reverb\Managers\ChannelManager;
use Laravel\Reverb\Managers\ConnectionManager;
use Laravel\Reverb\Managers\ArrayChannelConnectionManager;
use Laravel\Reverb\Managers\ArrayChannelManager;
use Laravel\Reverb\Managers\ArrayConnectionManager;

class ApiGatewayProvider extends ServerProvider
{
Expand Down Expand Up @@ -56,25 +59,22 @@ public function register(): void
*/
public function buildConnectionManager(): ConnectionManager
{
return new ConnectionManager(
$this->app['cache']->store(
$this->config['connection_manager']['store']
),
$this->config['connection_manager']['prefix']
);
return new ArrayConnectionManager;
}

/**
* Build the channel manager for the server.
*/
public function buildChannelManager(): ChannelManager
{
return new ChannelManager(
$this->app['cache']->store(
$this->config['connection_manager']['store']
),
$this->app->make(ConnectionManagerInterface::class),
$this->config['connection_manager']['prefix']
);
return new ArrayChannelManager;
}

/**
* Build the channel manager for the server.
*/
public function buildChannelConnectionManager(): ChannelConnectionManager
{
return new ArrayChannelConnectionManager;
}
}
17 changes: 14 additions & 3 deletions src/Servers/Reverb/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Laravel\Reverb\Contracts\ApplicationProvider;
use Laravel\Reverb\Contracts\ConnectionManager;
use Laravel\Reverb\Exceptions\InvalidApplication;
use Laravel\Reverb\Server;
use Laravel\Reverb\Servers\Reverb\Connection as ReverbConnection;
use Laravel\Reverb\WebSockets\WsConnection;
Expand All @@ -16,7 +17,9 @@ class Controller
*/
public function __invoke(RequestInterface $request, WsConnection $connection, string $appKey): void
{
$reverbConnection = $this->connection($request, $connection, $appKey);
if (! $reverbConnection = $this->connection($request, $connection, $appKey)) {
return;
}

$server = app(Server::class);
$server->open($reverbConnection);
Expand All @@ -28,10 +31,18 @@ public function __invoke(RequestInterface $request, WsConnection $connection, st
/**
* Get the Reverb connection instance for the request.
*/
protected function connection(RequestInterface $request, WsConnection $connection, string $key): ReverbConnection
protected function connection(RequestInterface $request, WsConnection $connection, string $key): ?ReverbConnection
{
try {
$application = app(ApplicationProvider::class)->findByKey($key);
} catch (InvalidApplication $e) {
$connection->send('{"event":"pusher:error","data":"{\"code\":4001,\"message\":\"Application does not exist\"}"}');

return $connection->close();
}

return app(ConnectionManager::class)
->for($application = app(ApplicationProvider::class)->findByKey($key))
->for($application)
->connect(
new ReverbConnection(
$connection,
Expand Down
9 changes: 8 additions & 1 deletion tests/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
use Carbon\Carbon;
use Illuminate\Testing\Assert;
use Laravel\Reverb\Application;
use Laravel\Reverb\Concerns\GeneratesPusherIdentifiers;
use Laravel\Reverb\Contracts\ApplicationProvider;
use Laravel\Reverb\Contracts\Connection as BaseConnection;

class Connection extends BaseConnection
{
use GeneratesPusherIdentifiers;

public $messages = [];

public $identifier = '19c1c8e8-351b-4eb5-b6d9-6cbfc54a3446';

public $id = '10000.00001';
public $id;

public function __construct(string $identifier = null)
{
Expand All @@ -31,6 +34,10 @@ public function identifier(): string

public function id(): string
{
if (! $this->id) {
$this->id = $this->generateId();
}

return $this->id;
}

Expand Down
Loading

0 comments on commit 283e8f6

Please sign in to comment.