From dafe800c3c181930d6265b4f6ac36a5ec5779c85 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Sun, 28 Dec 2025 09:38:42 +0330 Subject: [PATCH 1/5] docs: link to Session Testing --- user_guide_src/source/libraries/sessions.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/user_guide_src/source/libraries/sessions.rst b/user_guide_src/source/libraries/sessions.rst index 03c2bbe81fc2..322c3fd12322 100644 --- a/user_guide_src/source/libraries/sessions.rst +++ b/user_guide_src/source/libraries/sessions.rst @@ -507,7 +507,8 @@ Have it in mind though, every driver has different caveats, so be sure to get yourself familiar with them (below) before you make that choice. .. note:: The ArrayHandler is used during testing and stores all data within - a PHP array, while preventing the data from being persisted. + a PHP array, while preventing the data from being persisted. See + :doc:`Testing Sessions `. FileHandler Driver (the default) ================================ From 177178e4d4fe973044e7ede35e1ab15b247c58e1 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Sun, 28 Dec 2025 10:53:58 +0330 Subject: [PATCH 2/5] docs: add session testing --- user_guide_src/source/testing/feature.rst | 2 ++ user_guide_src/source/testing/index.rst | 41 +++++++++++----------- user_guide_src/source/testing/response.rst | 2 ++ 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/user_guide_src/source/testing/feature.rst b/user_guide_src/source/testing/feature.rst index 00100f8a7885..67337fa0464c 100644 --- a/user_guide_src/source/testing/feature.rst +++ b/user_guide_src/source/testing/feature.rst @@ -63,6 +63,8 @@ override any existing routes in the system: Each of the "routes" is a 3 element array containing the HTTP verb (or "add" for all), the URI to match, and the routing destination. +.. _feature-setting-session-values: + Setting Session Values ---------------------- diff --git a/user_guide_src/source/testing/index.rst b/user_guide_src/source/testing/index.rst index b6a71055698c..866ef30f5b6f 100644 --- a/user_guide_src/source/testing/index.rst +++ b/user_guide_src/source/testing/index.rst @@ -1,20 +1,21 @@ -####### -Testing -####### - -CodeIgniter ships with a number of tools to help you test and debug your application thoroughly. -The following sections should get you quickly testing your applications. - -.. toctree:: - :titlesonly: - - Getting Started - Database - Generating Data - Controller Testing - HTTP Testing - response - cli - Mocking - benchmark - debugging +####### +Testing +####### + +CodeIgniter ships with a number of tools to help you test and debug your application thoroughly. +The following sections should get you quickly testing your applications. + +.. toctree:: + :titlesonly: + + Getting Started + Database + Generating Data + Controller Testing + HTTP Testing + response + cli + Mocking + benchmark + debugging + Session Testing diff --git a/user_guide_src/source/testing/response.rst b/user_guide_src/source/testing/response.rst index 552133272a28..419df7620782 100644 --- a/user_guide_src/source/testing/response.rst +++ b/user_guide_src/source/testing/response.rst @@ -100,6 +100,8 @@ Asserts that the HTTP status code returned matches $code. .. literalinclude:: response/010.php :lines: 2- +.. _response-session-assertions: + Session Assertions ================== From 76efe75f13e1704e90073cfc936d309d9b363cb6 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Sun, 28 Dec 2025 10:57:07 +0330 Subject: [PATCH 3/5] docs: add all session testing code --- .../source/testing/session_testing.rst | 68 +++++++++++++++++++ .../source/testing/session_testing/001.php | 15 ++++ .../source/testing/session_testing/002.php | 10 +++ .../source/testing/session_testing/003.php | 38 +++++++++++ .../source/testing/session_testing/004.php | 16 +++++ 5 files changed, 147 insertions(+) create mode 100644 user_guide_src/source/testing/session_testing.rst create mode 100644 user_guide_src/source/testing/session_testing/001.php create mode 100644 user_guide_src/source/testing/session_testing/002.php create mode 100644 user_guide_src/source/testing/session_testing/003.php create mode 100644 user_guide_src/source/testing/session_testing/004.php diff --git a/user_guide_src/source/testing/session_testing.rst b/user_guide_src/source/testing/session_testing.rst new file mode 100644 index 000000000000..17f787454fac --- /dev/null +++ b/user_guide_src/source/testing/session_testing.rst @@ -0,0 +1,68 @@ +############### +Session Testing +############### + +Testing session behavior in your application is made simple with the ArrayHandler session driver. +Unlike other session drivers, ArrayHandler does not persist data to disk, database, or external storage. +This allows you to simulate session interactions safely during unit or integration tests, without affecting real session data. + +Using this driver, you can set, retrieve, and assert session data entirely in memory, making your tests faster and more isolated. +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. + +.. contents:: + :local: + :depth: 2 + +Initializing Sessions +===================== + +You can initialize a session using the ArrayHandler driver for testing. This example shows how to create a session instance with a proper configuration: + +.. literalinclude:: session_testing/001.php + +Setting and Retrieving Data +=========================== + +Once initialized, you can set session values and retrieve them as usual: + +.. literalinclude:: session_testing/002.php + +.. note:: + + Session data is stored in memory and lasts as long as the ArrayHandler object exists; + after the object is destroyed (typically at the end of a request or test), the data is lost. + +Example Test Case +================= + +Here's a simple example demonstrating usage of the ArrayHandler in a PHPUnit test: + +.. literalinclude:: session_testing/003.php + +Session Assertions +================== + +Using PHPUnit Assertions with ArrayHandler +------------------------------------------ + +When testing sessions directly with Session and ArrayHandler in a unit test, use standard PHPUnit assertions. +``assertSessionHas()`` and ``assertSessionMissing()`` are not available in this context because you are interacting directly with the session object, +not a response object. + +.. literalinclude:: session_testing/004.php + +Session Assertions via 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: :ref:`Session Assertions ` + +Custom Session Values +===================== + +In Feature Tests, you can provide custom session data for a single test using the ``withSession()`` method. +This allows you to simulate session states such as logged-in users or specific roles during the request. +For full details and examples, see: :ref:`Setting Session Values ` \ No newline at end of file diff --git a/user_guide_src/source/testing/session_testing/001.php b/user_guide_src/source/testing/session_testing/001.php new file mode 100644 index 000000000000..39ac8f848688 --- /dev/null +++ b/user_guide_src/source/testing/session_testing/001.php @@ -0,0 +1,15 @@ +set('framework', 'CodeIgniter4'); + +// Retrieve session data +echo $testSession->get('framework'); // outputs 'CodeIgniter4' + +// Remove session data +$testSession->remove('framework'); diff --git a/user_guide_src/source/testing/session_testing/003.php b/user_guide_src/source/testing/session_testing/003.php new file mode 100644 index 000000000000..3ed0972c9728 --- /dev/null +++ b/user_guide_src/source/testing/session_testing/003.php @@ -0,0 +1,38 @@ +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')); + } +} diff --git a/user_guide_src/source/testing/session_testing/004.php b/user_guide_src/source/testing/session_testing/004.php new file mode 100644 index 000000000000..25f221259e17 --- /dev/null +++ b/user_guide_src/source/testing/session_testing/004.php @@ -0,0 +1,16 @@ +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')); \ No newline at end of file From 13447b6dc72ab32173a27a31af7d822e193364c4 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Sun, 28 Dec 2025 11:24:39 +0330 Subject: [PATCH 4/5] style: run cs-fix --- user_guide_src/source/testing/session_testing/001.php | 5 ++--- user_guide_src/source/testing/session_testing/003.php | 4 ++-- user_guide_src/source/testing/session_testing/004.php | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/user_guide_src/source/testing/session_testing/001.php b/user_guide_src/source/testing/session_testing/001.php index 39ac8f848688..12a4964d1cb4 100644 --- a/user_guide_src/source/testing/session_testing/001.php +++ b/user_guide_src/source/testing/session_testing/001.php @@ -1,15 +1,14 @@ remove('framework'); // Should be null -$this->assertNull($testSession->get('framework')); \ No newline at end of file +$this->assertNull($testSession->get('framework')); From fbda65bbec5394d764acb6112bf9fc781fb7df45 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Sun, 28 Dec 2025 21:12:30 +0330 Subject: [PATCH 5/5] docs: fix load session config --- user_guide_src/source/testing/session_testing/001.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/user_guide_src/source/testing/session_testing/001.php b/user_guide_src/source/testing/session_testing/001.php index 12a4964d1cb4..01ad520b7ed0 100644 --- a/user_guide_src/source/testing/session_testing/001.php +++ b/user_guide_src/source/testing/session_testing/001.php @@ -2,10 +2,10 @@ use CodeIgniter\Session\Handlers\ArrayHandler; use CodeIgniter\Session\Session; -use Config\Session; +use Config\Session as SessionConfig; // Load session config -$config = new \config(Session::class); +$config = config(SessionConfig::class); // Initialize ArrayHandler with config and optional IP $arrayHandler = new ArrayHandler($config, '127.0.0.1');