PHPUnit and “Cannot regenerate session ID”

Notice: This is an old thread, there may be recent replies.

1. 2 years ago

OndrejSlamecka
Member
Registered: 2009-12-27
Location: Zlínsko
Posts: 39

PHPUnit and “Cannot regenerate session ID”

I am testing en event of login form sending (inspired by similar test in Návštěvní kniha v Nette s testy (TDD))

Problem is that even though I have started session in bootstrap.php (container->session->start()), the output after running tests reads:

<?php
AuthenticationPresenterTest::testLoginForm()
Nette\InvalidStateException: Cannot regenerate session ID after HTTP headers have been sent (output started at E:\...\php\pear\PHPUnit\Util\Printer.php:173).
?>

I am not sure if this problem is in Nette or PHPUnit. The form is submitted and AuthenticationPresenter::loginFormSubmitted(form) function tries to login the user ($this->getUser()->login(...) is called). The problem starts here and the error message (written above) is thrown on the line with “print($buffer);” (Printer.php:173). I tried using condition \Nette\Environment::getSession()->isStarted() before the print with no result (but test works when condition is negated!).

Please, is there a way how to solve this? Thank you

My test code:

<?php
    public function testLoginForm()
    {
        $requestData = array(
            'action' => 'login',
            'do' => 'loginForm-submit',
            ...
        );
        $request = new Request('Authentication', 'POST', $requestData, $requestData);
        $response = $this->presenter->run($request);

        self::assertInstanceOf(
                'Nette\Application\Responses\RedirectResponse', $response
        );
    }
?>

Last edited by OndrejSlamecka (2012-01-10 23:06)

 

2. 2 years ago

HosipLan
Moderator
Registered: 2009-06-01
Location: Brno
Posts: 4035

Re: PHPUnit and “Cannot regenerate session ID”

I won't say I've solved it, because I've just hacked it.

And, you should look at this

Last edited by HosipLan (2012-01-11 10:07)

 

3. 2 years ago

OndrejSlamecka
Member
Registered: 2009-12-27
Location: Zlínsko
Posts: 39

Re: PHPUnit and “Cannot regenerate session ID”

HosipLan wrote:

Thank you. Your solution hack is interesting and clear (as much as a hack can be), but quite big (I don't have my own classes for security/… so it would require a lot of code to write). Instead I have chosen a quick solution hack: ob_start(); before the test. But I am not yet sure if it won't create some problem…

Also thanks for pointing to recent changes in Nette. However I am not sure if I can use them somehow right now.

Last edited by OndrejSlamecka (2012-01-11 16:46)

 

4. 2 years ago

HosipLan
Moderator
Registered: 2009-06-01
Location: Brno
Posts: 4035

Re: PHPUnit and “Cannot regenerate session ID”

Than have a look at my new solution, with UserStorage

https://github.com/…toryTest.php & https://github.com/…rStorage.php

It's not hack anymore, because there is no hacking of framework classes with reflection ;)

Last edited by HosipLan (2012-01-11 17:09)

 

Board footer

Switch to desktop