Repository navigation
Expand file tree
/
Copy pathRedisHandlerTest.php
More file actions
130 lines (113 loc) · 4 KB
/
Copy pathRedisHandlerTest.php
File metadata and controls
130 lines (113 loc) · 4 KB
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
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\ModelCache\Handler;
use DateInterval;
use Hyperf\Context\ApplicationContext;
use Hyperf\ModelCache\Config;
use Hyperf\ModelCache\Handler\DefaultValueInterface;
use Hyperf\ModelCache\Handler\HandlerInterface;
use Hyperf\ModelCache\Handler\RedisHandler;
use Hyperf\Redis\RedisProxy;
use HyperfTest\ModelCache\Stub\ContainerStub;
use Mockery;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
#[CoversNothing]
class RedisHandlerTest extends TestCase
{
protected $handler = RedisHandler::class;
protected function tearDown(): void
{
Mockery::close();
}
public function testSetAndGet()
{
$handler = $this->mockHandler();
$key = 'test:model-cache:' . $this->handler . ':1';
$handler->set($key, ['id' => $id = uniqid()]);
$data = $handler->get($key);
$this->assertSame(['id' => $id], $data);
}
public function testSetTtl()
{
$handler = $this->mockHandler();
$key = 'test:model-cache:' . $this->handler . ':1';
$handler->set($key, ['id' => $id = uniqid()], 10);
$data = $handler->get($key);
$this->assertSame(['id' => $id], $data);
$redis = ApplicationContext::getContainer()->make(RedisProxy::class, ['pool' => 'default']);
$this->assertSame(10, $redis->ttl($key));
$handler->set($key, ['id' => $id = uniqid()], new DateInterval('PT12S'));
$this->assertSame(12, $redis->ttl($key));
$handler->set($key, ['id' => $id = uniqid()], new DateInterval('P1DT12S'));
$this->assertSame(86400 + 12, $redis->ttl($key));
}
public function testDelete()
{
$handler = $this->mockHandler();
$key = 'test:model-cache:' . $this->handler . ':1';
$handler->set($key, ['id' => $id = uniqid()]);
$this->assertTrue($handler->has($key));
$this->assertTrue($handler->delete($key));
$this->assertFalse($handler->has($key));
}
public function testGetMultiple()
{
$handler = $this->mockHandler();
$keys = [
'test:model-cache:' . $this->handler . ':1',
'test:model-cache:' . $this->handler . ':2',
];
$result = [];
foreach ($keys as $key) {
$handler->set($key, $item = ['id' => uniqid()]);
$result[] = $item;
}
$data = $handler->getMultiple($keys);
if ($handler instanceof DefaultValueInterface) {
foreach ($data as $i => $value) {
$data[$i] = $handler->clearDefaultValue($value);
}
}
$this->assertSame($result, $data);
}
public function testDefaultValue()
{
$handler = $this->mockHandler();
if (! $handler instanceof DefaultValueInterface) {
$this->markTestSkipped('Don\'t implements DefaultValueInterface');
}
$data = $handler->defaultValue(1);
$this->assertSame(['HF-DATA' => 1], $data);
$this->assertTrue($handler->isDefaultValue($data));
$this->assertFalse($handler->isDefaultValue(['HF-DATA' => 1, 'id' => 1]));
$this->assertSame(3, $handler->getPrimaryValue(['HF-DATA' => 3]));
$this->assertSame([], $handler->clearDefaultValue(['HF-DATA' => 3]));
}
protected function mockHandler(): HandlerInterface
{
$config = new Config([
'handler' => $this->handler,
'cache_key' => '{mc:%s:m:%s}:%s:%s',
'prefix' => 'default',
'pool' => 'default',
'ttl' => 3600 * 24,
'empty_model_ttl' => 3600,
'load_script' => true,
'use_default_value' => true,
], 'default');
return ContainerStub::mockContainer()->make($this->handler, ['config' => $config]);
}
}