I'm attempting to add phpunit functional tests of the login process for a Symfony system. The login process is simple, but I'm having problems establishing a client that doesn't suffer from one of a number of problems described below, and I've run out of ideas for work-arounds. My apologies for the length of the post. Any advice would be highly appreciated.
Details:
The sequence in a normal login for a debug system
is:
http://localhost:8000/login GET -> html form for login
http://localhost:8000/login_check POST(_username,_password) -> redirect
http://localhost:8000/useridx/NN (same as redirect)
My test class extends WebTestCase, and contains the corresponding public functions:
testGetLoginPage(): tests page returned from /login, returns $context (with $client)
testLoginCheck($context): posts the login data, validates the redirect page, and returns $context (with $client)
testUserIndex($context): validates the page returned by following the redirect
When I ran the test, testGetLoginPage succeeded, then testLoginCheck got a return page with a symfony stack trace indicating:
<title> Failed to start the session because headers have already been sent by "/home1/xxxxx-app/vendor/phpunit/phpunit/src/Util/Printer.php" at line 133. (500 Internal Server Error)</title>
with the line in Printer.php being:
print $buffer;
After some research, I found comments indicating that I could avoid this problem by declaring this for testLoginCheck:
/**
* @runInSeparateProcess
*/
After that declaration, testLoginCheck got the correct page (the redirect) to /useridx/NN. I then attempted to follow the redirect in testUserIndex, but the response returned a copy of the first login page, with an access-denied indication, even though the login test succeeded.
My next thought was that I also needed to declare @runInSeparateProcess for testUserIndex, but the result was the same. I'm guessing that resulted in one process for Print.php, a second process for testLoginCheck, and a third process for testUserIndex. I eventually found another directive which I applied to my test class:
/**
* @runTestsInSeparateProcesses
*/
and I removed the @runInSeparateProcess directives from the functions. That worked well until one of the test asserts failed, at which point I got a long symfony exception and stack trace indicating:
Caused by
exception 'ErrorException' with message 'Class __PHP_Incomplete_Class has no unserializer' in /home1/xxxxx-app/vendor/phpunit/phpunit/src/Util/PHP.php:107
By deliberately failing asserts one assert at a time, I found that the incomplete-class assertion was always triggered by the first failed assert.
The comments related to @preserveGlobalState indicate it could be disabled in order to solve serializer/deserializer problems between parent and child processes, but setting that for the class caused a new problem:
RuntimeException: You must override the KernelTestCase::createKernel() method.
I also tried just simulating authentication so I could get to my remaining tests (which require authentication), using this approach:
http://symfony.com/doc/current/testing/simulating_authentication.html
http://symfony.com/doc/current/testing/http_authentication.html
but it always gives me a redirect back to the login page.
I thought it best to stop and ask for help at this point, before starting over with a different testing system. This is such elementary testing, and I seem to be playing whack-a-mole.