Authentication ZF2模拟身份验证服务

Authentication ZF2模拟身份验证服务,authentication,zend-framework2,phpunit,Authentication,Zend Framework2,Phpunit,我使用本教程开发了简单的身份验证。一切正常,但现在我遇到了单元测试问题 要检查用户是否经过身份验证,我正在使用: public function onBootstrap(MvcEvent $e) { $auth = $e->getApplication()->getServiceManager()->get('AuthService'); $e->getTarget()->getEventManager()->getSharedManager

我使用本教程开发了简单的身份验证。一切正常,但现在我遇到了单元测试问题

要检查用户是否经过身份验证,我正在使用:

public function onBootstrap(MvcEvent $e)
{
    $auth = $e->getApplication()->getServiceManager()->get('AuthService');

    $e->getTarget()->getEventManager()->getSharedManager()
        ->attach('Admin', \Zend\Mvc\MvcEvent::EVENT_DISPATCH,
                 function($e) use ($auth) {
            $currentRouteName = $e->getRouteMatch()->getMatchedRouteName();
            $allowed = array(
                'admin/login',
                'admin/',
            );
            if (in_array($currentRouteName, $allowed)) {
                return;
            }
            if (!$auth->hasIdentity()) {
                $url = $e->getRouter()->assemble(array(),
                                                 array('name' => 'admin/login'));
                $response = $e->getResponse();
                $response->getHeaders()->addHeaderLine('Location', $url);
                $response->setStatusCode(302);
                $response->sendHeaders();
            }
        });
}
还有我的模拟代码:

    $authMock = $this->getMock('Zend\Authentication\AuthenticationService');

    $authMock->expects($this->once())
        ->method('hasIdentity')
        ->will($this->returnValue(true));

    $serviceManager = $this->getApplicationServiceLocator();
    $serviceManager->setAllowOverride(true);
    $serviceManager->setService('AuthService', $authMock);

我的问题是在单元测试期间没有调用mock
hasIdentity
。我做错了什么。

问题出在引导中。在模拟之前调用onBootstrap。因此,需要在事件处理程序中调用
get('AuthService')
。以下是工作引导示例:

public function onBootstrap(MvcEvent $e)
{
    $sm = $e->getApplication()->getServiceManager();
    $e->getTarget()->getEventManager()->getSharedManager()
        ->attach('Admin', \Zend\Mvc\MvcEvent::EVENT_DISPATCH,
                 function($e) use ($sm) {
            $auth = $sm->get('AuthService');
            $currentRouteName = $e->getRouteMatch()->getMatchedRouteName();
            $allowed = array(
                'admin/login',
                'admin/',
            );
            if (in_array($currentRouteName, $allowed)) {
                return;
            }
            if (!$auth->hasIdentity()) {
                $url = $e->getRouter()->assemble(array(),
                                                 array('name' => 'admin/login'));
                $response = $e->getResponse();
                $response->getHeaders()->addHeaderLine('Location', $url);
                $response->setStatusCode(302);
                $response->sendHeaders();
            }
        });
}

您如何知道没有调用您的hasIdentity()?请确保在将模拟设置为服务后,ServiceManager在测试中返回模拟。@TimFountain
$authMock->expects($this->once())
fails@gontrollez是的。我尝试了
$serviceManager=$this->getApplicationServiceLocator()$serviceManager->get('AuthService')->hasIdentity()在设置mock之后,调用了
hasIdentity