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

Problem with session in one Cest file #4112

Closed
tasselchof opened this Issue Apr 8, 2017 · 8 comments

Comments

Projects
None yet
3 participants
@tasselchof
Contributor

tasselchof commented Apr 8, 2017

Hello!

I have a problem with each test after second one. I am running suit:

[02:59 AM]-[vagrant@o]-[/home/dev/application]-[git master]
$ ./vendor/bin/codecept run ProductsApiFunctional ShopCrudCest
Codeception PHP Testing Framework v2.2.10
Powered by PHPUnit 5.7.19 by Sebastian Bergmann and contributors.

ProductsApiFunctional Tests (2) -----------------------------------------------------------------------------------------------------------------------------------------------
✔ ShopCrudCest: Get shops (3.21s)
E ShopCrudCest: Create shop
E ShopCrudCest: Create shop (1.88s)

Second one is failed because of:

  1. ShopCrudCest: Create shop
    Test codeception/ProductsApiFunctional/ShopCrudCest.php:createShop

[PHPUnit_Framework_Exception] session_regenerate_id(): Cannot regenerate session id - session is not active

Which is coming from Zend Framework Session manager.

Who faced the issue like this?

@Naktibalda

This comment has been minimized.

Show comment
Hide comment
@Naktibalda

Naktibalda Apr 8, 2017

Member

regenerateId

    public function regenerateId($deleteOldSession = true)
    {
        if ($this->sessionExists()) {
            session_regenerate_id((bool) $deleteOldSession);
        }
        return $this;
    }

You got this particular message, because sessionExists returns true even when the session is not active.

Anyway, even if the session was active, you would probably get a "headers already sent" error.
It is a general issue with framework modules that they run in a single process and PHP functions that are setting headers can't be tested.

A workaround is to add condition to your code to skip regenerateId() call in the test environment.

Member

Naktibalda commented Apr 8, 2017

regenerateId

    public function regenerateId($deleteOldSession = true)
    {
        if ($this->sessionExists()) {
            session_regenerate_id((bool) $deleteOldSession);
        }
        return $this;
    }

You got this particular message, because sessionExists returns true even when the session is not active.

Anyway, even if the session was active, you would probably get a "headers already sent" error.
It is a general issue with framework modules that they run in a single process and PHP functions that are setting headers can't be tested.

A workaround is to add condition to your code to skip regenerateId() call in the test environment.

@tasselchof

This comment has been minimized.

Show comment
Hide comment
@tasselchof

tasselchof Apr 8, 2017

Contributor

Got it, maybe it's because of authentication - inside the adapters I have session regeneration.

About "headers already sent" - this was in pure PHPUnit tests (without codeception), but fixed by just adding ob_start(); before code.

Contributor

tasselchof commented Apr 8, 2017

Got it, maybe it's because of authentication - inside the adapters I have session regeneration.

About "headers already sent" - this was in pure PHPUnit tests (without codeception), but fixed by just adding ob_start(); before code.

@tasselchof

This comment has been minimized.

Show comment
Hide comment
@tasselchof

tasselchof Apr 8, 2017

Contributor

You got this particular message, because sessionExists returns true even when the session is not active.

I check just now - it's like this, because (session_status() == PHP_SESSION_ACTIVE) is true. I added check in session manager:

if ($this->sessionExists()) { try { session_regenerate_id((bool)$deleteOldSession); } catch (\PHPUnit_Framework_Exception $e) { var_dump($this->sessionExists()); die(); } }

And it's outputting true.

The problem is coming from the adapter of ZfcUser (I stopped using this module, but adapter is left), here it has this regenerate session id:

// regen the id $session = new SessionContainer($this->getStorage()->getNameSpace()); $session->getManager()->regenerateId();

So the right solution here is to disable this for test environment?

Contributor

tasselchof commented Apr 8, 2017

You got this particular message, because sessionExists returns true even when the session is not active.

I check just now - it's like this, because (session_status() == PHP_SESSION_ACTIVE) is true. I added check in session manager:

if ($this->sessionExists()) { try { session_regenerate_id((bool)$deleteOldSession); } catch (\PHPUnit_Framework_Exception $e) { var_dump($this->sessionExists()); die(); } }

And it's outputting true.

The problem is coming from the adapter of ZfcUser (I stopped using this module, but adapter is left), here it has this regenerate session id:

// regen the id $session = new SessionContainer($this->getStorage()->getNameSpace()); $session->getManager()->regenerateId();

So the right solution here is to disable this for test environment?

@Naktibalda

This comment has been minimized.

Show comment
Hide comment
@Naktibalda

Naktibalda Apr 8, 2017

Member

It is a solution that works.

One alternative I can imagine (but I've never used it myself), is to set session.use_cookies to 0 to avoid issues with cookies.
And please try to remove session_write_close from ZF2::_after and see if it helps

Member

Naktibalda commented Apr 8, 2017

It is a solution that works.

One alternative I can imagine (but I've never used it myself), is to set session.use_cookies to 0 to avoid issues with cookies.
And please try to remove session_write_close from ZF2::_after and see if it helps

@tasselchof

This comment has been minimized.

Show comment
Hide comment
@tasselchof

tasselchof Apr 8, 2017

Contributor

And please try to remove session_write_close from ZF2::_after and see if it helps

This helped. Maybe to make correction and remove this from codeception ZF2 module? I don't this session_close() is needed here (made pull request for this).

Contributor

tasselchof commented Apr 8, 2017

And please try to remove session_write_close from ZF2::_after and see if it helps

This helped. Maybe to make correction and remove this from codeception ZF2 module? I don't this session_close() is needed here (made pull request for this).

tasselchof added a commit to tasselchof/Codeception that referenced this issue Apr 8, 2017

tasselchof added a commit to tasselchof/Codeception that referenced this issue Apr 8, 2017

tasselchof added a commit to tasselchof/Codeception that referenced this issue Apr 8, 2017

@Naktibalda

This comment has been minimized.

Show comment
Hide comment
@Naktibalda

Naktibalda Apr 9, 2017

Member

Does #1486 make sense to you?

@FnTm Have you got any objections to reverting your change or alternative ideas for solving regenerateId issue?

Member

Naktibalda commented Apr 9, 2017

Does #1486 make sense to you?

@FnTm Have you got any objections to reverting your change or alternative ideas for solving regenerateId issue?

@tasselchof

This comment has been minimized.

Show comment
Hide comment
@tasselchof

tasselchof Apr 9, 2017

Contributor

I can't confirm #1486 - my application changing session configuration in module bootstrap (with standard config).

Contributor

tasselchof commented Apr 9, 2017

I can't confirm #1486 - my application changing session configuration in module bootstrap (with standard config).

@FnTm

This comment has been minimized.

Show comment
Hide comment
@FnTm

FnTm Apr 9, 2017

Contributor

@Naktibalda No objections from me, haven't been working with PHP for a couple of years now, so lost touch with the tools. Do what needs to be done :)

Contributor

FnTm commented Apr 9, 2017

@Naktibalda No objections from me, haven't been working with PHP for a couple of years now, so lost touch with the tools. Do what needs to be done :)

Naktibalda added a commit that referenced this issue Apr 10, 2017

Merge pull request #4114 from tasselchof/2.2
Removed session_write_close() from ZF2 module (#4112)

@Naktibalda Naktibalda closed this Apr 10, 2017

chris1312 added a commit to chris1312/Codeception that referenced this issue Jun 16, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment