Zend Framework 2 php单元测试模拟具有acl权限的Identity对象

时间:2014-03-07 12:37:14

标签: php unit-testing mocking zend-framework2 acl

如何测试acl中的控制器。

Zend\ServiceManager\Exception\ServiceNotCreatedException: An exception was raised while creating "acl"; no instance returned

我的嘲笑代码

protected function mockLogin()
{
    $authService = $this->getMock('Zend\Authentication\AuthenticationService');

    $authService->expects($this->any())
        ->method('getIdentity')
        ->will($this->returnValue(1));

    $authService->expects($this->any())
        ->method('hasIdentity')
        ->will($this->returnValue(true));

    $authService->expects($this->any())
        ->method('getRole')
        ->will($this->returnValue(2));
    $this->getApplicationServiceLocator()->setAllowOverride(true);
    $this->getApplicationServiceLocator()->setService('Zend\Authentication\AuthenticationService',
        $authService);
}

public function testIndexActionCanBeAccessed()
{
    $this->mockLogin();
    $this->dispatch('/');
    $this->assertResponseStatusCode(200);
    $this->assertModuleName('Application');
    $this->assertControllerName('Application\Controller\Index');
    $this->assertControllerClass('IndexController');
    $this->assertMatchedRouteName('application');
}

0 个答案:

没有答案