|
| 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>` |
0 commit comments