I use something like this in my 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);
}
Then in my test itself:
$this->login(['admin']);
$this->dispatch($url);