Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

[2.x] Code coverage fixes #482

Merged
merged 22 commits into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
codecov:
notify:
require_ci_to_pass: yes

coverage:
precision: 2
round: down
range: "70...100"

status:
project: yes
patch: yes
changes: no

comment:
layout: "reach, diff, flags, files, footer"
behavior: default
require_changes: no
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench-browser-kit:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest

- name: Execute tests with Local driver
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ vendor
coverage
.phpunit.result.cache
.idea/
database.sqlite
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
},
"require-dev": {
"mockery/mockery": "^1.3",
"orchestra/testbench": "3.8.*|^4.0|^5.0",
"orchestra/testbench-browser-kit": "^4.0|^5.0",
"phpunit/phpunit": "^8.0|^9.0"
},
"autoload": {
Expand Down
32 changes: 32 additions & 0 deletions src/Contracts/PushesToPusher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace BeyondCode\LaravelWebSockets\Contracts;

use BeyondCode\LaravelWebSockets\PubSub\Broadcasters\RedisPusherBroadcaster;
use Illuminate\Broadcasting\Broadcasters\PusherBroadcaster;
use Pusher\Pusher;

trait PushesToPusher
{
/**
* Get the right Pusher broadcaster for the used driver.
*
* @param array $app
* @return \Illuminate\Broadcasting\Broadcasters\Broadcaster
*/
public function getPusherBroadcaster(array $app)
{
if (config('websockets.replication.driver') === 'redis') {
return new RedisPusherBroadcaster(
new Pusher($app['key'], $app['secret'], $app['id'], config('broadcasting.connections.websockets.options', [])),
$app['id'],
app('redis'),
config('broadcasting.connections.websockets.connection', null)
);
}

return new PusherBroadcaster(
new Pusher($app['key'], $app['secret'], $app['id'], config('broadcasting.connections.pusher.options', []))
);
}
}
15 changes: 8 additions & 7 deletions src/Dashboard/Http/Controllers/AuthenticateDashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers;

use BeyondCode\LaravelWebSockets\Apps\App;
use BeyondCode\LaravelWebSockets\Contracts\PushesToPusher;
use Illuminate\Broadcasting\Broadcasters\PusherBroadcaster;
use Illuminate\Http\Request;
use Pusher\Pusher;

class AuthenticateDashboard
{
use PushesToPusher;

/**
* Find the app by using the header
* and then reconstruct the PusherBroadcaster
Expand All @@ -21,12 +23,11 @@ public function __invoke(Request $request)
{
$app = App::findById($request->header('x-app-id'));

$broadcaster = new PusherBroadcaster(new Pusher(
$app->key,
$app->secret,
$app->id,
[]
));
$broadcaster = $this->getPusherBroadcaster([
'key' => $app->key,
'secret' => $app->secret,
'id' =>$app->id,
]);

/*
* Since the dashboard itself is already secured by the
Expand Down
51 changes: 25 additions & 26 deletions src/Dashboard/Http/Controllers/SendMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers;

use BeyondCode\LaravelWebSockets\Contracts\PushesToPusher;
use BeyondCode\LaravelWebSockets\Statistics\Rules\AppId;
use Illuminate\Broadcasting\Broadcasters\PusherBroadcaster;
use Exception;
use Illuminate\Http\Request;
use Pusher\Pusher;

class SendMessage
{
use PushesToPusher;

/**
* Send the message to the requested channel.
*
Expand All @@ -17,7 +19,7 @@ class SendMessage
*/
public function __invoke(Request $request)
{
$validated = $request->validate([
$request->validate([
'appId' => ['required', new AppId],
'key' => 'required|string',
'secret' => 'required|string',
Expand All @@ -26,30 +28,27 @@ public function __invoke(Request $request)
'data' => 'required|json',
]);

$this->getPusherBroadcaster($validated)->broadcast(
[$validated['channel']],
$validated['event'],
json_decode($validated['data'], true)
);
$broadcaster = $this->getPusherBroadcaster([
'key' => $request->key,
'secret' => $request->secret,
'id' => $request->appId,
]);

return 'ok';
}
try {
$broadcaster->broadcast(
[$request->channel],
$request->event,
json_decode($request->data, true)
);
} catch (Exception $e) {
return response()->json([
'ok' => false,
'exception' => $e->getMessage(),
]);
}

/**
* Get the pusher broadcaster for the current request.
*
* @param array $validated
* @return \Illuminate\Broadcasting\Broadcasters\PusherBroadcaster
*/
protected function getPusherBroadcaster(array $validated): PusherBroadcaster
{
$pusher = new Pusher(
$validated['key'],
$validated['secret'],
$validated['appId'],
config('broadcasting.connections.pusher.options', [])
);

return new PusherBroadcaster($pusher);
return response()->json([
'ok' => true,
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver;
use Illuminate\Http\Request;

class DashboardApiController
class ShowStatistics
{
/**
* Get statistics for an app ID.
Expand All @@ -15,7 +15,7 @@ class DashboardApiController
* @param mixed $appId
* @return \Illuminate\Http\Response
*/
public function getStatistics(Request $request, StatisticsDriver $driver, $appId)
public function __invoke(Request $request, StatisticsDriver $driver, $appId)
{
return $driver::get($appId, $request);
}
Expand Down
15 changes: 7 additions & 8 deletions src/PubSub/Broadcasters/RedisPusherBroadcaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ class RedisPusherBroadcaster extends Broadcaster
/**
* Create a new broadcaster instance.
*
* @param Pusher $pusher
* @param $appId
* @param \Illuminate\Contracts\Redis\Factory $redis
* @param string|null $connection
* @param Pusher $pusher
* @param mixed $appId
* @param \Illuminate\Contracts\Redis\Factory $redis
* @param string|null $connection
*/
public function __construct(Pusher $pusher, $appId, Redis $redis, $connection = null)
{
Expand All @@ -63,7 +63,6 @@ public function __construct(Pusher $pusher, $appId, Redis $redis, $connection =
*
* @param \Illuminate\Http\Request $request
* @return mixed
*
* @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
*/
public function auth($request)
Expand All @@ -83,8 +82,8 @@ public function auth($request)
/**
* Return the valid authentication response.
*
* @param \Illuminate\Http\Request $request
* @param mixed $result
* @param \Illuminate\Http\Request $request
* @param mixed $result
* @return mixed
* @throws \Pusher\PusherException
*/
Expand Down Expand Up @@ -144,7 +143,7 @@ public function broadcast(array $channels, $event, array $payload = [])
]);

foreach ($this->formatChannels($channels) as $channel) {
$connection->publish("{$this->appId}:$channel", $payload);
$connection->publish("{$this->appId}:{$channel}", $payload);
}
}
}
2 changes: 1 addition & 1 deletion src/PubSub/Drivers/RedisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public function channelMemberCounts($appId, array $channelNames): PromiseInterfa
* @param string $payload
* @return void
*/
protected function onMessage(string $redisChannel, string $payload)
public function onMessage(string $redisChannel, string $payload)
{
$payload = json_decode($payload);

Expand Down
59 changes: 0 additions & 59 deletions src/Statistics/DnsResolver.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Statistics/Drivers/DatabaseDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ public static function create(array $data): StatisticsDriver
* Get the records to show to the dashboard.
*
* @param mixed $appId
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Http\Request|null $request
* @return array
*/
public static function get($appId, Request $request): array
public static function get($appId, ?Request $request): array
{
$class = config('websockets.statistics.database.model');

Expand Down
4 changes: 2 additions & 2 deletions src/Statistics/Drivers/StatisticsDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ public static function create(array $data): StatisticsDriver;
* Get the records to show to the dashboard.
*
* @param mixed $appId
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Http\Request|null $request
* @return void
*/
public static function get($appId, Request $request);
public static function get($appId, ?Request $request);

/**
* Delete statistics from the store,
Expand Down
Loading