Remove duplicate client mock client, simplify test trait
This commit is contained in:
parent
ef86f86668
commit
e259cac51e
|
|
@ -1,126 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace BeyondCode\LaravelWebSockets\Tests\Mocks;
|
||||
|
||||
use stdClass;
|
||||
use React\EventLoop\LoopInterface;
|
||||
use React\Promise\FulfilledPromise;
|
||||
use React\Promise\PromiseInterface;
|
||||
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface;
|
||||
|
||||
class FakeReplicationClient implements ReplicationInterface
|
||||
{
|
||||
protected $channels = [];
|
||||
|
||||
/**
|
||||
* Boot the pub/sub provider (open connections, initial subscriptions, etc).
|
||||
*
|
||||
* @param LoopInterface $loop
|
||||
* @return self
|
||||
*/
|
||||
public function boot(LoopInterface $loop) : ReplicationInterface
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Publish a payload on a specific channel, for a specific app.
|
||||
*
|
||||
* @param string $appId
|
||||
* @param string $channel
|
||||
* @param stdClass $payload
|
||||
* @return bool
|
||||
*/
|
||||
public function publish(string $appId, string $channel, stdClass $payload) : bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe to receive messages for a channel.
|
||||
*
|
||||
* @param string $appId
|
||||
* @param string $channel
|
||||
* @return bool
|
||||
*/
|
||||
public function subscribe(string $appId, string $channel) : bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsubscribe from a channel.
|
||||
*
|
||||
* @param string $appId
|
||||
* @param string $channel
|
||||
* @return bool
|
||||
*/
|
||||
public function unsubscribe(string $appId, string $channel) : bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a member to a channel. To be called when they have
|
||||
* subscribed to the channel.
|
||||
*
|
||||
* @param string $appId
|
||||
* @param string $channel
|
||||
* @param string $socketId
|
||||
* @param string $data
|
||||
*/
|
||||
public function joinChannel(string $appId, string $channel, string $socketId, string $data)
|
||||
{
|
||||
$this->channels["$appId:$channel"][$socketId] = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a member from the channel. To be called when they have
|
||||
* unsubscribed from the channel.
|
||||
*
|
||||
* @param string $appId
|
||||
* @param string $channel
|
||||
* @param string $socketId
|
||||
*/
|
||||
public function leaveChannel(string $appId, string $channel, string $socketId)
|
||||
{
|
||||
unset($this->channels["$appId:$channel"][$socketId]);
|
||||
if (empty($this->channels["$appId:$channel"])) {
|
||||
unset($this->channels["$appId:$channel"]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the full information about the members in a presence channel.
|
||||
*
|
||||
* @param string $appId
|
||||
* @param string $channel
|
||||
* @return PromiseInterface
|
||||
*/
|
||||
public function channelMembers(string $appId, string $channel) : PromiseInterface
|
||||
{
|
||||
$data = array_map(function ($user) {
|
||||
return json_decode($user);
|
||||
}, $this->channels["$appId:$channel"]);
|
||||
|
||||
return new FulfilledPromise($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the amount of users subscribed for each presence channel.
|
||||
*
|
||||
* @param string $appId
|
||||
* @param array $channelNames
|
||||
* @return PromiseInterface
|
||||
*/
|
||||
public function channelMemberCounts(string $appId, array $channelNames) : PromiseInterface
|
||||
{
|
||||
$data = [];
|
||||
|
||||
foreach ($channelNames as $channel) {
|
||||
$data[$channel] = count($this->channels["$appId:$channel"]);
|
||||
}
|
||||
|
||||
return new FulfilledPromise($data);
|
||||
}
|
||||
}
|
||||
|
|
@ -2,17 +2,16 @@
|
|||
|
||||
namespace BeyondCode\LaravelWebSockets\Tests;
|
||||
|
||||
use React\EventLoop\Factory;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use BeyondCode\LaravelWebSockets\PubSub\Drivers\LocalClient;
|
||||
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface;
|
||||
use BeyondCode\LaravelWebSockets\Tests\Mocks\FakeReplicationClient;
|
||||
|
||||
trait TestsReplication
|
||||
{
|
||||
public function setupReplication()
|
||||
{
|
||||
app()->singleton(ReplicationInterface::class, function () {
|
||||
return (new FakeReplicationClient())->boot(Factory::create());
|
||||
return new LocalClient();
|
||||
});
|
||||
|
||||
Config::set([
|
||||
|
|
|
|||
Loading…
Reference in New Issue