Welcome Guest, Not a member yet? Register   Sign In
Session issue with CI Environment set to Testing (CI4)
#1

I was having trouble setting up session data and retrieve them in other files. After a lot of trial and error, I found out that was because of the CI_ENVIRONMENT set to 'testing' in .env file. When this was set to either 'development' or 'production', this was working all fine.

Upon investigating the matter, this is the piece of code I found in Session.php file.

protected function startSession()
    {
        if (ENVIRONMENT === 'testing')
        {
            $_SESSION = [];
            return;
        }

        // @codeCoverageIgnoreStart
        session_start();
        // @codeCoverageIgnoreEnd
}


[font=Tahoma, Verdana, Arial, sans-serif]So essentially, for 'testing' environment, it is setting session to null and hence session data was not available. Can somebody let me know why session is set to null in here? What is the reason behind this?[/font]
Reply
#2

Looks like a bug to me, you should open an issue on GitHub
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply
#3
Thumbs Up 

I have the same issue.
I also believe this issue is a bug, so I submitted it to GitHub.

Leave a link for your reference.
https://github.com/codeigniter4/CodeIgni...ssues/4248
Reply
#4

(10-27-2020, 06:59 AM)shanilsoni Wrote: [font=Tahoma, Verdana, Arial, sans-serif]So essentially, for 'testing' environment, it is setting session to null and hence session data was not available. Can somebody let me know why session is set to null in here? What is the reason behind this?[/font]

It is for unit testing with PHPUnit. In CI4, the environment `testing` is a special one for PHPUnit testing.

On unit testing, it is recommended every time all the status including global variables is the exactly same
before running a test. If not, the test result may differ.

If one test changes the session status, and the next test does not expect the status change, the next test would fail.
The failure is the real failure or not? Avoiding such a case, preparing to set the same status before executing a test
is recommended.
Reply
#5

I found out through the issue report that the code is a spac.
testing is only a property for php unit testing.

If you use a session,
you may need to add an arbitrary value, such as 'test'.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB