Skip to content

Commit c95820f

Browse files
committed
test: Add unit tests for Session FileHandler
1 parent f3829fe commit c95820f

3 files changed

Lines changed: 741 additions & 0 deletions

File tree

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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 Tests\Support\Session;
15+
16+
/**
17+
* A stream wrapper that can be configured to fail on lock, read, or write.
18+
*
19+
* Used by {@see \CodeIgniter\Session\Handlers\FileHandlerTest} to exercise the
20+
* failure branches of {@see \CodeIgniter\Session\Handlers\FileHandler::read()}
21+
* and {@see \CodeIgniter\Session\Handlers\FileHandler::write()}.
22+
*
23+
* @internal
24+
*/
25+
final class FaultyStream
26+
{
27+
public $context;
28+
private static bool $failLock = false;
29+
private static bool $failRead = false;
30+
private static bool $failWrite = false;
31+
private $stream;
32+
33+
public static function failLock(bool $fail): void
34+
{
35+
self::$failLock = $fail;
36+
}
37+
38+
public static function failRead(bool $fail): void
39+
{
40+
self::$failRead = $fail;
41+
}
42+
43+
public static function failWrite(bool $fail): void
44+
{
45+
self::$failWrite = $fail;
46+
}
47+
48+
public static function reset(): void
49+
{
50+
self::$failLock = false;
51+
self::$failRead = false;
52+
self::$failWrite = false;
53+
}
54+
55+
public function stream_open(string $path, string $mode, int $options, ?string &$openedPath): bool
56+
{
57+
$this->stream = fopen('php://temp', 'c+b');
58+
59+
return $this->stream !== false;
60+
}
61+
62+
public function stream_read(int $count): false|string
63+
{
64+
return self::$failRead ? false : fread($this->stream, $count);
65+
}
66+
67+
public function stream_write(string $data): false|int
68+
{
69+
return self::$failWrite ? false : fwrite($this->stream, $data);
70+
}
71+
72+
public function stream_lock(int $operation): bool
73+
{
74+
return self::$failLock ? false : true;
75+
}
76+
77+
public function stream_close(): void
78+
{
79+
fclose($this->stream);
80+
}
81+
82+
public function stream_eof(): bool
83+
{
84+
return feof($this->stream);
85+
}
86+
87+
public function stream_tell(): int
88+
{
89+
return ftell($this->stream);
90+
}
91+
92+
public function stream_seek(int $offset, int $whence): bool
93+
{
94+
return fseek($this->stream, $offset, $whence) === 0;
95+
}
96+
97+
public function stream_truncate(int $newSize): bool
98+
{
99+
return ftruncate($this->stream, $newSize);
100+
}
101+
102+
public function stream_stat(): array
103+
{
104+
return fstat($this->stream);
105+
}
106+
107+
/**
108+
* Reports a regular file so that `is_file()` and `filesize()` behave like
109+
* they do for a real session file.
110+
*
111+
* @return array<string, int>
112+
*/
113+
public function url_stat(string $path, int $flags): array
114+
{
115+
return [
116+
'dev' => 0,
117+
'ino' => 0,
118+
'mode' => 0o100600,
119+
'nlink' => 1,
120+
'uid' => 0,
121+
'gid' => 0,
122+
'rdev' => 0,
123+
'size' => 5,
124+
'atime' => time(),
125+
'mtime' => time(),
126+
'ctime' => time(),
127+
'blksize' => -1,
128+
'blocks' => -1,
129+
];
130+
}
131+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 Tests\Support\Session;
15+
16+
use CodeIgniter\Session\Handlers\FileHandler;
17+
18+
/**
19+
* A {@see FileHandler} whose `close()` always reports failure, so that the
20+
* alternative branch of {@see FileHandler::destroy()} can be exercised.
21+
*
22+
* @internal
23+
*/
24+
final class FileHandlerCloseFail extends FileHandler
25+
{
26+
public function close(): bool
27+
{
28+
return false;
29+
}
30+
}

0 commit comments

Comments
 (0)