Skip to content

Commit

Permalink
[9.x] Normalise predis command argument where it maybe an object.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
crynobone committed Jul 31, 2023
1 parent 8bfd22b commit bef8480
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Illuminate/Redis/Connections/PredisConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

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

/**
* @mixin \Predis\Client
Expand All @@ -28,6 +30,35 @@ public function __construct($client)
$this->client = $client;
}

/**
* Run a command against the Redis database.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function command($method, array $parameters = [])
{
$start = microtime(true);

$result = $this->client->{$method}(...$parameters);

$time = round((microtime(true) - $start) * 1000, 2);

if (isset($this->events)) {
$parameters = collect($parameters)
->transform(function ($parameter) {
return $parameter instanceof ArrayableArgument
? $parameter->toArray()
: $parameter;
})->all();

$this->event(new CommandExecuted($method, $parameters, $time, $this));
}

return $result;
}

/**
* Subscribe to a set of given channels for messages.
*
Expand Down
40 changes: 40 additions & 0 deletions tests/Integration/Redis/PredisConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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()
{
$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, $parameters) {
return $event->connection instanceof PredisConnection
&& $event->command === $command
&& $event->parameters === ['test', '*', ['DIALECT', '3', 'WITHSCORES']];
});
}
}

0 comments on commit bef8480

Please sign in to comment.