【发布时间】: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