-
Notifications
You must be signed in to change notification settings - Fork 325
/
PhpredisClientFactoryTest.php
359 lines (308 loc) · 12.5 KB
/
PhpredisClientFactoryTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
declare(strict_types=1);
namespace Snc\RedisBundle\Tests\Factory;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Redis;
use RedisCluster;
use Relay\Relay;
use Snc\RedisBundle\Factory\PhpredisClientFactory;
use Snc\RedisBundle\Logger\RedisCallInterceptor;
use Snc\RedisBundle\Logger\RedisLogger;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use function class_exists;
use function fsockopen;
use function sprintf;
class PhpredisClientFactoryTest extends TestCase
{
private MockObject $logger;
private RedisLogger $redisLogger;
protected function setUp(): void
{
if (!class_exists(Redis::class)) {
$this->markTestSkipped(sprintf('The %s requires phpredis extension.', self::class));
} elseif (!@fsockopen('127.0.0.1', 6379)) {
$this->markTestSkipped(sprintf('The %s requires a redis instance listening on 127.0.0.1:6379.', self::class));
}
$this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
$this->redisLogger = new RedisLogger($this->logger);
}
public function testCreateMinimalConfig(): void
{
$this->logger->expects($this->never())->method('debug');
$factory = new PhpredisClientFactory(new RedisCallInterceptor($this->redisLogger));
$client = $factory->create(Redis::class, ['redis://localhost:6379'], ['connection_timeout' => 5], 'default', false);
$this->assertInstanceOf(Redis::class, $client);
$this->assertNull($client->getOption(Redis::OPT_PREFIX));
$this->assertSame(0, $client->getOption(Redis::OPT_SERIALIZER));
$this->assertSame(0, $client->getDBNum());
$this->assertNull($client->getAuth());
$this->assertNull($client->getPersistentID());
}
/** @requires extension relay */
public function testCreateRelay(): void
{
$this->logger->method('debug')->withConsecutive(
[$this->stringContains('Executing command "CONNECT localhost 6379 5 <null>')],
);
$client = (new PhpredisClientFactory(new RedisCallInterceptor($this->redisLogger)))
->create(Relay::class, ['redis://localhost:6379'], ['connection_timeout' => 5], 'default', true);
$this->assertInstanceOf(Relay::class, $client);
}
public function testUnixDsnConfig(): void
{
$this->logger->expects($this->never())->method('debug');
$factory = new PhpredisClientFactory(new RedisCallInterceptor($this->redisLogger));
$client = $factory->create(Redis::class, ['redis:///tmp/redis.sock'], ['connection_timeout' => 5], 'default', false);
$this->assertInstanceOf(Redis::class, $client);
$this->assertNull($client->getOption(Redis::OPT_PREFIX));
$this->assertSame(0, $client->getOption(Redis::OPT_SERIALIZER));
$this->assertSame(0, $client->getDBNum());
$this->assertNull($client->getAuth());
$this->assertNull($client->getPersistentID());
}
public function testCreateMinimalClusterConfig(): void
{
$this->logger->expects($this->never())->method('debug');
$factory = new PhpredisClientFactory(new RedisCallInterceptor($this->redisLogger));
$client = $factory->create(
RedisCluster::class,
['redis://localhost:7000'],
['connection_timeout' => 5, 'connection_persistent' => false],
'phprediscluster',
false,
);
$this->assertInstanceOf(RedisCluster::class, $client);
$this->assertNull($client->getOption(Redis::OPT_PREFIX));
$this->assertSame(0, $client->getOption(Redis::OPT_SERIALIZER));
$this->assertSame(0., $client->getOption(Redis::OPT_READ_TIMEOUT));
$this->assertSame(0, $client->getOption(Redis::OPT_SCAN));
$this->assertSame(0, $client->getOption(RedisCluster::OPT_SLAVE_FAILOVER));
}
/**
* @requires extension relay
* @testWith ["RedisSentinel", "Redis"]
* ["Relay\\Sentinel", "Relay\\Relay"]
*/
public function testCreateSentinelConfig(string $sentinelClass, string $outputClass): void
{
$this->logger->method('debug')->withConsecutive(
[$this->stringContains('Executing command "CONNECT 127.0.0.1 6379 5 <null>')],
['Executing command "AUTH sncredis"'],
);
$factory = new PhpredisClientFactory(new RedisCallInterceptor($this->redisLogger));
$client = $factory->create(
$sentinelClass,
[
'redis://undefined@localhost:55555', // unreachable instance
'redis://sncredis@localhost:26379',
],
[
'connection_timeout' => 5,
'connection_persistent' => false,
'service' => 'mymaster',
],
'phpredissentinel',
true,
);
$this->assertInstanceOf($outputClass, $client);
$this->assertNull($client->getOption(Redis::OPT_PREFIX));
$this->assertSame(0, $client->getOption(Redis::OPT_SERIALIZER));
$this->assertSame(5., $client->getTimeout());
$this->assertSame('sncredis', $client->getAuth());
}
public function testCreateFullConfig(): void
{
$factory = new PhpredisClientFactory(new RedisCallInterceptor($this->redisLogger));
$client = $factory->create(
Redis::class,
['redis://localhost:6379'],
[
'connection_timeout' => 10,
'connection_persistent' => 'x',
'prefix' => 'toto',
'serialization' => 'php',
'read_write_timeout' => 4,
'parameters' => [
'database' => 3,
'password' => 'sncredis',
],
],
'alias_test',
false,
);
$this->assertInstanceOf(Redis::class, $client);
$this->assertSame('toto', $client->getOption(Redis::OPT_PREFIX));
$this->assertSame(1, $client->getOption(Redis::OPT_SERIALIZER));
$this->assertSame(4., $client->getOption(Redis::OPT_READ_TIMEOUT));
$this->assertSame(3, $client->getDBNum());
$this->assertSame('sncredis', $client->getAuth());
$this->assertNotNull($client->getPersistentID());
$this->assertNotFalse($client->getPersistentID());
}
public function testDsnConfig(): void
{
$this->logger->method('debug')->withConsecutive(
[$this->stringContains('Executing command "CONNECT localhost 6379 5')],
['Executing command "AUTH sncredis"'],
['Executing command "SELECT 2"'],
);
$factory = new PhpredisClientFactory(new RedisCallInterceptor($this->redisLogger));
$client = $factory->create(
Redis::class,
['redis://sncredis@localhost:6379/2'],
[
'parameters' => [
'database' => 3,
'password' => 'secret',
],
'connection_timeout' => 5,
],
'alias_test',
true,
);
$this->assertInstanceOf(Redis::class, $client);
$this->assertSame(2, $client->getDBNum());
$this->assertSame('sncredis', $client->getAuth());
$this->assertNull($client->getPersistentID());
}
public function testDsnConfigWithUsername(): void
{
$this->logger->method('debug')->withConsecutive(
[$this->stringContains('Executing command "CONNECT localhost 8000 5')],
['Executing command "AUTH snc_redis snc_password"'],
['Executing command "SELECT 0"'],
);
$factory = new PhpredisClientFactory(new RedisCallInterceptor($this->redisLogger));
$client = $factory->create(
Redis::class,
['redis://snc_redis:snc_password@localhost:8000/0'],
[
'parameters' => [
'database' => 3,
'password' => 'secret',
],
'connection_timeout' => 5,
],
'alias_test',
true,
);
$this->assertInstanceOf(Redis::class, $client);
$this->assertSame(0, $client->getDBNum());
$this->assertSame(['snc_redis', 'snc_password'], $client->getAuth());
$this->assertNull($client->getPersistentID());
}
public function testNestedDsnConfig(): void
{
$factory = new PhpredisClientFactory(new RedisCallInterceptor($this->redisLogger));
$client = $factory->create(
Redis::class,
[['redis://sncredis@localhost:6379/2']],
[
'parameters' => [
'database' => 3,
'password' => 'secret',
],
'connection_timeout' => 5,
],
'alias_test',
false,
);
$this->assertInstanceOf(Redis::class, $client);
$this->assertSame(2, $client->getDBNum());
$this->assertSame('sncredis', $client->getAuth());
$this->assertNull($client->getPersistentID());
}
/** @dataProvider serializationTypes */
public function testLoadSerializationType(string $serializationType, int $serializer): void
{
$factory = new PhpredisClientFactory(new RedisCallInterceptor($this->redisLogger));
$client = $factory->create(
Redis::class,
['redis://localhost:6379'],
[
'serialization' => $serializationType,
'connection_timeout' => 5,
],
'default',
false,
);
self::assertSame($serializer, $client->getOption(Redis::OPT_SERIALIZER));
}
public function testLoadSerializationTypeFail(): void
{
$factory = new PhpredisClientFactory(new RedisCallInterceptor($this->redisLogger));
$this->expectException(InvalidConfigurationException::class);
$factory->create(
Redis::class,
['redis://localhost:6379'],
[
'serialization' => 'unknown',
'connection_timeout' => 5,
],
'default',
false,
);
}
/** @return list<array{0: string, 1: Redis::SERIALIZER_*}> */
public function serializationTypes(): array
{
return [
['default', Redis::SERIALIZER_NONE],
['none', Redis::SERIALIZER_NONE],
['php', Redis::SERIALIZER_PHP],
['json', Redis::SERIALIZER_JSON],
];
}
public function testMethodsWithVariadicParameters(): void
{
$this->logger->method('debug')->withConsecutive(
[$this->stringContains('Executing command "CONNECT localhost 6379 5')],
['Executing command "AUTH sncredis"'],
['Executing command "SELECT 2"'],
['Executing command "RAWCOMMAND scan fleet cursor 0 limit 10"'],
['Executing command "HDEL foo bar"'],
['Executing command "UNLINK bar baz"'],
);
$factory = new PhpredisClientFactory(new RedisCallInterceptor($this->redisLogger));
$client = $factory->create(
Redis::class,
['redis://sncredis@localhost:6379/2'],
[
'parameters' => [
'database' => 3,
'password' => 'secret',
],
'connection_timeout' => 5,
],
'alias_test',
true,
);
/** @psalm-suppress TooManyArguments */
$this->assertFalse($client->rawCommand('scan', 'fleet', 'cursor', '0', 'limit', '10'));
$client->hDel('foo', 'bar');
$client->unlink('bar', 'baz');
}
public function testMethodWithPassByRefArgument(): void
{
$this->logger->method('debug')->withConsecutive(
[$this->stringContains('Executing command "CONNECT localhost 6379 5')],
['Executing command "AUTH sncredis"'],
['Executing command "SELECT 2"'],
['Executing command "SSCAN set 1 <null> 0"'],
);
$factory = new PhpredisClientFactory(new RedisCallInterceptor($this->redisLogger));
$client = $factory->create(
Redis::class,
['redis://sncredis@localhost:6379/2'],
['connection_timeout' => 5],
'alias_test',
true,
);
$iterator = 1;
/** @psalm-suppress TooManyArguments */
$client->sscan('set', $iterator, null, 0);
$this->assertSame(0, $iterator);
}
}