2014-11-13 13 views
0

다음과 같이하십시오. Unit Testing a Zend Framework 2 application ZfcUser에서 작업하고 컨트롤러에서 일부 작업을 테스트 해 봅니다. 하지만 테스트가 실행될 때 내보기에서 사용자 이름을 얻으려고 $this->zfcUserIdentity()을 사용하기 때문에 오류가 발생합니다.ZfcUserIdentity Helper를 사용하여 Zend Framework 2 및 ZfcUser에서 작업을 테스트하는 방법

이 테스트를 수행하는 방법은 무엇입니까? 나는이 방법을 조롱하는 것을 생각하지만, 어떻게 해야할지 모른다.

답변

0

당신은 ZfcUser 테스트에서 좀 걸릴 수 있습니다 : https://github.com/ZF-Commons/ZfcUser/blob/master/tests/ZfcUserTest/Controller/UserControllerTest.php#L61-L279

Basicly을 당신이 ZfcUserIdentity 컨트롤러 플러그인을 조롱하고 모의를 사용하는 관리자 플러그인 컨트롤러를 말해야한다.

+0

덕분에 Danielss89 @ 나는 ZfcUserIdentity 컨트롤러 플러그인 조롱하지만 난 방법을 완벽하게 이해할 수 없다 코드를 살펴보고 AuthenticationService를 조롱하고 getIdentity 메소드에서 user 엔티티를 기대합니다. 완벽하게 작동하십시오. –

0

나는이 같은 것을 사용 내 AbstractHttpControllerTestCase :

/** 
* @param array $roles e.g. ['admin'] 
*/ 
protected function login($roles) { 
    $userMock = $this->getMock('Application\Entity\User', [], [], '', false); 
    $userMock->expects($this->any()) 
    ->method('getRoles')->will($this->returnValue($roles)); 

    $storageMock = $this->getMock('\Zend\Authentication\Storage\NonPersistent'); 
    $storageMock->expects($this->any()) 
    ->method('isEmpty')->will($this->returnValue(false)); 
    $storageMock->expects($this->any()) 
    ->method('read')->will($this->returnValue($userMock)); 

    $sm = $this->getApplicationServiceLocator(); 
    $auth = $sm->get('Zend\Authentication\AuthenticationService'); 
    $auth->setStorage($storageMock); 
} 

그런 다음 내 테스트 자체에 :

$this->login(['admin']); 
$this->dispatch($url); 
+0

ZfcRbac (사용 권한)을 사용하고 있습니다. 만약 당신이'getRoles'를 필요로하지 않는다면'$ userMock'을 더 많이 사용하는 것에 집중할 것입니다. –