forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMemcachedHandlerTest.php
More file actions
131 lines (103 loc) · 3.85 KB
/
MemcachedHandlerTest.php
File metadata and controls
131 lines (103 loc) · 3.85 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
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Session\Handlers;
use CodeIgniter\Session\Exceptions\SessionException;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\TestLogger;
use Config\Logger as LoggerConfig;
use Config\Session as SessionConfig;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
/**
* @internal
*/
#[Group('DatabaseLive')]
#[RequiresPhpExtension('memcached')]
final class MemcachedHandlerTest extends CIUnitTestCase
{
private string $sessionDriver = MemcachedHandler::class;
private string $sessionName = 'ci_session';
private string $sessionSavePath = '127.0.0.1:11211';
private string $userIpAddress = '127.0.0.1';
/**
* @param array<string, bool|int|string|null> $options Replace values for `Config\Session`.
*/
protected function getInstance($options = []): MemcachedHandler
{
$defaults = [
'driver' => $this->sessionDriver,
'cookieName' => $this->sessionName,
'expiration' => 7200,
'savePath' => $this->sessionSavePath,
'matchIP' => false,
'timeToUpdate' => 300,
'regenerateDestroy' => false,
];
$sessionConfig = new SessionConfig();
$config = array_merge($defaults, $options);
foreach ($config as $key => $value) {
$sessionConfig->{$key} = $value;
}
$handler = new MemcachedHandler($sessionConfig, $this->userIpAddress);
$handler->setLogger(new TestLogger(new LoggerConfig()));
return $handler;
}
protected function tearDown(): void
{
parent::tearDown();
MemcachedHandler::resetPersistentConnections();
}
public function testConstructorThrowsWithEmptySavePath(): void
{
$this->expectException(SessionException::class);
$this->getInstance(['savePath' => '']);
}
public function testConstructorDoesNotThrowWithValidSavePath(): void
{
$handler = $this->getInstance(['savePath' => '127.0.0.1:11211']);
$this->assertInstanceOf(MemcachedHandler::class, $handler);
}
public function testOpen(): void
{
$handler = $this->getInstance();
$this->assertTrue($handler->open($this->sessionSavePath, $this->sessionName));
}
public function testWriteAndReadBack(): void
{
$handler = $this->getInstance();
$handler->open($this->sessionSavePath, $this->sessionName);
$sessionId = '555556b43phsnnf8if6bo33b635e4447';
// Initial read to acquire lock and set session ID
$this->assertSame('', $handler->read($sessionId));
$data = <<<'DATA'
__ci_last_regenerate|i:1664607454;_ci_previous_url|s:32:"http://localhost:8080/index.php/";key|s:5:"value";
DATA;
$this->assertTrue($handler->write($sessionId, $data));
$handler->close();
// Read back in a new handler to verify persistence
$handler2 = $this->getInstance();
$handler2->open($this->sessionSavePath, $this->sessionName);
$this->assertSame($data, $handler2->read($sessionId));
$handler2->close();
}
public function testReadEmptySession(): void
{
$handler = $this->getInstance();
$handler->open($this->sessionSavePath, $this->sessionName);
$this->assertSame('', $handler->read('123456b43phsnnf8if6bo33b635e4321'));
$handler->close();
}
public function testGC(): void
{
$handler = $this->getInstance();
$this->assertSame(1, $handler->gc(3600));
}
}