laravel-websockets/tests/Mocks/LazyClient.php

329 lines
7.0 KiB
PHP
Raw Normal View History

2020-08-14 12:35:36 +00:00
<?php
2025-01-16 07:54:02 +00:00
namespace BlaxSoftware\LaravelWebSockets\Test\Mocks;
2020-08-14 12:35:36 +00:00
2020-09-04 06:47:23 +00:00
use Clue\React\Redis\Factory;
2020-09-04 06:47:46 +00:00
use Clue\React\Redis\LazyClient as BaseLazyClient;
2020-09-04 18:50:38 +00:00
use Illuminate\Support\Facades\Redis;
2020-08-14 12:35:36 +00:00
use PHPUnit\Framework\Assert as PHPUnit;
2020-09-04 06:47:23 +00:00
use React\EventLoop\LoopInterface;
2020-08-14 12:35:36 +00:00
class LazyClient extends BaseLazyClient
{
/**
* A list of called methods for the connector.
*
* @var array
*/
protected $calls = [];
2020-08-17 08:47:50 +00:00
/**
* A list of called events for the connector.
*
* @var array
*/
protected $events = [];
2020-09-04 06:47:23 +00:00
/**
* The Redis manager instance.
*
* @var \Illuminate\Redis\RedisManager
*/
protected $redis;
/**
* The loop.
*
* @var \React\EventLoop\LoopInterface
*/
protected $loop;
2020-09-04 06:47:23 +00:00
/**
* {@inheritdoc}
*/
public function __construct($target, Factory $factory, LoopInterface $loop)
{
parent::__construct($target, $factory, $loop);
$this->loop = $loop;
2020-09-04 18:50:38 +00:00
$this->redis = Redis::connection();
2020-09-04 06:47:23 +00:00
}
2020-08-14 12:35:36 +00:00
/**
* {@inheritdoc}
*/
public function __call($name, $args)
{
$this->calls[] = [$name, $args];
2020-09-04 06:47:23 +00:00
if (! in_array($name, ['subscribe', 'psubscribe', 'unsubscribe', 'punsubscribe', 'onMessage'])) {
2020-09-26 07:30:53 +00:00
if ($name === 'eval') {
$this->redis->{$name}(...$args);
} else {
$this->redis->__call($name, $args);
}
2020-09-04 06:47:23 +00:00
}
return new PromiseResolver(
parent::__call($name, $args), $this->loop
);
2020-08-14 12:35:36 +00:00
}
2020-08-17 08:47:50 +00:00
/**
* {@inheritdoc}
*/
public function on($event, callable $listener)
{
$this->events[] = $event;
return parent::on($event, $listener);
}
2020-08-14 12:35:36 +00:00
/**
* Check if the method got called.
*
* @param string $name
* @return $this
*/
public function assertCalled($name)
{
foreach ($this->getCalledFunctions() as $function) {
2024-02-07 17:30:54 +00:00
[$calledName] = $function;
2020-08-14 12:35:36 +00:00
if ($calledName === $name) {
PHPUnit::assertTrue(true);
return $this;
}
}
PHPUnit::assertFalse(true);
return $this;
}
2020-09-26 07:30:53 +00:00
/**
* Check if the method got called.
*
* @param int $times
* @param string $name
* @return $this
*/
public function assertCalledCount(int $times, string $name)
{
$total = collect($this->getCalledFunctions())->filter(function ($function) use ($name) {
2024-02-07 17:30:54 +00:00
[$calledName] = $function;
2020-09-26 07:30:53 +00:00
return $calledName === $name;
});
PHPUnit::assertCount($times, $total);
return $this;
}
2020-08-14 12:35:36 +00:00
/**
* Check if the method with args got called.
*
* @param string $name
* @param array $args
* @return $this
*/
2020-09-26 07:30:53 +00:00
public function assertCalledWithArgs(string $name, array $args)
2020-08-14 12:35:36 +00:00
{
foreach ($this->getCalledFunctions() as $function) {
[$calledName, $calledArgs] = $function;
if ($calledName === $name && $calledArgs === $args) {
PHPUnit::assertTrue(true);
return $this;
}
}
PHPUnit::assertFalse(true);
return $this;
}
2020-09-04 06:47:23 +00:00
/**
* Check if the method with args got called an amount of times.
*
2020-09-26 07:30:53 +00:00
* @param int $times
2020-09-04 06:47:23 +00:00
* @param string $name
* @param array $args
* @return $this
*/
2020-09-26 07:30:53 +00:00
public function assertCalledWithArgsCount(int $times, string $name, array $args)
2020-09-04 06:47:23 +00:00
{
$total = collect($this->getCalledFunctions())->filter(function ($function) use ($name, $args) {
[$calledName, $calledArgs] = $function;
return $calledName === $name && $calledArgs === $args;
});
PHPUnit::assertCount($times, $total);
return $this;
}
2020-08-17 08:47:50 +00:00
/**
* Check if the method didn't call.
*
* @param string $name
* @return $this
*/
2020-09-26 07:30:53 +00:00
public function assertNotCalled(string $name)
2020-08-17 08:47:50 +00:00
{
foreach ($this->getCalledFunctions() as $function) {
2024-02-07 17:30:54 +00:00
[$calledName] = $function;
2020-08-17 08:47:50 +00:00
if ($calledName === $name) {
PHPUnit::assertFalse(true);
return $this;
}
}
PHPUnit::assertTrue(true);
return $this;
}
/**
* Check if the method got not called with specific args.
*
* @param string $name
* @param array $args
* @return $this
*/
2020-09-26 07:30:53 +00:00
public function assertNotCalledWithArgs(string $name, array $args)
2020-08-17 08:47:50 +00:00
{
foreach ($this->getCalledFunctions() as $function) {
[$calledName, $calledArgs] = $function;
if ($calledName === $name && $calledArgs === $args) {
PHPUnit::assertFalse(true);
return $this;
}
}
PHPUnit::assertTrue(true);
return $this;
}
2020-09-04 06:47:23 +00:00
/**
* Check if the method with args got called an amount of times.
*
2020-09-26 07:30:53 +00:00
* @param int $times
2020-09-04 06:47:23 +00:00
* @param string $name
* @param array $args
* @return $this
*/
2020-09-26 07:30:53 +00:00
public function assertNotCalledWithArgsCount(int $times, string $name, array $args)
2020-09-04 06:47:23 +00:00
{
$total = collect($this->getCalledFunctions())->filter(function ($function) use ($name, $args) {
[$calledName, $calledArgs] = $function;
return $calledName === $name && $calledArgs === $args;
});
PHPUnit::assertNotCount($times, $total);
return $this;
}
2020-08-14 12:35:36 +00:00
/**
* Check if no function got called.
*
* @return $this
*/
public function assertNothingCalled()
{
PHPUnit::assertEquals([], $this->getCalledFunctions());
return $this;
}
2020-08-17 08:47:50 +00:00
/**
* Check if the event got dispatched.
*
* @param string $event
* @return $this
*/
public function assertEventDispatched($event)
{
foreach ($this->getCalledEvents() as $dispatchedEvent) {
if ($dispatchedEvent === $event) {
PHPUnit::assertTrue(true);
return $this;
}
}
PHPUnit::assertFalse(true);
return $this;
}
/**
* Check if no function got called.
*
* @return $this
*/
public function assertNothingDispatched()
{
PHPUnit::assertEquals([], $this->getCalledEvents());
return $this;
}
2020-08-14 12:35:36 +00:00
/**
* Get the list of all calls.
*
* @return array
*/
public function getCalledFunctions()
{
return $this->calls;
}
2020-08-17 08:47:50 +00:00
/**
* Get the list of events.
*
* @return array
*/
public function getCalledEvents()
{
return $this->events;
}
/**
* Dump the assertions.
*
* @return void
*/
public function dd()
{
dd([
'functions' => $this->getCalledFunctions(),
'events' => $this->getCalledEvents(),
]);
}
/**
* Reset the assertions.
*
* @return $this
*/
public function resetAssertions()
{
$this->calls = [];
$this->events = [];
return $this;
}
2020-08-14 12:35:36 +00:00
}