1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Tests\Unit; |
6
|
|
|
|
7
|
|
|
use Longman\LaravelLodash\Redis\RedisManager; |
8
|
|
|
use Redis; |
9
|
|
|
use RedisArray; |
10
|
|
|
|
11
|
|
|
use function getenv; |
12
|
|
|
use function version_compare; |
13
|
|
|
|
14
|
|
|
class RedisTest extends TestCase |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
/** @test */ |
17
|
|
|
public function it_should_set_custom_serializer() |
18
|
|
|
{ |
19
|
|
|
$redis = $this->createConnection('phpredis', [ |
20
|
|
|
'cluster' => false, |
21
|
|
|
'default' => [ |
22
|
|
|
'host' => getenv('REDIS_HOST') ?: '127.0.0.1', |
23
|
|
|
'port' => getenv('REDIS_PORT') ?: 6379, |
24
|
|
|
'database' => 5, |
25
|
|
|
'options' => ['prefix' => 'lodash:'], |
26
|
|
|
'timeout' => 0.5, |
27
|
|
|
'read_timeout' => 1.5, |
28
|
|
|
'serializer' => 'igbinary', |
29
|
|
|
], |
30
|
|
|
]); |
31
|
|
|
/** @var \Redis $client */ |
32
|
|
|
$client = $redis->connection()->client(); |
33
|
|
|
|
34
|
|
|
$data = ['name' => 'Georgia']; |
35
|
|
|
$redis->set('country', $data, null, 60); |
|
|
|
|
36
|
|
|
|
37
|
|
|
$this->assertInstanceOf(Redis::class, $client); |
38
|
|
|
$this->assertEquals($client->getOption(Redis::OPT_SERIALIZER), Redis::SERIALIZER_IGBINARY); |
39
|
|
|
$this->assertEquals($redis->get('country'), $data); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** @test */ |
43
|
|
|
public function it_should_set_custom_serializer_for_cluster() |
44
|
|
|
{ |
45
|
|
|
$redis = $this->createConnection('phpredis', [ |
46
|
|
|
'clusters' => [ |
47
|
|
|
'options' => [ |
48
|
|
|
'lazy_connect' => true, |
49
|
|
|
'connect_timeout' => 1, |
50
|
|
|
'read_timeout' => 3, |
51
|
|
|
'password' => getenv('REDIS_PASSWORD') ?: null, |
52
|
|
|
'database' => 5, |
53
|
|
|
'prefix' => 'lodash:', |
54
|
|
|
'serializer' => 'igbinary', |
55
|
|
|
], |
56
|
|
|
|
57
|
|
|
'default' => [ |
58
|
|
|
[ |
59
|
|
|
'host' => getenv('REDIS_HOST') ?: '127.0.0.1', |
60
|
|
|
'port' => getenv('REDIS_PORT') ?: 6379, |
61
|
|
|
], |
62
|
|
|
], |
63
|
|
|
], |
64
|
|
|
]); |
65
|
|
|
/** @var \RedisArray $client */ |
66
|
|
|
$client = $redis->connection()->client(); |
67
|
|
|
|
68
|
|
|
$data = ['name' => 'Georgia']; |
69
|
|
|
$redis->set('country2', $data, null, 60); |
|
|
|
|
70
|
|
|
|
71
|
|
|
$this->assertInstanceOf(RedisArray::class, $client); |
72
|
|
|
$this->assertEquals($redis->get('country2'), $data); |
|
|
|
|
73
|
|
|
foreach ($client->getOption(Redis::OPT_SERIALIZER) as $serializer) { |
74
|
|
|
$this->assertEquals($serializer, Redis::SERIALIZER_IGBINARY); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function createConnection(string $driver, array $config = []): RedisManager |
79
|
|
|
{ |
80
|
|
|
if (version_compare($this->app->version(), '5.7', '<')) { |
81
|
|
|
$redis = new RedisManager($driver, $config); |
|
|
|
|
82
|
|
|
} else { |
83
|
|
|
$redis = new RedisManager($this->app, $driver, $config); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $redis; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|