Skip to content

Commit

Permalink
[9.x] Normalise predis command argument where it maybe an object. (#4…
Browse files Browse the repository at this point in the history
…7902)

* [9.x] Normalise predis command argument where it maybe an object.

Starting from predis 2.1.1 it is possible to apply an implementation of
`Predis\Command\Argument\ArrayableArgument`

fixes laravel/telescope#1366

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* Apply fixes from StyleCI

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* formatting

---------

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Co-authored-by: StyleCI Bot <bot@styleci.io>
Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
3 people committed Jul 31, 2023
1 parent 8bfd22b commit aeb8205
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/Illuminate/Redis/Connections/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,25 @@ public function command($method, array $parameters = [])
$time = round((microtime(true) - $start) * 1000, 2);

if (isset($this->events)) {
$this->event(new CommandExecuted($method, $parameters, $time, $this));
$this->event(new CommandExecuted(
$method, $this->parseParametersForEvent($parameters), $time, $this
));
}

return $result;
}

/**
* Parse the command's parameters for event dispatching.
*
* @param array $parameters
* @return array
*/
protected function parseParametersForEvent(array $parameters)
{
return $parameters;
}

/**
* Fire the given event if possible.
*
Expand Down
17 changes: 17 additions & 0 deletions src/Illuminate/Redis/Connections/PredisConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Illuminate\Contracts\Redis\Connection as ConnectionContract;
use Predis\Command\Argument\ArrayableArgument;

/**
* @mixin \Predis\Client
Expand Down Expand Up @@ -50,4 +51,20 @@ public function createSubscription($channels, Closure $callback, $method = 'subs

unset($loop);
}

/**
* Parse the command's parameters for event dispatching.
*
* @param array $parameters
* @return array
*/
protected function parseParametersForEvent(array $parameters)
{
return collect($parameters)
->transform(function ($parameter) {
return $parameter instanceof ArrayableArgument
? $parameter->toArray()
: $parameter;
})->all();
}
}
44 changes: 44 additions & 0 deletions tests/Integration/Redis/PredisConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Illuminate\Tests\Integration\Redis;

use Illuminate\Redis\Connections\PredisConnection;
use Illuminate\Redis\Events\CommandExecuted;
use Illuminate\Support\Facades\Event;
use Mockery as m;
use Orchestra\Testbench\TestCase;
use Predis\Client;
use Predis\Command\Argument\Search\SearchArguments;

class PredisConnectionTest extends TestCase
{
protected function defineEnvironment($app)
{
$app->get('config')->set('database.redis.client', 'predis');
}

public function testPredisCanEmitEventWithArrayableArgumentObject()
{
if (! class_exists(SearchArguments::class)) {
return $this->markTestSkipped('Skipped tests on predis/predis dependency without '.SearchArguments::class);
}

$event = Event::fake();

$command = 'ftSearch';
$parameters = ['test', '*', (new SearchArguments())->dialect('3')->withScores()];

$predis = new PredisConnection($client = m::mock(Client::class));
$predis->setEventDispatcher($event);

$client->shouldReceive($command)->with(...$parameters)->andReturnTrue();

$this->assertTrue($predis->command($command, $parameters));

$event->assertDispatched(function (CommandExecuted $event) use ($command) {
return $event->connection instanceof PredisConnection
&& $event->command === $command
&& $event->parameters === ['test', '*', ['DIALECT', '3', 'WITHSCORES']];
});
}
}

0 comments on commit aeb8205

Please sign in to comment.