Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[zend-session] Testing with PhpUnit ini_set error #22

Open
avonarb opened this issue Jul 29, 2020 · 1 comment
Open

[zend-session] Testing with PhpUnit ini_set error #22

avonarb opened this issue Jul 29, 2020 · 1 comment

Comments

@avonarb
Copy link

avonarb commented Jul 29, 2020

I am updating a ZF1 app from PHP 5.3 ZF 1.12.20 to PHP 7.4.

I'm using zf1s 1.13.1 on PHP 7.4.8 with PHPUnit 7.5.20 and a Zend_Test_PHPUnit_ControllerTestCase and I'm getting the error

ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

I've taken a closer look. My application is in testing ENV and is testing itself. I have a TestHelper that bootstraps my application in "testing" ENV and a ControllerTestCase class that bootstraps another instance of the application in the setUp method for testing. Bootstraping the second instance fails with the error above.

Here is my setup for the TestHelper which is included for all tests:

...

$application = new Zend_Application(
    'testing',
    APPLICATION_PATH . DIRECTORY_SEPARATOR.'configs'.DIRECTORY_SEPARATOR.'application.ini'
);

Zend_Session::$_unitTestEnabled = true;

$application->bootstrap(); 

The setUp() method in the ControllerTestCase class looks like this:

class ConfigurationTest extends Zend_Test_PHPUnit_ControllerTestCase
{
    public function setUp()
    {
        $this->bootstrap = new Zend_Application(
            'testing',
            APPLICATION_PATH . '/configs/application.ini'
        );

        $someSession = new Zend_Session_Namespace('someSession');
        $someSession->data =  ....

        //more code here

        parent::setUp();  // <-- this will bootstrap the test instance, error is here
    }

Problem is this code in Zend_Session, starting at line 212:


        // set the options the user has requested to set
        foreach ($userOptions as $userOptionName => $userOptionValue) {

            $userOptionName = strtolower($userOptionName);

            // set the ini based values
            if (array_key_exists($userOptionName, self::$_defaultOptions)) {
                ini_set("session.$userOptionName", $userOptionValue); // <-----  error is here
            }

As the session was initiated correctly the first time and is up and running, this here is a possible solution:

Shardj/zf1-future@82a4367#diff-dbc2d7706346875f0cbfb79798dfbfdd

What do you think?

@avonarb avonarb changed the title [zend-session] ini_set error [zend-session] Testing with PhpUnit ini_set error Jul 29, 2020
@falkenhawk
Copy link
Member

I remember we also had this issue, but if I recall correctly, we simply ensured sessions are not used in tests.
There is Zend_Session::$_unitTestEnabled flag which is being set to true in our test suites (also zf test suite sets this flag to true internally), but this flag is not checked when Zend_Session::setOptions() is called - so we added a condition checks so that this method is not called.

But, we also do not use Zend_Test_PHPUnit_ControllerTestCase at all.. and that is probably calling that sessions options somewhere on the way (but is it really necessary? maybe the actual call to Zend_Session::setOptions() should be guarded?)

Anyway, since I do not have time resources at hand to test it on my own, I'd be glad if you could add a PR with a test for this issue. 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants