2020-08-14 12:35:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
namespace BeyondCode\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;
|
|
|
|
|
|
2020-09-05 19:40:52 +00:00
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
|
2020-09-05 19:40:52 +00:00
|
|
|
$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'])) {
|
|
|
|
|
$this->redis->__call($name, $args);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-05 19:40:52 +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) {
|
|
|
|
|
[$calledName, ] = $function;
|
|
|
|
|
|
|
|
|
|
if ($calledName === $name) {
|
|
|
|
|
PHPUnit::assertTrue(true);
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PHPUnit::assertFalse(true);
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if the method with args got called.
|
|
|
|
|
*
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param array $args
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
public function assertCalledWithArgs($name, array $args)
|
|
|
|
|
{
|
|
|
|
|
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.
|
|
|
|
|
*
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param array $args
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
public function assertCalledWithArgsCount($times = 1, $name, array $args)
|
|
|
|
|
{
|
|
|
|
|
$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
|
|
|
|
|
*/
|
|
|
|
|
public function assertNotCalled($name)
|
|
|
|
|
{
|
|
|
|
|
foreach ($this->getCalledFunctions() as $function) {
|
|
|
|
|
[$calledName, ] = $function;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
public function assertNotCalledWithArgs($name, array $args)
|
|
|
|
|
{
|
|
|
|
|
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.
|
|
|
|
|
*
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param array $args
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
public function assertNotCalledWithArgsCount($times = 1, $name, array $args)
|
|
|
|
|
{
|
|
|
|
|
$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
|
|
|
}
|