From 66017906e92a73bd7fb632f61b1273328390c12a Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Fri, 17 May 2019 11:13:36 +0300 Subject: [PATCH 1/3] make config ready for zend-expressive and ZF --- composer.json | 4 + src/ConfigProvider.php | 75 +++++++++++++++++++ src/{ModuleConfig.php => Module.php} | 23 +++--- test/ConfigProviderTest.php | 67 +++++++++++++++++ test/{ModuleConfigTest.php => ModuleTest.php} | 30 +++----- test/Role/HierarchicalRoleTest.php | 27 +++++++ 6 files changed, 195 insertions(+), 31 deletions(-) create mode 100644 src/ConfigProvider.php rename src/{ModuleConfig.php => Module.php} (70%) create mode 100644 test/ConfigProviderTest.php rename test/{ModuleConfigTest.php => ModuleTest.php} (63%) diff --git a/composer.json b/composer.json index 21b19f0c..1e62bedc 100644 --- a/composer.json +++ b/composer.json @@ -52,6 +52,10 @@ "branch-alias": { "dev-master": "2.4-dev", "dev-develop": "3.0-dev" + }, + "zf": { + "component": "ZfcRbac", + "config-provider": "ZfcRbac\\ConfigProvider" } }, "scripts": { diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php new file mode 100644 index 00000000..440bc939 --- /dev/null +++ b/src/ConfigProvider.php @@ -0,0 +1,75 @@ + $this->getDependencyConfig(), + 'zfc_rbac' => $this->getModuleConfig(), + ]; + } + + /** + * Returns the container dependencies + */ + public function getDependencyConfig(): array + { + return [ + '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, + ], + ]; + } + + /** + * Get module configuration + */ + public function getModuleConfig(): array + { + return [ + // Assertion plugin manager + 'assertion_manager' => [], + ]; + } +} diff --git a/src/ModuleConfig.php b/src/Module.php similarity index 70% rename from src/ModuleConfig.php rename to src/Module.php index 7cfe8a6c..75482137 100644 --- a/src/ModuleConfig.php +++ b/src/Module.php @@ -21,20 +21,17 @@ namespace ZfcRbac; -/** - * Class ModuleConfig - * - * @author Florent Blaison - * @licence MIT - */ -final class ModuleConfig +final class Module { - public function __invoke(): array + /** + * Return default ZfcRbac configuration for zend-mvc applications. + */ + public function getConfig() { - $config = []; - $config = array_merge_recursive($config, require __DIR__ . '/../config/config.global.php'); - $config = array_merge_recursive($config, require __DIR__ . '/../config/dependencies.global.php'); - - return $config; + $provider = new ConfigProvider(); + return [ + 'service_manager' => $provider->getDependencyConfig(), + 'zfc_rbac' => $provider->getModuleConfig(), + ]; } } diff --git a/test/ConfigProviderTest.php b/test/ConfigProviderTest.php new file mode 100644 index 00000000..0d60827d --- /dev/null +++ b/test/ConfigProviderTest.php @@ -0,0 +1,67 @@ + [ + \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, + ], + ]; + $this->assertEquals($expected, $provider->getDependencyConfig()); + } + + public function testProvidesExpectedModuleConfiguration() + { + $provider = new ConfigProvider(); + $expected = [ + 'assertion_manager' => [], + ]; + $this->assertEquals($expected, $provider->getModuleConfig()); + } + + public function testInvocationProvidesDependencyConfiguration() + { + $provider = new ConfigProvider(); + $expected = [ + 'dependencies' => $provider->getDependencyConfig(), + 'zfc_rbac' => $provider->getModuleConfig(), + ]; + $this->assertEquals($expected, $provider()); + } +} diff --git a/test/ModuleConfigTest.php b/test/ModuleTest.php similarity index 63% rename from test/ModuleConfigTest.php rename to test/ModuleTest.php index ccd5836d..a00cce28 100644 --- a/test/ModuleConfigTest.php +++ b/test/ModuleTest.php @@ -22,28 +22,22 @@ namespace ZfcRbacTest; use PHPUnit\Framework\TestCase; -use ZfcRbac\ModuleConfig; +use ZfcRbac\ConfigProvider; +use ZfcRbac\Module; /** - * @author Bas Kamer - * @covers \ZfcRbac\ModuleConfig + * @covers \ZfcRbac\Module */ -class ModuleConfigTest extends TestCase +class ModuleTest extends TestCase { - public function testCanBeInvoked(): void + public function testProvidesExpectedConfiguration() { - $moduleConfig = new ModuleConfig(); - - static::assertTrue(is_callable($moduleConfig)); - } - - public function testGetArrayWith(): void - { - $moduleConfig = new ModuleConfig(); - $config = $moduleConfig->__invoke(); - - $this->assertInternalType('array', $config); - $this->assertArrayHasKey('zfc_rbac', $config); - $this->assertArrayHasKey('dependencies', $config); + $provider = new ConfigProvider(); + $module = new Module(); + $expected = [ + 'service_manager' => $provider->getDependencyConfig(), + 'zfc_rbac' => $provider->getModuleConfig(), + ]; + $this->assertEquals($expected, $module->getConfig()); } } diff --git a/test/Role/HierarchicalRoleTest.php b/test/Role/HierarchicalRoleTest.php index a01bc178..82436e19 100644 --- a/test/Role/HierarchicalRoleTest.php +++ b/test/Role/HierarchicalRoleTest.php @@ -75,4 +75,31 @@ public function testCanGetChildren(): void $this->assertCount(2, $children); $this->assertContainsOnlyInstancesOf(HierarchicalRoleInterface::class, $children); } + + /** + * @covers \ZfcRbac\Role\HierarchicalRole::addPermission + */ + public function testRoleCanAddPermission(): void + { + $role = new HierarchicalRole('php'); + $role->addPermission('debug'); + $this->assertTrue($role->hasPermission('debug')); + $role->addPermission('delete'); + $this->assertTrue($role->hasPermission('delete')); + } + + /** + * @covers \ZfcRbac\Role\HierarchicalRole::getPermissions + */ + public function testRoleCanGetPermissions(): void + { + $role = new HierarchicalRole('php'); + $role->addPermission('foo'); + $role->addPermission('bar'); + $expectedPermissions = [ + 'foo' => 'foo', + 'bar' => 'bar', + ]; + $this->assertEquals($expectedPermissions, $role->getPermissions()); + } } From ba6e4b1d712210fc6ddb2767b3bb626bcfc3b169 Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Fri, 17 May 2019 11:17:11 +0300 Subject: [PATCH 2/3] make config ready for zend-expressive and ZF --- config/dependencies.global.php | 38 ---------------------------------- src/Module.php | 1 + 2 files changed, 1 insertion(+), 38 deletions(-) delete mode 100644 config/dependencies.global.php diff --git a/config/dependencies.global.php b/config/dependencies.global.php deleted file mode 100644 index 100e2636..00000000 --- a/config/dependencies.global.php +++ /dev/null @@ -1,38 +0,0 @@ - [ - '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, - ], - ], - - 'zfc_rbac' => [ - // Assertion plugin manager - 'assertion_manager' => [], - ], -]; diff --git a/src/Module.php b/src/Module.php index 75482137..c4236a6b 100644 --- a/src/Module.php +++ b/src/Module.php @@ -29,6 +29,7 @@ final class Module public function getConfig() { $provider = new ConfigProvider(); + return [ 'service_manager' => $provider->getDependencyConfig(), 'zfc_rbac' => $provider->getModuleConfig(), From d3d32ae50001b544eb3830f3f7f9b449c548ae89 Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Fri, 17 May 2019 11:26:15 +0300 Subject: [PATCH 3/3] fix coverage tests config --- .travis.yml | 2 +- composer.json | 8 ++++++++ src/ConfigProvider.php | 14 -------------- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/.travis.yml b/.travis.yml index 28b3e8af..6b811d12 100644 --- a/.travis.yml +++ b/.travis.yml @@ -34,7 +34,7 @@ script: - if [[ $EXECUTE_CS_CHECK == 'true' ]]; then ./vendor/bin/docheader check src/ tests/; fi after_success: - - if [[ $TEST_COVERAGE == 'true' ]]; then php vendor/bin/coveralls -v; fi + - if [[ $TEST_COVERAGE == 'true' ]]; then travis_retry php vendor/bin/php-coveralls -v; fi notifications: webhooks: diff --git a/composer.json b/composer.json index 1e62bedc..feb15484 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,14 @@ { "name": "Jean-Marie Leroux", "email": "jmleroux.pro@gmail.com" + }, + { + "name": "Florent Blaison", + "email": "florent.blaison@gmail.com" + }, + { + "name": "Bas Kamer", + "email": "baskamer@gmail.com" } ], "require": { diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index 440bc939..3ff5496e 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -28,14 +28,6 @@ */ final class ConfigProvider { - /** - * Returns the configuration array - * - * To add a bit of a structure, each section is defined in a separate - * method which returns an array with its configuration. - * - * @return array - */ public function __invoke() { return [ @@ -44,9 +36,6 @@ public function __invoke() ]; } - /** - * Returns the container dependencies - */ public function getDependencyConfig(): array { return [ @@ -62,9 +51,6 @@ public function getDependencyConfig(): array ]; } - /** - * Get module configuration - */ public function getModuleConfig(): array { return [