-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRedisHandlerTest.php
More file actions
151 lines (137 loc) · 4.33 KB
/
Copy pathRedisHandlerTest.php
File metadata and controls
151 lines (137 loc) · 4.33 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
<?php
/*
* This file is part of Aplus Framework Session Library.
*
* (c) Natan Felles <natanfelles@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tests\Session\SaveHandlers;
use Framework\Session\SaveHandlers\RedisHandler;
use Redis;
/**
* Class RedisHandlerTest.
*
* @runTestsInSeparateProcesses
*/
class RedisHandlerTest extends AbstractHandler
{
protected string $handlerClass = RedisHandler::class;
public function setUp() : void
{
$this->replaceConfig([
'host' => \getenv('REDIS_HOST'),
]);
parent::setUp();
}
public function testFailToConnect() : void
{
$this->replaceConfig([
'host' => 'unknown',
]);
$handler = new RedisHandler($this->config, $this->logger);
self::assertFalse($handler->open('', ''));
self::assertSame(
'Session (redis): Could not connect to server unknown:6379',
$this->logger->getLastLog()->message
);
}
public function testFailToConnectWithPassword() : void
{
$this->replaceConfig([
'password' => 'foo',
]);
$handler = new RedisHandler($this->config, $this->logger);
self::assertFalse($handler->open('', ''));
self::assertSame(
'Session (redis): Authentication failed',
$this->logger->getLastLog()->message
);
}
public function testFailToConnectWithDatabase() : void
{
$this->replaceConfig([
'database' => 25,
]);
$handler = new RedisHandler($this->config, $this->logger);
self::assertFalse($handler->open('', ''));
self::assertSame(
"Session (redis): Could not select the database '25'",
$this->logger->getLastLog()->message
);
}
public function testFailToRead() : void
{
$handler = new class($this->config) extends RedisHandler {
public ?Redis $redis;
};
$handler->redis = null;
self::assertSame('', $handler->read('foo'));
}
public function testFailToWrite() : void
{
$handler = new class($this->config) extends RedisHandler {
public ?Redis $redis;
public ?string $sessionId;
protected function unlock() : bool
{
return false;
}
};
$handler->open('', '');
$handler->sessionId = null;
self::assertFalse($handler->write('foo', 'bar'));
$handler->redis = null;
self::assertFalse($handler->write('foo', 'bar'));
}
public function testFailToDestroy() : void
{
$handler = new RedisHandler($this->config);
self::assertFalse($handler->destroy('foo'));
}
public function testFailToClose() : void
{
$handler = new class($this->config) extends RedisHandler {
public ?Redis $redis;
};
$handler->open('', '');
$redis = $handler->redis;
$handler->redis = null;
self::assertTrue($handler->close());
$handler->redis = $redis;
$handler->redis->close();
self::assertTrue($handler->close());
self::assertTrue($handler->close());
}
public function testUnlock() : void
{
$handler = new class($this->config, $this->logger) extends RedisHandler {
public false | string $lockId;
public function unlock() : bool
{
return parent::unlock();
}
};
$handler->open('', '');
$handler->lockId = false;
self::assertTrue($handler->unlock());
$handler->lockId = 'foo';
self::assertFalse($handler->unlock());
self::assertSame(
'Session (redis): Error while trying to unlock foo',
$this->logger->getLastLog()->message
);
}
public function testRedisSetterAndGetter() : void
{
$handler = new RedisHandler($this->config);
$redis = new Redis();
self::assertNull($handler->getRedis());
$handler->setRedis($redis);
self::assertTrue($handler->open('', ''));
self::assertSame($redis, $handler->getRedis());
self::assertTrue($handler->close());
self::assertSame($redis, $handler->getRedis());
}
}