Skip to content

Commit 28cf9e1

Browse files
committed
test: add unit tests for Session BaseHandler (100% coverage)
1 parent 0d1d224 commit 28cf9e1

1 file changed

Lines changed: 236 additions & 0 deletions

File tree

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Session\Handlers;
15+
16+
use CodeIgniter\Test\CIUnitTestCase;
17+
use CodeIgniter\Test\TestLogger;
18+
use Config\Cookie as CookieConfig;
19+
use Config\Logger as LoggerConfig;
20+
use Config\Session as SessionConfig;
21+
22+
/**
23+
* @internal
24+
*/
25+
final class BaseHandlerTest extends CIUnitTestCase
26+
{
27+
private string $sessionName = 'ci_session';
28+
private string $sessionSavePath = '/tmp';
29+
private string $userIpAddress = '127.0.0.1';
30+
31+
/**
32+
* @param array<string, bool|int|string|null> $options
33+
*/
34+
private function createHandler(array $options = []): ArrayHandler
35+
{
36+
$defaults = [
37+
'driver' => ArrayHandler::class,
38+
'cookieName' => $this->sessionName,
39+
'expiration' => 7200,
40+
'savePath' => $this->sessionSavePath,
41+
'matchIP' => false,
42+
'timeToUpdate' => 300,
43+
'regenerateDestroy' => false,
44+
];
45+
$sessionConfig = new SessionConfig();
46+
$config = array_merge($defaults, $options);
47+
48+
foreach ($config as $key => $value) {
49+
$sessionConfig->{$key} = $value;
50+
}
51+
52+
$handler = new ArrayHandler($sessionConfig, $this->userIpAddress);
53+
$handler->setLogger(new TestLogger(new LoggerConfig()));
54+
55+
return $handler;
56+
}
57+
58+
// ========== Constructor ==========
59+
60+
public function testConstructorSetsCookieName(): void
61+
{
62+
$handler = $this->createHandler(['cookieName' => 'my_cookie']);
63+
64+
$this->assertSame('my_cookie', $this->getPrivateProperty($handler, 'cookieName'));
65+
}
66+
67+
public function testConstructorSetsMatchIP(): void
68+
{
69+
$handler = $this->createHandler(['matchIP' => true]);
70+
71+
$this->assertTrue($this->getPrivateProperty($handler, 'matchIP'));
72+
}
73+
74+
public function testConstructorSetsMatchIPDefaultFalse(): void
75+
{
76+
$handler = $this->createHandler();
77+
78+
$this->assertFalse($this->getPrivateProperty($handler, 'matchIP'));
79+
}
80+
81+
public function testConstructorSetsSavePath(): void
82+
{
83+
$handler = $this->createHandler(['savePath' => '/custom/path']);
84+
85+
$this->assertSame('/custom/path', $this->getPrivateProperty($handler, 'savePath'));
86+
}
87+
88+
public function testConstructorSetsIpAddress(): void
89+
{
90+
$handler = $this->createHandler();
91+
92+
$this->assertSame('127.0.0.1', $this->getPrivateProperty($handler, 'ipAddress'));
93+
}
94+
95+
public function testConstructorSetsCookieDomainFromConfig(): void
96+
{
97+
$config = new CookieConfig();
98+
$handler = $this->createHandler();
99+
100+
$this->assertSame($config->domain, $this->getPrivateProperty($handler, 'cookieDomain'));
101+
}
102+
103+
public function testConstructorSetsCookiePathFromConfig(): void
104+
{
105+
$config = new CookieConfig();
106+
$handler = $this->createHandler();
107+
108+
$this->assertSame($config->path, $this->getPrivateProperty($handler, 'cookiePath'));
109+
}
110+
111+
public function testConstructorSetsCookieSecureFromConfig(): void
112+
{
113+
$config = new CookieConfig();
114+
$handler = $this->createHandler();
115+
116+
$this->assertSame($config->secure, $this->getPrivateProperty($handler, 'cookieSecure'));
117+
}
118+
119+
public function testConstructorSetsCookiePrefixEmpty(): void
120+
{
121+
$handler = $this->createHandler();
122+
123+
$this->assertSame('', $this->getPrivateProperty($handler, 'cookiePrefix'));
124+
}
125+
126+
// ========== lockSession ==========
127+
128+
public function testLockSessionSetsLockTrue(): void
129+
{
130+
$handler = $this->createHandler();
131+
$this->setPrivateProperty($handler, 'lock', false);
132+
133+
$lockSession = $this->getPrivateMethodInvoker($handler, 'lockSession');
134+
$result = $lockSession('test-session-id');
135+
136+
$this->assertTrue($result);
137+
$this->assertTrue($this->getPrivateProperty($handler, 'lock'));
138+
}
139+
140+
public function testLockSessionCalledMultipleTimes(): void
141+
{
142+
$handler = $this->createHandler();
143+
144+
$lockSession = $this->getPrivateMethodInvoker($handler, 'lockSession');
145+
146+
$this->assertTrue($lockSession('id-1'));
147+
$this->assertTrue($this->getPrivateProperty($handler, 'lock'));
148+
$this->assertTrue($lockSession('id-2'));
149+
$this->assertTrue($this->getPrivateProperty($handler, 'lock'));
150+
}
151+
152+
// ========== releaseLock ==========
153+
154+
public function testReleaseLockSetsLockFalse(): void
155+
{
156+
$handler = $this->createHandler();
157+
$this->setPrivateProperty($handler, 'lock', true);
158+
159+
$releaseLock = $this->getPrivateMethodInvoker($handler, 'releaseLock');
160+
$result = $releaseLock();
161+
162+
$this->assertTrue($result);
163+
$this->assertFalse($this->getPrivateProperty($handler, 'lock'));
164+
}
165+
166+
public function testReleaseLockWhenAlreadyUnlocked(): void
167+
{
168+
$handler = $this->createHandler();
169+
$this->setPrivateProperty($handler, 'lock', false);
170+
171+
$releaseLock = $this->getPrivateMethodInvoker($handler, 'releaseLock');
172+
$result = $releaseLock();
173+
174+
$this->assertTrue($result);
175+
$this->assertFalse($this->getPrivateProperty($handler, 'lock'));
176+
}
177+
178+
// ========== fail ==========
179+
180+
public function testFailReturnsFalse(): void
181+
{
182+
$handler = $this->createHandler();
183+
$fail = $this->getPrivateMethodInvoker($handler, 'fail');
184+
185+
$this->assertFalse($fail());
186+
}
187+
188+
public function testFailSetsIniSavePath(): void
189+
{
190+
$handler = $this->createHandler(['savePath' => '/test/path']);
191+
$fail = $this->getPrivateMethodInvoker($handler, 'fail');
192+
193+
$fail();
194+
195+
$this->assertSame('/test/path', ini_get('session.save_path'));
196+
}
197+
198+
// ========== destroyCookie ==========
199+
200+
public function testDestroyCookieReturnsBool(): void
201+
{
202+
$handler = $this->createHandler(['cookieName' => 'test_cookie']);
203+
$destroyCookie = $this->getPrivateMethodInvoker($handler, 'destroyCookie');
204+
205+
$this->assertIsBool($destroyCookie());
206+
}
207+
208+
public function testDestroyCookieUsesConfiguredName(): void
209+
{
210+
$handler = $this->createHandler(['cookieName' => 'custom_session_name']);
211+
$destroyCookie = $this->getPrivateMethodInvoker($handler, 'destroyCookie');
212+
213+
$destroyCookie();
214+
215+
$this->assertHeaderEmitted('Set-Cookie: custom_session_name=');
216+
}
217+
218+
public function testDestroyCookieSetsEmptyCookie(): void
219+
{
220+
$handler = $this->createHandler(['cookieName' => 'empty_test']);
221+
$destroyCookie = $this->getPrivateMethodInvoker($handler, 'destroyCookie');
222+
223+
$destroyCookie();
224+
225+
$this->assertHeaderEmitted('Set-Cookie: empty_test=');
226+
}
227+
228+
// ========== Logger (LoggerAwareTrait) ==========
229+
230+
public function testLoggerIsAccessible(): void
231+
{
232+
$handler = $this->createHandler();
233+
234+
$this->assertInstanceOf(TestLogger::class, $this->getPrivateProperty($handler, 'logger'));
235+
}
236+
}

0 commit comments

Comments
 (0)