2016-08-22 76 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); 
}