test: Add unit tests for Session FileHandler - #10435
Conversation
c95820f to
5a6106e
Compare
ff3a113 to
e4a9039
Compare
a6c9a5e to
7aad5ac
Compare
There was a problem hiding this comment.
Pull request overview
Adds unit tests and failure-injection helpers for the Session FileHandler.
Changes:
- Covers constructor, file operations, cleanup, and failure paths.
- Adds configurable stream and close-failure test doubles.
- Assumed
developbase; focused tests were not run.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
tests/system/Session/Handlers/FileHandlerTest.php |
Adds comprehensive handler tests. |
tests/_support/Session/FileHandlerCloseFail.php |
Simulates failed session closing. |
tests/_support/Session/FaultyStream.php |
Simulates stream operation failures. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| public $context; | ||
| private static bool $failLock = false; | ||
| private static bool $failRead = false; | ||
| private static bool $failWrite = false; | ||
| private $stream; |
| ini_set('session.sid_bits_per_character', $this->originalSidBits); | ||
| ini_set('session.sid_length', $this->originalSidLength); | ||
|
|
||
| FaultyStream::reset(); |
| * @return array<string, int> | ||
| */ | ||
| public function url_stat(): array | ||
| { | ||
| return [ | ||
| 'dev' => 0, | ||
| 'ino' => 0, | ||
| 'mode' => 0o100600, | ||
| 'nlink' => 1, | ||
| 'uid' => 0, | ||
| 'gid' => 0, | ||
| 'rdev' => 0, | ||
| 'size' => 5, | ||
| 'atime' => time(), | ||
| 'mtime' => time(), | ||
| 'ctime' => time(), | ||
| 'blksize' => -1, | ||
| 'blocks' => -1, | ||
| ]; |
| { | ||
| $dir = $this->tempPath . '/readonly'; | ||
| mkdir($dir, 0700); | ||
| chmod($dir, 0555); |
| { | ||
| $dir = $this->tempPath . '/unreadable'; | ||
| mkdir($dir, 0700); | ||
| chmod($dir, 0000); |
| public function stream_write(string $data): false|int | ||
| { | ||
| return self::$failWrite ? false : fwrite($this->stream, $data); |
857e85c to
99bb18a
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
Suppressed comments (3)
tests/_support/Session/FileHandlerCloseFail.php:28
- Returning
falsewithout performing the parent's cleanup leaves the session file open and locked. The corresponding destroy test then relies on POSIX's ability to unlink an open file; on Windowsdestroy()returnsfalsebecause the open file cannot be deleted. Close the resource normally, then report failure so the fallback branch remains exercised portably.
return false;
tests/system/Session/Handlers/FileHandlerTest.php:242
- The OS requirement does not exclude a root test process. Under root, a
0555directory is still writable, soopen()succeeds and this expected-exception test fails (a common case in containers). Skip when the permission change cannot make the directory non-writable, or use a deterministic filesystem double.
chmod($dir, 0555);
tests/system/Session/Handlers/FileHandlerTest.php:534
- A root process can still open a
0000directory, so on root-based containersgc()scans this empty directory and returns0rather thanfalse. The Linux/macOS attribute does not guard that case; verify thatopendir()actually fails and skip otherwise, or use a deterministic failure double.
chmod($dir, 0000);
783396f to
60bae9e
Compare
michalsn
left a comment
There was a problem hiding this comment.
Thank you for adding tests for FileHandler. More coverage here is valuable, but I don't think reaching 100% should be the goal.
Several tests inspect or modify protected state through getPrivateProperty() and setPrivateProperty(). This makes internal implementation details part of the effective test contract and may cause harmless refactoring to break the suite.
Please refocus the tests on observable behavior: return values, exceptions, logs, created files, session isolation, and cleanup. Tests and fixtures that exist only to reach otherwise inaccessible branches can be removed unless they protect a meaningful regression or documented behavior.
It is completely acceptable for coverage to fall below 100%. A smaller suite for testing the stable public contract will be more valuable and easier to maintain.
51096b0 to
d71b33c
Compare
d71b33c to
51096b0
Compare
Description
Adds unit tests for
system/Session/Handlers/FileHandler.php, reaching 100%statement and method coverage. No production code changed.
Checklist: