세션 테스트
ArrayHandler 세션 드라이버를 사용하면 애플리케이션의 세션 동작을 간단하게 테스트할 수 있습니다. 다른 세션 드라이버와 달리, ArrayHandler는 디스크, 데이터베이스 또는 외부 저장소에 데이터를 저장하지 않습니다. 이를 통해 실제 세션 데이터에 영향을 주지 않고 단위 테스트 또는 통합 테스트 중에 안전하게 세션 상호작용을 시뮬레이션할 수 있습니다.
이 드라이버를 사용하면 세션 데이터를 완전히 메모리 내에서 설정, 검색 및 어설션할 수 있어 테스트를 더 빠르고 격리된 상태로 유지할 수 있습니다. 대부분의 프로덕션 환경에서는 파일, 데이터베이스 또는 캐시 기반 세션을 사용하지만, ArrayHandler는 테스트 워크플로우를 지원하고 부작용을 방지하기 위해 특별히 존재합니다.
세션 초기화
테스트를 위해 ArrayHandler 드라이버를 사용하여 세션을 초기화할 수 있습니다. 다음 예시는 적절한 설정으로 세션 인스턴스를 생성하는 방법을 보여줍니다:
<?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);
데이터 설정 및 검색
초기화 후에는 평소와 같이 세션 값을 설정하고 검색할 수 있습니다:
<?php
// Set session data
$testSession->set('framework', 'CodeIgniter4');
// Retrieve session data
echo $testSession->get('framework'); // outputs 'CodeIgniter4'
// Remove session data
$testSession->remove('framework');
참고
세션 데이터는 메모리에 저장되며 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'));
}
}
세션 어설션
ArrayHandler와 함께 PHPUnit 어설션 사용
단위 테스트에서 Session과 ArrayHandler를 사용하여 세션을 직접 테스트할 때는 표준 PHPUnit 어설션을 사용합니다. 응답 객체가 아닌 세션 객체와 직접 상호작용하기 때문에 이 컨텍스트에서는 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를 통한 세션 어설션
When testing controllers or HTTP responses, you can use CodeIgniter 4’s session
assertion helpers, such as assertSessionHas() and assertSessionMissing(),
which are available on the TestResponse object. These helpers allow you to
assert the state of the session during the HTTP request/response lifecycle.
See more: Session Assertions
커스텀 세션 값
기능 테스트에서는 withSession() 메서드를 사용하여 단일 테스트에 대한 커스텀 세션 데이터를 제공할 수 있습니다. 이를 통해 요청 중에 로그인한 사용자나 특정 역할과 같은 세션 상태를 시뮬레이션할 수 있습니다. 자세한 내용과 예시는 세션 값 설정을 참고하십시오.