Welcome Guest, Not a member yet? Register   Sign In
Enabling "resetServices" causes "Session ini settings cannot be changed..." error
#1

(This post was last modified: 06-09-2022, 08:43 AM by umizoomi.)

I need to clear sessions between post calls in my test suite and it appears that the best way to do that is to enable resetServices in the $setUpMethods array:
https://github.com/codeigniter4/CodeIgni...-835353623
However, when I do that, I receive the following error:

Code:
ErrorException: ini_set(): Session ini settings cannot be changed after headers have already been sent

C:\sites\www\vendor\codeigniter4\framework\system\Session\Session.php:290
C:\sites\www\vendor\codeigniter4\framework\system\Session\Session.php:228
C:\sites\www\vendor\codeigniter4\framework\system\Config\Services.php:577
C:\sites\www\vendor\codeigniter4\framework\system\Config\BaseService.php:248
C:\sites\www\vendor\codeigniter4\framework\system\Config\BaseService.php:189
C:\sites\www\vendor\codeigniter4\framework\system\Config\Services.php:563
C:\sites\www\vendor\codeigniter4\framework\system\Config\BaseService.php:248
C:\sites\www\vendor\codeigniter4\framework\system\Common.php:929
C:\sites\www\app\Controllers\BaseController.php:77
C:\sites\www\vendor\codeigniter4\framework\system\CodeIgniter.php:810
C:\sites\www\vendor\codeigniter4\framework\system\CodeIgniter.php:410
C:\sites\www\vendor\codeigniter4\framework\system\CodeIgniter.php:326
C:\sites\www\vendor\codeigniter4\framework\system\Test\FeatureTestTrait.php:188
C:\sites\www\vendor\codeigniter4\framework\system\Test\FeatureTestTrait.php:235
C:\sites\www\tests\app\Controllers\ContactPostTest.php:76
C:\sites\www\tests\app\Controllers\ContactPostTest.php:48

Is there a way to set the resetServices to true before the headers have been set?

For reference, I have tried enabling resetServices in the setUpMethods:

PHP Code:
protected $setUpMethods = ["resetServices"]; 

Enabling it in the setUp function:

PHP Code:
protected function setUp(): void
{
    parent::setUp();

    $this->resetServices();


And enabling it directly in one of my functions just before the $this->post call:

PHP Code:
$this->resetServices(); 

And it results in the same error. I have also tried to only reset the single MockSession service in my function:

PHP Code:
Services::resetSingle("mockSession"); 

But that doesn't do anything.

This was done with CI4.1.9 and since CI4.2 enables resetServices by default, I tried upgrading to that, which corrects the issue; however, I am still running into the same error that I was trying to fix with resetServices. You can see more info on that here:

https://stackoverflow.com/q/72539728/1698908
Reply
#2

(06-09-2022, 08:02 AM)umizoomi Wrote: This was done with CI4.1.9 and since CI4.2 enables resetServices by default, I tried upgrading to that, which corrects the issue; however, I am still running into the same error that I was trying to fix with resetServices.

No, CI4.2 does not enable resetServices by default.
It just changed the default value for the parameter (from false to true).

Read carefully.
https://codeigniter4.github.io/CodeIgnit...re-changes
Reply
#3

(06-09-2022, 04:47 PM)kenjis Wrote:
(06-09-2022, 08:02 AM)umizoomi Wrote: This was done with CI4.1.9 and since CI4.2 enables resetServices by default, I tried upgrading to that, which corrects the issue; however, I am still running into the same error that I was trying to fix with resetServices.

No, CI4.2 does not enable resetServices by default.
It just changed the default value for the parameter (from false to true).

Read carefully.
https://codeigniter4.github.io/CodeIgnit...re-changes

Thanks for the clarification. When I add "resetServices" to the $setUpMethods array (in CI4.2), I see the same ini settings error. Do you have any idea what could be causing it?
Reply
#4

@kenjis any suggestions?
Reply
#5

(This post was last modified: 06-11-2022, 09:03 PM by kenjis.)

Use mockSession instead of real Session class.

You cannot test Session perfectly, because Session does not work on CLI.
Reply
#6

(06-11-2022, 09:00 PM)kenjis Wrote: Use mockSession instead of real Session class.

You cannot test Session perfectly, because Session does not work on CLI.

Got it all to work! Thanks for your help. If it helps anyone, I posted a working code example here: https://stackoverflow.com/a/72607508/1698908
Reply
#7

(This post was last modified: 06-13-2022, 04:14 PM by kenjis.)

@umizoomi Thank you for posting at stackoverflow.

But you should not override $setUpMethods, because it may destroy the functionality by default.
In fact, `resetServices` and `mockSession` are defined by default.
See https://github.com/codeigniter4/CodeIgni...hp#L50-L55

If you didn't override $setUpMethods, probably the session error was never happened.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB