Symfony2功能测试打印出重定向html并停止测试执行


Symfony2 functional test prints out redirect html and stops test execution

我正在努力理解我的功能测试或项目设置出了什么问题:phpunit 执行只是打印出以下信息(我没有在测试套件中打印出来 - 即它不是来自客户端>getResponse() 打印或其他任何东西)。此外,在将文本打印到命令行后,整个测试执行会立即停止,没有任何结果信息:

<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>

从命令行运行 phpunit 后:

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

我的测试代码相当简单:

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);
    }
    //....
}

此外,完全相同的测试用例在我正在处理的另一个项目上运行良好,因此我一直试图挖掘出项目配置等方面的差异,但没有运气。

非常感谢任何帮助 - 如果可能有帮助/相关,请随时询问其他代码/配置文件内容。

使用 Symfony 2.3.12 和 phpunit 4.1.0

已更新:导致错误的特定代码链

因此,在几天前设法通过解决基础项目会话问题来解决此问题之后,我又回来进一步调试此问题。目前看来,结果是由于以下代码链,首先向前调用:

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

在转发的控制器操作调用重定向中:

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

显然,这个控制器流总体上似乎有点奇怪,但问题仍然存在,为什么这会导致观察到的结果?

我在

功能测试(官方文档)中登录时遇到了这个问题,当我第二次执行$client->request(...)时。在自己的测试类中分离这些单个测试并不能解决问题。

我通过设置 cookie 解决了这个问题。幸运的是,我的测试不依赖于cookie,所以所有的测试都通过了。

也许这些信息会帮助你,更好地隔离你的问题。

您可以尝试在登录功能中添加一些检查:

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;
}

我在测试代码中遇到了这个问题

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

我通过这个断言运行了一个相对 url 数组。 其中一个 url 不成功,我的应用程序针对这种情况进行了重定向。 phpunit 没有失败断言,而是吐出 RedirectResponse html 代码并死亡。