-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRedisTest.php
More file actions
231 lines (179 loc) · 6.21 KB
/
Copy pathRedisTest.php
File metadata and controls
231 lines (179 loc) · 6.21 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
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
<?php
declare(strict_types=1);
namespace tests;
use PHPUnit\Framework\TestCase;
use Predis\Client as Predis;
use Rancoud\Session\Redis;
use Rancoud\Session\SessionException;
/**
* @internal
*/
class RedisTest extends TestCase
{
private static Predis $redis;
public static function setUpBeforeClass(): void
{
$params = [
'scheme' => 'tcp',
'port' => 6379,
];
$redisHost = \getenv('REDIS_HOST', true);
$params['host'] = ($redisHost !== false) ? $redisHost : '127.0.0.1';
static::$redis = new Predis($params);
static::$redis->flushdb();
}
protected function setUp(): void
{
static::$redis->flushdb();
}
public function testOpen(): void
{
$redis = new Redis();
$redis->setCurrentRedis(static::$redis);
$savePath = '';
$sessionName = '';
$success = $redis->open($savePath, $sessionName);
static::assertTrue($success);
}
public function testClose(): void
{
$redis = new Redis();
$redis->setCurrentRedis(static::$redis);
$success = $redis->close();
static::assertTrue($success);
}
public function testWrite(): void
{
$redis = new Redis();
$redis->setCurrentRedis(static::$redis);
$sessionId = 'sessionId';
$data = 'azerty';
$success = $redis->write($sessionId, $data);
static::assertTrue($success);
$dataInRedis = static::$redis->get($sessionId);
static::assertSame($data, $dataInRedis);
}
public function testRead(): void
{
$redis = new Redis();
$redis->setCurrentRedis(static::$redis);
$sessionId = 'sessionId';
$data = 'azerty';
$success = $redis->write($sessionId, $data);
static::assertTrue($success);
$dataOutput = $redis->read($sessionId);
static::assertNotEmpty($dataOutput);
static::assertIsString($dataOutput);
static::assertSame($data, $dataOutput);
$sessionId = '';
$dataOutput = $redis->read($sessionId);
static::assertEmpty($dataOutput);
static::assertIsString($dataOutput);
}
public function testDestroy(): void
{
$redis = new Redis();
$redis->setCurrentRedis(static::$redis);
$sessionId = 'todelete';
$success = $redis->destroy($sessionId);
static::assertTrue($success);
$sessionId = 'sessionId';
$data = 'azerty';
$success = $redis->write($sessionId, $data);
static::assertTrue($success);
$isKeyExist = static::$redis->exists($sessionId) === 1;
static::assertTrue($isKeyExist);
$success = $redis->destroy($sessionId);
static::assertTrue($success);
$isKeyNotExist = static::$redis->exists($sessionId) === 0;
static::assertTrue($isKeyNotExist);
}
public function testGc(): void
{
$redis = new Redis();
$redis->setCurrentRedis(static::$redis);
$redis->setLifetime(1);
$sessionId = 'sessionId';
$data = 'azerty';
$success = $redis->write($sessionId, $data);
static::assertTrue($success);
$isKeyExist = static::$redis->exists($sessionId) === 1;
static::assertTrue($isKeyExist);
\sleep(2);
$lifetime = 0;
$success = $redis->gc($lifetime);
static::assertTrue($success);
$isKeyNotExist = static::$redis->exists($sessionId) === 0;
static::assertTrue($isKeyNotExist);
}
public function testSetNewRedis(): void
{
$redis = new Redis();
$params = [
'scheme' => 'tcp',
'port' => 6379,
];
$redisHost = \getenv('REDIS_HOST', true);
$params['host'] = ($redisHost !== false) ? $redisHost : '127.0.0.1';
$redis->setNewRedis($params);
$sessionId = 'sessionId';
$data = 'azerty';
$success = $redis->write($sessionId, $data);
static::assertTrue($success);
}
public function testValidateId(): void
{
$redis = new Redis();
$redis->setCurrentRedis(static::$redis);
$baseId = 'DiqKrZDUGp5ubt3klF0oorIlFiADXC9jxig9e8leUcCYuZ9w0mXh0b1foEGIBs7SSsdOuLor58vU5liBRVPsTobnvt';
$endId1 = 'Dj8hh65DlR3tTFI1SGX3mFciDA9rMOa4LlnMr';
$endId2 = 'Dklezfoipvfk0lferijkoefzjklgrvefLlnMr';
$redis->write($baseId . $endId1, 'a');
static::assertTrue($redis->validateId($baseId . $endId1));
static::assertFalse($redis->validateId($baseId . $endId2));
static::assertFalse($redis->validateId('kjlfez/fez'));
}
public function testUpdateTimestamp(): void
{
$redis = new Redis();
$redis->setCurrentRedis(static::$redis);
$sessionId = 'sessionId';
$data = 'azerty';
$success = $redis->write($sessionId, $data);
static::assertTrue($success);
$dataInRedis = static::$redis->get($sessionId);
static::assertSame($data, $dataInRedis);
$ttl1 = static::$redis->ttl($sessionId);
\sleep(2);
$ttl2 = static::$redis->ttl($sessionId);
$success = $redis->updateTimestamp($sessionId, $data);
static::assertTrue($success);
$dataInRedis2 = static::$redis->get($sessionId);
static::assertSame($data, $dataInRedis2);
$ttl3 = static::$redis->ttl($sessionId);
static::assertTrue($ttl2 < $ttl1);
static::assertTrue($ttl3 > $ttl2);
}
/** @throws \Exception */
public function testCreateId(): void
{
$redis = new Redis();
$redis->setCurrentRedis(static::$redis);
$string = $redis->create_sid();
static::assertMatchesRegularExpression('/^[a-zA-Z0-9-]{127}+$/', $string);
}
/** @throws SessionException */
public function testLengthSessionID(): void
{
$redis = new Redis();
$redis->setLengthSessionID(50);
static::assertSame(50, $redis->getLengthSessionID());
}
public function testLengthSessionIDSessionException(): void
{
$this->expectException(SessionException::class);
$this->expectExceptionMessage('could not set length session ID below 32');
$redis = new Redis();
$redis->setLengthSessionID(1);
}
}