Session 测试

使用 ArrayHandler Session 驱动,可轻松测试应用程序中的 Session 行为。 与其他 Session 驱动不同,ArrayHandler 不会将数据持久化到磁盘、数据库或外部存储。 这样可以在单元测试或集成测试期间安全地模拟 Session 交互,且不影响真实的 Session 数据。

使用此驱动可完全在内存中设置、检索和断言 Session 数据,使测试更快速、更独立。 虽然生产环境通常使用文件、数据库或缓存驱动,但 ArrayHandler 专为支持测试工作流并防止副作用而设计。

初始化 Session

可使用 ArrayHandler 驱动初始化测试用 Session。下例展示了如何通过正确配置创建 Session 实例:

<?php

use CodeIgniter\Session\Handlers\ArrayHandler;
use CodeIgniter\Session\Session;
use Config\Session as SessionConfig;

// Load session config
$config = config(SessionConfig::class);

// Initialize ArrayHandler with config and optional IP
$arrayHandler = new ArrayHandler($config, '127.0.0.1');

// Create session instance for testing
$testSession = new Session($arrayHandler, $config);

设置与检索数据

初始化完成后,即可像平时一样设置并检索 Session 值:

<?php

// Set session data
$testSession->set('framework', 'CodeIgniter4');

// Retrieve session data
echo $testSession->get('framework'); // outputs 'CodeIgniter4'

// Remove session data
$testSession->remove('framework');

备注

Session 数据存储在内存中,生命周期与 ArrayHandler 对象一致。 对象销毁后(通常在请求或测试结束时),数据将丢失。

测试用例示例

以下是在 PHPUnit 测试中使用 ArrayHandler 的简单示例:

<?php

use CodeIgniter\Session\Handlers\ArrayHandler;
use CodeIgniter\Session\Session;
use CodeIgniter\Test\CIUnitTestCase;
use Config\Session as SessionConfig;

class SessionTest extends CIUnitTestCase
{
    protected Session $testSession;

    protected function setUp(): void
    {
        parent::setUp();

        // Load session configuration
        $config = new SessionConfig();

        // Initialize ArrayHandler with config
        $arrayHandler = new ArrayHandler($config, '127.0.0.1');

        // Create session instance
        $this->testSession = new Session($arrayHandler, $config);
    }

    public function testFrameworkNameInSession(): void
    {
        // Set a session value
        $this->testSession->set('framework', 'CodeIgniter');

        // Assert the value exists and is correct
        $this->assertSame('CodeIgniter', $this->testSession->get('framework'));

        // Remove the session value
        $this->testSession->remove('framework');
        $this->assertNull($this->testSession->get('framework'));
    }
}

Session 断言

在 ArrayHandler 中使用 PHPUnit 断言

在单元测试中直接使用 Session 和 ArrayHandler 测试时,请使用标准的 PHPUnit 断言。 由于此时是直接与 Session 对象(而非响应对象)交互,因此 assertSessionHas()assertSessionMissing() 在此上下文中不可用。

<?php

// Set a session value
$testSession->set('framework', 'CodeIgniter4');

// Assert the state of the session using PHPUnit assertions
$this->assertSame('CodeIgniter4', $testSession->get('framework')); // Value exists

// Not empty
$this->assertNotEmpty($testSession->get('framework'));

// Remove the value and assert it's gone
$testSession->remove('framework');

// Should be null
$this->assertNull($testSession->get('framework'));

通过 TestResponse 进行 Session 断言

测试控制器或 HTTP 响应时,可使用 CodeIgniter 4 提供的 Session 断言辅助函数,例如 TestResponse 对象中的 assertSessionHas()assertSessionMissing()。 这些辅助函数允许在 HTTP 请求/响应生命周期内对 Session 状态进行断言。 更多详情请参阅:Session 断言

自定义 Session 值

在功能测试中,可通过 withSession() 方法为单个测试提供自定义 Session 数据。 由此可在请求期间模拟登录用户或特定角色等 Session 状态。 完整详情与示例请参阅:设置 Session 值