Skip to content

Commit 76efe75

Browse files
committed
docs: add all session testing code
1 parent 177178e commit 76efe75

File tree

5 files changed

+147
-0
lines changed

5 files changed

+147
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
###############
2+
Session Testing
3+
###############
4+
5+
Testing session behavior in your application is made simple with the ArrayHandler session driver.
6+
Unlike other session drivers, ArrayHandler does not persist data to disk, database, or external storage.
7+
This allows you to simulate session interactions safely during unit or integration tests, without affecting real session data.
8+
9+
Using this driver, you can set, retrieve, and assert session data entirely in memory, making your tests faster and more isolated.
10+
While in most production scenarios you would use file, database, or cache-backed sessions, ArrayHandler exists specifically to support testing workflows and prevent side effects.
11+
12+
.. contents::
13+
:local:
14+
:depth: 2
15+
16+
Initializing Sessions
17+
=====================
18+
19+
You can initialize a session using the ArrayHandler driver for testing. This example shows how to create a session instance with a proper configuration:
20+
21+
.. literalinclude:: session_testing/001.php
22+
23+
Setting and Retrieving Data
24+
===========================
25+
26+
Once initialized, you can set session values and retrieve them as usual:
27+
28+
.. literalinclude:: session_testing/002.php
29+
30+
.. note::
31+
32+
Session data is stored in memory and lasts as long as the ArrayHandler object exists;
33+
after the object is destroyed (typically at the end of a request or test), the data is lost.
34+
35+
Example Test Case
36+
=================
37+
38+
Here's a simple example demonstrating usage of the ArrayHandler in a PHPUnit test:
39+
40+
.. literalinclude:: session_testing/003.php
41+
42+
Session Assertions
43+
==================
44+
45+
Using PHPUnit Assertions with ArrayHandler
46+
------------------------------------------
47+
48+
When testing sessions directly with Session and ArrayHandler in a unit test, use standard PHPUnit assertions.
49+
``assertSessionHas()`` and ``assertSessionMissing()`` are not available in this context because you are interacting directly with the session object,
50+
not a response object.
51+
52+
.. literalinclude:: session_testing/004.php
53+
54+
Session Assertions via TestResponse
55+
-----------------------------------
56+
57+
When testing controllers or HTTP responses, you can use CodeIgniter 4’s session
58+
assertion helpers, such as ``assertSessionHas()`` and ``assertSessionMissing()``,
59+
which are available on the ``TestResponse`` object. These helpers allow you to
60+
assert the state of the session during the HTTP request/response lifecycle.
61+
See more: :ref:`Session Assertions <response-session-assertions>`
62+
63+
Custom Session Values
64+
=====================
65+
66+
In Feature Tests, you can provide custom session data for a single test using the ``withSession()`` method.
67+
This allows you to simulate session states such as logged-in users or specific roles during the request.
68+
For full details and examples, see: :ref:`Setting Session Values <feature-setting-session-values>`
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
use CodeIgniter\Session\Session;
4+
use CodeIgniter\Session\Handlers\ArrayHandler;
5+
use Config\Session;
6+
7+
// Load session config
8+
$config = new config(Session::class);
9+
10+
// Initialize ArrayHandler with config and optional IP
11+
$arrayHandler = new ArrayHandler($config, '127.0.0.1');
12+
13+
// Create session instance for testing
14+
$testSession = new Session($arrayHandler, $config);
15+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
// Set session data
4+
$testSession->set('framework', 'CodeIgniter4');
5+
6+
// Retrieve session data
7+
echo $testSession->get('framework'); // outputs 'CodeIgniter4'
8+
9+
// Remove session data
10+
$testSession->remove('framework');
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use CodeIgniter\Test\CIUnitTestCase;
4+
use CodeIgniter\Session\Session;
5+
use CodeIgniter\Session\Handlers\ArrayHandler;
6+
use Config\Session as SessionConfig;
7+
8+
class SessionTest extends CIUnitTestCase
9+
{
10+
protected Session $testSession;
11+
12+
protected function setUp(): void
13+
{
14+
parent::setUp();
15+
16+
// Load session configuration
17+
$config = new SessionConfig();
18+
19+
// Initialize ArrayHandler with config
20+
$arrayHandler = new ArrayHandler($config, '127.0.0.1');
21+
22+
// Create session instance
23+
$this->testSession = new Session($arrayHandler, $config);
24+
}
25+
26+
public function testFrameworkNameInSession(): void
27+
{
28+
// Set a session value
29+
$this->testSession->set('framework', 'CodeIgniter');
30+
31+
// Assert the value exists and is correct
32+
$this->assertSame('CodeIgniter', $this->testSession->get('framework'));
33+
34+
// Remove the session value
35+
$this->testSession->remove('framework');
36+
$this->assertNull($this->testSession->get('framework'));
37+
}
38+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
// Set a session value
4+
$testSession->set('framework', 'CodeIgniter4');
5+
6+
// Assert the state of the session using PHPUnit assertions
7+
$this->assertSame('CodeIgniter4', $testSession->get('framework')); // Value exists
8+
9+
// Not empty
10+
$this->assertNotEmpty($testSession->get('framework'));
11+
12+
// Remove the value and assert it's gone
13+
$testSession->remove('framework');
14+
15+
// Should be null
16+
$this->assertNull($testSession->get('framework'));

0 commit comments

Comments
 (0)