0
votes

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 &quot;/home1/xxxxx-app/vendor/phpunit/phpunit/src/Util/Printer.php&quot; 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.

1
I'm not sure of the reason that pushed you to get 3 separated tests. One single test is enough to test your login. - Massimiliano Arione
My intent, after testing login, is to continue to test using the client with the validated login session. I could probably do all of the testing in one massive testTheWorld function, but I'd like to split it into smaller functions/tests so that others can understand what's going on. - MgFrobozz
And I absolutely agree that splitting into smaller tests is actually a good thing. But I think that this is not the case. You're testing a single functionality: the login. There's no need to split it in three steps, being such steps just a part of the whole. I wrote many many tests like this one before, and also I've seen tests written by other devs, so you can trust me: go ahead with one test. - Massimiliano Arione
Two thoughts did you look at using the "insulate" command at all? Second, regarding the last portion of the question regarding the simulate authentication, I literally just did that this weekend and got it working. One is the user you use needs to be in your database if you are using DB authentication, two the name of the firewall in the second reference needs to match the name of your main firewall, so make sure it is not left as "your_firewall_name:" - MEmerson

1 Answers

0
votes

I found something that seems to work, but it may not be "symfony way" ...

  • Do not set @runTestsInSeparateProcesses for the test class.

  • Merge the login/login_check into a single test (as suggested by Massimiliano), the first test in the class, with "@runInSeparateProcess" set. Then $client = static::createClient(); $context["client"] = $client; return $context;

  • In all follow-up tests, $client = $context["client"]; on entry, and return $context. Do not set @runInSeparateProcess for the remaining tests. Include as many testXxx functions as desired using this approach. The client is already logged in, so there's no issue with permissions.

  • I had tried adding a security/firewall/test/http_basic rule to app/config/security.yml, but it didn't work for the way my system was doing authorization, and all the follow-up tests failed with permission issues.

  • The "Class __PHP_Incomplete_Class has no unserializer" exception is still a problem when the test is run as "bin/phpunit -c app src/BundleXxx/Tests/Controller/TheTest.php", but does not occur when it's run as part of "bin/phpunit -c app". I can live with that, and I've added documentation to that effect.