Symfony2 function test prints html redirect and stops test execution

I am trying to understand what is wrong with my functional testing or project settings: running phpunit just prints the following information (I do not print this in the test suite, i.e. it is not from client-> getResponse () or anything else). In addition, the entire test immediately stops without any information about the results after this text is printed on the command line:

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="refresh" content="1;url=/" /> <title>Redirecting to /</title> </head> <body> Redirecting to <a href="/">/</a>. </body> </html> 

After running phpunit from the command line:

 phpunit -c app --group temp1 src/AppBundle/Tests/Controller/SecurityControllerTest.php 

My test code is pretty simple:

 class SecurityControllerTest extends WebTestCase { /** * test login with succesfull case * * @group login * @group temp1 */ public function testLogin_success() { $client = static::createClient(); $crawler = $client->request('GET', '/'); // temp - just to test that the initial crawler location is correct $this->assertGreaterThan(0, $crawler->filter('html:contains("System Login")')->count(), "login page not found"); $client->followRedirects(true); $crawler = $this->loginSubmit($client, $crawler, " basic_user1@email.com ", "basic_user1_password"); // THE BELOW ROWS ARE NEVER REACHED // test that the user can access the user home $crawler = $client->request('GET', '/user/myprofile'); $this->assertGreaterThan(0, $crawler->filter('html:contains("Welcome to your user profile")')->count(), "User homepage not accessible after login"); } private function loginSubmit($client, $crawler, $username, $password) { $loginButton = $crawler->selectButton('Login'); $loginForm = $loginButton->form(); $loginForm['form[email]'] = $username; $loginForm['form[password]'] = $password; // BELOW IS THE ROW THAT RESULTS IN THE PRINTOUT + TEST EXECUTION STOP return $client->submit($loginForm); } //.... } 

In addition, the exact same test script works fine in another project that I am working on, so I try to dig up differences in project configurations, etc. no luck.

Any help is greatly appreciated - feel free to ask for some other code / contents of the configuration file if this may be useful / relevant.

using symfony 2.3.12 and phpunit 4.1.0

UPDATED: specific code chain that leads to error

So, after you decided to work around this problem by solving a couple of problems associated with the project a couple of days ago, I returned to debug this problem a little more. And at present it seems that the result is due to the following code chain, first calling forward:

 $this->forward('bundle:controller:action')->send(); 

and in a redirected call to the redirected controller:

 $this->redirect($this->generateUrl($route, $parameters))->send(); 

Obviously, this regulator flow seems a little strange in general, but the question remains, why does this lead to the observed result?

+5
source share
3 answers

I had this problem when entering a functional test ( official document ), when I ran $client->request(...) second time, Some of those single tests in my own test classes do not solve the problem.

I solved this with no cookie setting. Fortunately, my tests did not depend on the cookie, so all the tests passed, than.

Perhaps this information will help you to isolate your problem more.

+2
source

You can try adding some checks to the login function:

 private function loginSubmit($client, $crawler, $username, $password) { // Check that the HTTP status is good. $this->assertEquals(200, $client->getResponse()->getStatusCode()); // Check that there is a form. $this->assertEquals(1, $crawler->filter('form')->count()); $loginButton = $crawler->selectButton('Login'); $loginForm = $loginButton->form(); $loginForm['form[email]'] = $username; $loginForm['form[password]'] = $password; // BELOW IS THE ROW THAT RESULTS IN THE PRINTOUT + TEST EXECUTION STOP $crawler = $client->submit($loginForm); // Check that the HTTP status is good. $this->assertEquals(200, $client->getResponse()->getStatusCode()); // Check that the login is successful, it depends on your code. // For example if you show a message "Logged in succesfully." in a <div>: $this->assertEquals(1, $crawler->filter('div:contains("Logged in succesfully.")')->count()); // If nothing works, display the rendered HTML to see if there is an error: // die($this->client->getResponse()->getContent()); return $crawler; } 
+1
source

I had this problem with test code

  $this->assertTrue($client->getResponse()->isSuccessful()); 

I used an array of relative URLs through this statement. One of the URLs was not successful, and my application was redirected to this case. Instead of rejecting the statement, phpunit spills out the RedirectResponse html code and dies.

+1
source

Source: https://habr.com/ru/post/1247937/


All Articles