Вопрос

I have developed simple authentification using this tutorial http://samsonasik.wordpress.com/2012/10/23/zend-framework-2-create-login-authentication-using-authenticationservice-with-rememberme/. Everything works fine, but now I have unit testing issues.

To check if user is authentified I am using:

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();
            }
        });
}

And my mock code:

    $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);

My issue is that mocks hasIdentity is not being called during unit test. What I have Did wrong.

Это было полезно?

Решение

The problem was in bootstrap. onBootstrap is being called before mocking. So get('AuthService') needs to be called in event handler. Here is working bootstrap example:

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();
            }
        });
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top