Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions tests/_support/Session/FaultyStream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Tests\Support\Session;

/**
* A stream wrapper that can be configured to fail on lock, read, or write.
*
* Used by {@see \CodeIgniter\Session\Handlers\FileHandlerTest} to exercise the
* failure branches of {@see \CodeIgniter\Session\Handlers\FileHandler::read()}
* and {@see \CodeIgniter\Session\Handlers\FileHandler::write()}.
*
* @internal
*/
final class FaultyStream
{
public $context;
private static bool $failLock = false;
private static bool $failRead = false;
private static bool $failWrite = false;
private $stream;

public static function failLock(bool $fail): void
{
self::$failLock = $fail;
}

public static function failRead(bool $fail): void
{
self::$failRead = $fail;
}

public static function failWrite(bool $fail): void
{
self::$failWrite = $fail;
}

public static function reset(): void
{
self::$failLock = false;
self::$failRead = false;
self::$failWrite = false;
}

public function stream_open(): bool
{
$this->stream = fopen('php://temp', 'c+b');

return $this->stream !== false;
}

public function stream_read(int $count): false|string
{
return self::$failRead ? false : fread($this->stream, $count);
}

public function stream_write(string $data): false|int
{
return self::$failWrite ? false : fwrite($this->stream, $data);
}

public function stream_lock(): bool
{
return ! self::$failLock;
}

public function stream_close(): void
{
fclose($this->stream);
}

public function stream_eof(): bool
{
return feof($this->stream);
}

public function stream_tell(): int
{
return ftell($this->stream);
}

public function stream_seek(int $offset, int $whence): bool
{
return fseek($this->stream, $offset, $whence) === 0;
}

public function stream_truncate(int $newSize): bool
{
return ftruncate($this->stream, $newSize);
}

public function stream_stat(): array
{
return fstat($this->stream);
}

/**
* Reports a regular file so that `is_file()` and `filesize()` behave like
* they do for a real session file.
*
* @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,
];
}
}
30 changes: 30 additions & 0 deletions tests/_support/Session/FileHandlerCloseFail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Tests\Support\Session;

use CodeIgniter\Session\Handlers\FileHandler;

/**
* A {@see FileHandler} whose `close()` always reports failure, so that the
* alternative branch of {@see FileHandler::destroy()} can be exercised.
*
* @internal
*/
final class FileHandlerCloseFail extends FileHandler
{
public function close(): bool
{
return false;
}
}
Loading
Loading