Skip to content

Commit aeb8205

Browse files
crynoboneStyleCIBottaylorotwell
authored
[9.x] Normalise predis command argument where it maybe an object. (#47902)
* [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 <[email protected]> * Apply fixes from StyleCI * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * formatting --------- Signed-off-by: Mior Muhammad Zaki <[email protected]> Co-authored-by: StyleCI Bot <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 8bfd22b commit aeb8205

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

src/Illuminate/Redis/Connections/Connection.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,25 @@ public function command($method, array $parameters = [])
118118
$time = round((microtime(true) - $start) * 1000, 2);
119119

120120
if (isset($this->events)) {
121-
$this->event(new CommandExecuted($method, $parameters, $time, $this));
121+
$this->event(new CommandExecuted(
122+
$method, $this->parseParametersForEvent($parameters), $time, $this
123+
));
122124
}
123125

124126
return $result;
125127
}
126128

129+
/**
130+
* Parse the command's parameters for event dispatching.
131+
*
132+
* @param array $parameters
133+
* @return array
134+
*/
135+
protected function parseParametersForEvent(array $parameters)
136+
{
137+
return $parameters;
138+
}
139+
127140
/**
128141
* Fire the given event if possible.
129142
*

src/Illuminate/Redis/Connections/PredisConnection.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Closure;
66
use Illuminate\Contracts\Redis\Connection as ConnectionContract;
7+
use Predis\Command\Argument\ArrayableArgument;
78

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

5152
unset($loop);
5253
}
54+
55+
/**
56+
* Parse the command's parameters for event dispatching.
57+
*
58+
* @param array $parameters
59+
* @return array
60+
*/
61+
protected function parseParametersForEvent(array $parameters)
62+
{
63+
return collect($parameters)
64+
->transform(function ($parameter) {
65+
return $parameter instanceof ArrayableArgument
66+
? $parameter->toArray()
67+
: $parameter;
68+
})->all();
69+
}
5370
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Redis;
4+
5+
use Illuminate\Redis\Connections\PredisConnection;
6+
use Illuminate\Redis\Events\CommandExecuted;
7+
use Illuminate\Support\Facades\Event;
8+
use Mockery as m;
9+
use Orchestra\Testbench\TestCase;
10+
use Predis\Client;
11+
use Predis\Command\Argument\Search\SearchArguments;
12+
13+
class PredisConnectionTest extends TestCase
14+
{
15+
protected function defineEnvironment($app)
16+
{
17+
$app->get('config')->set('database.redis.client', 'predis');
18+
}
19+
20+
public function testPredisCanEmitEventWithArrayableArgumentObject()
21+
{
22+
if (! class_exists(SearchArguments::class)) {
23+
return $this->markTestSkipped('Skipped tests on predis/predis dependency without '.SearchArguments::class);
24+
}
25+
26+
$event = Event::fake();
27+
28+
$command = 'ftSearch';
29+
$parameters = ['test', '*', (new SearchArguments())->dialect('3')->withScores()];
30+
31+
$predis = new PredisConnection($client = m::mock(Client::class));
32+
$predis->setEventDispatcher($event);
33+
34+
$client->shouldReceive($command)->with(...$parameters)->andReturnTrue();
35+
36+
$this->assertTrue($predis->command($command, $parameters));
37+
38+
$event->assertDispatched(function (CommandExecuted $event) use ($command) {
39+
return $event->connection instanceof PredisConnection
40+
&& $event->command === $command
41+
&& $event->parameters === ['test', '*', ['DIALECT', '3', 'WITHSCORES']];
42+
});
43+
}
44+
}

0 commit comments

Comments
 (0)