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

时间:2016-04-26 11:38:42

标签: php symfony phpunit functional-testing

我很难理解我的功能测试或项目设置有什么问题:phpunit执行只打印出以下信息(我不会在测试套件中打印出来 - 即它没有#&# 39;来自客户端 - > 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&amp; phpunit 4.1.0

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

因此,在通过几天前解决基础项目会话问题来解决此问题之后,我又回到调试这个问题了。目前似乎结果是由于以下代码链,首先调用前进:

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

并在转发的控制器操作中调用redirect:

$this->redirect($this->generateUrl($route, $parameters))->send();
显然这个控制器流程看起来有点奇怪,但问题仍然存在为什么会导致观察到的结果呢?

3 个答案:

答案 0 :(得分:2)

我在功能测试(official doc)中登录时遇到此问题,当我第二次执行$client->request(...)时。将自己的测试类中的单个测试分开并没有解决问题。

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

也许这些信息对您有所帮助,更能隔离您的问题。

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

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

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

我通过这个断言运行了一系列相对url。其中一个网址没有成功,我的应用程序有一个重定向的情况。 phpunit没有使断言失败,而是吐出RedirectResponse html代码并死掉。