Enabling "resetServices" causes "Session ini settings cannot be changed..." error |
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 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 (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 (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?
Use mockSession instead of real Session class.
You cannot test Session perfectly, because Session does not work on CLI. (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
@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. |
Welcome Guest, Not a member yet? Register Sign In |