public function testReturnGuestRoleIfNoIdentityIsFound() { $identityProvider = $this->getMock('ZfjRbac\\Identity\\IdentityProviderInterface'); $identityProvider->expects($this->any())->method('getIdentity')->will($this->returnValue(null)); $roleService = new RoleService($identityProvider, new InMemoryRoleProvider([]), $this->getMock('Rbac\\Traversal\\Strategy\\TraversalStrategyInterface')); $roleService->setGuestRole('guest'); $result = $roleService->getIdentityRoles(); $this->assertEquals('guest', $roleService->getGuestRole()); $this->assertCount(1, $result); $this->assertInstanceOf('Rbac\\Role\\RoleInterface', $result[0]); $this->assertEquals('guest', $result[0]->getName()); }
/** * Create an object * * @param ContainerInterface $container * @param string $requestedName * @param null|array $options * @return object * @throws ServiceNotFoundException if unable to resolve the service. * @throws ServiceNotCreatedException if an exception is raised when * creating a service. * @throws ContainerException if any other error occurs */ public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { /* @var \ZfjRbac\Options\ModuleOptions $moduleOptions */ $moduleOptions = $container->get('ZfjRbac\\Options\\ModuleOptions'); /* @var \ZfjRbac\Identity\IdentityProviderInterface $identityProvider */ $identityProvider = $container->get($moduleOptions->getIdentityProvider()); $roleProviderConfig = $moduleOptions->getRoleProvider(); if (empty($roleProviderConfig)) { throw new RuntimeException('No role provider has been set for ZfjRbac'); } /* @var \ZfjRbac\Role\RoleProviderPluginManager $pluginManager */ $pluginManager = $container->get('ZfjRbac\\Role\\RoleProviderPluginManager'); /* @var \ZfjRbac\Role\RoleProviderInterface $roleProvider */ $roleProvider = $pluginManager->get(key($roleProviderConfig), current($roleProviderConfig)); $roleService = new RoleService($identityProvider, $roleProvider); $roleService->setGuestRole($moduleOptions->getGuestRole()); return $roleService; }