eju*_*jav 5 php phpunit functional-testing symfony
我很难理解我的功能测试或项目设置有什么问题: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>
Run Code Online (Sandbox Code Playgroud)
从命令行运行phpunit后:
phpunit -c app --group temp1 src/AppBundle/Tests/Controller/SecurityControllerTest.php
Run Code Online (Sandbox Code Playgroud)
我的测试代码很简单:
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);
}
//....
}
Run Code Online (Sandbox Code Playgroud)
另外,完全相同的测试用例在我正在处理的另一个项目上工作正常,所以我一直试图挖掘项目配置中的差异等,没有运气.
非常感谢任何帮助 - 如果可能有用/相关,请随意询问其他一些代码/配置文件内容.
使用symfony 2.3.12和phpunit 4.1.0
更新:导致错误的特定代码链
因此,在通过几天前解决基础项目会话问题来解决此问题之后,我又回到调试这个问题了.目前似乎结果是由于以下代码链,首先调用前进:
$this->forward('bundle:controller:action')->send();
Run Code Online (Sandbox Code Playgroud)
并在转发的控制器操作中调用重定向:
$this->redirect($this->generateUrl($route, $parameters))->send();
Run Code Online (Sandbox Code Playgroud)
显然这个控制器流程一般看起来有点奇怪,但问题仍然是为什么这导致了观察结果?
您可以尝试在登录功能中添加一些检查:
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
990 次 |
| 最近记录: |