【问题标题】:Symfony2 functional test prints out redirect html and stops test executionSymfony2 功能测试打印出重定向 html 并停止测试执行
【发布时间】:2016-08-20 05:14:33
【问题描述】:

我正在努力理解我的功能测试或项目设置有什么问题:phpunit 执行只打印出以下信息(我没有在测试套件中打印出来 - 即它不是来自client->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

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

因此,在几天前通过解决底层项目会话问题设法解决了这个问题之后,我又进一步调试了这个问题。而目前看来结果是由于下面的代码链,先调用forward:

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

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

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

显然这个控制器流程总体上看起来有点奇怪,但问题仍然是为什么这会导致观察到的结果?

【问题讨论】:

  • 断言出现在以下行永远不会到达的评论之后。我也会将这些添加到问题中
  • 您是在使用默认 Web 服务器还是使用 apache 进行检查?
  • @GaneshPatil 我正在使用 phpunit 从命令行运行它(更新了问题)
  • 能否请您在 $crawler = $client->request('GET', '/'); 之后添加断言这段代码也是如此。只需检查您收到的响应。
  • 完全相同的测试用例在另一个项目上运行良好这些事情可能不同:Composer 安装的依赖项版本不同,服务器版本或配置不同。

标签: php symfony phpunit functional-testing


【解决方案1】:

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

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

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

【讨论】:

  • 感谢您的回答:几天前,我设法解决了这个问题,通过使用请求中的会话和 php 标准会话功能来整理项目中完成的会话管理,并愉快地混合使用(如 session_id() - 在测试环境中没有访问相同的会话)。我现在回到这个原始问题,并更详细地指出了罪魁祸首;更新了我的问题,因为我仍然不知道导致此结果的确切原因是什么。
  • 我有同样的问题,我认为这是由于我在重定向期间测试会话 flashbag 造成的。执行很好,测试通过,但仍然有丑陋的 html 输出。
【解决方案2】:

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

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

【讨论】:

  • 感谢您的努力,但由此产生的行为保持不变。 $client->submit($loginForm); 的调用仍然打印出相同的消息并中断测试执行。
  • @ejuhjav :如果我们评论 $crawler = $client->submit($loginForm);这段代码,它是否为所有剩余的断言提供了正确的结果?
  • @GaneshPatil $crawler 不会被定义,它会抛出一个 PHP 错误。
  • @GaneshPatil 删除该行时,上面的答案代码将在最后一次断言检查“登录成功”时失败,正常 phpunit 断言失败($crawler 作为函数参数传递,所以它不会' t 抛出一个 php 错误)
【解决方案3】:

我的测试代码有这个问题

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多