2016-08-22 121 views
2

我的单元测试运行在我的IndexController类中时出现问题。ZF3单元测试身份验证onBootstrap

单元测试只是做以下(从unit-test tutorial of zf3启发):

IndexControllerTest.php

public function testIndexActionCanBeAccessed() 
{ 
    $this->dispatch('/', 'GET'); 
    $this->assertResponseStatusCode(200); 
    $this->assertModuleName('main'); 
    $this->assertControllerName(IndexController::class); // as specified in router's controller name alias 
    $this->assertControllerClass('IndexController'); 
    $this->assertMatchedRouteName('main'); 
} 

Module.php我有一些功能,以检查是否有用户登录,否则他将被重定向到login路线。

Module.php

public function onBootstrap(MvcEvent $mvcEvent) 
{ 
    /** @var AuthService $authService */ 
    $authService = $mvcEvent->getApplication()->getServiceManager()->get(AuthService::class); 
    $this->auth = $authService->getAuth(); // returns the Zend AuthenticationService object 

    // store user and role in global viewmodel 
    if ($this->auth->hasIdentity()) { 
     $curUser = $this->auth->getIdentity(); 
     $mvcEvent->getViewModel()->setVariable('curUser', $curUser['system_name']); 
     $mvcEvent->getViewModel()->setVariable('role', $curUser['role']); 
     $mvcEvent->getApplication()->getEventManager()->attach(MvcEvent::EVENT_ROUTE, [$this, 'checkPermission']); 
    } else { 
     $mvcEvent->getApplication()->getEventManager()->attach(MvcEvent::EVENT_DISPATCH, [$this, 'authRedirect'], 1000); 
    } 
} 

checkPermission方法只是检查,如果用户角色和与之匹配的路由在ACL存储。 如果失败我将重定向器404

问题的状态码:该单元测试失败:“无法断言响应代码‘200’,实际状态代码是‘302’ 因此,单元测试跳入。从我在Module.phponBootstrap方法其他情况下发生重定向

我没有在TestCase的以下setUp,但它不工作:

​​3210 个

提示非常赞赏

的代码可以从Zend框架2不同了一点,但如果你在ZF2一个简单的工作示例也许我可以将其转换ZF3风格。

我不使用ZfcUser - 只是在Zend的ACL/Zend的认证东西

回答

3

后头痛我有一个有效的解决方案的数天。

首先,我将onBootstrap中的所有代码移动到了Listener中,因为phpunit mocks是在zf引导之后生成的,因此在我的单元测试中不存在。

关键是,服务是在我的可调用侦听器方法中生成的,它在zf完成自举之后调用。 然后PHPUnit可以用提供的模拟覆盖服务。

AuthenticationListener

class AuthenticationListener implements ListenerAggregateInterface 
{ 
use ListenerAggregateTrait; 

/** 
* @var AuthenticationService 
*/ 
private $auth; 

/** 
* @var Acl 
*/ 
private $acl; 

/** 
* Attach one or more listeners 
* 
* Implementors may add an optional $priority argument; the EventManager 
* implementation will pass this to the aggregate. 
* 
* @param EventManagerInterface $events 
* @param int $priority 
* 
* @return void 
*/ 
public function attach(EventManagerInterface $events, $priority = 1) 
{ 
    $this->listeners[] = $events->attach(MvcEvent::EVENT_ROUTE, [$this, 'checkAuthentication']); 
} 

/** 
* @param MvcEvent $event 
*/ 
public function checkAuthentication($event) 
{ 
    $this->auth = $event->getApplication()->getServiceManager()->get(AuthenticationService::class); 
    $aclService = $event->getApplication()->getServiceManager()->get(AclService::class); 
    $this->acl = $aclService->init(); 

    $event->getViewModel()->setVariable('acl', $this->acl); 
    if ($this->auth->hasIdentity()) { 
     $this->checkPermission($event); 
    } else { 
     $this->authRedirect($event); 
    } 
} 

// checkPermission & authRedirect method 
} 

现在我onBootstrap得到了非常小的,就像ZF想它。documentation reference

Module.php

public function onBootstrap(MvcEvent $event) 
{ 
    $authListener = new AuthenticationListener(); 
    $authListener->attach($event->getApplication()->getEventManager()); 
} 

最后我在单元测试嘲讽看起来是这样的:

IndexControllerTest

private function authMock() 
{ 
    $mockAuth = $this->getMockBuilder(AuthenticationService::class)->disableOriginalConstructor()->getMock(); 
    $mockAuth->expects($this->any())->method('hasIdentity')->willReturn(true); 
    $mockAuth->expects($this->any())->method('getIdentity')->willReturn(['id' => 1, 'systemName' => 'admin', 'role' => 'Admin']); 

    $this->getApplicationServiceLocator()->setAllowOverride(true); 
    $this->getApplicationServiceLocator()->setService(AuthenticationService::class, $mockAuth); 
    $this->getApplicationServiceLocator()->setAllowOverride(false); 
}