Skip to content

Commit f77d6b8

Browse files
authored
docs: clarify Session Testing behavior and assertion scope (codeigniter4#9857)
* docs: link to Session Testing * docs: add session testing * docs: add all session testing code * style: run cs-fix * docs: fix load session config
1 parent 62c952c commit f77d6b8

File tree

9 files changed

+173
-21
lines changed

9 files changed

+173
-21
lines changed

user_guide_src/source/libraries/sessions.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,8 @@ Have it in mind though, every driver has different caveats, so be sure to
514514
get yourself familiar with them (below) before you make that choice.
515515

516516
.. note:: The ArrayHandler is used during testing and stores all data within
517-
a PHP array, while preventing the data from being persisted.
517+
a PHP array, while preventing the data from being persisted. See
518+
:doc:`Testing Sessions </testing/session_testing>`.
518519

519520
FileHandler Driver (the default)
520521
================================

user_guide_src/source/testing/feature.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ override any existing routes in the system:
6363
Each of the "routes" is a 3 element array containing the HTTP verb (or "add" for all),
6464
the URI to match, and the routing destination.
6565

66+
.. _feature-setting-session-values:
67+
6668
Setting Session Values
6769
----------------------
6870

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
#######
2-
Testing
3-
#######
4-
5-
CodeIgniter ships with a number of tools to help you test and debug your application thoroughly.
6-
The following sections should get you quickly testing your applications.
7-
8-
.. toctree::
9-
:titlesonly:
10-
11-
Getting Started <overview>
12-
Database <database>
13-
Generating Data <fabricator>
14-
Controller Testing <controllers>
15-
HTTP Testing <feature>
16-
response
17-
cli
18-
Mocking <mocking>
19-
benchmark
20-
debugging
1+
#######
2+
Testing
3+
#######
4+
5+
CodeIgniter ships with a number of tools to help you test and debug your application thoroughly.
6+
The following sections should get you quickly testing your applications.
7+
8+
.. toctree::
9+
:titlesonly:
10+
11+
Getting Started <overview>
12+
Database <database>
13+
Generating Data <fabricator>
14+
Controller Testing <controllers>
15+
HTTP Testing <feature>
16+
response
17+
cli
18+
Mocking <mocking>
19+
benchmark
20+
debugging
21+
Session Testing <session_testing>

user_guide_src/source/testing/response.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ Asserts that the HTTP status code returned matches $code.
100100
.. literalinclude:: response/010.php
101101
:lines: 2-
102102

103+
.. _response-session-assertions:
104+
103105
Session Assertions
104106
==================
105107

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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
use CodeIgniter\Session\Handlers\ArrayHandler;
4+
use CodeIgniter\Session\Session;
5+
use Config\Session as SessionConfig;
6+
7+
// Load session config
8+
$config = config(SessionConfig::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);
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\Session\Handlers\ArrayHandler;
4+
use CodeIgniter\Session\Session;
5+
use CodeIgniter\Test\CIUnitTestCase;
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)