Enabling "resetServices" causes "Session ini settings cannot be changed..." error - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Enabling "resetServices" causes "Session ini settings cannot be changed..." error (/showthread.php?tid=82082) |
Enabling "resetServices" causes "Session ini settings cannot be changed..." error - umizoomi - 06-09-2022 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/CodeIgniter4/issues/3578#issuecomment-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 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 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 RE: Enabling "resetServices" causes "Session ini settings cannot be changed..." error - kenjis - 06-09-2022 (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/CodeIgniter4/changelogs/v4.2.0.html#method-signature-changes RE: Enabling "resetServices" causes "Session ini settings cannot be changed..." error - umizoomi - 06-09-2022 (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. 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? RE: Enabling "resetServices" causes "Session ini settings cannot be changed..." error - umizoomi - 06-11-2022 @kenjis any suggestions? RE: Enabling "resetServices" causes "Session ini settings cannot be changed..." error - kenjis - 06-11-2022 Use mockSession instead of real Session class. You cannot test Session perfectly, because Session does not work on CLI. RE: Enabling "resetServices" causes "Session ini settings cannot be changed..." error - umizoomi - 06-13-2022 (06-11-2022, 09:00 PM)kenjis Wrote: Use mockSession instead of real Session class. 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 RE: Enabling "resetServices" causes "Session ini settings cannot be changed..." error - kenjis - 06-13-2022 @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/CodeIgniter4/blob/8e8973634ce98558fd230a663c00475dcd8f2dca/system/Test/CIUnitTestCase.php#L50-L55 If you didn't override $setUpMethods, probably the session error was never happened. |