Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 functional test prints out redirect html and stops test execution

Tags:

I'm struggling to understand what is wrong with my functional testing or project settings: the phpunit execution just prints out the following information (I'm not printing this out in the test suite - i.e. it doesn't come from the client->getResponse() printing or anything). Additionally the whole test execution stops immediately without any result information after this text is printed out to 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 command line:

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

My test code is rather 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, "[email protected]", "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);
    }

    //....
}

Additionally the exactly same test case is working fine on another project that I'm working on so I've been trying to dig out the differences in the project configurations etc without luck.

Any help is greatly appreciated - feel free to ask for some other code / configuration file contents if that might be helpful/relevant.

using symfony 2.3.12 & phpunit 4.1.0

UPDATED: specific code chain that leads to the error

So after managing to work around this problem by solving the underlying project session issues couple of days ago, I returned to debug this issue still a bit further. And currently it seems that the result is due to the following code chain, first calling forward:

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

and in the forwarded controller action calling redirect:

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

obviously this controller flow seems a bit strange in general, but the question still remains why this leads into the observed result?

like image 633
ejuhjav Avatar asked Apr 26 '16 11:04

ejuhjav


2 Answers

I had this problem when logging-in in a functional test (official doc), when i was executing the second time an $client->request(...). Separate those single tests in own test classes did not solve the problem.

I solved this by not setting a cookie. Luckily my tests was not depending on a cookie, so all the tests passed than.

Maybe this information would help you, to more isolate your issue.

like image 182
fabpico Avatar answered Sep 28 '22 04:09

fabpico


You can try to add some checks in 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;
}
like image 33
A.L Avatar answered Sep 28 '22 03:09

A.L