Skip to content

Commit db4e2d9

Browse files
committed
test: apply code review suggestions for Session FileHandler tests
1 parent 11f757a commit db4e2d9

2 files changed

Lines changed: 48 additions & 7 deletions

File tree

tests/_support/Session/FaultyStream.php

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,19 @@
2424
*/
2525
final class FaultyStream
2626
{
27-
public $context;
27+
/**
28+
* @var resource|null
29+
*/
30+
public mixed $context = null;
31+
2832
private static bool $failLock = false;
2933
private static bool $failRead = false;
3034
private static bool $failWrite = false;
31-
private $stream;
35+
36+
/**
37+
* @var resource|false|null
38+
*/
39+
private mixed $stream = null;
3240

3341
public static function failLock(bool $fail): void
3442
{
@@ -66,7 +74,15 @@ public function stream_read(int $count): false|string
6674

6775
public function stream_write(string $data): false|int
6876
{
69-
return self::$failWrite ? false : fwrite($this->stream, $data);
77+
if (self::$failWrite) {
78+
$stream = fopen('php://temp', 'r');
79+
$result = fwrite($stream, $data);
80+
fclose($stream);
81+
82+
return $result;
83+
}
84+
85+
return fwrite($this->stream, $data);
7086
}
7187

7288
public function stream_lock(): bool
@@ -108,11 +124,26 @@ public function stream_stat(): array
108124
* Reports a regular file so that `is_file()` and `filesize()` behave like
109125
* they do for a real session file.
110126
*
111-
* @return array<string, int>
127+
* @return array<int|string, int>
112128
*/
113129
public function url_stat(): array
114130
{
131+
$now = time();
132+
115133
return [
134+
0 => 0,
135+
1 => 0,
136+
2 => 0o100600,
137+
3 => 1,
138+
4 => 0,
139+
5 => 0,
140+
6 => 0,
141+
7 => 5,
142+
8 => $now,
143+
9 => $now,
144+
10 => $now,
145+
11 => -1,
146+
12 => -1,
116147
'dev' => 0,
117148
'ino' => 0,
118149
'mode' => 0o100600,
@@ -121,9 +152,9 @@ public function url_stat(): array
121152
'gid' => 0,
122153
'rdev' => 0,
123154
'size' => 5,
124-
'atime' => time(),
125-
'mtime' => time(),
126-
'ctime' => time(),
155+
'atime' => $now,
156+
'mtime' => $now,
157+
'ctime' => $now,
127158
'blksize' => -1,
128159
'blocks' => -1,
129160
];

tests/system/Session/Handlers/FileHandlerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Config\Logger as LoggerConfig;
2121
use Config\Session as SessionConfig;
2222
use PHPUnit\Framework\Attributes\Group;
23+
use PHPUnit\Framework\Attributes\RequiresOperatingSystem;
2324
use Tests\Support\Session\FaultyStream;
2425
use Tests\Support\Session\FileHandlerCloseFail;
2526

@@ -36,13 +37,15 @@ final class FileHandlerTest extends CIUnitTestCase
3637
private string $originalSavePath;
3738
private string $originalSidBits;
3839
private string $originalSidLength;
40+
private bool $registeredFaultyStream = false;
3941

4042
protected function setUp(): void
4143
{
4244
parent::setUp();
4345

4446
if (! in_array('faulty', stream_get_wrappers(), true)) {
4547
stream_wrapper_register('faulty', FaultyStream::class);
48+
$this->registeredFaultyStream = true;
4649
}
4750

4851
$this->originalSavePath = (string) ini_get('session.save_path');
@@ -63,6 +66,11 @@ protected function tearDown(): void
6366

6467
FaultyStream::reset();
6568

69+
if ($this->registeredFaultyStream && in_array('faulty', stream_get_wrappers(), true)) {
70+
stream_wrapper_unregister('faulty');
71+
$this->registeredFaultyStream = false;
72+
}
73+
6674
$this->removeDirectory($this->tempPath);
6775
}
6876

@@ -226,6 +234,7 @@ public function testOpenThrowsForInvalidSavePath(): void
226234
$this->withSuppressedErrors(fn (): bool => $handler->open($this->tempPath . '/blocker/sub', $this->sessionName));
227235
}
228236

237+
#[RequiresOperatingSystem('Linux|Darwin')]
229238
public function testOpenThrowsForWriteProtectedSavePath(): void
230239
{
231240
$dir = $this->tempPath . '/readonly';
@@ -517,6 +526,7 @@ public function testGCWhenSavePathMissing(): void
517526
$this->assertLogContains('debug', "Session: Garbage collector couldn't list files under directory");
518527
}
519528

529+
#[RequiresOperatingSystem('Linux|Darwin')]
520530
public function testGCWhenDirectoryCannotBeOpened(): void
521531
{
522532
$dir = $this->tempPath . '/unreadable';

0 commit comments

Comments
 (0)