-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathRedisTest.php
More file actions
186 lines (166 loc) · 6.04 KB
/
RedisTest.php
File metadata and controls
186 lines (166 loc) · 6.04 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
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
<?php
declare(strict_types=1);
namespace WyriHaximus\Tests\React\Cache;
use Exception;
use Mockery;
use PHPUnit\Framework\Attributes\Test;
use WyriHaximus\AsyncTestUtilities\AsyncTestCase;
use WyriHaximus\React\Cache\Redis;
use function React\Async\await;
use function React\Promise\reject;
use function React\Promise\resolve;
final class RedisTest extends AsyncTestCase
{
#[Test]
public function get(): void
{
$client = Mockery::mock(ClientStub::class);
$prefix = 'root:';
$key = 'key';
$value = 'value';
$client->expects('exists')->with($prefix . $key)->once()->andReturn(resolve(1));
$client->expects('get')->with($prefix . $key)->once()->andReturn(resolve($value));
$promise = new Redis($client, $prefix)->get($key);
self::assertSame($value, await($promise));
}
#[Test]
public function getNonExistant(): void
{
$client = Mockery::mock(ClientStub::class);
$prefix = 'root:';
$key = 'key';
$client->expects('exists')->with($prefix . $key)->once()->andReturn(resolve(0));
$client->expects('get')->with($prefix . $key)->once()->andReturn(resolve(null));
self::assertNull(await(new Redis($client, $prefix)->get($key)));
}
#[Test]
public function set(): void
{
$client = Mockery::mock(ClientStub::class);
$prefix = 'root:';
$key = 'key';
$value = 'value';
$client->expects('set')->with($prefix . $key, $value)->once()->andReturn(resolve('OK'));
new Redis($client, $prefix)->set($key, $value);
}
#[Test]
public function setGlobalTtl(): void
{
$client = Mockery::mock(ClientStub::class);
$prefix = 'root:';
$key = 'key';
$value = 'value';
$ttl = 123;
$client->expects('psetex')->with($prefix . $key, $ttl * 1000, $value)->once()->andReturn(resolve(null));
self::assertTrue(await(new Redis($client, $prefix, $ttl)->set($key, $value)));
}
#[Test]
public function setTtl(): void
{
$client = Mockery::mock(ClientStub::class);
$prefix = 'root:';
$key = 'key';
$value = 'value';
$ttl = 123;
$client->expects('psetex')->with($prefix . $key, $ttl * 1000, $value)->once()->andReturn(resolve(null));
self::assertTrue(await(new Redis($client, $prefix)->set($key, $value, $ttl)));
}
#[Test]
public function setTtlException(): void
{
$client = Mockery::mock(ClientStub::class);
$prefix = 'root:';
$key = 'key';
$value = 'value';
$ttl = 123;
$client->expects('psetex')->with($prefix . $key, $ttl * 1000, $value)->once()->andReturn(reject(new Exception('fail!')));
self::assertFalse(await(new Redis($client, $prefix)->set($key, $value, $ttl)));
}
#[Test]
public function setException(): void
{
$client = Mockery::mock(ClientStub::class);
$prefix = 'root:';
$key = 'key';
$value = 'value';
$client->expects('set')->with($prefix . $key, $value)->once()->andReturn(reject(new Exception('fail!')));
self::assertFalse(await(new Redis($client, $prefix)->set($key, $value)));
}
#[Test]
public function delete(): void
{
$client = Mockery::mock(ClientStub::class);
$prefix = 'root:';
$key = 'key';
$client->expects('del')->with($prefix . $key)->once()->andReturn(resolve(1));
new Redis($client, $prefix)->delete($key);
}
#[Test]
public function deleteException(): void
{
$client = Mockery::mock(ClientStub::class);
$prefix = 'root:';
$key = 'key';
$client->expects('del')->with($prefix . $key)->once()->andReturn(reject(new Exception('fail!')));
new Redis($client, $prefix)->delete($key);
}
#[Test]
public function has(): void
{
$client = Mockery::mock(ClientStub::class);
$prefix = 'root:';
$key = 'key';
$client->expects('exists')->with($prefix . $key)->once()->andReturn(resolve(1));
new Redis($client, $prefix)->has($key);
}
#[Test]
public function deleteMultiple(): void
{
$client = Mockery::mock(ClientStub::class);
$prefix = 'root:';
$key = 'key';
$client->expects('del')->with($prefix . $key)->once()->andReturn(resolve(1));
new Redis($client, $prefix)->deleteMultiple([$key]);
}
#[Test]
public function deleteMultipleException(): void
{
$client = Mockery::mock(ClientStub::class);
$prefix = 'root:';
$key = 'key';
$client->expects('del')->with($prefix . $key)->once()->andReturn(reject(new Exception('fail!')));
new Redis($client, $prefix)->deleteMultiple([$key]);
}
#[Test]
public function cLear(): void
{
$client = Mockery::mock(ClientStub::class);
$prefix = 'root:';
$key = 'key';
$client->expects('keys')->with($prefix . '*')->once()->andReturn(resolve([$key]));
$client->expects('del')->with($prefix . $key)->once()->andReturn(resolve(1));
new Redis($client, $prefix)->clear();
}
#[Test]
public function setMultiple(): void
{
$client = Mockery::mock(ClientStub::class);
$prefix = 'root:';
$key = 'key';
$value = 'value';
$ttl = 123;
$client->expects('psetex')->with($prefix . $key, $ttl * 1000, $value)->once()->andReturn(resolve(true));
self::assertSame(true, await(new Redis($client, $prefix)->setMultiple([$key => $value], $ttl)));
}
#[Test]
public function getMultiple(): void
{
$client = Mockery::mock(ClientStub::class);
$prefix = 'root:';
$key = 'key';
$value = 'value';
$client->expects('exists')->with($prefix . $key)->once()->andReturn(resolve(true));
$client->expects('get')->with($prefix . $key)->once()->andReturn(resolve($value));
self::assertSame([$key => $value], await(new Redis($client, $prefix)->getMultiple([$key])));
}
}