我正在尝试测试我的登录控制器,这是我到目前为止所写的:
public function testValidUserIsredirectedToIndex()
{
$this->dispatch('/index/logincra');
$this->resetResponse();
$this->request->setPost(array(
'login' => 'aymeric',
'password' => 'toto2016',
));
$this->request->setMethod('POST');
$this->assertRedirectTo('/index');
}
/index/logincra是我要测试的url, index是控制器的名称logincra是操作的名称
public function logincraAction()
{
if($this->getRequest()->isPost()){
$data = $this->getRequest()->getPost(); // Recupere les posts
if(!empty($data['login']) && !empty($data['password'])){
$oLdap = new Mediagong_Ldap_Connect($data['login'], $data['password']);
$oLdap->setLogin($data['login']);
$oLdap->setPassword($data['password']);
$oLdap->getUserInfos();
if($oLdap->isLoggin()){
$user = User::getUserByLogin($oLdap->getUserName());
if(!empty($user)){
if($user->is_cra){
$frontLogin = new Zend_Session_Namespace('front');
$frontLogin->user = $user->id_user;
$frontLogin->setExpirationSeconds(1800);
$this->_redirect('/index/');
}else{
$this->view->information = 'Vous n''êtes pas autorisé à accéder à cette interface';
}
}else{
$this->view->information = 'Compte CRA inactif, veuillez vous retourner vers Damien...';
}
}else{
$this->view->information = 'Mauvais login ou mot de passe';
}
}else{
$this->view->information = 'Veuillez saisir tous les champs';
}
}
}
我想测试用户是否被正确转发到/index url,如果登录名和密码是正确的(例如aymeric/toto2016)。
到目前为止,我有错误:
断言响应重定向到"/index"失败
提前感谢您的帮助
请查看Zend_test参考。我想这会对你有帮助。
你没有得到一个错误…您的测试失败,因为页面没有按您的意图重定向。
如何重定向
参见此问题Zend Framework url redirect
根据Zend手册:
由于重定向操作帮助器插件的性质,它会发出一个重定向,然后退出。因为你不能测试an的一部分应用程序发出退出调用Zend_Test_PHPUnit_ControllerTestCase自动禁用重定向器的出口部分,因为它可能导致测试行为要与实际应用程序不同。确保你的控制器可以正常测试,请使用重定向器当您需要将用户重定向到另一个页面时:
$this->_redirect('/index/');
行给您带来了问题。