Skip to content

Commit c9e2042

Browse files
committed
test: add RedisHandlerTest
1 parent 2a0ae4a commit c9e2042

1 file changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Session\Handlers\Database;
13+
14+
use CodeIgniter\Session\Handlers\RedisHandler;
15+
use CodeIgniter\Test\CIUnitTestCase;
16+
use Config\App as AppConfig;
17+
use Redis;
18+
19+
/**
20+
* @internal
21+
*/
22+
final class RedisHandlerTest extends CIUnitTestCase
23+
{
24+
private string $sessionName = 'ci_session';
25+
private string $sessionSavePath = 'tcp://127.0.0.1:6379';
26+
private string $userIpAddress = '127.0.0.1';
27+
28+
protected function setUp(): void
29+
{
30+
parent::setUp();
31+
32+
if (! class_exists(Redis::class)) {
33+
$this->markTestSkipped(
34+
'Redis Session Handler requires PHP Redis extention and Redis server'
35+
);
36+
}
37+
}
38+
39+
private function getInstance($options = [])
40+
{
41+
$defaults = [
42+
'sessionDriver' => RedisHandler::class,
43+
'sessionCookieName' => $this->sessionName,
44+
'sessionExpiration' => 7200,
45+
'sessionSavePath' => $this->sessionSavePath,
46+
'sessionMatchIP' => false,
47+
'sessionTimeToUpdate' => 300,
48+
'sessionRegenerateDestroy' => false,
49+
'cookieDomain' => '',
50+
'cookiePrefix' => '',
51+
'cookiePath' => '/',
52+
'cookieSecure' => false,
53+
'cookieSameSite' => 'Lax',
54+
];
55+
56+
$config = array_merge($defaults, $options);
57+
$appConfig = new AppConfig();
58+
59+
foreach ($config as $key => $c) {
60+
$appConfig->{$key} = $c;
61+
}
62+
63+
return new RedisHandler($appConfig, $this->userIpAddress);
64+
}
65+
66+
public function testOpen()
67+
{
68+
$handler = $this->getInstance();
69+
$this->assertTrue($handler->open($this->sessionSavePath, $this->sessionName));
70+
}
71+
72+
public function testWrite()
73+
{
74+
$handler = $this->getInstance();
75+
$handler->open($this->sessionSavePath, $this->sessionName);
76+
$handler->read('555556b43phsnnf8if6bo33b635e4447');
77+
78+
$data = <<<'DATA'
79+
__ci_last_regenerate|i:1664607454;_ci_previous_url|s:32:"http://localhost:8080/index.php/";key|s:5:"value";
80+
DATA;
81+
$this->assertTrue($handler->write('555556b43phsnnf8if6bo33b635e4447', $data));
82+
83+
$handler->close();
84+
}
85+
86+
public function testReadSuccess()
87+
{
88+
$handler = $this->getInstance();
89+
$handler->open($this->sessionSavePath, $this->sessionName);
90+
91+
$expected = <<<'DATA'
92+
__ci_last_regenerate|i:1664607454;_ci_previous_url|s:32:"http://localhost:8080/index.php/";key|s:5:"value";
93+
DATA;
94+
$this->assertSame($expected, $handler->read('555556b43phsnnf8if6bo33b635e4447'));
95+
96+
$handler->close();
97+
}
98+
99+
public function testReadFailure()
100+
{
101+
$handler = $this->getInstance();
102+
$handler->open($this->sessionSavePath, $this->sessionName);
103+
104+
$this->assertSame('', $handler->read('123456b43phsnnf8if6bo33b635e4321'));
105+
106+
$handler->close();
107+
}
108+
109+
public function testGC()
110+
{
111+
$handler = $this->getInstance();
112+
$this->assertSame(1, $handler->gc(3600));
113+
}
114+
}

0 commit comments

Comments
 (0)