Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 8 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']];
});
}
}