单元测试认证zend2

时间:2015-06-15 07:51:26

标签: unit-testing zend-framework2 phpunit

我正在为zend2身份验证编写单元测试。我正在使用Doctrine2。

我的测试用例是:登录方法应该在传递正确的凭据时返回True。

登录正在进行中。但是当我运行单元测试用例时,我发现了以下错误:

“PHP致命错误:在.....等中调用未定义的方法Mock_Adapter_8d9bbc77 :: setIdentityValue()”。

我尝试了很多,但没有找到任何解决方案。非常感谢您的帮助。

我在服务中进行身份验证的实际代码:

$authService = $this->getServiceLocator()->get('Zend\Authentication\AuthenticationService');

$adapter = $authService->getAdapter();
$adapter->setIdentityValue($data->login);//error line
$adapter->setCredentialValue($data->password);

我的测试用例:

public function LoginMethodShouldReturnTrueOnPassingCorrectCredentials() {

 $this->loginForm = new Login;
  $this->loginForm->init();
  $this->loginForm->get('email')->setValue('test@test.com');
  $this->loginForm->get('password')->setValue('gshafdsadf123123');
  $this->loginForm->get('loginSubmit')->setValue('Login');

  $serviceLocator = Bootstrap::getServiceManager();

  $entityManager = $serviceLocator->get('doctrine.entitymanager.orm_default');

  $entityRepositoryMock = $this->getMockBuilder('Ease\EntityRepository\User')
  ->disableOriginalConstructor()
  ->getMock();

  $entityManager->method("getRepository")
  ->willReturn($entityRepositoryMock);

  $adapterMock = $this->getMockBuilder('Zend\Authentication\Adapter\Adapter')
  ->disableOriginalConstructor()
  ->getMock();
  $adapterMock->expects($this->once())->method('setIdentityValue')->with('test@test.com');
  $adapterMock->expects($this->once())->method('setCredentialValue')->with('gshafdsadf123123');

  $authMock = $this->getMockBuilder('Zend\Authentication\AuthenticationService')
  ->disableOriginalConstructor()
  ->getMock();
  $authMock->expects($this->once())->method('getAdapter')->willReturn($adapterMock);

  $serviceLocator->setService('Zend\Authentication\AuthenticationService', $authMock);


  $serviceLocator->setService('doctrine.entitymanager.orm_default', $entityManager);

  $userServiceInstance = $this->getUserService();
  $returnValue = $userServiceInstance->handleLoginForm($this->loginForm);
  $this->assertSame($returnValue, true);
  }

0 个答案:

没有答案