PHPUnit and “Cannot regenerate session ID”

Notice: This thread is very old.
OndrejSlamecka
Member | 41
+
0
-

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)

HosipLan
Moderator | 4668
+
0
-

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)

OndrejSlamecka
Member | 41
+
0
-

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)

HosipLan
Moderator | 4668
+
0
-

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)