From 2e7186cb6cf880b69d6a43bc25059977c557a970 Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Wed, 21 Feb 2018 15:27:43 +0200 Subject: [PATCH 01/15] added AssertionContainerInterface --- src/Assertion/AssertionContainerInterface.php | 27 ++++++++++++++ src/Assertion/AssertionPluginManager.php | 9 +++-- src/Assertion/AssertionSet.php | 4 +- src/Service/AuthorizationService.php | 6 +-- test/Assertion/AssertionSetTest.php | 32 ++++++++-------- .../AuthorizationServiceFactoryTest.php | 3 +- test/Service/AuthorizationServiceTest.php | 37 ++++++++++--------- 7 files changed, 75 insertions(+), 43 deletions(-) create mode 100644 src/Assertion/AssertionContainerInterface.php diff --git a/src/Assertion/AssertionContainerInterface.php b/src/Assertion/AssertionContainerInterface.php new file mode 100644 index 00000000..32a99432 --- /dev/null +++ b/src/Assertion/AssertionContainerInterface.php @@ -0,0 +1,27 @@ +rbac = $rbac; diff --git a/test/Assertion/AssertionSetTest.php b/test/Assertion/AssertionSetTest.php index db316272..c27d008f 100644 --- a/test/Assertion/AssertionSetTest.php +++ b/test/Assertion/AssertionSetTest.php @@ -21,8 +21,8 @@ namespace ZfcRbacTest\Assertion; use PHPUnit\Framework\TestCase; +use ZfcRbac\Assertion\AssertionContainerInterface; use ZfcRbac\Assertion\AssertionInterface; -use ZfcRbac\Assertion\AssertionPluginManager; use ZfcRbac\Assertion\AssertionSet; use ZfcRbac\Exception\InvalidArgumentException; use ZfcRbac\Identity\IdentityInterface; @@ -35,7 +35,7 @@ class AssertionSetTest extends TestCase { public function testImplementsAssertionInterface() { - $assertionPluginManager = $this->getMockBuilder(AssertionPluginManager::class)->disableOriginalConstructor()->getMock(); + $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); $assertionSet = new AssertionSet($assertionPluginManager, []); $this->assertInstanceOf(AssertionInterface::class, $assertionSet); @@ -43,7 +43,7 @@ public function testImplementsAssertionInterface() public function testWhenNoAssertionsArePresentTheAssertionWillFail() { - $assertionPluginManager = $this->getMockBuilder(AssertionPluginManager::class)->disableOriginalConstructor()->getMock(); + $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); $assertionSet = new AssertionSet($assertionPluginManager, []); $this->assertFalse($assertionSet->assert('foo')); @@ -51,7 +51,7 @@ public function testWhenNoAssertionsArePresentTheAssertionWillFail() public function testAcceptsAnAndCondition() { - $assertionPluginManager = $this->getMockBuilder(AssertionPluginManager::class)->disableOriginalConstructor()->getMock(); + $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); $assertionSet = new AssertionSet($assertionPluginManager, ['condition' => AssertionSet::CONDITION_AND]); $this->assertFalse($assertionSet->assert('foo')); @@ -59,7 +59,7 @@ public function testAcceptsAnAndCondition() public function testAcceptsAnOrCondition() { - $assertionPluginManager = $this->getMockBuilder(AssertionPluginManager::class)->disableOriginalConstructor()->getMock(); + $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); $assertionSet = new AssertionSet($assertionPluginManager, ['condition' => AssertionSet::CONDITION_OR]); $this->assertFalse($assertionSet->assert('foo')); @@ -67,7 +67,7 @@ public function testAcceptsAnOrCondition() public function testThrowsExceptionForAnUnknownCondition() { - $assertionPluginManager = $this->getMockBuilder(AssertionPluginManager::class)->disableOriginalConstructor()->getMock(); + $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); $this->expectException(InvalidArgumentException::class); new AssertionSet($assertionPluginManager, ['condition' => 'unknown']); @@ -78,7 +78,7 @@ public function testWhenNoConditionIsGivenAndIsUsed() $fooAssertion = new SimpleAssertion(true); $barAssertion = new SimpleAssertion(false); - $assertionPluginManager = $this->getMockBuilder(AssertionPluginManager::class)->disableOriginalConstructor()->getMock(); + $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); $assertionSet = new AssertionSet($assertionPluginManager, ['fooFactory', 'barFactory']); $assertionPluginManager->expects($this->at(0))->method('get')->with('fooFactory')->willReturn($fooAssertion); @@ -95,7 +95,7 @@ public function testAndConditionWillBreakEarlyWithFailure() $fooAssertion = new SimpleAssertion(false); $barAssertion = new SimpleAssertion(true); - $assertionPluginManager = $this->getMockBuilder(AssertionPluginManager::class)->disableOriginalConstructor()->getMock(); + $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); $assertionSet = new AssertionSet($assertionPluginManager, ['fooFactory', 'barFactory', 'condition' => AssertionSet::CONDITION_AND]); $assertionPluginManager->expects($this->at(0))->method('get')->with('fooFactory')->willReturn($fooAssertion); @@ -111,7 +111,7 @@ public function testOrConditionWillBreakEarlyWithSuccess() $fooAssertion = new SimpleAssertion(true); $barAssertion = new SimpleAssertion(false); - $assertionPluginManager = $this->getMockBuilder(AssertionPluginManager::class)->disableOriginalConstructor()->getMock(); + $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); $assertionSet = new AssertionSet($assertionPluginManager, ['fooFactory', 'barFactory', 'condition' => AssertionSet::CONDITION_OR]); $assertionPluginManager->expects($this->at(0))->method('get')->with('fooFactory')->willReturn($fooAssertion); @@ -126,7 +126,7 @@ public function testAssertionsAsStringsAreCached() { $fooAssertion = new SimpleAssertion(true); - $assertionPluginManager = $this->getMockBuilder(AssertionPluginManager::class)->disableOriginalConstructor()->getMock(); + $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); $assertionSet = new AssertionSet($assertionPluginManager, ['fooFactory']); $assertionPluginManager->expects($this->once())->method('get')->with('fooFactory')->willReturn($fooAssertion); @@ -142,7 +142,7 @@ public function testUsesAssertionsAsStrings() { $fooAssertion = new SimpleAssertion(true); - $assertionPluginManager = $this->getMockBuilder(AssertionPluginManager::class)->disableOriginalConstructor()->getMock(); + $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); $assertionSet = new AssertionSet($assertionPluginManager, ['fooFactory']); $assertionPluginManager->expects($this->once())->method('get')->with('fooFactory')->willReturn($fooAssertion); @@ -156,7 +156,7 @@ public function testUsesAssertionsAsInstances() { $fooAssertion = new SimpleAssertion(true); - $assertionPluginManager = $this->getMockBuilder(AssertionPluginManager::class)->disableOriginalConstructor()->getMock(); + $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); $assertionSet = new AssertionSet($assertionPluginManager, [$fooAssertion]); $this->assertTrue($assertionSet->assert('permission')); @@ -173,7 +173,7 @@ public function testUsesAssertionsAsCallables() return true; }; - $assertionPluginManager = $this->getMockBuilder(AssertionPluginManager::class)->disableOriginalConstructor()->getMock(); + $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); $assertionSet = new AssertionSet($assertionPluginManager, [$fooAssertion]); $this->assertTrue($assertionSet->assert('permission')); @@ -186,7 +186,7 @@ public function testUsesAssertionsAsArrays() $fooAssertion = new SimpleAssertion(true); $barAssertion = new SimpleAssertion(true); - $assertionPluginManager = $this->getMockBuilder(AssertionPluginManager::class)->disableOriginalConstructor()->getMock(); + $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); $assertionSet = new AssertionSet($assertionPluginManager, ['fooFactory', ['barFactory']]); $assertionPluginManager->expects($this->at(0))->method('get')->with('fooFactory')->willReturn($fooAssertion); @@ -202,7 +202,7 @@ public function testThrowExceptionForInvalidAssertion() { $fooAssertion = new \stdClass(); - $assertionPluginManager = $this->getMockBuilder(AssertionPluginManager::class)->disableOriginalConstructor()->getMock(); + $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); $assertionSet = new AssertionSet($assertionPluginManager, [$fooAssertion]); $this->expectException(InvalidArgumentException::class); @@ -214,7 +214,7 @@ public function testThrowExceptionForInvalidAssertion() */ public function testMatrix(array $assertions, bool $expectedResult, array $assertionCalledCount) { - $assertionPluginManager = $this->getMockBuilder(AssertionPluginManager::class)->disableOriginalConstructor()->getMock(); + $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); $assertionSet = new AssertionSet($assertionPluginManager, $assertions); $this->assertSame($expectedResult, $assertionSet->assert('permission')); diff --git a/test/Container/AuthorizationServiceFactoryTest.php b/test/Container/AuthorizationServiceFactoryTest.php index 66aceeb8..e4e4e40d 100644 --- a/test/Container/AuthorizationServiceFactoryTest.php +++ b/test/Container/AuthorizationServiceFactoryTest.php @@ -23,6 +23,7 @@ use PHPUnit\Framework\TestCase; use Psr\Container\ContainerInterface; +use ZfcRbac\Assertion\AssertionContainerInterface; use ZfcRbac\Assertion\AssertionPluginManager; use ZfcRbac\Container\AuthorizationServiceFactory; use ZfcRbac\Options\ModuleOptions; @@ -40,7 +41,7 @@ public function testCanCreateAuthorizationService(): void $container = $this->prophesize(ContainerInterface::class); $container->get(ModuleOptions::class)->willReturn(new ModuleOptions([])); $container->get(RoleServiceInterface::class)->willReturn($this->createMock(RoleServiceInterface::class)); - $container->get(AssertionPluginManager::class)->willReturn($this->createMock(AssertionPluginManager::class)); + $container->get(AssertionPluginManager::class)->willReturn($this->createMock(AssertionContainerInterface::class)); $container->get(Rbac::class)->willReturn(new Rbac()); $factory = new AuthorizationServiceFactory(); diff --git a/test/Service/AuthorizationServiceTest.php b/test/Service/AuthorizationServiceTest.php index e320a2c1..88c894f7 100644 --- a/test/Service/AuthorizationServiceTest.php +++ b/test/Service/AuthorizationServiceTest.php @@ -22,6 +22,7 @@ namespace ZfcRbacTest\Service; use PHPUnit\Framework\TestCase; +use ZfcRbac\Assertion\AssertionContainerInterface; use Zend\ServiceManager\ServiceManager; use ZfcRbac\Assertion\AssertionPluginManager; use ZfcRbac\Assertion\AssertionSet; @@ -193,10 +194,10 @@ public function testDoNotCallAssertionIfThePermissionIsNotGranted(): void $role = $this->getMockBuilder(RoleInterface::class)->getMock(); $rbac = $this->getMockBuilder(Rbac::class)->disableOriginalConstructor()->getMock(); - $roleService = $this->getMockBuilder(RoleServiceInterface::class)->disableOriginalConstructor()->getMock(); + $roleService = $this->getMockBuilder(RoleServiceInterface::class)->getMock(); $roleService->expects($this->once())->method('getIdentityRoles')->will($this->returnValue([$role])); - $assertionPluginManager = $this->getMockBuilder(AssertionPluginManager::class)->disableOriginalConstructor()->getMock(); + $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); $assertionPluginManager->expects($this->never())->method('get'); $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager); @@ -211,10 +212,10 @@ public function testReturnsFalseForIdentityWithoutRoles(): void $rbac = $this->getMockBuilder(Rbac::class)->disableOriginalConstructor()->getMock(); $rbac->expects($this->never())->method('isGranted'); - $roleService = $this->getMockBuilder(RoleServiceInterface::class)->disableOriginalConstructor()->getMock(); + $roleService = $this->getMockBuilder(RoleServiceInterface::class)->getMock(); $roleService->expects($this->once())->method('getIdentityRoles')->will($this->returnValue($identity->getRoles())); - $assertionPluginManager = $this->getMockBuilder(AssertionPluginManager::class)->disableOriginalConstructor()->getMock(); + $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); $assertionPluginManager->expects($this->never())->method('get'); $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager); @@ -227,13 +228,13 @@ public function testReturnsTrueForIdentityWhenHasPermissionButNoAssertionsExists $role = new FlatRole('admin'); $identity = new Identity([$role]); - $roleService = $this->getMockBuilder(RoleServiceInterface::class)->disableOriginalConstructor()->getMock(); + $roleService = $this->getMockBuilder(RoleServiceInterface::class)->getMock(); $roleService->expects($this->once())->method('getIdentityRoles')->will($this->returnValue($identity->getRoles())); $rbac = $this->getMockBuilder(Rbac::class)->disableOriginalConstructor()->getMock(); $rbac->expects($this->once())->method('isGranted')->willReturn(true); - $assertionPluginManager = $this->getMockBuilder(AssertionPluginManager::class)->disableOriginalConstructor()->getMock(); + $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); $assertionPluginManager->expects($this->never())->method('get'); $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager); @@ -247,13 +248,13 @@ public function testUsesAssertionsAsInstances(): void $identity = new Identity([$role]); $assertion = new SimpleAssertion(); - $roleService = $this->getMockBuilder(RoleServiceInterface::class)->disableOriginalConstructor()->getMock(); + $roleService = $this->getMockBuilder(RoleServiceInterface::class)->getMock(); $roleService->expects($this->once())->method('getIdentityRoles')->will($this->returnValue($identity->getRoles())); $rbac = $this->getMockBuilder(Rbac::class)->disableOriginalConstructor()->getMock(); $rbac->expects($this->once())->method('isGranted')->willReturn(true); - $assertionPluginManager = $this->getMockBuilder(AssertionPluginManager::class)->disableOriginalConstructor()->getMock(); + $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); $assertionPluginManager->expects($this->never())->method('get'); $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager, ['foo' => $assertion]); @@ -269,13 +270,13 @@ public function testUsesAssertionsAsStrings(): void $identity = new Identity([$role]); $assertion = new SimpleAssertion(); - $roleService = $this->getMockBuilder(RoleServiceInterface::class)->disableOriginalConstructor()->getMock(); + $roleService = $this->getMockBuilder(RoleServiceInterface::class)->getMock(); $roleService->expects($this->once())->method('getIdentityRoles')->will($this->returnValue($identity->getRoles())); $rbac = $this->getMockBuilder(Rbac::class)->disableOriginalConstructor()->getMock(); $rbac->expects($this->once())->method('isGranted')->willReturn(true); - $assertionPluginManager = $this->getMockBuilder(AssertionPluginManager::class)->disableOriginalConstructor()->getMock(); + $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); $assertionPluginManager->expects($this->once())->method('get')->with('fooFactory')->willReturn($assertion); $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager, ['foo' => 'fooFactory']); @@ -290,13 +291,13 @@ public function testUsesAssertionsAsCallable(): void $role = new FlatRole('admin'); $identity = new Identity([$role]); - $roleService = $this->getMockBuilder(RoleServiceInterface::class)->disableOriginalConstructor()->getMock(); + $roleService = $this->getMockBuilder(RoleServiceInterface::class)->getMock(); $roleService->expects($this->once())->method('getIdentityRoles')->will($this->returnValue($identity->getRoles())); $rbac = $this->getMockBuilder(Rbac::class)->disableOriginalConstructor()->getMock(); $rbac->expects($this->once())->method('isGranted')->willReturn(true); - $assertionPluginManager = $this->getMockBuilder(AssertionPluginManager::class)->disableOriginalConstructor()->getMock(); + $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); $assertionPluginManager->expects($this->never())->method('get'); $called = false; @@ -321,13 +322,13 @@ public function testUsesAssertionsAsArrays(): void $role = new FlatRole('admin'); $identity = new Identity([$role]); - $roleService = $this->getMockBuilder(RoleServiceInterface::class)->disableOriginalConstructor()->getMock(); + $roleService = $this->getMockBuilder(RoleServiceInterface::class)->getMock(); $roleService->expects($this->once())->method('getIdentityRoles')->will($this->returnValue($identity->getRoles())); $rbac = $this->getMockBuilder(Rbac::class)->disableOriginalConstructor()->getMock(); $rbac->expects($this->once())->method('isGranted')->willReturn(true); - $assertionPluginManager = $this->getMockBuilder(AssertionPluginManager::class)->disableOriginalConstructor()->getMock(); + $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); $assertionPluginManager->expects($this->never())->method('get'); $called1 = false; @@ -361,10 +362,10 @@ public function testThrowExceptionForInvalidAssertion(): void $rbac->expects($this->once())->method('isGranted')->will($this->returnValue(true)); - $roleService = $this->getMockBuilder(RoleServiceInterface::class)->disableOriginalConstructor()->getMock(); + $roleService = $this->getMockBuilder(RoleServiceInterface::class)->getMock(); $roleService->expects($this->once())->method('getIdentityRoles')->will($this->returnValue([$role])); - $assertionPluginManager = $this->getMockBuilder(AssertionPluginManager::class)->disableOriginalConstructor()->getMock(); + $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->disableOriginalConstructor()->getMock(); $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager, ['foo' => new \stdClass()]); $this->expectException(InvalidArgumentException::class); @@ -378,8 +379,8 @@ public function testContextIsPassedToRoleService(): void $context = 'context'; $rbac = $this->getMockBuilder(Rbac::class)->disableOriginalConstructor()->getMock(); - $roleService = $this->getMockBuilder(RoleServiceInterface::class)->disableOriginalConstructor()->getMock(); - $assertionPluginManager = $this->getMockBuilder(AssertionPluginManager::class)->disableOriginalConstructor()->getMock(); + $roleService = $this->getMockBuilder(RoleServiceInterface::class)->getMock(); + $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager); $roleService->expects($this->once())->method('getIdentityRoles')->with($identity, $context)->willReturn([]); From be038a2b33604624f2cde5ceffda31e9d7e36a99 Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Wed, 21 Feb 2018 16:02:22 +0200 Subject: [PATCH 02/15] try to fix php7.2 --- src/Assertion/AssertionPluginManager.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Assertion/AssertionPluginManager.php b/src/Assertion/AssertionPluginManager.php index 207b8b70..af4ade6e 100644 --- a/src/Assertion/AssertionPluginManager.php +++ b/src/Assertion/AssertionPluginManager.php @@ -33,8 +33,11 @@ final class AssertionPluginManager extends AbstractPluginManager implements Asse { protected $instanceOf = AssertionInterface::class; - public function get($name): AssertionInterface + /** + * {@inheritdoc} + */ + public function get($name, array $options = null): AssertionInterface { - return parent::get($name); + return parent::get($name, $options); } } From 43cbf0d6005f636991adde5d03cdcc9e5b9a84e5 Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Wed, 21 Feb 2018 18:19:20 +0200 Subject: [PATCH 03/15] Remove RoleProviderPluginManager --- config/dependencies.global.php | 3 +- ...ry.php => InMemoryRoleProviderFactory.php} | 16 +-- .../ObjectRepositoryRoleProviderFactory.php | 4 +- src/Container/RoleServiceFactory.php | 12 +- src/Role/RoleProviderPluginManager.php | 47 ------- ...bjectRepositoryRoleProviderFactoryTest.php | 124 +++++++++--------- .../RoleProviderPluginManagerFactoryTest.php | 48 ------- test/Container/RoleServiceFactoryTest.php | 22 ++-- test/Role/RoleProviderPluginManagerTest.php | 53 -------- 9 files changed, 85 insertions(+), 244 deletions(-) rename src/Container/{RoleProviderPluginManagerFactory.php => InMemoryRoleProviderFactory.php} (71%) delete mode 100644 src/Role/RoleProviderPluginManager.php delete mode 100644 test/Container/RoleProviderPluginManagerFactoryTest.php delete mode 100644 test/Role/RoleProviderPluginManagerTest.php diff --git a/config/dependencies.global.php b/config/dependencies.global.php index bb670fb4..ec1d0756 100644 --- a/config/dependencies.global.php +++ b/config/dependencies.global.php @@ -23,7 +23,8 @@ 'factories' => [ ZfcRbac\Assertion\AssertionPluginManager::class => ZfcRbac\Container\AssertionPluginManagerFactory::class, ZfcRbac\Options\ModuleOptions::class => ZfcRbac\Container\ModuleOptionsFactory::class, - ZfcRbac\Role\RoleProviderPluginManager::class => ZfcRbac\Container\RoleProviderPluginManagerFactory::class, + ZfcRbac\Role\InMemoryRoleProvider::class => ZfcRbac\Container\InMemoryRoleProviderFactory::class, + ZfcRbac\Role\ObjectRepositoryRoleProvider::class => ZfcRbac\Container\ObjectRepositoryRoleProviderFactory::class, ZfcRbac\Service\AuthorizationServiceInterface::class => ZfcRbac\Container\AuthorizationServiceFactory::class, ZfcRbac\Service\RoleServiceInterface::class => ZfcRbac\Container\RoleServiceFactory::class, ZfcRbac\Rbac::class => \Zend\ServiceManager\Factory\InvokableFactory::class, diff --git a/src/Container/RoleProviderPluginManagerFactory.php b/src/Container/InMemoryRoleProviderFactory.php similarity index 71% rename from src/Container/RoleProviderPluginManagerFactory.php rename to src/Container/InMemoryRoleProviderFactory.php index 7c69c8fc..c89e18c9 100644 --- a/src/Container/RoleProviderPluginManagerFactory.php +++ b/src/Container/InMemoryRoleProviderFactory.php @@ -22,20 +22,20 @@ namespace ZfcRbac\Container; use Psr\Container\ContainerInterface; -use ZfcRbac\Role\RoleProviderPluginManager; +use ZfcRbac\Role\InMemoryRoleProvider; /** - * Factory to create a role provider plugin manager + * Factory used to create an in memory role provider * - * @author Michaël Gallego + * @author Vytautas Stankus * @licence MIT */ -final class RoleProviderPluginManagerFactory +final class InMemoryRoleProviderFactory { - public function __invoke(ContainerInterface $container): RoleProviderPluginManager + public function __invoke(ContainerInterface $container): InMemoryRoleProvider { - $config = $container->get('config')['zfc_rbac']['role_provider_manager']; - - return new RoleProviderPluginManager($container, $config); + return new InMemoryRoleProvider( + $container->get('config')['zfc_rbac']['role_provider'][InMemoryRoleProvider::class] ?? [] + ); } } diff --git a/src/Container/ObjectRepositoryRoleProviderFactory.php b/src/Container/ObjectRepositoryRoleProviderFactory.php index ceaae90b..f8beb1d7 100644 --- a/src/Container/ObjectRepositoryRoleProviderFactory.php +++ b/src/Container/ObjectRepositoryRoleProviderFactory.php @@ -33,8 +33,10 @@ */ final class ObjectRepositoryRoleProviderFactory { - public function __invoke(ContainerInterface $container, $requestedName, ?array $options = []): ObjectRepositoryRoleProvider + public function __invoke(ContainerInterface $container): ObjectRepositoryRoleProvider { + $options = $container->get('config')['zfc_rbac']['role_provider'][ObjectRepositoryRoleProvider::class] ?? []; + if (! isset($options['role_name_property'])) { throw new Exception\RuntimeException('The "role_name_property" option is missing'); } diff --git a/src/Container/RoleServiceFactory.php b/src/Container/RoleServiceFactory.php index a477621a..f04fec22 100644 --- a/src/Container/RoleServiceFactory.php +++ b/src/Container/RoleServiceFactory.php @@ -24,7 +24,7 @@ use Psr\Container\ContainerInterface; use ZfcRbac\Exception\RuntimeException; use ZfcRbac\Options\ModuleOptions; -use ZfcRbac\Role\RoleProviderPluginManager; +use ZfcRbac\Role\RoleProviderInterface; use ZfcRbac\Service\RoleService; /** @@ -39,15 +39,7 @@ public function __invoke(ContainerInterface $container): RoleService { $moduleOptions = $container->get(ModuleOptions::class); - $roleProviderConfig = $moduleOptions->getRoleProvider(); - - if (empty($roleProviderConfig)) { - throw new RuntimeException('No role provider has been set for ZfcRbac'); - } - - $pluginManager = $container->get(RoleProviderPluginManager::class); - $roleProvider = $pluginManager->get(key($roleProviderConfig), current($roleProviderConfig)); - $roleService = new RoleService($roleProvider); + $roleService = new RoleService($container->get(RoleProviderInterface::class)); $roleService->setGuestRole($moduleOptions->getGuestRole()); return $roleService; diff --git a/src/Role/RoleProviderPluginManager.php b/src/Role/RoleProviderPluginManager.php deleted file mode 100644 index d7376f85..00000000 --- a/src/Role/RoleProviderPluginManager.php +++ /dev/null @@ -1,47 +0,0 @@ - - * @license MIT - */ -final class RoleProviderPluginManager extends AbstractPluginManager -{ - protected $instanceOf = RoleProviderInterface::class; - - /** - * @var array - */ - protected $factories = [ - InMemoryRoleProvider::class => InvokableFactory::class, - ObjectRepositoryRoleProvider::class => ObjectRepositoryRoleProviderFactory::class, - ]; -} diff --git a/test/Container/ObjectRepositoryRoleProviderFactoryTest.php b/test/Container/ObjectRepositoryRoleProviderFactoryTest.php index 44734d74..44aed0c0 100644 --- a/test/Container/ObjectRepositoryRoleProviderFactoryTest.php +++ b/test/Container/ObjectRepositoryRoleProviderFactoryTest.php @@ -24,11 +24,10 @@ use Doctrine\Common\Persistence\ObjectManager; use Doctrine\Common\Persistence\ObjectRepository; use PHPUnit\Framework\TestCase; -use Zend\ServiceManager\Exception\ServiceNotCreatedException; use Zend\ServiceManager\ServiceManager; +use ZfcRbac\Container\ObjectRepositoryRoleProviderFactory; use ZfcRbac\Exception\RuntimeException; use ZfcRbac\Role\ObjectRepositoryRoleProvider; -use ZfcRbac\Role\RoleProviderPluginManager; /** * @covers \ZfcRbac\Container\ObjectRepositoryRoleProviderFactory @@ -37,40 +36,46 @@ class ObjectRepositoryRoleProviderFactoryTest extends TestCase { public function testFactoryUsingObjectRepository(): void { - $serviceManager = new ServiceManager(); - - $options = [ - 'role_name_property' => 'name', - 'object_repository' => 'RoleObjectRepository', - ]; - $pluginManager = new RoleProviderPluginManager($serviceManager); - - $serviceManager->setService('RoleObjectRepository', $this->getMockBuilder(ObjectRepository::class)->getMock()); - - $roleProvider = $pluginManager->get(ObjectRepositoryRoleProvider::class, $options); + $container = new ServiceManager(); + $container->setService('config', [ + 'zfc_rbac' => [ + 'role_provider' => [ + ObjectRepositoryRoleProvider::class => [ + 'role_name_property' => 'name', + 'object_repository' => 'RoleObjectRepository', + ] + ] + ] + ]); + $container->setService('RoleObjectRepository', $this->getMockBuilder(ObjectRepository::class)->getMock()); + + $roleProvider = (new ObjectRepositoryRoleProviderFactory)($container); $this->assertInstanceOf(ObjectRepositoryRoleProvider::class, $roleProvider); } public function testFactoryUsingObjectManager(): void { - $serviceManager = new ServiceManager(); - $pluginManager = new RoleProviderPluginManager($serviceManager); - - $options = [ - 'role_name_property' => 'name', - 'object_manager' => 'ObjectManager', - 'class_name' => 'Role', - ]; - + $container = new ServiceManager(); + $container->setService('config', [ + 'zfc_rbac' => [ + 'role_provider' => [ + ObjectRepositoryRoleProvider::class => [ + 'role_name_property' => 'name', + 'object_manager' => 'ObjectManager', + 'class_name' => 'Role', + ] + ] + ] + ]); $objectManager = $this->getMockBuilder(ObjectManager::class)->getMock(); $objectManager->expects($this->once()) - ->method('getRepository') - ->with($options['class_name']) - ->will($this->returnValue($this->getMockBuilder(ObjectRepository::class)->getMock())); + ->method('getRepository') + ->with('Role') + ->will($this->returnValue($this->getMockBuilder(ObjectRepository::class)->getMock())); - $serviceManager->setService('ObjectManager', $objectManager); + $container->setService('ObjectManager', $objectManager); - $roleProvider = $pluginManager->get(ObjectRepositoryRoleProvider::class, $options); + $roleProvider = (new ObjectRepositoryRoleProviderFactory)($container); $this->assertInstanceOf(ObjectRepositoryRoleProvider::class, $roleProvider); } @@ -79,24 +84,18 @@ public function testFactoryUsingObjectManager(): void */ public function testThrowExceptionIfNoRoleNamePropertyIsSet(): void { - try { - $serviceManager = new ServiceManager(); - $pluginManager = new RoleProviderPluginManager($serviceManager); - - $pluginManager->get(ObjectRepositoryRoleProvider::class, []); - } catch (ServiceNotCreatedException $e) { - while ($e = $e->getPrevious()) { - if ($e instanceof RuntimeException) { - $this->assertTrue(true); // we got here - return; - } - } - } - - $this->fail( - 'ZfcRbac\Factory\ObjectRepositoryRoleProviderFactory::createService() :: ' - .'ZfcRbac\Exception\RuntimeException was not found in the previous Exceptions' - ); + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('The "role_name_property" option is missing'); + + $container = new ServiceManager(); + $container->setService('config', [ + 'zfc_rbac' => [ + 'role_provider' => [ + ObjectRepositoryRoleProvider::class => [] + ] + ] + ]); + (new ObjectRepositoryRoleProviderFactory)($container); } /** @@ -104,25 +103,20 @@ public function testThrowExceptionIfNoRoleNamePropertyIsSet(): void */ public function testThrowExceptionIfNoObjectManagerNorObjectRepositoryIsSet(): void { - try { - $serviceManager = new ServiceManager(); - $pluginManager = new RoleProviderPluginManager($serviceManager); - - $pluginManager->get(ObjectRepositoryRoleProvider::class, [ - 'role_name_property' => 'name', - ]); - } catch (ServiceNotCreatedException $e) { - while ($e = $e->getPrevious()) { - if ($e instanceof RuntimeException) { - $this->assertTrue(true); // we got here - return; - } - } - } - - $this->fail( - 'ZfcRbac\Factory\ObjectRepositoryRoleProviderFactory::createService() :: ' - .'ZfcRbac\Exception\RuntimeException was not found in the previous Exceptions' - ); + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('No object repository was found while creating the ZfcRbac object repository role provider. Are + you sure you specified either the "object_repository" option or "object_manager"/"class_name" options?'); + + $container = new ServiceManager(); + $container->setService('config', [ + 'zfc_rbac' => [ + 'role_provider' => [ + ObjectRepositoryRoleProvider::class => [ + 'role_name_property' => 'name', + ] + ] + ] + ]); + (new ObjectRepositoryRoleProviderFactory)($container); } } diff --git a/test/Container/RoleProviderPluginManagerFactoryTest.php b/test/Container/RoleProviderPluginManagerFactoryTest.php deleted file mode 100644 index 62a42d4f..00000000 --- a/test/Container/RoleProviderPluginManagerFactoryTest.php +++ /dev/null @@ -1,48 +0,0 @@ -setService('config', [ - 'zfc_rbac' => [ - 'role_provider_manager' => [], - ], - ]); - - $factory = new RoleProviderPluginManagerFactory(); - $pluginManager = $factory($serviceManager); - - $this->assertInstanceOf(RoleProviderPluginManager::class, $pluginManager); - } -} diff --git a/test/Container/RoleServiceFactoryTest.php b/test/Container/RoleServiceFactoryTest.php index 0c7afb50..eeb1e84b 100644 --- a/test/Container/RoleServiceFactoryTest.php +++ b/test/Container/RoleServiceFactoryTest.php @@ -21,12 +21,12 @@ namespace ZfcRbacTest\Container; -use Interop\Container\ContainerInterface; use PHPUnit\Framework\TestCase; +use Zend\ServiceManager\ServiceManager; use ZfcRbac\Container\RoleServiceFactory; -use ZfcRbac\Exception\RuntimeException; use ZfcRbac\Options\ModuleOptions; -use ZfcRbac\Role\RoleProviderPluginManager; +use ZfcRbac\Role\InMemoryRoleProvider; +use ZfcRbac\Role\RoleProviderInterface; /** * @covers \ZfcRbac\Container\RoleServiceFactory @@ -44,10 +44,10 @@ public function testCanCreateRoleService(): void ], ]); - $container = $this->getMockBuilder(ContainerInterface::class)->getMock(); - - $container->expects($this->at(0))->method('get')->with(ModuleOptions::class)->willReturn($options); - $container->expects($this->at(1))->method('get')->with(RoleProviderPluginManager::class)->willReturn(new RoleProviderPluginManager($this->getMockBuilder(ContainerInterface::class)->getMock())); + $container = new ServiceManager(['services' => [ + ModuleOptions::class => $options, + RoleProviderInterface::class => new InMemoryRoleProvider([]), + ]]); $factory = new RoleServiceFactory(); $roleService = $factory($container); @@ -58,16 +58,16 @@ public function testCanCreateRoleService(): void public function testThrowExceptionIfNoRoleProvider(): void { - $this->expectException(RuntimeException::class); + $this->expectException(\Psr\Container\NotFoundExceptionInterface::class); $options = new ModuleOptions([ 'guest_role' => 'guest', 'role_provider' => [], ]); - $container = $this->getMockBuilder(ContainerInterface::class)->getMock(); - - $container->expects($this->at(0))->method('get')->with(ModuleOptions::class)->willReturn($options); + $container = new ServiceManager(['services' => [ + ModuleOptions::class => $options, + ]]); $factory = new RoleServiceFactory(); $factory($container); diff --git a/test/Role/RoleProviderPluginManagerTest.php b/test/Role/RoleProviderPluginManagerTest.php deleted file mode 100644 index e55f74a5..00000000 --- a/test/Role/RoleProviderPluginManagerTest.php +++ /dev/null @@ -1,53 +0,0 @@ -getMockBuilder(ContainerInterface::class)->getMock(); - $pluginMock = $this->getMockBuilder(RoleProviderInterface::class)->getMock(); - $pluginManager = new RoleProviderPluginManager($containerMock); - - $this->assertNull($pluginManager->validate($pluginMock)); - } - - public function testValidationOfPluginFailsIfRoleProviderInterfaceIsNotImplemented(): void - { - $this->expectException(InvalidServiceException::class); - $containerMock = $this->getMockBuilder(ContainerInterface::class)->getMock(); - $pluginManager = new RoleProviderPluginManager($containerMock); - - $plugin = new \stdClass(); - $pluginManager->validate($plugin); - } -} From 711e2a329a288351fdc573f3d9841f8852096787 Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Wed, 21 Feb 2018 18:21:28 +0200 Subject: [PATCH 04/15] fix cs issues --- src/Container/RoleServiceFactory.php | 1 - ...bjectRepositoryRoleProviderFactoryTest.php | 32 +++++++++---------- test/Container/RoleServiceFactoryTest.php | 2 +- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/Container/RoleServiceFactory.php b/src/Container/RoleServiceFactory.php index f04fec22..0df25f72 100644 --- a/src/Container/RoleServiceFactory.php +++ b/src/Container/RoleServiceFactory.php @@ -22,7 +22,6 @@ namespace ZfcRbac\Container; use Psr\Container\ContainerInterface; -use ZfcRbac\Exception\RuntimeException; use ZfcRbac\Options\ModuleOptions; use ZfcRbac\Role\RoleProviderInterface; use ZfcRbac\Service\RoleService; diff --git a/test/Container/ObjectRepositoryRoleProviderFactoryTest.php b/test/Container/ObjectRepositoryRoleProviderFactoryTest.php index 44aed0c0..11c7607e 100644 --- a/test/Container/ObjectRepositoryRoleProviderFactoryTest.php +++ b/test/Container/ObjectRepositoryRoleProviderFactoryTest.php @@ -43,13 +43,13 @@ public function testFactoryUsingObjectRepository(): void ObjectRepositoryRoleProvider::class => [ 'role_name_property' => 'name', 'object_repository' => 'RoleObjectRepository', - ] - ] - ] + ], + ], + ], ]); $container->setService('RoleObjectRepository', $this->getMockBuilder(ObjectRepository::class)->getMock()); - $roleProvider = (new ObjectRepositoryRoleProviderFactory)($container); + $roleProvider = (new ObjectRepositoryRoleProviderFactory())($container); $this->assertInstanceOf(ObjectRepositoryRoleProvider::class, $roleProvider); } @@ -63,9 +63,9 @@ public function testFactoryUsingObjectManager(): void 'role_name_property' => 'name', 'object_manager' => 'ObjectManager', 'class_name' => 'Role', - ] - ] - ] + ], + ], + ], ]); $objectManager = $this->getMockBuilder(ObjectManager::class)->getMock(); $objectManager->expects($this->once()) @@ -75,7 +75,7 @@ public function testFactoryUsingObjectManager(): void $container->setService('ObjectManager', $objectManager); - $roleProvider = (new ObjectRepositoryRoleProviderFactory)($container); + $roleProvider = (new ObjectRepositoryRoleProviderFactory())($container); $this->assertInstanceOf(ObjectRepositoryRoleProvider::class, $roleProvider); } @@ -91,11 +91,11 @@ public function testThrowExceptionIfNoRoleNamePropertyIsSet(): void $container->setService('config', [ 'zfc_rbac' => [ 'role_provider' => [ - ObjectRepositoryRoleProvider::class => [] - ] - ] + ObjectRepositoryRoleProvider::class => [], + ], + ], ]); - (new ObjectRepositoryRoleProviderFactory)($container); + (new ObjectRepositoryRoleProviderFactory())($container); } /** @@ -113,10 +113,10 @@ public function testThrowExceptionIfNoObjectManagerNorObjectRepositoryIsSet(): v 'role_provider' => [ ObjectRepositoryRoleProvider::class => [ 'role_name_property' => 'name', - ] - ] - ] + ], + ], + ], ]); - (new ObjectRepositoryRoleProviderFactory)($container); + (new ObjectRepositoryRoleProviderFactory())($container); } } diff --git a/test/Container/RoleServiceFactoryTest.php b/test/Container/RoleServiceFactoryTest.php index eeb1e84b..96dc681d 100644 --- a/test/Container/RoleServiceFactoryTest.php +++ b/test/Container/RoleServiceFactoryTest.php @@ -45,7 +45,7 @@ public function testCanCreateRoleService(): void ]); $container = new ServiceManager(['services' => [ - ModuleOptions::class => $options, + ModuleOptions::class => $options, RoleProviderInterface::class => new InMemoryRoleProvider([]), ]]); From 35776dee1f4fd78cba83c7067b1d35bdf0ecf1ff Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Thu, 22 Feb 2018 10:25:20 +0200 Subject: [PATCH 05/15] fixes per review and refactored RoleService --- src/Assertion/AssertionContainerInterface.php | 4 +- src/Container/InMemoryRoleProviderFactory.php | 4 +- .../ObjectRepositoryRoleProviderFactory.php | 4 +- src/Container/RoleServiceFactory.php | 5 +- src/Service/RoleService.php | 20 ++----- ...bjectRepositoryRoleProviderFactoryTest.php | 53 ++++++++----------- test/Container/RoleServiceFactoryTest.php | 1 - test/Service/RoleServiceTest.php | 10 +--- 8 files changed, 38 insertions(+), 63 deletions(-) diff --git a/src/Assertion/AssertionContainerInterface.php b/src/Assertion/AssertionContainerInterface.php index 32a99432..f8b7cf2e 100644 --- a/src/Assertion/AssertionContainerInterface.php +++ b/src/Assertion/AssertionContainerInterface.php @@ -21,7 +21,9 @@ namespace ZfcRbac\Assertion; -interface AssertionContainerInterface +use Psr\Container\ContainerInterface; + +interface AssertionContainerInterface extends ContainerInterface { public function get($name): AssertionInterface; } diff --git a/src/Container/InMemoryRoleProviderFactory.php b/src/Container/InMemoryRoleProviderFactory.php index c89e18c9..71643ffd 100644 --- a/src/Container/InMemoryRoleProviderFactory.php +++ b/src/Container/InMemoryRoleProviderFactory.php @@ -34,8 +34,10 @@ final class InMemoryRoleProviderFactory { public function __invoke(ContainerInterface $container): InMemoryRoleProvider { + $moduleOptions = $container->get(ModuleOptions::class); + return new InMemoryRoleProvider( - $container->get('config')['zfc_rbac']['role_provider'][InMemoryRoleProvider::class] ?? [] + $moduleOptions->getRoleProvider()[InMemoryRoleProvider::class] ?? [] ); } } diff --git a/src/Container/ObjectRepositoryRoleProviderFactory.php b/src/Container/ObjectRepositoryRoleProviderFactory.php index f8beb1d7..e892c4d7 100644 --- a/src/Container/ObjectRepositoryRoleProviderFactory.php +++ b/src/Container/ObjectRepositoryRoleProviderFactory.php @@ -23,6 +23,7 @@ use Psr\Container\ContainerInterface; use ZfcRbac\Exception; +use ZfcRbac\Options\ModuleOptions; use ZfcRbac\Role\ObjectRepositoryRoleProvider; /** @@ -35,7 +36,8 @@ final class ObjectRepositoryRoleProviderFactory { public function __invoke(ContainerInterface $container): ObjectRepositoryRoleProvider { - $options = $container->get('config')['zfc_rbac']['role_provider'][ObjectRepositoryRoleProvider::class] ?? []; + $moduleOptions = $container->get(ModuleOptions::class); + $options = $moduleOptions->getRoleProvider()[ObjectRepositoryRoleProvider::class] ?? []; if (! isset($options['role_name_property'])) { throw new Exception\RuntimeException('The "role_name_property" option is missing'); diff --git a/src/Container/RoleServiceFactory.php b/src/Container/RoleServiceFactory.php index 0df25f72..bbf518e0 100644 --- a/src/Container/RoleServiceFactory.php +++ b/src/Container/RoleServiceFactory.php @@ -38,9 +38,6 @@ public function __invoke(ContainerInterface $container): RoleService { $moduleOptions = $container->get(ModuleOptions::class); - $roleService = new RoleService($container->get(RoleProviderInterface::class)); - $roleService->setGuestRole($moduleOptions->getGuestRole()); - - return $roleService; + return new RoleService($container->get(RoleProviderInterface::class), $moduleOptions->getGuestRole()); } } diff --git a/src/Service/RoleService.php b/src/Service/RoleService.php index f2cf62a5..9fe64424 100644 --- a/src/Service/RoleService.php +++ b/src/Service/RoleService.php @@ -37,33 +37,19 @@ final class RoleService implements RoleServiceInterface /** * @var RoleProviderInterface */ - protected $roleProvider; + private $roleProvider; /** * @var string */ - protected $guestRole = ''; + private $guestRole; - public function __construct(RoleProviderInterface $roleProvider) + public function __construct(RoleProviderInterface $roleProvider, string $guestRole) { $this->roleProvider = $roleProvider; - } - - public function setRoleProvider(RoleProviderInterface $roleProvider): void - { - $this->roleProvider = $roleProvider; - } - - public function setGuestRole(string $guestRole): void - { $this->guestRole = $guestRole; } - public function getGuestRole(): string - { - return $this->guestRole; - } - /** * Get the identity roles from the current identity, applying some more logic * diff --git a/test/Container/ObjectRepositoryRoleProviderFactoryTest.php b/test/Container/ObjectRepositoryRoleProviderFactoryTest.php index 11c7607e..ecfd5836 100644 --- a/test/Container/ObjectRepositoryRoleProviderFactoryTest.php +++ b/test/Container/ObjectRepositoryRoleProviderFactoryTest.php @@ -27,6 +27,7 @@ use Zend\ServiceManager\ServiceManager; use ZfcRbac\Container\ObjectRepositoryRoleProviderFactory; use ZfcRbac\Exception\RuntimeException; +use ZfcRbac\Options\ModuleOptions; use ZfcRbac\Role\ObjectRepositoryRoleProvider; /** @@ -37,16 +38,14 @@ class ObjectRepositoryRoleProviderFactoryTest extends TestCase public function testFactoryUsingObjectRepository(): void { $container = new ServiceManager(); - $container->setService('config', [ - 'zfc_rbac' => [ - 'role_provider' => [ - ObjectRepositoryRoleProvider::class => [ - 'role_name_property' => 'name', - 'object_repository' => 'RoleObjectRepository', - ], + $container->setService(ModuleOptions::class, new ModuleOptions([ + 'role_provider' => [ + ObjectRepositoryRoleProvider::class => [ + 'role_name_property' => 'name', + 'object_repository' => 'RoleObjectRepository', ], ], - ]); + ])); $container->setService('RoleObjectRepository', $this->getMockBuilder(ObjectRepository::class)->getMock()); $roleProvider = (new ObjectRepositoryRoleProviderFactory())($container); @@ -56,17 +55,15 @@ public function testFactoryUsingObjectRepository(): void public function testFactoryUsingObjectManager(): void { $container = new ServiceManager(); - $container->setService('config', [ - 'zfc_rbac' => [ - 'role_provider' => [ - ObjectRepositoryRoleProvider::class => [ - 'role_name_property' => 'name', - 'object_manager' => 'ObjectManager', - 'class_name' => 'Role', - ], + $container->setService(ModuleOptions::class, new ModuleOptions([ + 'role_provider' => [ + ObjectRepositoryRoleProvider::class => [ + 'role_name_property' => 'name', + 'object_manager' => 'ObjectManager', + 'class_name' => 'Role', ], ], - ]); + ])); $objectManager = $this->getMockBuilder(ObjectManager::class)->getMock(); $objectManager->expects($this->once()) ->method('getRepository') @@ -88,13 +85,11 @@ public function testThrowExceptionIfNoRoleNamePropertyIsSet(): void $this->expectExceptionMessage('The "role_name_property" option is missing'); $container = new ServiceManager(); - $container->setService('config', [ - 'zfc_rbac' => [ - 'role_provider' => [ - ObjectRepositoryRoleProvider::class => [], - ], + $container->setService(ModuleOptions::class, new ModuleOptions([ + 'role_provider' => [ + ObjectRepositoryRoleProvider::class => [], ], - ]); + ])); (new ObjectRepositoryRoleProviderFactory())($container); } @@ -108,15 +103,13 @@ public function testThrowExceptionIfNoObjectManagerNorObjectRepositoryIsSet(): v you sure you specified either the "object_repository" option or "object_manager"/"class_name" options?'); $container = new ServiceManager(); - $container->setService('config', [ - 'zfc_rbac' => [ - 'role_provider' => [ - ObjectRepositoryRoleProvider::class => [ - 'role_name_property' => 'name', - ], + $container->setService(ModuleOptions::class, new ModuleOptions([ + 'role_provider' => [ + ObjectRepositoryRoleProvider::class => [ + 'role_name_property' => 'name', ], ], - ]); + ])); (new ObjectRepositoryRoleProviderFactory())($container); } } diff --git a/test/Container/RoleServiceFactoryTest.php b/test/Container/RoleServiceFactoryTest.php index 96dc681d..bd87b4d7 100644 --- a/test/Container/RoleServiceFactoryTest.php +++ b/test/Container/RoleServiceFactoryTest.php @@ -53,7 +53,6 @@ public function testCanCreateRoleService(): void $roleService = $factory($container); $this->assertInstanceOf(\ZfcRbac\Service\RoleService::class, $roleService); - $this->assertEquals('guest', $roleService->getGuestRole()); } public function testThrowExceptionIfNoRoleProvider(): void diff --git a/test/Service/RoleServiceTest.php b/test/Service/RoleServiceTest.php index d0228feb..3132266b 100644 --- a/test/Service/RoleServiceTest.php +++ b/test/Service/RoleServiceTest.php @@ -39,13 +39,10 @@ class RoleServiceTest extends TestCase { public function testReturnGuestRoleIfNoIdentityIsGiven(): void { - $roleService = new RoleService(new InMemoryRoleProvider([])); - - $roleService->setGuestRole('guest'); + $roleService = new RoleService(new InMemoryRoleProvider([]), 'guest'); $result = $roleService->getIdentityRoles(null); - $this->assertEquals('guest', $roleService->getGuestRole()); $this->assertCount(1, $result); $this->assertInstanceOf(RoleInterface::class, $result[0]); $this->assertEquals('guest', $result[0]->getName()); @@ -53,15 +50,12 @@ public function testReturnGuestRoleIfNoIdentityIsGiven(): void public function testReturnGuestRoleIfGuestIdentityIsGiven(): void { - $roleService = new RoleService(new InMemoryRoleProvider([])); - - $roleService->setGuestRole('guest'); + $roleService = new RoleService(new InMemoryRoleProvider([]), 'guest'); $identity = new Identity(['guest']); $result = $roleService->getIdentityRoles($identity); - $this->assertEquals('guest', $roleService->getGuestRole()); $this->assertCount(1, $result); $this->assertInstanceOf(RoleInterface::class, $result[0]); $this->assertEquals('guest', $result[0]->getName()); From a9a8d8b70088586a6b4d94c6cb1815da98b5c3f6 Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Thu, 22 Feb 2018 11:00:44 +0200 Subject: [PATCH 06/15] removed unused config entry --- config/dependencies.global.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/config/dependencies.global.php b/config/dependencies.global.php index ec1d0756..4b392975 100644 --- a/config/dependencies.global.php +++ b/config/dependencies.global.php @@ -32,9 +32,6 @@ ], 'zfc_rbac' => [ - // Role provider plugin manager - 'role_provider_manager' => [], - // Assertion plugin manager 'assertion_manager' => [], ], From 2da347b9322ea929280d44dad777a4a95b7a7ff0 Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Fri, 23 Feb 2018 09:04:26 +0200 Subject: [PATCH 07/15] rebase --- test/Service/AuthorizationServiceTest.php | 2 +- test/Service/RoleServiceTest.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/Service/AuthorizationServiceTest.php b/test/Service/AuthorizationServiceTest.php index 88c894f7..8598e7aa 100644 --- a/test/Service/AuthorizationServiceTest.php +++ b/test/Service/AuthorizationServiceTest.php @@ -181,7 +181,7 @@ public function testGranted($role, $permission, $context, bool $isGranted, array ], ]; - $roleService = new RoleService(new InMemoryRoleProvider($roleConfig)); + $roleService = new RoleService(new InMemoryRoleProvider($roleConfig), 'guest'); $assertionPluginManager = new AssertionPluginManager(new ServiceManager(), $assertionPluginConfig); $identity = new Identity((array) $role); $authorizationService = new AuthorizationService(new Rbac(), $roleService, $assertionPluginManager, $assertions); diff --git a/test/Service/RoleServiceTest.php b/test/Service/RoleServiceTest.php index 3132266b..289b88a1 100644 --- a/test/Service/RoleServiceTest.php +++ b/test/Service/RoleServiceTest.php @@ -63,7 +63,7 @@ public function testReturnGuestRoleIfGuestIdentityIsGiven(): void public function testReturnTraversableRolesFromIdentityGiven(): void { - $roleService = new RoleService(new InMemoryRoleProvider([])); + $roleService = new RoleService(new InMemoryRoleProvider([]), 'guest'); $identity = $this->prophesize(IdentityInterface::class); $identity->getRoles()->willReturn($roles = new \ArrayIterator(['first', 'second', 'third'])); @@ -81,7 +81,7 @@ public function testWillNotInvokeRoleProviderIfAllRolesCollected(): void $roleProvider = $this->prophesize(RoleProviderInterface::class); $roleProvider->getRoles(Argument::any())->shouldNotBeCalled(); - $roleService = new RoleService($roleProvider->reveal()); + $roleService = new RoleService($roleProvider->reveal(), 'guest'); $roles = [new Role('first'), new HierarchicalRole('second'), new Role('third')]; $identity = new Identity($roles); @@ -98,7 +98,7 @@ public function testWillCollectRolesOnlyIfRequired(): void $roles = [new Role('first'), new HierarchicalRole('second'), 'third']; $roleProvider->getRoles(['third'])->shouldBeCalled()->willReturn([new Role('third')]); - $roleService = new RoleService($roleProvider->reveal()); + $roleService = new RoleService($roleProvider->reveal(), 'guest'); $identity = new Identity($roles); $result = $roleService->getIdentityRoles($identity); From 5150cdbfc6aeeba5a0c737029cfddafdfa46e36c Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Fri, 23 Feb 2018 09:07:59 +0200 Subject: [PATCH 08/15] fix cs issues --- test/Service/AuthorizationServiceTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Service/AuthorizationServiceTest.php b/test/Service/AuthorizationServiceTest.php index 8598e7aa..d23ad3d9 100644 --- a/test/Service/AuthorizationServiceTest.php +++ b/test/Service/AuthorizationServiceTest.php @@ -22,8 +22,8 @@ namespace ZfcRbacTest\Service; use PHPUnit\Framework\TestCase; -use ZfcRbac\Assertion\AssertionContainerInterface; use Zend\ServiceManager\ServiceManager; +use ZfcRbac\Assertion\AssertionContainerInterface; use ZfcRbac\Assertion\AssertionPluginManager; use ZfcRbac\Assertion\AssertionSet; use ZfcRbac\Exception\InvalidArgumentException; From 2a2586104a0b91fb8e26c088240eb4e54372aaef Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Fri, 23 Feb 2018 09:28:14 +0200 Subject: [PATCH 09/15] rename AssertionPluginManager to AssertionContainer --- config/dependencies.global.php | 14 ++-- ...uginManager.php => AssertionContainer.php} | 2 +- src/Assertion/AssertionSet.php | 10 +-- ...tory.php => AssertionContainerFactory.php} | 8 +- src/Container/AuthorizationServiceFactory.php | 4 +- src/Service/AuthorizationService.php | 8 +- ...gerTest.php => AssertionContainerTest.php} | 10 +-- test/Assertion/AssertionSetTest.php | 76 +++++++++---------- ....php => AssertionContainerFactoryTest.php} | 12 +-- .../AuthorizationServiceFactoryTest.php | 3 +- test/Service/AuthorizationServiceTest.php | 56 +++++++------- 11 files changed, 101 insertions(+), 102 deletions(-) rename src/Assertion/{AssertionPluginManager.php => AssertionContainer.php} (92%) rename src/Container/{AssertionPluginManagerFactory.php => AssertionContainerFactory.php} (88%) rename test/Assertion/{AssertionPluginManagerTest.php => AssertionContainerTest.php} (86%) rename test/Container/{AssertionPluginManagerFactoryTest.php => AssertionContainerFactoryTest.php} (79%) diff --git a/config/dependencies.global.php b/config/dependencies.global.php index 4b392975..0b8a4ee7 100644 --- a/config/dependencies.global.php +++ b/config/dependencies.global.php @@ -21,13 +21,13 @@ return [ 'dependencies' => [ 'factories' => [ - ZfcRbac\Assertion\AssertionPluginManager::class => ZfcRbac\Container\AssertionPluginManagerFactory::class, - ZfcRbac\Options\ModuleOptions::class => ZfcRbac\Container\ModuleOptionsFactory::class, - ZfcRbac\Role\InMemoryRoleProvider::class => ZfcRbac\Container\InMemoryRoleProviderFactory::class, - ZfcRbac\Role\ObjectRepositoryRoleProvider::class => ZfcRbac\Container\ObjectRepositoryRoleProviderFactory::class, - ZfcRbac\Service\AuthorizationServiceInterface::class => ZfcRbac\Container\AuthorizationServiceFactory::class, - ZfcRbac\Service\RoleServiceInterface::class => ZfcRbac\Container\RoleServiceFactory::class, - ZfcRbac\Rbac::class => \Zend\ServiceManager\Factory\InvokableFactory::class, + ZfcRbac\Assertion\AssertionContainerInterface::class => ZfcRbac\Container\AssertionContainerFactory::class, + ZfcRbac\Options\ModuleOptions::class => ZfcRbac\Container\ModuleOptionsFactory::class, + ZfcRbac\Role\InMemoryRoleProvider::class => ZfcRbac\Container\InMemoryRoleProviderFactory::class, + ZfcRbac\Role\ObjectRepositoryRoleProvider::class => ZfcRbac\Container\ObjectRepositoryRoleProviderFactory::class, + ZfcRbac\Service\AuthorizationServiceInterface::class => ZfcRbac\Container\AuthorizationServiceFactory::class, + ZfcRbac\Service\RoleServiceInterface::class => ZfcRbac\Container\RoleServiceFactory::class, + ZfcRbac\Rbac::class => \Zend\ServiceManager\Factory\InvokableFactory::class, ], ], diff --git a/src/Assertion/AssertionPluginManager.php b/src/Assertion/AssertionContainer.php similarity index 92% rename from src/Assertion/AssertionPluginManager.php rename to src/Assertion/AssertionContainer.php index af4ade6e..c3242fc0 100644 --- a/src/Assertion/AssertionPluginManager.php +++ b/src/Assertion/AssertionContainer.php @@ -29,7 +29,7 @@ * @author Aeneas Rekkas * @licence MIT */ -final class AssertionPluginManager extends AbstractPluginManager implements AssertionContainerInterface +final class AssertionContainer extends AbstractPluginManager implements AssertionContainerInterface { protected $instanceOf = AssertionInterface::class; diff --git a/src/Assertion/AssertionSet.php b/src/Assertion/AssertionSet.php index d0ebba79..b7debbe5 100644 --- a/src/Assertion/AssertionSet.php +++ b/src/Assertion/AssertionSet.php @@ -35,7 +35,7 @@ final class AssertionSet implements AssertionInterface /** * @var AssertionContainerInterface */ - private $assertionPluginManager; + private $assertionContainer; /** * @var array @@ -44,7 +44,7 @@ final class AssertionSet implements AssertionInterface private $condition = self::CONDITION_AND; - public function __construct(AssertionContainerInterface $assertionPluginManager, array $assertions) + public function __construct(AssertionContainerInterface $assertionContainer, array $assertions) { if (isset($assertions['condition'])) { if ($assertions['condition'] !== AssertionSet::CONDITION_AND @@ -58,7 +58,7 @@ public function __construct(AssertionContainerInterface $assertionPluginManager, } $this->assertions = $assertions; - $this->assertionPluginManager = $assertionPluginManager; + $this->assertionContainer = $assertionContainer; } public function assert(string $permission, IdentityInterface $identity = null, $context = null): bool @@ -78,12 +78,12 @@ public function assert(string $permission, IdentityInterface $identity = null, $ $asserted = $assertion->assert($permission, $identity, $context); break; case is_string($assertion): - $this->assertions[$index] = $assertion = $this->assertionPluginManager->get($assertion); + $this->assertions[$index] = $assertion = $this->assertionContainer->get($assertion); $asserted = $assertion->assert($permission, $identity, $context); break; case is_array($assertion): - $this->assertions[$index] = $assertion = new AssertionSet($this->assertionPluginManager, $assertion); + $this->assertions[$index] = $assertion = new AssertionSet($this->assertionContainer, $assertion); $asserted = $assertion->assert($permission, $identity, $context); break; default: diff --git a/src/Container/AssertionPluginManagerFactory.php b/src/Container/AssertionContainerFactory.php similarity index 88% rename from src/Container/AssertionPluginManagerFactory.php rename to src/Container/AssertionContainerFactory.php index c8e64b48..57b1b0f2 100644 --- a/src/Container/AssertionPluginManagerFactory.php +++ b/src/Container/AssertionContainerFactory.php @@ -22,7 +22,7 @@ namespace ZfcRbac\Container; use Psr\Container\ContainerInterface; -use ZfcRbac\Assertion\AssertionPluginManager; +use ZfcRbac\Assertion\AssertionContainer; /** * Factory to create a assertion plugin manager @@ -30,12 +30,12 @@ * @author Aeneas Rekkas * @licence MIT */ -final class AssertionPluginManagerFactory +final class AssertionContainerFactory { - public function __invoke(ContainerInterface $container): AssertionPluginManager + public function __invoke(ContainerInterface $container): AssertionContainer { $config = $container->get('config')['zfc_rbac']['assertion_manager']; - return new AssertionPluginManager($container, $config); + return new AssertionContainer($container, $config); } } diff --git a/src/Container/AuthorizationServiceFactory.php b/src/Container/AuthorizationServiceFactory.php index 25eaf81d..bce0bcad 100644 --- a/src/Container/AuthorizationServiceFactory.php +++ b/src/Container/AuthorizationServiceFactory.php @@ -22,7 +22,7 @@ namespace ZfcRbac\Container; use Psr\Container\ContainerInterface; -use ZfcRbac\Assertion\AssertionPluginManager; +use ZfcRbac\Assertion\AssertionContainerInterface; use ZfcRbac\Options\ModuleOptions; use ZfcRbac\Rbac; use ZfcRbac\Service\AuthorizationService; @@ -43,7 +43,7 @@ public function __invoke(ContainerInterface $container): AuthorizationService return new AuthorizationService( $container->get(Rbac::class), $container->get(RoleServiceInterface::class), - $container->get(AssertionPluginManager::class), + $container->get(AssertionContainerInterface::class), $moduleOptions->getAssertionMap() ); } diff --git a/src/Service/AuthorizationService.php b/src/Service/AuthorizationService.php index b4cda041..58a505ae 100644 --- a/src/Service/AuthorizationService.php +++ b/src/Service/AuthorizationService.php @@ -48,7 +48,7 @@ final class AuthorizationService implements AuthorizationServiceInterface /** * @var AssertionContainerInterface */ - private $assertionPluginManager; + private $assertionContainer; /** * @var array @@ -58,12 +58,12 @@ final class AuthorizationService implements AuthorizationServiceInterface public function __construct( Rbac $rbac, RoleServiceInterface $roleService, - AssertionContainerInterface $assertionPluginManager, + AssertionContainerInterface $assertionContainer, array $assertions = [] ) { $this->rbac = $rbac; $this->roleService = $roleService; - $this->assertionPluginManager = $assertionPluginManager; + $this->assertionContainer = $assertionContainer; $this->assertions = $assertions; } @@ -86,7 +86,7 @@ public function isGranted(?IdentityInterface $identity, string $permission, $con $permissionAssertions = [$this->assertions[$permission]]; } - $assertionSet = new AssertionSet($this->assertionPluginManager, $permissionAssertions); + $assertionSet = new AssertionSet($this->assertionContainer, $permissionAssertions); return $assertionSet->assert($permission, $identity, $context); } diff --git a/test/Assertion/AssertionPluginManagerTest.php b/test/Assertion/AssertionContainerTest.php similarity index 86% rename from test/Assertion/AssertionPluginManagerTest.php rename to test/Assertion/AssertionContainerTest.php index 6cb68d6e..73bf7952 100644 --- a/test/Assertion/AssertionPluginManagerTest.php +++ b/test/Assertion/AssertionContainerTest.php @@ -24,19 +24,19 @@ use Interop\Container\ContainerInterface; use PHPUnit\Framework\TestCase; use Zend\ServiceManager\Exception\InvalidServiceException; +use ZfcRbac\Assertion\AssertionContainer; use ZfcRbac\Assertion\AssertionInterface; -use ZfcRbac\Assertion\AssertionPluginManager; /** - * @covers \ZfcRbac\Assertion\AssertionPluginManager + * @covers \ZfcRbac\Assertion\AssertionContainer */ -class AssertionPluginManagerTest extends TestCase +class AssertionContainerTest extends TestCase { public function testValidationOfPluginSucceedsIfAssertionInterfaceIsImplemented() { $containerMock = $this->getMockBuilder(ContainerInterface::class)->getMock(); $pluginMock = $this->getMockBuilder(AssertionInterface::class)->getMock(); - $pluginManager = new AssertionPluginManager($containerMock); + $pluginManager = new AssertionContainer($containerMock); $this->assertNull($pluginManager->validate($pluginMock)); } @@ -45,7 +45,7 @@ public function testValidationOfPluginFailsIfAssertionInterfaceIsNotImplemented( { $this->expectException(InvalidServiceException::class); $containerMock = $this->getMockBuilder(ContainerInterface::class)->getMock(); - $pluginManager = new AssertionPluginManager($containerMock); + $pluginManager = new AssertionContainer($containerMock); $plugin = new \stdClass(); $pluginManager->validate($plugin); diff --git a/test/Assertion/AssertionSetTest.php b/test/Assertion/AssertionSetTest.php index c27d008f..86e8e64e 100644 --- a/test/Assertion/AssertionSetTest.php +++ b/test/Assertion/AssertionSetTest.php @@ -35,42 +35,42 @@ class AssertionSetTest extends TestCase { public function testImplementsAssertionInterface() { - $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionPluginManager, []); + $assertionContainer = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); + $assertionSet = new AssertionSet($assertionContainer, []); $this->assertInstanceOf(AssertionInterface::class, $assertionSet); } public function testWhenNoAssertionsArePresentTheAssertionWillFail() { - $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionPluginManager, []); + $assertionContainer = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); + $assertionSet = new AssertionSet($assertionContainer, []); $this->assertFalse($assertionSet->assert('foo')); } public function testAcceptsAnAndCondition() { - $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionPluginManager, ['condition' => AssertionSet::CONDITION_AND]); + $assertionContainer = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); + $assertionSet = new AssertionSet($assertionContainer, ['condition' => AssertionSet::CONDITION_AND]); $this->assertFalse($assertionSet->assert('foo')); } public function testAcceptsAnOrCondition() { - $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionPluginManager, ['condition' => AssertionSet::CONDITION_OR]); + $assertionContainer = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); + $assertionSet = new AssertionSet($assertionContainer, ['condition' => AssertionSet::CONDITION_OR]); $this->assertFalse($assertionSet->assert('foo')); } public function testThrowsExceptionForAnUnknownCondition() { - $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); + $assertionContainer = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); $this->expectException(InvalidArgumentException::class); - new AssertionSet($assertionPluginManager, ['condition' => 'unknown']); + new AssertionSet($assertionContainer, ['condition' => 'unknown']); } public function testWhenNoConditionIsGivenAndIsUsed() @@ -78,11 +78,11 @@ public function testWhenNoConditionIsGivenAndIsUsed() $fooAssertion = new SimpleAssertion(true); $barAssertion = new SimpleAssertion(false); - $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionPluginManager, ['fooFactory', 'barFactory']); + $assertionContainer = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); + $assertionSet = new AssertionSet($assertionContainer, ['fooFactory', 'barFactory']); - $assertionPluginManager->expects($this->at(0))->method('get')->with('fooFactory')->willReturn($fooAssertion); - $assertionPluginManager->expects($this->at(1))->method('get')->with('barFactory')->willReturn($barAssertion); + $assertionContainer->expects($this->at(0))->method('get')->with('fooFactory')->willReturn($fooAssertion); + $assertionContainer->expects($this->at(1))->method('get')->with('barFactory')->willReturn($barAssertion); $this->assertFalse($assertionSet->assert('permission')); @@ -95,10 +95,10 @@ public function testAndConditionWillBreakEarlyWithFailure() $fooAssertion = new SimpleAssertion(false); $barAssertion = new SimpleAssertion(true); - $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionPluginManager, ['fooFactory', 'barFactory', 'condition' => AssertionSet::CONDITION_AND]); + $assertionContainer = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); + $assertionSet = new AssertionSet($assertionContainer, ['fooFactory', 'barFactory', 'condition' => AssertionSet::CONDITION_AND]); - $assertionPluginManager->expects($this->at(0))->method('get')->with('fooFactory')->willReturn($fooAssertion); + $assertionContainer->expects($this->at(0))->method('get')->with('fooFactory')->willReturn($fooAssertion); $this->assertFalse($assertionSet->assert('permission')); @@ -111,10 +111,10 @@ public function testOrConditionWillBreakEarlyWithSuccess() $fooAssertion = new SimpleAssertion(true); $barAssertion = new SimpleAssertion(false); - $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionPluginManager, ['fooFactory', 'barFactory', 'condition' => AssertionSet::CONDITION_OR]); + $assertionContainer = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); + $assertionSet = new AssertionSet($assertionContainer, ['fooFactory', 'barFactory', 'condition' => AssertionSet::CONDITION_OR]); - $assertionPluginManager->expects($this->at(0))->method('get')->with('fooFactory')->willReturn($fooAssertion); + $assertionContainer->expects($this->at(0))->method('get')->with('fooFactory')->willReturn($fooAssertion); $this->assertTrue($assertionSet->assert('permission')); @@ -126,10 +126,10 @@ public function testAssertionsAsStringsAreCached() { $fooAssertion = new SimpleAssertion(true); - $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionPluginManager, ['fooFactory']); + $assertionContainer = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); + $assertionSet = new AssertionSet($assertionContainer, ['fooFactory']); - $assertionPluginManager->expects($this->once())->method('get')->with('fooFactory')->willReturn($fooAssertion); + $assertionContainer->expects($this->once())->method('get')->with('fooFactory')->willReturn($fooAssertion); $this->assertTrue($assertionSet->assert('permission')); $this->assertTrue($assertionSet->assert('permission')); @@ -142,10 +142,10 @@ public function testUsesAssertionsAsStrings() { $fooAssertion = new SimpleAssertion(true); - $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionPluginManager, ['fooFactory']); + $assertionContainer = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); + $assertionSet = new AssertionSet($assertionContainer, ['fooFactory']); - $assertionPluginManager->expects($this->once())->method('get')->with('fooFactory')->willReturn($fooAssertion); + $assertionContainer->expects($this->once())->method('get')->with('fooFactory')->willReturn($fooAssertion); $this->assertTrue($assertionSet->assert('permission')); @@ -156,8 +156,8 @@ public function testUsesAssertionsAsInstances() { $fooAssertion = new SimpleAssertion(true); - $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionPluginManager, [$fooAssertion]); + $assertionContainer = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); + $assertionSet = new AssertionSet($assertionContainer, [$fooAssertion]); $this->assertTrue($assertionSet->assert('permission')); @@ -173,8 +173,8 @@ public function testUsesAssertionsAsCallables() return true; }; - $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionPluginManager, [$fooAssertion]); + $assertionContainer = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); + $assertionSet = new AssertionSet($assertionContainer, [$fooAssertion]); $this->assertTrue($assertionSet->assert('permission')); @@ -186,11 +186,11 @@ public function testUsesAssertionsAsArrays() $fooAssertion = new SimpleAssertion(true); $barAssertion = new SimpleAssertion(true); - $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionPluginManager, ['fooFactory', ['barFactory']]); + $assertionContainer = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); + $assertionSet = new AssertionSet($assertionContainer, ['fooFactory', ['barFactory']]); - $assertionPluginManager->expects($this->at(0))->method('get')->with('fooFactory')->willReturn($fooAssertion); - $assertionPluginManager->expects($this->at(1))->method('get')->with('barFactory')->willReturn($barAssertion); + $assertionContainer->expects($this->at(0))->method('get')->with('fooFactory')->willReturn($fooAssertion); + $assertionContainer->expects($this->at(1))->method('get')->with('barFactory')->willReturn($barAssertion); $this->assertTrue($assertionSet->assert('permission')); @@ -202,8 +202,8 @@ public function testThrowExceptionForInvalidAssertion() { $fooAssertion = new \stdClass(); - $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionPluginManager, [$fooAssertion]); + $assertionContainer = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); + $assertionSet = new AssertionSet($assertionContainer, [$fooAssertion]); $this->expectException(InvalidArgumentException::class); $this->assertTrue($assertionSet->assert('permission')); @@ -214,8 +214,8 @@ public function testThrowExceptionForInvalidAssertion() */ public function testMatrix(array $assertions, bool $expectedResult, array $assertionCalledCount) { - $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionPluginManager, $assertions); + $assertionContainer = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); + $assertionSet = new AssertionSet($assertionContainer, $assertions); $this->assertSame($expectedResult, $assertionSet->assert('permission')); diff --git a/test/Container/AssertionPluginManagerFactoryTest.php b/test/Container/AssertionContainerFactoryTest.php similarity index 79% rename from test/Container/AssertionPluginManagerFactoryTest.php rename to test/Container/AssertionContainerFactoryTest.php index 2308a78b..d74d7758 100644 --- a/test/Container/AssertionPluginManagerFactoryTest.php +++ b/test/Container/AssertionContainerFactoryTest.php @@ -23,13 +23,13 @@ use PHPUnit\Framework\TestCase; use Zend\ServiceManager\ServiceManager; -use ZfcRbac\Assertion\AssertionPluginManager; -use ZfcRbac\Container\AssertionPluginManagerFactory; +use ZfcRbac\Assertion\AssertionContainer; +use ZfcRbac\Container\AssertionContainerFactory; /** - * @covers \ZfcRbac\Container\AssertionPluginManagerFactory + * @covers \ZfcRbac\Container\AssertionContainerFactory */ -class AssertionPluginManagerFactoryTest extends TestCase +class AssertionContainerFactoryTest extends TestCase { public function testFactory(): void { @@ -40,9 +40,9 @@ public function testFactory(): void ], ]); - $factory = new AssertionPluginManagerFactory(); + $factory = new AssertionContainerFactory(); $pluginManager = $factory($serviceManager); - $this->assertInstanceOf(AssertionPluginManager::class, $pluginManager); + $this->assertInstanceOf(AssertionContainer::class, $pluginManager); } } diff --git a/test/Container/AuthorizationServiceFactoryTest.php b/test/Container/AuthorizationServiceFactoryTest.php index e4e4e40d..f16061cf 100644 --- a/test/Container/AuthorizationServiceFactoryTest.php +++ b/test/Container/AuthorizationServiceFactoryTest.php @@ -24,7 +24,6 @@ use PHPUnit\Framework\TestCase; use Psr\Container\ContainerInterface; use ZfcRbac\Assertion\AssertionContainerInterface; -use ZfcRbac\Assertion\AssertionPluginManager; use ZfcRbac\Container\AuthorizationServiceFactory; use ZfcRbac\Options\ModuleOptions; use ZfcRbac\Rbac; @@ -41,7 +40,7 @@ public function testCanCreateAuthorizationService(): void $container = $this->prophesize(ContainerInterface::class); $container->get(ModuleOptions::class)->willReturn(new ModuleOptions([])); $container->get(RoleServiceInterface::class)->willReturn($this->createMock(RoleServiceInterface::class)); - $container->get(AssertionPluginManager::class)->willReturn($this->createMock(AssertionContainerInterface::class)); + $container->get(AssertionContainerInterface::class)->willReturn($this->createMock(AssertionContainerInterface::class)); $container->get(Rbac::class)->willReturn(new Rbac()); $factory = new AuthorizationServiceFactory(); diff --git a/test/Service/AuthorizationServiceTest.php b/test/Service/AuthorizationServiceTest.php index d23ad3d9..d0a6cdab 100644 --- a/test/Service/AuthorizationServiceTest.php +++ b/test/Service/AuthorizationServiceTest.php @@ -23,8 +23,8 @@ use PHPUnit\Framework\TestCase; use Zend\ServiceManager\ServiceManager; +use ZfcRbac\Assertion\AssertionContainer; use ZfcRbac\Assertion\AssertionContainerInterface; -use ZfcRbac\Assertion\AssertionPluginManager; use ZfcRbac\Assertion\AssertionSet; use ZfcRbac\Exception\InvalidArgumentException; use ZfcRbac\Identity\IdentityInterface; @@ -182,9 +182,9 @@ public function testGranted($role, $permission, $context, bool $isGranted, array ]; $roleService = new RoleService(new InMemoryRoleProvider($roleConfig), 'guest'); - $assertionPluginManager = new AssertionPluginManager(new ServiceManager(), $assertionPluginConfig); + $assertionContainer = new AssertionContainer(new ServiceManager(), $assertionPluginConfig); $identity = new Identity((array) $role); - $authorizationService = new AuthorizationService(new Rbac(), $roleService, $assertionPluginManager, $assertions); + $authorizationService = new AuthorizationService(new Rbac(), $roleService, $assertionContainer, $assertions); $this->assertEquals($isGranted, $authorizationService->isGranted($identity, $permission, $context)); } @@ -197,10 +197,10 @@ public function testDoNotCallAssertionIfThePermissionIsNotGranted(): void $roleService = $this->getMockBuilder(RoleServiceInterface::class)->getMock(); $roleService->expects($this->once())->method('getIdentityRoles')->will($this->returnValue([$role])); - $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); - $assertionPluginManager->expects($this->never())->method('get'); + $assertionContainer = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); + $assertionContainer->expects($this->never())->method('get'); - $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager); + $authorizationService = new AuthorizationService($rbac, $roleService, $assertionContainer); $this->assertFalse($authorizationService->isGranted(null, 'foo')); } @@ -215,10 +215,10 @@ public function testReturnsFalseForIdentityWithoutRoles(): void $roleService = $this->getMockBuilder(RoleServiceInterface::class)->getMock(); $roleService->expects($this->once())->method('getIdentityRoles')->will($this->returnValue($identity->getRoles())); - $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); - $assertionPluginManager->expects($this->never())->method('get'); + $assertionContainer = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); + $assertionContainer->expects($this->never())->method('get'); - $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager); + $authorizationService = new AuthorizationService($rbac, $roleService, $assertionContainer); $this->assertFalse($authorizationService->isGranted($identity, 'foo')); } @@ -234,10 +234,10 @@ public function testReturnsTrueForIdentityWhenHasPermissionButNoAssertionsExists $rbac = $this->getMockBuilder(Rbac::class)->disableOriginalConstructor()->getMock(); $rbac->expects($this->once())->method('isGranted')->willReturn(true); - $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); - $assertionPluginManager->expects($this->never())->method('get'); + $assertionContainer = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); + $assertionContainer->expects($this->never())->method('get'); - $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager); + $authorizationService = new AuthorizationService($rbac, $roleService, $assertionContainer); $this->assertTrue($authorizationService->isGranted($identity, 'foo')); } @@ -254,10 +254,10 @@ public function testUsesAssertionsAsInstances(): void $rbac = $this->getMockBuilder(Rbac::class)->disableOriginalConstructor()->getMock(); $rbac->expects($this->once())->method('isGranted')->willReturn(true); - $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); - $assertionPluginManager->expects($this->never())->method('get'); + $assertionContainer = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); + $assertionContainer->expects($this->never())->method('get'); - $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager, ['foo' => $assertion]); + $authorizationService = new AuthorizationService($rbac, $roleService, $assertionContainer, ['foo' => $assertion]); $authorizationService->isGranted($identity, 'foo'); @@ -276,10 +276,10 @@ public function testUsesAssertionsAsStrings(): void $rbac = $this->getMockBuilder(Rbac::class)->disableOriginalConstructor()->getMock(); $rbac->expects($this->once())->method('isGranted')->willReturn(true); - $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); - $assertionPluginManager->expects($this->once())->method('get')->with('fooFactory')->willReturn($assertion); + $assertionContainer = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); + $assertionContainer->expects($this->once())->method('get')->with('fooFactory')->willReturn($assertion); - $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager, ['foo' => 'fooFactory']); + $authorizationService = new AuthorizationService($rbac, $roleService, $assertionContainer, ['foo' => 'fooFactory']); $authorizationService->isGranted($identity, 'foo'); @@ -297,12 +297,12 @@ public function testUsesAssertionsAsCallable(): void $rbac = $this->getMockBuilder(Rbac::class)->disableOriginalConstructor()->getMock(); $rbac->expects($this->once())->method('isGranted')->willReturn(true); - $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); - $assertionPluginManager->expects($this->never())->method('get'); + $assertionContainer = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); + $assertionContainer->expects($this->never())->method('get'); $called = false; - $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager, + $authorizationService = new AuthorizationService($rbac, $roleService, $assertionContainer, [ 'foo' => function ($permission, IdentityInterface $identity = null, $context = null) use (&$called) { $called = true; @@ -328,13 +328,13 @@ public function testUsesAssertionsAsArrays(): void $rbac = $this->getMockBuilder(Rbac::class)->disableOriginalConstructor()->getMock(); $rbac->expects($this->once())->method('isGranted')->willReturn(true); - $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); - $assertionPluginManager->expects($this->never())->method('get'); + $assertionContainer = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); + $assertionContainer->expects($this->never())->method('get'); $called1 = false; $called2 = false; - $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager, [ + $authorizationService = new AuthorizationService($rbac, $roleService, $assertionContainer, [ 'foo' => [ function ($permission, IdentityInterface $identity = null, $context = null) use (&$called1) { $called1 = true; @@ -365,8 +365,8 @@ public function testThrowExceptionForInvalidAssertion(): void $roleService = $this->getMockBuilder(RoleServiceInterface::class)->getMock(); $roleService->expects($this->once())->method('getIdentityRoles')->will($this->returnValue([$role])); - $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->disableOriginalConstructor()->getMock(); - $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager, ['foo' => new \stdClass()]); + $assertionContainer = $this->getMockBuilder(AssertionContainerInterface::class)->disableOriginalConstructor()->getMock(); + $authorizationService = new AuthorizationService($rbac, $roleService, $assertionContainer, ['foo' => new \stdClass()]); $this->expectException(InvalidArgumentException::class); @@ -380,8 +380,8 @@ public function testContextIsPassedToRoleService(): void $rbac = $this->getMockBuilder(Rbac::class)->disableOriginalConstructor()->getMock(); $roleService = $this->getMockBuilder(RoleServiceInterface::class)->getMock(); - $assertionPluginManager = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); - $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager); + $assertionContainer = $this->getMockBuilder(AssertionContainerInterface::class)->getMock(); + $authorizationService = new AuthorizationService($rbac, $roleService, $assertionContainer); $roleService->expects($this->once())->method('getIdentityRoles')->with($identity, $context)->willReturn([]); $authorizationService->isGranted($identity, 'foo', $context); From 7e370363a226c55a2a411d134f4bbc3a11185106 Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Fri, 23 Feb 2018 09:39:10 +0200 Subject: [PATCH 10/15] remove those spaces... gives only problems and cs fixer does not work correctly to fix them --- .php_cs | 3 +-- config/dependencies.global.php | 14 +++++++------- .../ObjectRepositoryRoleProviderFactoryTest.php | 6 +++--- test/Container/RoleServiceFactoryTest.php | 6 +++--- test/Options/ModuleOptionsTest.php | 2 +- test/Role/InMemoryRoleProviderTest.php | 4 ++-- test/Service/AuthorizationServiceTest.php | 10 +++++----- 7 files changed, 22 insertions(+), 23 deletions(-) diff --git a/.php_cs b/.php_cs index c97631f8..483979bc 100644 --- a/.php_cs +++ b/.php_cs @@ -18,8 +18,7 @@ class Config extends PhpCsFixerConfig '@PHP71Migration' => true, 'array_syntax' => ['syntax' => 'short'], 'binary_operator_spaces' => [ - 'align_double_arrow' => true, - 'align_equals' => false, + 'default' => 'single_space', ], 'blank_line_after_opening_tag' => true, 'blank_line_after_namespace' => true, diff --git a/config/dependencies.global.php b/config/dependencies.global.php index 0b8a4ee7..100e2636 100644 --- a/config/dependencies.global.php +++ b/config/dependencies.global.php @@ -21,13 +21,13 @@ return [ 'dependencies' => [ 'factories' => [ - ZfcRbac\Assertion\AssertionContainerInterface::class => ZfcRbac\Container\AssertionContainerFactory::class, - ZfcRbac\Options\ModuleOptions::class => ZfcRbac\Container\ModuleOptionsFactory::class, - ZfcRbac\Role\InMemoryRoleProvider::class => ZfcRbac\Container\InMemoryRoleProviderFactory::class, - ZfcRbac\Role\ObjectRepositoryRoleProvider::class => ZfcRbac\Container\ObjectRepositoryRoleProviderFactory::class, - ZfcRbac\Service\AuthorizationServiceInterface::class => ZfcRbac\Container\AuthorizationServiceFactory::class, - ZfcRbac\Service\RoleServiceInterface::class => ZfcRbac\Container\RoleServiceFactory::class, - ZfcRbac\Rbac::class => \Zend\ServiceManager\Factory\InvokableFactory::class, + ZfcRbac\Assertion\AssertionContainerInterface::class => ZfcRbac\Container\AssertionContainerFactory::class, + ZfcRbac\Options\ModuleOptions::class => ZfcRbac\Container\ModuleOptionsFactory::class, + ZfcRbac\Role\InMemoryRoleProvider::class => ZfcRbac\Container\InMemoryRoleProviderFactory::class, + ZfcRbac\Role\ObjectRepositoryRoleProvider::class => ZfcRbac\Container\ObjectRepositoryRoleProviderFactory::class, + ZfcRbac\Service\AuthorizationServiceInterface::class => ZfcRbac\Container\AuthorizationServiceFactory::class, + ZfcRbac\Service\RoleServiceInterface::class => ZfcRbac\Container\RoleServiceFactory::class, + ZfcRbac\Rbac::class => \Zend\ServiceManager\Factory\InvokableFactory::class, ], ], diff --git a/test/Container/ObjectRepositoryRoleProviderFactoryTest.php b/test/Container/ObjectRepositoryRoleProviderFactoryTest.php index ecfd5836..bc27f6b8 100644 --- a/test/Container/ObjectRepositoryRoleProviderFactoryTest.php +++ b/test/Container/ObjectRepositoryRoleProviderFactoryTest.php @@ -42,7 +42,7 @@ public function testFactoryUsingObjectRepository(): void 'role_provider' => [ ObjectRepositoryRoleProvider::class => [ 'role_name_property' => 'name', - 'object_repository' => 'RoleObjectRepository', + 'object_repository' => 'RoleObjectRepository', ], ], ])); @@ -59,8 +59,8 @@ public function testFactoryUsingObjectManager(): void 'role_provider' => [ ObjectRepositoryRoleProvider::class => [ 'role_name_property' => 'name', - 'object_manager' => 'ObjectManager', - 'class_name' => 'Role', + 'object_manager' => 'ObjectManager', + 'class_name' => 'Role', ], ], ])); diff --git a/test/Container/RoleServiceFactoryTest.php b/test/Container/RoleServiceFactoryTest.php index bd87b4d7..3bf89154 100644 --- a/test/Container/RoleServiceFactoryTest.php +++ b/test/Container/RoleServiceFactoryTest.php @@ -36,7 +36,7 @@ class RoleServiceFactoryTest extends TestCase public function testCanCreateRoleService(): void { $options = new ModuleOptions([ - 'guest_role' => 'guest', + 'guest_role' => 'guest', 'role_provider' => [ \ZfcRbac\Role\InMemoryRoleProvider::class => [ 'foo', @@ -45,7 +45,7 @@ public function testCanCreateRoleService(): void ]); $container = new ServiceManager(['services' => [ - ModuleOptions::class => $options, + ModuleOptions::class => $options, RoleProviderInterface::class => new InMemoryRoleProvider([]), ]]); @@ -60,7 +60,7 @@ public function testThrowExceptionIfNoRoleProvider(): void $this->expectException(\Psr\Container\NotFoundExceptionInterface::class); $options = new ModuleOptions([ - 'guest_role' => 'guest', + 'guest_role' => 'guest', 'role_provider' => [], ]); diff --git a/test/Options/ModuleOptionsTest.php b/test/Options/ModuleOptionsTest.php index f8e8b020..9d0b4187 100644 --- a/test/Options/ModuleOptionsTest.php +++ b/test/Options/ModuleOptionsTest.php @@ -42,7 +42,7 @@ public function testAssertModuleDefaultOptions(): void public function testSettersAndGetters(): void { $moduleOptions = new ModuleOptions([ - 'guest_role' => 'unknown', + 'guest_role' => 'unknown', 'role_provider' => [], 'assertion_map' => [ 'foo' => 'bar', diff --git a/test/Role/InMemoryRoleProviderTest.php b/test/Role/InMemoryRoleProviderTest.php index 66a98b1e..4b2f52f2 100644 --- a/test/Role/InMemoryRoleProviderTest.php +++ b/test/Role/InMemoryRoleProviderTest.php @@ -35,11 +35,11 @@ public function testInMemoryProvider(): void { $inMemoryProvider = new InMemoryRoleProvider([ 'admin' => [ - 'children' => ['member'], + 'children' => ['member'], 'permissions' => ['delete'], ], 'member' => [ - 'children' => ['guest'], + 'children' => ['guest'], 'permissions' => ['write'], ], 'mrx' => [ diff --git a/test/Service/AuthorizationServiceTest.php b/test/Service/AuthorizationServiceTest.php index d0a6cdab..c998b062 100644 --- a/test/Service/AuthorizationServiceTest.php +++ b/test/Service/AuthorizationServiceTest.php @@ -161,22 +161,22 @@ public function grantedProvider(): array public function testGranted($role, $permission, $context, bool $isGranted, array $assertions = []): void { $roleConfig = [ - 'admin' => [ - 'children' => ['member'], + 'admin' => [ + 'children' => ['member'], 'permissions' => ['delete'], ], 'member' => [ - 'children' => ['guest'], + 'children' => ['guest'], 'permissions' => ['write'], ], - 'guest' => [ + 'guest' => [ 'permissions' => ['read'], ], ]; $assertionPluginConfig = [ 'services' => [ - 'true_assertion' => new SimpleAssertion(true), + 'true_assertion' => new SimpleAssertion(true), 'false_assertion' => new SimpleAssertion(false), ], ]; From fc8accb9981debfba6b24165e18b7117f4f49354 Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Fri, 23 Feb 2018 10:15:16 +0200 Subject: [PATCH 11/15] somehow sliped from rebase --- src/Options/ModuleOptions.php | 8 -------- test/Options/ModuleOptionsTest.php | 7 ------- 2 files changed, 15 deletions(-) diff --git a/src/Options/ModuleOptions.php b/src/Options/ModuleOptions.php index 8e999c2e..9bf69d41 100644 --- a/src/Options/ModuleOptions.php +++ b/src/Options/ModuleOptions.php @@ -22,7 +22,6 @@ namespace ZfcRbac\Options; use Zend\Stdlib\AbstractOptions; -use ZfcRbac\Exception; /** * Options for ZfcRbac module @@ -111,16 +110,9 @@ public function getGuestRole(): string * Set the configuration for the role provider * * @param array $roleProvider - * @throws Exception\RuntimeException */ public function setRoleProvider(array $roleProvider): void { - if (count($roleProvider) > 1) { - throw new Exception\RuntimeException( - 'You can only have one role provider' - ); - } - $this->roleProvider = $roleProvider; } diff --git a/test/Options/ModuleOptionsTest.php b/test/Options/ModuleOptionsTest.php index 9d0b4187..6be4e116 100644 --- a/test/Options/ModuleOptionsTest.php +++ b/test/Options/ModuleOptionsTest.php @@ -53,11 +53,4 @@ public function testSettersAndGetters(): void $this->assertEquals([], $moduleOptions->getRoleProvider()); $this->assertEquals(['foo' => 'bar'], $moduleOptions->getAssertionMap()); } - - public function testThrowExceptionIfMoreThanOneRoleProviderIsSet(): void - { - $this->expectException('ZfcRbac\Exception\RuntimeException'); - $moduleOptions = new ModuleOptions(); - $moduleOptions->setRoleProvider(['foo', 'bar']); - } } From a098c92c23b13fe111bc605701dd8644ed43995a Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Fri, 23 Feb 2018 10:30:05 +0200 Subject: [PATCH 12/15] Test AssertionContainerInterface method instead of validate method --- test/Assertion/AssertionContainerTest.php | 32 ++++++++++++++--------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/test/Assertion/AssertionContainerTest.php b/test/Assertion/AssertionContainerTest.php index 73bf7952..65c689a9 100644 --- a/test/Assertion/AssertionContainerTest.php +++ b/test/Assertion/AssertionContainerTest.php @@ -23,9 +23,11 @@ use Interop\Container\ContainerInterface; use PHPUnit\Framework\TestCase; -use Zend\ServiceManager\Exception\InvalidServiceException; +use Psr\Container\ContainerExceptionInterface; +use Zend\ServiceManager\Factory\InvokableFactory; use ZfcRbac\Assertion\AssertionContainer; use ZfcRbac\Assertion\AssertionInterface; +use ZfcRbacTest\Asset\SimpleAssertion; /** * @covers \ZfcRbac\Assertion\AssertionContainer @@ -34,20 +36,26 @@ class AssertionContainerTest extends TestCase { public function testValidationOfPluginSucceedsIfAssertionInterfaceIsImplemented() { - $containerMock = $this->getMockBuilder(ContainerInterface::class)->getMock(); - $pluginMock = $this->getMockBuilder(AssertionInterface::class)->getMock(); - $pluginManager = new AssertionContainer($containerMock); - - $this->assertNull($pluginManager->validate($pluginMock)); + $containerMock = $this->createMock(ContainerInterface::class); + $container = new AssertionContainer($containerMock, [ + 'factories' => [ + SimpleAssertion::class => InvokableFactory::class, + ], + ]); + + $this->assertInstanceOf(AssertionInterface::class, $container->get(SimpleAssertion::class)); } public function testValidationOfPluginFailsIfAssertionInterfaceIsNotImplemented() { - $this->expectException(InvalidServiceException::class); - $containerMock = $this->getMockBuilder(ContainerInterface::class)->getMock(); - $pluginManager = new AssertionContainer($containerMock); - - $plugin = new \stdClass(); - $pluginManager->validate($plugin); + $containerMock = $this->createMock(ContainerInterface::class); + $container = new AssertionContainer($containerMock, [ + 'factories' => [ + \stdClass::class => InvokableFactory::class, + ], + ]); + + $this->expectException(ContainerExceptionInterface::class); + $this->assertInstanceOf(AssertionInterface::class, $container->get(\stdClass::class)); } } From 05a66e257105d044a492db854100c21496194598 Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Fri, 23 Feb 2018 10:58:14 +0200 Subject: [PATCH 13/15] again rebase problems.. last cleanup --- src/Assertion/AssertionSet.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Assertion/AssertionSet.php b/src/Assertion/AssertionSet.php index b7debbe5..684faf8a 100644 --- a/src/Assertion/AssertionSet.php +++ b/src/Assertion/AssertionSet.php @@ -40,7 +40,7 @@ final class AssertionSet implements AssertionInterface /** * @var array */ - private $assertions = []; + private $assertions; private $condition = self::CONDITION_AND; From 274b5180e7414dfc6fb339047667cc636364e603 Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Mon, 26 Feb 2018 09:12:32 +0200 Subject: [PATCH 14/15] Add 100% test coverage --- src/Container/InMemoryRoleProviderFactory.php | 1 + .../InMemoryRoleProviderFactoryTest.php | 58 +++++++++++++++++++ test/RbacTest.php | 16 +++++ 3 files changed, 75 insertions(+) create mode 100644 test/Container/InMemoryRoleProviderFactoryTest.php diff --git a/src/Container/InMemoryRoleProviderFactory.php b/src/Container/InMemoryRoleProviderFactory.php index 71643ffd..bac08f3d 100644 --- a/src/Container/InMemoryRoleProviderFactory.php +++ b/src/Container/InMemoryRoleProviderFactory.php @@ -22,6 +22,7 @@ namespace ZfcRbac\Container; use Psr\Container\ContainerInterface; +use ZfcRbac\Options\ModuleOptions; use ZfcRbac\Role\InMemoryRoleProvider; /** diff --git a/test/Container/InMemoryRoleProviderFactoryTest.php b/test/Container/InMemoryRoleProviderFactoryTest.php new file mode 100644 index 00000000..a425f992 --- /dev/null +++ b/test/Container/InMemoryRoleProviderFactoryTest.php @@ -0,0 +1,58 @@ +setService(ModuleOptions::class, new ModuleOptions([ + 'role_provider' => [ + InMemoryRoleProvider::class => [ + 'admin' => [ + 'children' => ['member'], + 'permissions' => ['delete'], + ], + 'member' => [ + 'children' => ['guest'], + 'permissions' => ['write'], + ], + 'guest', + ], + ], + ])); + + $roleProvider = (new InMemoryRoleProviderFactory())($container); + $this->assertInstanceOf(InMemoryRoleProvider::class, $roleProvider); + $this->assertCount(3, $roleProvider->getRoles(['admin', 'member', 'guest'])); + } +} diff --git a/test/RbacTest.php b/test/RbacTest.php index 0aec4426..32df577e 100644 --- a/test/RbacTest.php +++ b/test/RbacTest.php @@ -114,4 +114,20 @@ public function testReturnFalseIfNoHierarchicalRoleHasPermission(): void $this->assertFalse($rbac->isGranted($parentRole, 'permission')); } + + /** + * @covers \ZfcRbac\Rbac::isGranted + */ + public function testCanCheckTraversableAsRolesList(): void + { + $role1 = new Role('Foo'); + + $role2 = new Role('Bar'); + $role2->addPermission('permission'); + + $roles = new \ArrayIterator([$role1, $role2]); + $rbac = new Rbac(); + + $this->assertTrue($rbac->isGranted($roles, 'permission')); + } } From e52eb56fc4d17102df0bd9bb8a2b84737b51448c Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Mon, 26 Feb 2018 09:17:08 +0200 Subject: [PATCH 15/15] One last thing from feedback --- src/Assertion/AssertionContainer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Assertion/AssertionContainer.php b/src/Assertion/AssertionContainer.php index c3242fc0..2fe7084a 100644 --- a/src/Assertion/AssertionContainer.php +++ b/src/Assertion/AssertionContainer.php @@ -38,6 +38,6 @@ final class AssertionContainer extends AbstractPluginManager implements Assertio */ public function get($name, array $options = null): AssertionInterface { - return parent::get($name, $options); + return parent::get($name); } }