Skip to content
This repository has been archived by the owner on Jul 3, 2020. It is now read-only.

Commit

Permalink
Merge pull request #374 from ashatrov/373_Cannot_get_a_name_and_param…
Browse files Browse the repository at this point in the history
…eters_for_RoleProviderPluginManager

Cannot get a name and parameters for RoleProviderPluginManager #373
  • Loading branch information
prolic authored May 7, 2017
2 parents 2ce24f1 + 6dea80b commit dd1857a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/ZfcRbac/Factory/RoleServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
$pluginManager = $container->get('ZfcRbac\Role\RoleProviderPluginManager');

/* @var \ZfcRbac\Role\RoleProviderInterface $roleProvider */
reset($roleProviderConfig);
$roleProvider = $pluginManager->get(key($roleProviderConfig), current($roleProviderConfig));

/* @var \Rbac\Traversal\Strategy\TraversalStrategyInterface $traversalStrategy */
Expand Down
90 changes: 78 additions & 12 deletions tests/ZfcRbacTest/Factory/RoleServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,95 @@ public function testFactory()
]
]);

$serviceManager = new ServiceManager();
$serviceManager->setService('ZfcRbac\Options\ModuleOptions', $options);
$serviceManager->setService('ZfcRbac\Role\RoleProviderPluginManager', new RoleProviderPluginManager($serviceManager));
$serviceManager->setService(
'ZfcRbac\Identity\AuthenticationProvider',
$this->getMock('ZfcRbac\Identity\IdentityProviderInterface')
);

$traversalStrategy = $this->getMock('Rbac\Traversal\Strategy\TraversalStrategyInterface');
$rbac = $this->getMock('Rbac\Rbac', [], [], '', false);
$roleProvider = $this->getMock('\ZfcRbac\Role\RoleProviderInterface');

$rbac->expects($this->once())->method('getTraversalStrategy')->will($this->returnValue($traversalStrategy));
$rbac = $this
->getMockBuilder('Rbac\Rbac')
->disableOriginalConstructor()
->getMock();
$rbac->expects($this->once())
->method('getTraversalStrategy')
->will($this->returnValue(
$traversalStrategy
));

$pluginManager = $this
->getMockBuilder('\ZfcRbac\Role\RoleProviderPluginManager')
->disableOriginalConstructor()
->getMock();
$pluginManager->expects($this->once())
->method('get')
->with('ZfcRbac\Role\InMemoryRoleProvider', ['foo'])
->will($this->returnValue(
$roleProvider
));

$serviceManager = new ServiceManager();
$serviceManager->setService('ZfcRbac\Options\ModuleOptions', $options);
$serviceManager->setService('Rbac\Rbac', $rbac);
$serviceManager->setService('ZfcRbac\Role\RoleProviderPluginManager', $pluginManager);
$serviceManager->setService('ZfcRbac\Identity\AuthenticationProvider', $this->getMock('ZfcRbac\Identity\IdentityProviderInterface'));

$factory = new RoleServiceFactory();
$factory = new RoleServiceFactory();
$roleService = $factory->createService($serviceManager);

$this->assertInstanceOf('ZfcRbac\Service\RoleService', $roleService);
$this->assertEquals('guest', $roleService->getGuestRole());
$this->assertAttributeSame($traversalStrategy, 'traversalStrategy', $roleService);
}

public function testIfRoleArrayPointerBeyondArrayEnd()
{
$options = new ModuleOptions([
'identity_provider' => 'ZfcRbac\Identity\AuthenticationProvider',
'guest_role' => 'guest',
'role_provider' => [
'ZfcRbac\Role\InMemoryRoleProvider' => [
'foo'
]
]
]);

// Simulate if array pointer beyond end of array. E.g after 'while(next($roleProvider)) { //do }'
$roleProvider = $options->getRoleProvider();
next($roleProvider);
$options->setRoleProvider($roleProvider);

$traversalStrategy = $this->getMock('Rbac\Traversal\Strategy\TraversalStrategyInterface');
$roleProvider = $this->getMock('\ZfcRbac\Role\RoleProviderInterface');

$rbac = $this
->getMockBuilder('Rbac\Rbac')
->disableOriginalConstructor()
->getMock();
$rbac->expects($this->once())
->method('getTraversalStrategy')
->will($this->returnValue(
$traversalStrategy
));

$pluginManager = $this
->getMockBuilder('\ZfcRbac\Role\RoleProviderPluginManager')
->disableOriginalConstructor()
->getMock();
$pluginManager->expects($this->once())
->method('get')
->with('ZfcRbac\Role\InMemoryRoleProvider', ['foo'])
->will($this->returnValue(
$roleProvider
));

$serviceManager = new ServiceManager();
$serviceManager->setService('ZfcRbac\Options\ModuleOptions', $options);
$serviceManager->setService('Rbac\Rbac', $rbac);
$serviceManager->setService('ZfcRbac\Role\RoleProviderPluginManager', $pluginManager);
$serviceManager->setService('ZfcRbac\Identity\AuthenticationProvider', $this->getMock('ZfcRbac\Identity\IdentityProviderInterface'));

$factory = new RoleServiceFactory();
$factory->createService($serviceManager);
}

public function testThrowExceptionIfNoRoleProvider()
{
$this->setExpectedException('ZfcRbac\Exception\RuntimeException');
Expand All @@ -81,6 +147,6 @@ public function testThrowExceptionIfNoRoleProvider()
);

$factory = new RoleServiceFactory();
$roleService = $factory->createService($serviceManager);
$factory->createService($serviceManager);
}
}

0 comments on commit dd1857a

Please sign in to comment.