From c359ffe68cf001cc2f5f1e39a5bc4966ab626878 Mon Sep 17 00:00:00 2001 From: prolic Date: Thu, 23 Jun 2016 16:32:33 +0800 Subject: [PATCH 01/29] allow zf3 --- composer.json | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index b5bf4814..33f66f24 100644 --- a/composer.json +++ b/composer.json @@ -28,13 +28,18 @@ ], "require": { "php": ">=5.4", - "zendframework/zend-mvc": "~2.2", - "zendframework/zend-servicemanager": "~2.2", + "zendframework/zend-mvc": "~2.2|^3.0", + "zendframework/zend-servicemanager": "~2.2|^3.0", "zfr/rbac": "~1.2" }, "require-dev": { - "zendframework/zendframework": "~2.2", + "zendframework/zend-authentication": "~2.2", "zendframework/zend-developer-tools": "dev-master", + "zendframework/zend-log": "~2.2", + "zendframework/zend-http": "~2.2", + "zendframework/zend-i18n": "~2.2", + "zendframework/zend-serializer": "~2.2", + "zendframework/zend-view": "~2.2", "phpunit/phpunit": "~3.7", "squizlabs/php_codesniffer": "1.4.*", "satooshi/php-coveralls": "~0.6", From 673874fb5b481919c074aaa4604b091e0437b6e7 Mon Sep 17 00:00:00 2001 From: prolic Date: Fri, 24 Jun 2016 21:40:37 +0800 Subject: [PATCH 02/29] build on travis with zf2 & zf3. drop support php < 5.6 --- .travis.yml | 10 +++++++--- README.md | 2 +- composer.json | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 796ebed7..e84ec3c8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,14 +1,18 @@ language: php php: - - 5.4 - - 5.5 - 5.6 + - 7 - hhvm +env: + matrix: + - DEPENDENCIES="" + - DEPENDENCIES="--prefer-lowest --prefer-stable" + before_script: - composer self-update - - composer update --prefer-source + - composer update --prefer-dist $DEPENDENCIES script: - ./vendor/bin/phpunit --coverage-clover ./build/logs/clover.xml --exclude-group Functional diff --git a/README.md b/README.md index a2f5ecf9..b3d9e004 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ ZfcRbac is an access control module for Zend Framework 2, based on the RBAC perm ## Requirements -- PHP 5.4 or higher +- PHP 5.6 or higher - [Rbac component](https://github.com/zf-fr/rbac): this is actually a prototype for the ZF3 Rbac component. - [Zend Framework 2.2 or higher](http://www.github.com/zendframework/zf2) diff --git a/composer.json b/composer.json index 33f66f24..2457fe25 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ } ], "require": { - "php": ">=5.4", + "php": "5.6|~7.0", "zendframework/zend-mvc": "~2.2|^3.0", "zendframework/zend-servicemanager": "~2.2|^3.0", "zfr/rbac": "~1.2" From aefc37f1a8a04818aaeb1fbc25640eca78ca218b Mon Sep 17 00:00:00 2001 From: prolic Date: Fri, 24 Jun 2016 21:45:24 +0800 Subject: [PATCH 03/29] fix typo in composer json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 2457fe25..7fc32777 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ } ], "require": { - "php": "5.6|~7.0", + "php": "~5.6|~7.0", "zendframework/zend-mvc": "~2.2|^3.0", "zendframework/zend-servicemanager": "~2.2|^3.0", "zfr/rbac": "~1.2" From ef117e1961a5fe0bcbb04a2490765b271bcd646f Mon Sep 17 00:00:00 2001 From: prolic Date: Fri, 24 Jun 2016 21:58:04 +0800 Subject: [PATCH 04/29] other composer json formatting for or (||) --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 7fc32777..64ab07fe 100644 --- a/composer.json +++ b/composer.json @@ -27,9 +27,9 @@ } ], "require": { - "php": "~5.6|~7.0", - "zendframework/zend-mvc": "~2.2|^3.0", - "zendframework/zend-servicemanager": "~2.2|^3.0", + "php": "~5.6 || ~7.0", + "zendframework/zend-mvc": "~2.2 || ^3.0", + "zendframework/zend-servicemanager": "~2.2 || ^3.0", "zfr/rbac": "~1.2" }, "require-dev": { From d67810225641d44129781c792a77bb530cb3ce4b Mon Sep 17 00:00:00 2001 From: prolic Date: Fri, 8 Jul 2016 15:09:32 +0800 Subject: [PATCH 05/29] update composer deps --- composer.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 64ab07fe..2c30bd1f 100644 --- a/composer.json +++ b/composer.json @@ -34,18 +34,18 @@ }, "require-dev": { "zendframework/zend-authentication": "~2.2", - "zendframework/zend-developer-tools": "dev-master", + "zendframework/zend-developer-tools": "~1.1", "zendframework/zend-log": "~2.2", "zendframework/zend-http": "~2.2", "zendframework/zend-i18n": "~2.2", "zendframework/zend-serializer": "~2.2", "zendframework/zend-view": "~2.2", - "phpunit/phpunit": "~3.7", + "phpunit/phpunit": "~4.8", "squizlabs/php_codesniffer": "1.4.*", "satooshi/php-coveralls": "~0.6", "doctrine/common": "~2.4", - "doctrine/doctrine-module": "~0.8", - "doctrine/doctrine-orm-module": "~0.8" + "doctrine/doctrine-module": "~1.1", + "doctrine/doctrine-orm-module": "~0.11" }, "suggest": { "zendframework/zend-developer-tools": "if you want to show information about the roles", From 6a0b20a23798be83756ce19d2b3a773a3d33c2f6 Mon Sep 17 00:00:00 2001 From: prolic Date: Fri, 8 Jul 2016 17:15:26 +0800 Subject: [PATCH 06/29] update to work with ZF3 --- README.md | 2 +- .../Assertion/AssertionPluginManager.php | 10 +++- .../Factory/AssertionPluginManagerFactory.php | 24 ++++++--- .../AuthenticationIdentityProviderFactory.php | 18 +++++-- .../AuthorizationServiceDelegatorFactory.php | 30 ++++++++++-- .../Factory/AuthorizationServiceFactory.php | 24 ++++++--- .../Factory/ControllerGuardFactory.php | 37 ++++++-------- .../ControllerPermissionsGuardFactory.php | 41 +++++++--------- .../Factory/GuardPluginManagerFactory.php | 21 ++++++-- src/ZfcRbac/Factory/GuardsFactory.php | 25 ++++++++-- .../Factory/HasRoleViewHelperFactory.php | 21 ++++++-- .../Factory/IsGrantedPluginFactory.php | 21 ++++++-- .../Factory/IsGrantedViewHelperFactory.php | 21 ++++++-- src/ZfcRbac/Factory/ModuleOptionsFactory.php | 14 +++++- .../ObjectRepositoryRoleProviderFactory.php | 49 +++++++++---------- src/ZfcRbac/Factory/RbacFactory.php | 21 +++++--- .../Factory/RedirectStrategyFactory.php | 20 ++++++-- .../RoleProviderPluginManagerFactory.php | 21 +++++--- src/ZfcRbac/Factory/RoleServiceFactory.php | 24 ++++++--- src/ZfcRbac/Factory/RouteGuardFactory.php | 44 ++++++++--------- .../Factory/RoutePermissionsGuardFactory.php | 43 ++++++++-------- .../Factory/UnauthorizedStrategyFactory.php | 18 +++++-- src/ZfcRbac/Guard/AbstractGuard.php | 4 +- src/ZfcRbac/Guard/GuardPluginManager.php | 10 +++- .../AuthorizationServiceInitializer.php | 22 ++++++--- src/ZfcRbac/Module.php | 2 +- .../Role/RoleProviderPluginManager.php | 10 +++- .../View/Strategy/AbstractStrategy.php | 4 +- .../AssertionPluginManagerFactoryTest.php | 1 - .../AuthorizationServiceDelegatorTest.php | 5 +- .../Factory/ControllerGuardFactoryTest.php | 5 +- .../ControllerPermissionsGuardFactoryTest.php | 5 +- .../Factory/GuardPluginManagerFactoryTest.php | 1 - .../ZfcRbacTest/Factory/GuardsFactoryTest.php | 11 ++--- .../Factory/HasRoleViewHelperFactoryTest.php | 5 +- .../Factory/IsGrantedPluginFactoryTest.php | 5 +- .../IsGrantedViewHelperFactoryTest.php | 5 +- ...bjectRepositoryRoleProviderFactoryTest.php | 14 ++---- .../RoleProviderPluginManagerFactoryTest.php | 1 - .../Factory/RoleServiceFactoryTest.php | 2 +- .../Factory/RouteGuardFactoryTest.php | 5 +- .../RoutePermissionsGuardFactoryTest.php | 5 +- .../Guard/GuardPluginManagerTest.php | 6 +-- .../Role/RoleProviderPluginManagerTest.php | 7 +-- .../Service/AuthorizationServiceTest.php | 3 +- .../Util/ServiceManagerFactory.php | 4 +- 46 files changed, 442 insertions(+), 249 deletions(-) diff --git a/README.md b/README.md index b3d9e004..2cf8a0b6 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ ZfcRbac is an access control module for Zend Framework 2, based on the RBAC perm ## Requirements -- PHP 5.6 or higher +- PHP 5.6, PHP 7.0 or higher - [Rbac component](https://github.com/zf-fr/rbac): this is actually a prototype for the ZF3 Rbac component. - [Zend Framework 2.2 or higher](http://www.github.com/zendframework/zf2) diff --git a/src/ZfcRbac/Assertion/AssertionPluginManager.php b/src/ZfcRbac/Assertion/AssertionPluginManager.php index b5b156e3..7f9cd05a 100644 --- a/src/ZfcRbac/Assertion/AssertionPluginManager.php +++ b/src/ZfcRbac/Assertion/AssertionPluginManager.php @@ -34,7 +34,7 @@ class AssertionPluginManager extends AbstractPluginManager /** * {@inheritDoc} */ - public function validatePlugin($plugin) + public function validate($plugin) { if ($plugin instanceof AssertionInterface) { return; // we're okay @@ -46,6 +46,14 @@ public function validatePlugin($plugin) )); } + /** + * {@inheritDoc} + */ + public function validatePlugin($plugin) + { + $this->validate($plugin); + } + /** * {@inheritDoc} */ diff --git a/src/ZfcRbac/Factory/AssertionPluginManagerFactory.php b/src/ZfcRbac/Factory/AssertionPluginManagerFactory.php index 301eb6f9..2c3f364e 100644 --- a/src/ZfcRbac/Factory/AssertionPluginManagerFactory.php +++ b/src/ZfcRbac/Factory/AssertionPluginManagerFactory.php @@ -18,7 +18,11 @@ namespace ZfcRbac\Factory; +use Interop\Container\ContainerInterface; +use Interop\Container\Exception\ContainerException; use Zend\ServiceManager\Config; +use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\ServiceManager\Exception\ServiceNotFoundException; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Assertion\AssertionPluginManager; @@ -32,16 +36,24 @@ class AssertionPluginManagerFactory implements FactoryInterface { /** - * {@inheritDoc} + * @param ContainerInterface $container + * @param string $requestedName + * @param array|null $options * @return AssertionPluginManager */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { - $config = $serviceLocator->get('Config')['zfc_rbac']['assertion_manager']; + $config = $container->get('Config')['zfc_rbac']['assertion_manager']; - $pluginManager = new AssertionPluginManager(new Config($config)); - $pluginManager->setServiceLocator($serviceLocator); + return new AssertionPluginManager($container, $config); + } - return $pluginManager; + /** + * {@inheritDoc} + * @return AssertionPluginManager + */ + public function createService(ServiceLocatorInterface $serviceLocator) + { + return $this($serviceLocator, AssertionPluginManager::class); } } diff --git a/src/ZfcRbac/Factory/AuthenticationIdentityProviderFactory.php b/src/ZfcRbac/Factory/AuthenticationIdentityProviderFactory.php index 04cbaec9..604ba1f5 100644 --- a/src/ZfcRbac/Factory/AuthenticationIdentityProviderFactory.php +++ b/src/ZfcRbac/Factory/AuthenticationIdentityProviderFactory.php @@ -18,6 +18,7 @@ namespace ZfcRbac\Factory; +use Interop\Container\ContainerInterface; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Identity\AuthenticationIdentityProvider; @@ -31,14 +32,25 @@ class AuthenticationIdentityProviderFactory implements FactoryInterface { /** - * {@inheritDoc} + * @param ContainerInterface $container + * @param string $requestedName + * @param array|null $options * @return AuthenticationIdentityProvider */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { /* @var \Zend\Authentication\AuthenticationService $authenticationProvider */ - $authenticationProvider = $serviceLocator->get('Zend\Authentication\AuthenticationService'); + $authenticationProvider = $container->get('Zend\Authentication\AuthenticationService'); return new AuthenticationIdentityProvider($authenticationProvider); } + + /** + * {@inheritDoc} + * @return AuthenticationIdentityProvider + */ + public function createService(ServiceLocatorInterface $serviceLocator) + { + return $this($serviceLocator, AuthenticationIdentityProvider::class); + } } diff --git a/src/ZfcRbac/Factory/AuthorizationServiceDelegatorFactory.php b/src/ZfcRbac/Factory/AuthorizationServiceDelegatorFactory.php index feed7970..d57a1f87 100644 --- a/src/ZfcRbac/Factory/AuthorizationServiceDelegatorFactory.php +++ b/src/ZfcRbac/Factory/AuthorizationServiceDelegatorFactory.php @@ -18,6 +18,7 @@ namespace ZfcRbac\Factory; +use Interop\Container\ContainerInterface; use Zend\ServiceManager\AbstractPluginManager; use Zend\ServiceManager\DelegatorFactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; @@ -32,21 +33,40 @@ */ class AuthorizationServiceDelegatorFactory implements DelegatorFactoryInterface { - public function createDelegatorWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName, $callback) + /** + * @param ContainerInterface $container + * @param string $name + * @param callable $callback + * @param array|null $options + * @return mixed + */ + public function __invoke(ContainerInterface $container, $name, callable $callback, array $options = null) { $instanceToDecorate = call_user_func($callback); if (!$instanceToDecorate instanceof AuthorizationServiceAwareInterface) { - throw new RuntimeException("The service $requestedName must implement AuthorizationServiceAwareInterface."); + throw new RuntimeException("The service $name must implement AuthorizationServiceAwareInterface."); } - if ($serviceLocator instanceof AbstractPluginManager) { - $serviceLocator = $serviceLocator->getServiceLocator(); + if ($container instanceof AbstractPluginManager) { + $container = $container->getServiceLocator(); } - $authorizationService = $serviceLocator->get('ZfcRbac\Service\AuthorizationService'); + $authorizationService = $container->get('ZfcRbac\Service\AuthorizationService'); $instanceToDecorate->setAuthorizationService($authorizationService); return $instanceToDecorate; } + + /** + * @param ServiceLocatorInterface $serviceLocator + * @param string $name + * @param string $requestedName + * @param callable $callback + * @return mixed + */ + public function createDelegatorWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName, $callback) + { + return $this($serviceLocator, $requestedName, $callback); + } } diff --git a/src/ZfcRbac/Factory/AuthorizationServiceFactory.php b/src/ZfcRbac/Factory/AuthorizationServiceFactory.php index 2f3300f2..4d0164c8 100644 --- a/src/ZfcRbac/Factory/AuthorizationServiceFactory.php +++ b/src/ZfcRbac/Factory/AuthorizationServiceFactory.php @@ -18,6 +18,7 @@ namespace ZfcRbac\Factory; +use Interop\Container\ContainerInterface; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Service\AuthorizationService; @@ -31,26 +32,37 @@ class AuthorizationServiceFactory implements FactoryInterface { /** - * {@inheritDoc} + * @param ContainerInterface $container + * @param string $requestedName + * @param array|null $options * @return AuthorizationService */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { /* @var \Rbac\Rbac $rbac */ - $rbac = $serviceLocator->get('Rbac\Rbac'); + $rbac = $container->get('Rbac\Rbac'); /* @var \ZfcRbac\Service\RoleService $roleService */ - $roleService = $serviceLocator->get('ZfcRbac\Service\RoleService'); + $roleService = $container->get('ZfcRbac\Service\RoleService'); /* @var \ZfcRbac\Assertion\AssertionPluginManager $assertionPluginManager */ - $assertionPluginManager = $serviceLocator->get('ZfcRbac\Assertion\AssertionPluginManager'); + $assertionPluginManager = $container->get('ZfcRbac\Assertion\AssertionPluginManager'); /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ - $moduleOptions = $serviceLocator->get('ZfcRbac\Options\ModuleOptions'); + $moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions'); $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager); $authorizationService->setAssertions($moduleOptions->getAssertionMap()); return $authorizationService; } + + /** + * {@inheritDoc} + * @return AuthorizationService + */ + public function createService(ServiceLocatorInterface $serviceLocator) + { + return $this($serviceLocator, AuthorizationService::class); + } } diff --git a/src/ZfcRbac/Factory/ControllerGuardFactory.php b/src/ZfcRbac/Factory/ControllerGuardFactory.php index 1fbfa4a5..e60a6de5 100644 --- a/src/ZfcRbac/Factory/ControllerGuardFactory.php +++ b/src/ZfcRbac/Factory/ControllerGuardFactory.php @@ -18,8 +18,8 @@ namespace ZfcRbac\Factory; +use Interop\Container\ContainerInterface; use Zend\ServiceManager\FactoryInterface; -use Zend\ServiceManager\MutableCreationOptionsInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Guard\ControllerGuard; @@ -29,38 +29,33 @@ * @author Michaël Gallego * @license MIT */ -class ControllerGuardFactory implements FactoryInterface, MutableCreationOptionsInterface +class ControllerGuardFactory implements FactoryInterface { - /** - * @var array - */ - protected $options = []; - - /** - * {@inheritDoc} - */ - public function setCreationOptions(array $options) - { - $this->options = $options; - } - /** * {@inheritDoc} * @return ControllerGuard */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $requestedName, array $options = []) { - $parentLocator = $serviceLocator->getServiceLocator(); - /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ - $moduleOptions = $parentLocator->get('ZfcRbac\Options\ModuleOptions'); + $moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions'); /* @var \ZfcRbac\Service\RoleService $roleService */ - $roleService = $parentLocator->get('ZfcRbac\Service\RoleService'); + $roleService = $container->get('ZfcRbac\Service\RoleService'); - $controllerGuard = new ControllerGuard($roleService, $this->options); + $controllerGuard = new ControllerGuard($roleService, $options); $controllerGuard->setProtectionPolicy($moduleOptions->getProtectionPolicy()); return $controllerGuard; } + + + /** + * {@inheritDoc} + * @return ControllerGuard + */ + public function createService(ServiceLocatorInterface $serviceLocator) + { + return $this($serviceLocator, ControllerGuard::class); + } } diff --git a/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php b/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php index 6e80fd27..0a83d3f5 100644 --- a/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php +++ b/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php @@ -18,8 +18,8 @@ namespace ZfcRbac\Factory; +use Interop\Container\ContainerInterface; use Zend\ServiceManager\FactoryInterface; -use Zend\ServiceManager\MutableCreationOptionsInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Guard\ControllerPermissionsGuard; use ZfcRbac\Guard\RouteGuard; @@ -30,38 +30,35 @@ * @author JM Lerouxw * @license MIT */ -class ControllerPermissionsGuardFactory implements FactoryInterface, MutableCreationOptionsInterface +class ControllerPermissionsGuardFactory implements FactoryInterface { /** - * @var array + * @param ContainerInterface $container + * @param string $requestedName + * @param array $options + * @return ControllerPermissionsGuard */ - protected $options = []; - - /** - * {@inheritDoc} - */ - public function setCreationOptions(array $options) + public function __invoke(ContainerInterface $container, $requestedName, array $options = []) { - $this->options = $options; + /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ + $moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions'); + + /* @var \ZfcRbac\Service\AuthorizationService $authorizationService */ + $authorizationService = $container->get('ZfcRbac\Service\AuthorizationService'); + + $guard = new ControllerPermissionsGuard($authorizationService, $options); + $guard->setProtectionPolicy($moduleOptions->getProtectionPolicy()); + + return $guard; } + /** * @param \Zend\ServiceManager\AbstractPluginManager|ServiceLocatorInterface $serviceLocator * @return RouteGuard */ public function createService(ServiceLocatorInterface $serviceLocator) { - $parentLocator = $serviceLocator->getServiceLocator(); - - /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ - $moduleOptions = $parentLocator->get('ZfcRbac\Options\ModuleOptions'); - - /* @var \ZfcRbac\Service\AuthorizationService $authorizationService */ - $authorizationService = $parentLocator->get('ZfcRbac\Service\AuthorizationService'); - - $guard = new ControllerPermissionsGuard($authorizationService, $this->options); - $guard->setProtectionPolicy($moduleOptions->getProtectionPolicy()); - - return $guard; + return $this($serviceLocator, ControllerPermissionsGuard::class); } } diff --git a/src/ZfcRbac/Factory/GuardPluginManagerFactory.php b/src/ZfcRbac/Factory/GuardPluginManagerFactory.php index 4b067b6f..cb24e00b 100644 --- a/src/ZfcRbac/Factory/GuardPluginManagerFactory.php +++ b/src/ZfcRbac/Factory/GuardPluginManagerFactory.php @@ -18,6 +18,7 @@ namespace ZfcRbac\Factory; +use Interop\Container\ContainerInterface; use Zend\ServiceManager\Config; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; @@ -32,16 +33,26 @@ class GuardPluginManagerFactory implements FactoryInterface { /** - * {@inheritDoc} + * @param ContainerInterface $container + * @param string $requestedName + * @param array|null $options * @return GuardPluginManager */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { - $config = $serviceLocator->get('Config')['zfc_rbac']['guard_manager']; + $config = $container->get('Config')['zfc_rbac']['guard_manager']; - $pluginManager = new GuardPluginManager(new Config($config)); - $pluginManager->setServiceLocator($serviceLocator); + $pluginManager = new GuardPluginManager($container, $config); return $pluginManager; } + + /** + * {@inheritDoc} + * @return GuardPluginManager + */ + public function createService(ServiceLocatorInterface $serviceLocator) + { + return $this($serviceLocator, GuardPluginManager::class); + } } diff --git a/src/ZfcRbac/Factory/GuardsFactory.php b/src/ZfcRbac/Factory/GuardsFactory.php index 29bb7816..4771b362 100644 --- a/src/ZfcRbac/Factory/GuardsFactory.php +++ b/src/ZfcRbac/Factory/GuardsFactory.php @@ -18,6 +18,10 @@ namespace ZfcRbac\Factory; +use Interop\Container\ContainerInterface; +use Interop\Container\Exception\ContainerException; +use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\ServiceManager\Exception\ServiceNotFoundException; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; @@ -30,13 +34,15 @@ class GuardsFactory implements FactoryInterface { /** - * {@inheritDoc} - * @return \ZfcRbac\Guard\GuardInterface[]|array + * @param ContainerInterface $container + * @param string $requestedName + * @param array|null $options + * @return array */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { /* @var \ZfcRbac\Options\ModuleOptions $options */ - $options = $serviceLocator->get('ZfcRbac\Options\ModuleOptions'); + $options = $container->get('ZfcRbac\Options\ModuleOptions'); $guardsOptions = $options->getGuards(); if (empty($guardsOptions)) { @@ -44,7 +50,7 @@ public function createService(ServiceLocatorInterface $serviceLocator) } /* @var \ZfcRbac\Guard\GuardPluginManager $pluginManager */ - $pluginManager = $serviceLocator->get('ZfcRbac\Guard\GuardPluginManager'); + $pluginManager = $container->get('ZfcRbac\Guard\GuardPluginManager'); $guards = []; foreach ($guardsOptions as $type => $options) { @@ -53,4 +59,13 @@ public function createService(ServiceLocatorInterface $serviceLocator) return $guards; } + + /** + * {@inheritDoc} + * @return \ZfcRbac\Guard\GuardInterface[]|array + */ + public function createService(ServiceLocatorInterface $serviceLocator) + { + return $this($serviceLocator, GuardInterface::class); + } } diff --git a/src/ZfcRbac/Factory/HasRoleViewHelperFactory.php b/src/ZfcRbac/Factory/HasRoleViewHelperFactory.php index 387e7510..0978f9d2 100644 --- a/src/ZfcRbac/Factory/HasRoleViewHelperFactory.php +++ b/src/ZfcRbac/Factory/HasRoleViewHelperFactory.php @@ -18,6 +18,10 @@ namespace ZfcRbac\Factory; +use Interop\Container\ContainerInterface; +use Interop\Container\Exception\ContainerException; +use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\ServiceManager\Exception\ServiceNotFoundException; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Service\RoleService; @@ -32,14 +36,25 @@ class HasRoleViewHelperFactory implements FactoryInterface { /** - * {@inheritDoc} + * @param ContainerInterface $container + * @param string $requestedName + * @param array|null $options * @return HasRole */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { /* @var RoleService $roleService */ - $roleService = $serviceLocator->getServiceLocator()->get('ZfcRbac\Service\RoleService'); + $roleService = $container->get('ZfcRbac\Service\RoleService'); return new HasRole($roleService); } + + /** + * {@inheritDoc} + * @return HasRole + */ + public function createService(ServiceLocatorInterface $serviceLocator) + { + return $this($serviceLocator, HasRole::class); + } } diff --git a/src/ZfcRbac/Factory/IsGrantedPluginFactory.php b/src/ZfcRbac/Factory/IsGrantedPluginFactory.php index 531d5181..8f4198b8 100644 --- a/src/ZfcRbac/Factory/IsGrantedPluginFactory.php +++ b/src/ZfcRbac/Factory/IsGrantedPluginFactory.php @@ -18,6 +18,10 @@ namespace ZfcRbac\Factory; +use Interop\Container\ContainerInterface; +use Interop\Container\Exception\ContainerException; +use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\ServiceManager\Exception\ServiceNotFoundException; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Mvc\Controller\Plugin\IsGranted; @@ -31,14 +35,25 @@ class IsGrantedPluginFactory implements FactoryInterface { /** - * {@inheritDoc} + * @param ContainerInterface $container + * @param string $requestedName + * @param array|null $options * @return IsGranted */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { /* @var \ZfcRbac\Service\AuthorizationService $authorizationService */ - $authorizationService = $serviceLocator->getServiceLocator()->get('ZfcRbac\Service\AuthorizationService'); + $authorizationService = $container->get('ZfcRbac\Service\AuthorizationService'); return new IsGranted($authorizationService); } + + /** + * {@inheritDoc} + * @return IsGranted + */ + public function createService(ServiceLocatorInterface $serviceLocator) + { + return $this($serviceLocator, IsGranted::class); + } } diff --git a/src/ZfcRbac/Factory/IsGrantedViewHelperFactory.php b/src/ZfcRbac/Factory/IsGrantedViewHelperFactory.php index ba0fa347..bc0db739 100644 --- a/src/ZfcRbac/Factory/IsGrantedViewHelperFactory.php +++ b/src/ZfcRbac/Factory/IsGrantedViewHelperFactory.php @@ -18,6 +18,10 @@ namespace ZfcRbac\Factory; +use Interop\Container\ContainerInterface; +use Interop\Container\Exception\ContainerException; +use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\ServiceManager\Exception\ServiceNotFoundException; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\View\Helper\IsGranted; @@ -31,14 +35,25 @@ class IsGrantedViewHelperFactory implements FactoryInterface { /** - * {@inheritDoc} + * @param ContainerInterface $container + * @param string $requestedName + * @param array|null $options * @return IsGranted */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { /* @var \ZfcRbac\Service\AuthorizationService $authorizationService */ - $authorizationService = $serviceLocator->getServiceLocator()->get('ZfcRbac\Service\AuthorizationService'); + $authorizationService = $container->get('ZfcRbac\Service\AuthorizationService'); return new IsGranted($authorizationService); } + + /** + * {@inheritDoc} + * @return IsGranted + */ + public function createService(ServiceLocatorInterface $serviceLocator) + { + return $this($serviceLocator, IsGranted::class); + } } diff --git a/src/ZfcRbac/Factory/ModuleOptionsFactory.php b/src/ZfcRbac/Factory/ModuleOptionsFactory.php index bfc1b713..a1c1f137 100644 --- a/src/ZfcRbac/Factory/ModuleOptionsFactory.php +++ b/src/ZfcRbac/Factory/ModuleOptionsFactory.php @@ -18,6 +18,7 @@ namespace ZfcRbac\Factory; +use Interop\Container\ContainerInterface; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Options\ModuleOptions; @@ -30,12 +31,23 @@ */ class ModuleOptionsFactory implements FactoryInterface { + /** + * @param ContainerInterface $container + * @param string $requestedName + * @param array|null $options + * @return ModuleOptions + */ + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) + { + return new ModuleOptions($container->get('Config')['zfc_rbac']); + } + /** * {@inheritDoc} * @return ModuleOptions */ public function createService(ServiceLocatorInterface $serviceLocator) { - return new ModuleOptions($serviceLocator->get('Config')['zfc_rbac']); + return $this($serviceLocator, ModuleOptions::class); } } diff --git a/src/ZfcRbac/Factory/ObjectRepositoryRoleProviderFactory.php b/src/ZfcRbac/Factory/ObjectRepositoryRoleProviderFactory.php index 5a19e30e..1d3fd43f 100644 --- a/src/ZfcRbac/Factory/ObjectRepositoryRoleProviderFactory.php +++ b/src/ZfcRbac/Factory/ObjectRepositoryRoleProviderFactory.php @@ -18,8 +18,8 @@ namespace ZfcRbac\Factory; +use Interop\Container\ContainerInterface; use Zend\ServiceManager\FactoryInterface; -use Zend\ServiceManager\MutableCreationOptionsInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Exception; use ZfcRbac\Role\ObjectRepositoryRoleProvider; @@ -30,47 +30,35 @@ * @author Michaël Gallego * @license MIT */ -class ObjectRepositoryRoleProviderFactory implements FactoryInterface, MutableCreationOptionsInterface +class ObjectRepositoryRoleProviderFactory implements FactoryInterface { /** - * @var array - */ - protected $options = []; - - /** - * {@inheritDoc} - */ - public function setCreationOptions(array $options) - { - $this->options = $options; - } - - /** - * {@inheritDoc} + * @param ContainerInterface $container + * @param string $requestedName + * @param array|null $options * @return ObjectRepositoryRoleProvider */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { - $parentLocator = $serviceLocator->getServiceLocator(); $objectRepository = null; - if (!isset($this->options['role_name_property'])) { + if (!isset($options['role_name_property'])) { throw new Exception\RuntimeException('The "role_name_property" option is missing'); } - if (isset($this->options['object_repository'])) { + if (isset($options['object_repository'])) { /* @var \Doctrine\Common\Persistence\ObjectRepository $objectRepository */ - $objectRepository = $parentLocator->get($this->options['object_repository']); + $objectRepository = $container->get($options['object_repository']); - return new ObjectRepositoryRoleProvider($objectRepository, $this->options['role_name_property']); + return new ObjectRepositoryRoleProvider($objectRepository, $options['role_name_property']); } - if (isset($this->options['object_manager']) && isset($this->options['class_name'])) { + if (isset($options['object_manager']) && isset($options['class_name'])) { /* @var \Doctrine\Common\Persistence\ObjectManager $objectManager */ - $objectManager = $parentLocator->get($this->options['object_manager']); - $objectRepository = $objectManager->getRepository($this->options['class_name']); + $objectManager = $container->get($options['object_manager']); + $objectRepository = $objectManager->getRepository($options['class_name']); - return new ObjectRepositoryRoleProvider($objectRepository, $this->options['role_name_property']); + return new ObjectRepositoryRoleProvider($objectRepository, $options['role_name_property']); } throw new Exception\RuntimeException( @@ -78,4 +66,13 @@ public function createService(ServiceLocatorInterface $serviceLocator) you sure you specified either the "object_repository" option or "object_manager"/"class_name" options?' ); } + + /** + * {@inheritDoc} + * @return ObjectRepositoryRoleProvider + */ + public function createService(ServiceLocatorInterface $serviceLocator) + { + return $this($serviceLocator, ObjectRepositoryRoleProvider::class); + } } diff --git a/src/ZfcRbac/Factory/RbacFactory.php b/src/ZfcRbac/Factory/RbacFactory.php index 8cf50884..cbd5e3ff 100644 --- a/src/ZfcRbac/Factory/RbacFactory.php +++ b/src/ZfcRbac/Factory/RbacFactory.php @@ -18,9 +18,9 @@ namespace ZfcRbac\Factory; +use Interop\Container\ContainerInterface; use Rbac\Rbac; use Rbac\Traversal\Strategy\GeneratorStrategy; -use Rbac\Traversal\Strategy\RecursiveRoleIteratorStrategy; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; @@ -30,17 +30,22 @@ */ class RbacFactory implements FactoryInterface { + /** + * @param ContainerInterface $container + * @param string $requestedName + * @param array|null $options + * @return Rbac + */ + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) + { + return new Rbac(new GeneratorStrategy()); + } + /** * {@inheritDoc} */ public function createService(ServiceLocatorInterface $serviceLocator) { - if (version_compare(PHP_VERSION, '5.5.0', '>=')) { - $traversalStrategy = new GeneratorStrategy(); - } else { - $traversalStrategy = new RecursiveRoleIteratorStrategy(); - } - - return new Rbac($traversalStrategy); + return $this($serviceLocator, Rbac::class); } } diff --git a/src/ZfcRbac/Factory/RedirectStrategyFactory.php b/src/ZfcRbac/Factory/RedirectStrategyFactory.php index 455e6219..e9e28365 100644 --- a/src/ZfcRbac/Factory/RedirectStrategyFactory.php +++ b/src/ZfcRbac/Factory/RedirectStrategyFactory.php @@ -18,6 +18,7 @@ namespace ZfcRbac\Factory; +use Interop\Container\ContainerInterface; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\View\Strategy\RedirectStrategy; @@ -31,15 +32,26 @@ class RedirectStrategyFactory implements FactoryInterface { /** - * {@inheritDoc} + * @param ContainerInterface $container + * @param string $requestedName + * @param array|null $options + * @return RedirectStrategy */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ - $moduleOptions = $serviceLocator->get('ZfcRbac\Options\ModuleOptions'); + $moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions'); /** @var \Zend\Authentication\AuthenticationService $authenticationService */ - $authenticationService = $serviceLocator->get('Zend\Authentication\AuthenticationService'); + $authenticationService = $container->get('Zend\Authentication\AuthenticationService'); return new RedirectStrategy($moduleOptions->getRedirectStrategy(), $authenticationService); } + + /** + * {@inheritDoc} + */ + public function createService(ServiceLocatorInterface $serviceLocator) + { + return $this($serviceLocator, RedirectStrategy::class); + } } diff --git a/src/ZfcRbac/Factory/RoleProviderPluginManagerFactory.php b/src/ZfcRbac/Factory/RoleProviderPluginManagerFactory.php index 58aada1f..ff1f916a 100644 --- a/src/ZfcRbac/Factory/RoleProviderPluginManagerFactory.php +++ b/src/ZfcRbac/Factory/RoleProviderPluginManagerFactory.php @@ -18,6 +18,7 @@ namespace ZfcRbac\Factory; +use Interop\Container\ContainerInterface; use Zend\ServiceManager\Config; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; @@ -32,16 +33,24 @@ class RoleProviderPluginManagerFactory implements FactoryInterface { /** - * {@inheritDoc} + * @param ContainerInterface $container + * @param string $requestedName + * @param array|null $options * @return RoleProviderPluginManager */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { - $config = $serviceLocator->get('Config')['zfc_rbac']['role_provider_manager']; + $config = $container->get('Config')['zfc_rbac']['role_provider_manager']; - $pluginManager = new RoleProviderPluginManager(new Config($config)); - $pluginManager->setServiceLocator($serviceLocator); + return new RoleProviderPluginManager($container, $config); + } - return $pluginManager; + /** + * {@inheritDoc} + * @return RoleProviderPluginManager + */ + public function createService(ServiceLocatorInterface $serviceLocator) + { + return $this($serviceLocator, RoleProviderPluginManager::class); } } diff --git a/src/ZfcRbac/Factory/RoleServiceFactory.php b/src/ZfcRbac/Factory/RoleServiceFactory.php index b3626d05..6eae6086 100644 --- a/src/ZfcRbac/Factory/RoleServiceFactory.php +++ b/src/ZfcRbac/Factory/RoleServiceFactory.php @@ -18,6 +18,7 @@ namespace ZfcRbac\Factory; +use Interop\Container\ContainerInterface; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Exception\RuntimeException; @@ -32,16 +33,18 @@ class RoleServiceFactory implements FactoryInterface { /** - * {@inheritDoc} + * @param ContainerInterface $container + * @param string $requestedName + * @param array|null $options * @return RoleService */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ - $moduleOptions = $serviceLocator->get('ZfcRbac\Options\ModuleOptions'); + $moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions'); /* @var \ZfcRbac\Identity\IdentityProviderInterface $identityProvider */ - $identityProvider = $serviceLocator->get($moduleOptions->getIdentityProvider()); + $identityProvider = $container->get($moduleOptions->getIdentityProvider()); $roleProviderConfig = $moduleOptions->getRoleProvider(); @@ -50,17 +53,26 @@ public function createService(ServiceLocatorInterface $serviceLocator) } /* @var \ZfcRbac\Role\RoleProviderPluginManager $pluginManager */ - $pluginManager = $serviceLocator->get('ZfcRbac\Role\RoleProviderPluginManager'); + $pluginManager = $container->get('ZfcRbac\Role\RoleProviderPluginManager'); /* @var \ZfcRbac\Role\RoleProviderInterface $roleProvider */ $roleProvider = $pluginManager->get(key($roleProviderConfig), current($roleProviderConfig)); /* @var \Rbac\Traversal\Strategy\TraversalStrategyInterface $traversalStrategy */ - $traversalStrategy = $serviceLocator->get('Rbac\Rbac')->getTraversalStrategy(); + $traversalStrategy = $container->get('Rbac\Rbac')->getTraversalStrategy(); $roleService = new RoleService($identityProvider, $roleProvider, $traversalStrategy); $roleService->setGuestRole($moduleOptions->getGuestRole()); return $roleService; } + + /** + * {@inheritDoc} + * @return RoleService + */ + public function createService(ServiceLocatorInterface $serviceLocator) + { + return $this($serviceLocator, RoleService::class); + } } diff --git a/src/ZfcRbac/Factory/RouteGuardFactory.php b/src/ZfcRbac/Factory/RouteGuardFactory.php index 3a926631..2212fa37 100644 --- a/src/ZfcRbac/Factory/RouteGuardFactory.php +++ b/src/ZfcRbac/Factory/RouteGuardFactory.php @@ -18,8 +18,11 @@ namespace ZfcRbac\Factory; +use Interop\Container\ContainerInterface; +use Interop\Container\Exception\ContainerException; +use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\ServiceManager\Exception\ServiceNotFoundException; use Zend\ServiceManager\FactoryInterface; -use Zend\ServiceManager\MutableCreationOptionsInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Guard\RouteGuard; @@ -29,38 +32,35 @@ * @author Michaël Gallego * @license MIT */ -class RouteGuardFactory implements FactoryInterface, MutableCreationOptionsInterface +class RouteGuardFactory implements FactoryInterface { /** - * @var array - */ - protected $options = []; - - /** - * {@inheritDoc} - */ - public function setCreationOptions(array $options) - { - $this->options = $options; - } - - /** - * {@inheritDoc} + * @param ContainerInterface $container + * @param string $requestedName + * @param array $options * @return RouteGuard */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $requestedName, array $options = []) { - $parentLocator = $serviceLocator->getServiceLocator(); - /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ - $moduleOptions = $parentLocator->get('ZfcRbac\Options\ModuleOptions'); + $moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions'); /* @var \ZfcRbac\Service\RoleService $roleService */ - $roleService = $parentLocator->get('ZfcRbac\Service\RoleService'); + $roleService = $container->get('ZfcRbac\Service\RoleService'); - $routeGuard = new RouteGuard($roleService, $this->options); + $routeGuard = new RouteGuard($roleService, $options); $routeGuard->setProtectionPolicy($moduleOptions->getProtectionPolicy()); return $routeGuard; } + + + /** + * {@inheritDoc} + * @return RouteGuard + */ + public function createService(ServiceLocatorInterface $serviceLocator) + { + return $this($serviceLocator, RouteGuard::class); + } } diff --git a/src/ZfcRbac/Factory/RoutePermissionsGuardFactory.php b/src/ZfcRbac/Factory/RoutePermissionsGuardFactory.php index c6fdc25e..2071fd38 100644 --- a/src/ZfcRbac/Factory/RoutePermissionsGuardFactory.php +++ b/src/ZfcRbac/Factory/RoutePermissionsGuardFactory.php @@ -18,8 +18,11 @@ namespace ZfcRbac\Factory; +use Interop\Container\ContainerInterface; +use Interop\Container\Exception\ContainerException; +use Zend\ServiceManager\Exception\ServiceNotCreatedException; +use Zend\ServiceManager\Exception\ServiceNotFoundException; use Zend\ServiceManager\FactoryInterface; -use Zend\ServiceManager\MutableCreationOptionsInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Guard\RouteGuard; use ZfcRbac\Guard\RoutePermissionsGuard; @@ -31,19 +34,26 @@ * @author JM Lerouxw * @license MIT */ -class RoutePermissionsGuardFactory implements FactoryInterface, MutableCreationOptionsInterface +class RoutePermissionsGuardFactory implements FactoryInterface { /** - * @var array + * @param ContainerInterface $container + * @param string $requestedName + * @param array $options + * @return RoutePermissionsGuard */ - protected $options = []; - - /** - * {@inheritDoc} - */ - public function setCreationOptions(array $options) + public function __invoke(ContainerInterface $container, $requestedName, array $options = []) { - $this->options = $options; + /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ + $moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions'); + + /* @var \ZfcRbac\Service\AuthorizationService $authorizationService */ + $authorizationService = $container->get('ZfcRbac\Service\AuthorizationService'); + + $routeGuard = new RoutePermissionsGuard($authorizationService, $options); + $routeGuard->setProtectionPolicy($moduleOptions->getProtectionPolicy()); + + return $routeGuard; } /** @@ -52,17 +62,6 @@ public function setCreationOptions(array $options) */ public function createService(ServiceLocatorInterface $serviceLocator) { - $parentLocator = $serviceLocator->getServiceLocator(); - - /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ - $moduleOptions = $parentLocator->get('ZfcRbac\Options\ModuleOptions'); - - /* @var \ZfcRbac\Service\AuthorizationService $authorizationService */ - $authorizationService = $parentLocator->get('ZfcRbac\Service\AuthorizationService'); - - $routeGuard = new RoutePermissionsGuard($authorizationService, $this->options); - $routeGuard->setProtectionPolicy($moduleOptions->getProtectionPolicy()); - - return $routeGuard; + return $this($serviceLocator, RoutePermissionsGuard::class); } } diff --git a/src/ZfcRbac/Factory/UnauthorizedStrategyFactory.php b/src/ZfcRbac/Factory/UnauthorizedStrategyFactory.php index 699dd23f..31763582 100644 --- a/src/ZfcRbac/Factory/UnauthorizedStrategyFactory.php +++ b/src/ZfcRbac/Factory/UnauthorizedStrategyFactory.php @@ -18,6 +18,7 @@ namespace ZfcRbac\Factory; +use Interop\Container\ContainerInterface; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\View\Strategy\UnauthorizedStrategy; @@ -31,13 +32,24 @@ class UnauthorizedStrategyFactory implements FactoryInterface { /** - * {@inheritDoc} + * @param ContainerInterface $container + * @param string $requestedName + * @param array|null $options + * @return UnauthorizedStrategy */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ - $moduleOptions = $serviceLocator->get('ZfcRbac\Options\ModuleOptions'); + $moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions'); return new UnauthorizedStrategy($moduleOptions->getUnauthorizedStrategy()); } + + /** + * {@inheritDoc} + */ + public function createService(ServiceLocatorInterface $serviceLocator) + { + return $this($serviceLocator, UnauthorizedStrategy::class); + } } diff --git a/src/ZfcRbac/Guard/AbstractGuard.php b/src/ZfcRbac/Guard/AbstractGuard.php index 4eba2d74..33796c4a 100644 --- a/src/ZfcRbac/Guard/AbstractGuard.php +++ b/src/ZfcRbac/Guard/AbstractGuard.php @@ -46,9 +46,9 @@ abstract class AbstractGuard implements GuardInterface /** * {@inheritDoc} */ - public function attach(EventManagerInterface $events) + public function attach(EventManagerInterface $events, $priority = self::EVENT_PRIORITY) { - $this->listeners[] = $events->attach(static::EVENT_NAME, [$this, 'onResult'], static::EVENT_PRIORITY); + $this->listeners[] = $events->attach(static::EVENT_NAME, [$this, 'onResult'], $priority); } /** diff --git a/src/ZfcRbac/Guard/GuardPluginManager.php b/src/ZfcRbac/Guard/GuardPluginManager.php index 0a510482..2f54b8aa 100644 --- a/src/ZfcRbac/Guard/GuardPluginManager.php +++ b/src/ZfcRbac/Guard/GuardPluginManager.php @@ -45,7 +45,7 @@ class GuardPluginManager extends AbstractPluginManager /** * {@inheritDoc} */ - public function validatePlugin($plugin) + public function validate($plugin) { if ($plugin instanceof GuardInterface) { return; // we're okay @@ -57,6 +57,14 @@ public function validatePlugin($plugin) )); } + /** + * {@inheritDoc} + */ + public function validatePlugin($plugin) + { + $this->validate($plugin); + } + /** * {@inheritDoc} */ diff --git a/src/ZfcRbac/Initializer/AuthorizationServiceInitializer.php b/src/ZfcRbac/Initializer/AuthorizationServiceInitializer.php index dcaa358a..c3a32e84 100644 --- a/src/ZfcRbac/Initializer/AuthorizationServiceInitializer.php +++ b/src/ZfcRbac/Initializer/AuthorizationServiceInitializer.php @@ -18,6 +18,7 @@ namespace ZfcRbac\Initializer; +use Interop\Container\ContainerInterface; use Zend\ServiceManager\AbstractPluginManager; use Zend\ServiceManager\InitializerInterface; use Zend\ServiceManager\ServiceLocatorInterface; @@ -32,17 +33,26 @@ class AuthorizationServiceInitializer implements InitializerInterface { /** - * @see \Zend\ServiceManager\InitializerInterface::initialize() + * @param ContainerInterface $container + * @param object $instance */ - public function initialize($instance, ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $instance) { if ($instance instanceof AuthorizationServiceAwareInterface) { - if ($serviceLocator instanceof AbstractPluginManager) { - $serviceLocator = $serviceLocator->getServiceLocator(); + if ($container instanceof AbstractPluginManager) { + $serviceLocator = $container->getServiceLocator(); } - - $authorizationService = $serviceLocator->get('ZfcRbac\Service\AuthorizationService'); + + $authorizationService = $container->get('ZfcRbac\Service\AuthorizationService'); $instance->setAuthorizationService($authorizationService); } } + + /** + * @see \Zend\ServiceManager\InitializerInterface::initialize() + */ + public function initialize($instance, ServiceLocatorInterface $serviceLocator) + { + $this($serviceLocator, $instance); + } } diff --git a/src/ZfcRbac/Module.php b/src/ZfcRbac/Module.php index 372b044e..255dbb31 100644 --- a/src/ZfcRbac/Module.php +++ b/src/ZfcRbac/Module.php @@ -45,7 +45,7 @@ public function onBootstrap(EventInterface $event) // Register listeners, if any foreach ($guards as $guard) { - $eventManager->attachAggregate($guard); + $guard->attach($eventManager); } } diff --git a/src/ZfcRbac/Role/RoleProviderPluginManager.php b/src/ZfcRbac/Role/RoleProviderPluginManager.php index d8f995f1..02e2d5fb 100644 --- a/src/ZfcRbac/Role/RoleProviderPluginManager.php +++ b/src/ZfcRbac/Role/RoleProviderPluginManager.php @@ -48,7 +48,7 @@ class RoleProviderPluginManager extends AbstractPluginManager /** * {@inheritDoc} */ - public function validatePlugin($plugin) + public function validate($plugin) { if ($plugin instanceof RoleProviderInterface) { return; // we're okay @@ -60,6 +60,14 @@ public function validatePlugin($plugin) )); } + /** + * {@inheritDoc} + */ + public function validatePlugin($plugin) + { + $this->validate($plugin); + } + /** * {@inheritDoc} */ diff --git a/src/ZfcRbac/View/Strategy/AbstractStrategy.php b/src/ZfcRbac/View/Strategy/AbstractStrategy.php index a245de2d..8eda6034 100644 --- a/src/ZfcRbac/View/Strategy/AbstractStrategy.php +++ b/src/ZfcRbac/View/Strategy/AbstractStrategy.php @@ -33,9 +33,9 @@ abstract class AbstractStrategy extends AbstractListenerAggregate /** * {@inheritDoc} */ - public function attach(EventManagerInterface $events) + public function attach(EventManagerInterface $events, $priority = 1) { - $this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH_ERROR, [$this, 'onError']); + $this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH_ERROR, [$this, 'onError'], $priority); } /** diff --git a/tests/ZfcRbacTest/Factory/AssertionPluginManagerFactoryTest.php b/tests/ZfcRbacTest/Factory/AssertionPluginManagerFactoryTest.php index 4d83c21a..62d22f66 100644 --- a/tests/ZfcRbacTest/Factory/AssertionPluginManagerFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/AssertionPluginManagerFactoryTest.php @@ -39,6 +39,5 @@ public function testFactory() $pluginManager = $factory->createService($serviceManager); $this->assertInstanceOf('ZfcRbac\Assertion\AssertionPluginManager', $pluginManager); - $this->assertSame($serviceManager, $pluginManager->getServiceLocator()); } } diff --git a/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php b/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php index ca6661dd..97ee413f 100644 --- a/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php +++ b/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php @@ -87,7 +87,7 @@ public function testAuthorizationServiceIsInjectedWithDelegator() ); $serviceManager->addDelegator( - 'ZfcRbacTest\AuthorizationAware', + 'ZfcRbacTest\Initializer\AuthorizationAwareFake', 'ZfcRbac\Factory\AuthorizationServiceDelegatorFactory' ); @@ -120,9 +120,10 @@ function () { ); $this->setExpectedException( - 'ZfcRbac\Exception\RuntimeException', + 'Zend\ServiceManager\Exception\ServiceNotCreatedException', 'The service ZfcRbacTest\AuthorizationAware must implement AuthorizationServiceAwareInterface.' ); + $serviceManager->get('ZfcRbacTest\AuthorizationAware'); } } diff --git a/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php index 850c0001..888ab56f 100644 --- a/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php @@ -50,11 +50,10 @@ public function testFactory() $this->getMock('ZfcRbac\Service\RoleService', [], [], '', false) ); - $pluginManager = new GuardPluginManager(); - $pluginManager->setServiceLocator($serviceManager); + $pluginManager = new GuardPluginManager($serviceManager); $factory = new ControllerGuardFactory(); - $controllerGuard = $factory->createService($pluginManager); + $controllerGuard = $factory->createService($serviceManager); $this->assertInstanceOf('ZfcRbac\Guard\ControllerGuard', $controllerGuard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $controllerGuard->getProtectionPolicy()); diff --git a/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php index 04674105..35c0ea09 100644 --- a/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php @@ -50,11 +50,10 @@ public function testFactory() $this->getMock('ZfcRbac\Service\AuthorizationService', [], [], '', false) ); - $pluginManager = new GuardPluginManager(); - $pluginManager->setServiceLocator($serviceManager); + $pluginManager = new GuardPluginManager($serviceManager); $factory = new ControllerPermissionsGuardFactory(); - $guard = $factory->createService($pluginManager); + $guard = $factory->createService($serviceManager); $this->assertInstanceOf('ZfcRbac\Guard\ControllerPermissionsGuard', $guard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $guard->getProtectionPolicy()); diff --git a/tests/ZfcRbacTest/Factory/GuardPluginManagerFactoryTest.php b/tests/ZfcRbacTest/Factory/GuardPluginManagerFactoryTest.php index 1f7b2c28..1ef9f2a0 100644 --- a/tests/ZfcRbacTest/Factory/GuardPluginManagerFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/GuardPluginManagerFactoryTest.php @@ -39,6 +39,5 @@ public function testFactory() $pluginManager = $factory->createService($serviceManager); $this->assertInstanceOf('ZfcRbac\Guard\GuardPluginManager', $pluginManager); - $this->assertSame($serviceManager, $pluginManager->getServiceLocator()); } } diff --git a/tests/ZfcRbacTest/Factory/GuardsFactoryTest.php b/tests/ZfcRbacTest/Factory/GuardsFactoryTest.php index f1c494b7..494526b4 100644 --- a/tests/ZfcRbacTest/Factory/GuardsFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/GuardsFactoryTest.php @@ -51,9 +51,9 @@ public function testFactory() ] ]); - $pluginManager = new GuardPluginManager(); - $serviceManager = new ServiceManager(); + $pluginManager = new GuardPluginManager($serviceManager); + $serviceManager->setService('ZfcRbac\Options\ModuleOptions', $moduleOptions); $serviceManager->setService('ZfcRbac\Guard\GuardPluginManager', $pluginManager); $serviceManager->setService( @@ -65,8 +65,6 @@ public function testFactory() $this->getMock('ZfcRbac\Service\AuthorizationServiceInterface', [], [], '', false) ); - $pluginManager->setServiceLocator($serviceManager); - $factory = new GuardsFactory(); $guards = $factory->createService($serviceManager); @@ -85,11 +83,10 @@ public function testReturnArrayIfNoConfig() 'guards' => [] ]); - $pluginManager = new GuardPluginManager(); - $serviceManager = new ServiceManager(); + $pluginManager = new GuardPluginManager($serviceManager); + $serviceManager->setService('ZfcRbac\Options\ModuleOptions', $moduleOptions); - $pluginManager->setServiceLocator($serviceManager); $factory = new GuardsFactory(); $guards = $factory->createService($serviceManager); diff --git a/tests/ZfcRbacTest/Factory/HasRoleViewHelperFactoryTest.php b/tests/ZfcRbacTest/Factory/HasRoleViewHelperFactoryTest.php index c2a052ee..2b61d271 100644 --- a/tests/ZfcRbacTest/Factory/HasRoleViewHelperFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/HasRoleViewHelperFactoryTest.php @@ -31,8 +31,7 @@ public function testFactory() { $serviceManager = new ServiceManager(); - $pluginManager = new HelperPluginManager(); - $pluginManager->setServiceLocator($serviceManager); + $pluginManager = new HelperPluginManager($serviceManager); $serviceManager->setService( 'ZfcRbac\Service\RoleService', @@ -40,7 +39,7 @@ public function testFactory() ); $factory = new HasRoleViewHelperFactory(); - $viewHelper = $factory->createService($pluginManager); + $viewHelper = $factory->createService($serviceManager); $this->assertInstanceOf('ZfcRbac\View\Helper\HasRole', $viewHelper); } diff --git a/tests/ZfcRbacTest/Factory/IsGrantedPluginFactoryTest.php b/tests/ZfcRbacTest/Factory/IsGrantedPluginFactoryTest.php index 9095c91b..503398d8 100644 --- a/tests/ZfcRbacTest/Factory/IsGrantedPluginFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/IsGrantedPluginFactoryTest.php @@ -31,8 +31,7 @@ public function testFactory() { $serviceManager = new ServiceManager(); - $pluginManager = new PluginManager(); - $pluginManager->setServiceLocator($serviceManager); + $pluginManager = new PluginManager($serviceManager); $serviceManager->setService( 'ZfcRbac\Service\AuthorizationService', @@ -40,7 +39,7 @@ public function testFactory() ); $factory = new IsGrantedPluginFactory(); - $isGranted = $factory->createService($pluginManager); + $isGranted = $factory->createService($serviceManager); $this->assertInstanceOf('ZfcRbac\Mvc\Controller\Plugin\IsGranted', $isGranted); } diff --git a/tests/ZfcRbacTest/Factory/IsGrantedViewHelperFactoryTest.php b/tests/ZfcRbacTest/Factory/IsGrantedViewHelperFactoryTest.php index aa638d39..89ee57a5 100644 --- a/tests/ZfcRbacTest/Factory/IsGrantedViewHelperFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/IsGrantedViewHelperFactoryTest.php @@ -31,8 +31,7 @@ public function testFactory() { $serviceManager = new ServiceManager(); - $pluginManager = new HelperPluginManager(); - $pluginManager->setServiceLocator($serviceManager); + $pluginManager = new HelperPluginManager($serviceManager); $serviceManager->setService( 'ZfcRbac\Service\AuthorizationService', @@ -40,7 +39,7 @@ public function testFactory() ); $factory = new IsGrantedViewHelperFactory(); - $isGranted = $factory->createService($pluginManager); + $isGranted = $factory->createService($serviceManager); $this->assertInstanceOf('ZfcRbac\View\Helper\IsGranted', $isGranted); } diff --git a/tests/ZfcRbacTest/Factory/ObjectRepositoryRoleProviderFactoryTest.php b/tests/ZfcRbacTest/Factory/ObjectRepositoryRoleProviderFactoryTest.php index bd328e02..6888e967 100644 --- a/tests/ZfcRbacTest/Factory/ObjectRepositoryRoleProviderFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/ObjectRepositoryRoleProviderFactoryTest.php @@ -30,10 +30,8 @@ class ObjectRepositoryRoleProviderFactoryTest extends \PHPUnit_Framework_TestCas { public function testFactoryUsingObjectRepository() { - $pluginManager = new RoleProviderPluginManager(); $serviceManager = new ServiceManager(); - - $pluginManager->setServiceLocator($serviceManager); + $pluginManager = new RoleProviderPluginManager($serviceManager); $options = [ 'role_name_property' => 'name', @@ -48,10 +46,8 @@ public function testFactoryUsingObjectRepository() public function testFactoryUsingObjectManager() { - $pluginManager = new RoleProviderPluginManager(); $serviceManager = new ServiceManager(); - - $pluginManager->setServiceLocator($serviceManager); + $pluginManager = new RoleProviderPluginManager($serviceManager); $options = [ 'role_name_property' => 'name', @@ -77,10 +73,9 @@ public function testFactoryUsingObjectManager() public function testThrowExceptionIfNoRoleNamePropertyIsSet() { try { - $pluginManager = new RoleProviderPluginManager(); $serviceManager = new ServiceManager(); + $pluginManager = new RoleProviderPluginManager($serviceManager); - $pluginManager->setServiceLocator($serviceManager); $pluginManager->get('ZfcRbac\Role\ObjectRepositoryRoleProvider', []); } catch (ServiceNotCreatedException $smException) { while ($e = $smException->getPrevious()) { @@ -102,10 +97,9 @@ public function testThrowExceptionIfNoRoleNamePropertyIsSet() public function testThrowExceptionIfNoObjectManagerNorObjectRepositoryIsSet() { try { - $pluginManager = new RoleProviderPluginManager(); $serviceManager = new ServiceManager(); + $pluginManager = new RoleProviderPluginManager($serviceManager); - $pluginManager->setServiceLocator($serviceManager); $pluginManager->get('ZfcRbac\Role\ObjectRepositoryRoleProvider', [ 'role_name_property' => 'name' ]); diff --git a/tests/ZfcRbacTest/Factory/RoleProviderPluginManagerFactoryTest.php b/tests/ZfcRbacTest/Factory/RoleProviderPluginManagerFactoryTest.php index 13c490e5..d99f6f6d 100644 --- a/tests/ZfcRbacTest/Factory/RoleProviderPluginManagerFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/RoleProviderPluginManagerFactoryTest.php @@ -39,6 +39,5 @@ public function testFactory() $pluginManager = $factory->createService($serviceManager); $this->assertInstanceOf('ZfcRbac\Role\RoleProviderPluginManager', $pluginManager); - $this->assertSame($serviceManager, $pluginManager->getServiceLocator()); } } diff --git a/tests/ZfcRbacTest/Factory/RoleServiceFactoryTest.php b/tests/ZfcRbacTest/Factory/RoleServiceFactoryTest.php index 63eafca8..f420242d 100644 --- a/tests/ZfcRbacTest/Factory/RoleServiceFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/RoleServiceFactoryTest.php @@ -42,7 +42,7 @@ public function testFactory() $serviceManager = new ServiceManager(); $serviceManager->setService('ZfcRbac\Options\ModuleOptions', $options); - $serviceManager->setService('ZfcRbac\Role\RoleProviderPluginManager', new RoleProviderPluginManager()); + $serviceManager->setService('ZfcRbac\Role\RoleProviderPluginManager', new RoleProviderPluginManager($serviceManager)); $serviceManager->setService( 'ZfcRbac\Identity\AuthenticationProvider', $this->getMock('ZfcRbac\Identity\IdentityProviderInterface') diff --git a/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php index 8063ab49..f9491383 100644 --- a/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php @@ -50,11 +50,10 @@ public function testFactory() $this->getMock('ZfcRbac\Service\RoleService', [], [], '', false) ); - $pluginManager = new GuardPluginManager(); - $pluginManager->setServiceLocator($serviceManager); + $pluginManager = new GuardPluginManager($serviceManager); $factory = new RouteGuardFactory(); - $routeGuard = $factory->createService($pluginManager); + $routeGuard = $factory->createService($serviceManager); $this->assertInstanceOf('ZfcRbac\Guard\RouteGuard', $routeGuard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $routeGuard->getProtectionPolicy()); diff --git a/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php index 6aee9e56..cf263878 100644 --- a/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php @@ -50,11 +50,10 @@ public function testFactory() $this->getMock('ZfcRbac\Service\AuthorizationService', [], [], '', false) ); - $pluginManager = new GuardPluginManager(); - $pluginManager->setServiceLocator($serviceManager); + $pluginManager = new GuardPluginManager($serviceManager); $factory = new RoutePermissionsGuardFactory(); - $routeGuard = $factory->createService($pluginManager); + $routeGuard = $factory->createService($serviceManager); $this->assertInstanceOf('ZfcRbac\Guard\RoutePermissionsGuard', $routeGuard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $routeGuard->getProtectionPolicy()); diff --git a/tests/ZfcRbacTest/Guard/GuardPluginManagerTest.php b/tests/ZfcRbacTest/Guard/GuardPluginManagerTest.php index ff43d263..49d29eb0 100644 --- a/tests/ZfcRbacTest/Guard/GuardPluginManagerTest.php +++ b/tests/ZfcRbacTest/Guard/GuardPluginManagerTest.php @@ -18,6 +18,7 @@ namespace ZfcRbacTest\Guard; +use Interop\Container\ContainerInterface; use Zend\ServiceManager\ServiceManager; use ZfcRbac\Guard\GuardPluginManager; use ZfcRbac\Options\ModuleOptions; @@ -81,8 +82,7 @@ public function testCanCreateDefaultGuards($type, $options) $this->getMock('ZfcRbac\Service\AuthorizationService', [], [], '', false) ); - $pluginManager = new GuardPluginManager(); - $pluginManager->setServiceLocator($serviceManager); + $pluginManager = new GuardPluginManager($serviceManager); $guard = $pluginManager->get($type, $options); @@ -93,7 +93,7 @@ public function testThrowExceptionForInvalidPlugin() { $this->setExpectedException('ZfcRbac\Exception\RuntimeException'); - $pluginManager = new GuardPluginManager(); + $pluginManager = new GuardPluginManager($this->getMock(ContainerInterface::class)); $pluginManager->get('stdClass'); } } diff --git a/tests/ZfcRbacTest/Role/RoleProviderPluginManagerTest.php b/tests/ZfcRbacTest/Role/RoleProviderPluginManagerTest.php index 8667acb0..32452c27 100644 --- a/tests/ZfcRbacTest/Role/RoleProviderPluginManagerTest.php +++ b/tests/ZfcRbacTest/Role/RoleProviderPluginManagerTest.php @@ -18,6 +18,7 @@ namespace ZfcRbacTest\Role; +use Interop\Container\ContainerInterface; use ZfcRbac\Role\RoleProviderPluginManager; /** @@ -28,16 +29,16 @@ class RoleProviderPluginManagerTest extends \PHPUnit_Framework_TestCase public function testValidationOfPluginSucceedsIfRoleProviderInterfaceIsImplemented() { $pluginMock = $this->getMock('ZfcRbac\Role\RoleProviderInterface'); - $pluginManager = new RoleProviderPluginManager(); + $pluginManager = new RoleProviderPluginManager($this->getMock(ContainerInterface::class)); - $this->assertNull($pluginManager->validatePlugin($pluginMock)); + $this->assertNull($pluginManager->validate($pluginMock)); } public function testValidationOfPluginFailsIfRoleProviderInterfaceIsNotImplemented() { $this->setExpectedException('ZfcRbac\Exception\RuntimeException'); - $pluginManager = new RoleProviderPluginManager(); + $pluginManager = new RoleProviderPluginManager($this->getMock(ContainerInterface::class)); $pluginManager->get('stdClass', []); } } diff --git a/tests/ZfcRbacTest/Service/AuthorizationServiceTest.php b/tests/ZfcRbacTest/Service/AuthorizationServiceTest.php index 1685dc01..73e3fb63 100644 --- a/tests/ZfcRbacTest/Service/AuthorizationServiceTest.php +++ b/tests/ZfcRbacTest/Service/AuthorizationServiceTest.php @@ -18,6 +18,7 @@ namespace ZfcRbacTest\Service; +use Interop\Container\ContainerInterface; use Rbac\Rbac; use Rbac\Traversal\Strategy\RecursiveRoleIteratorStrategy; use ZfcRbac\Role\InMemoryRoleProvider; @@ -138,7 +139,7 @@ public function testGranted($role, $permission, $context, $isGranted, $assertion new InMemoryRoleProvider($roleConfig), $rbac->getTraversalStrategy() ); - $assertionPluginManager = new AssertionPluginManager(new Config($assertionPluginConfig)); + $assertionPluginManager = new AssertionPluginManager($this->getMock(ContainerInterface::class), $assertionPluginConfig); $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager); $authorizationService->setAssertions($assertions); diff --git a/tests/ZfcRbacTest/Util/ServiceManagerFactory.php b/tests/ZfcRbacTest/Util/ServiceManagerFactory.php index 964f19ab..5caeff9e 100644 --- a/tests/ZfcRbacTest/Util/ServiceManagerFactory.php +++ b/tests/ZfcRbacTest/Util/ServiceManagerFactory.php @@ -60,9 +60,7 @@ public static function getServiceManager(array $config = null) { $config = $config ?: static::getApplicationConfig(); $serviceManager = new ServiceManager( - new ServiceManagerConfig( - isset($config['service_manager']) ? $config['service_manager'] : [] - ) + (new ServiceManagerConfig(isset($config['service_manager']) ? $config['service_manager'] : []))->toArray() ); $serviceManager->setService('ApplicationConfig', $config); From a07ee7d1a101c32e090695426a6b5f246a61779f Mon Sep 17 00:00:00 2001 From: prolic Date: Fri, 8 Jul 2016 17:36:32 +0800 Subject: [PATCH 07/29] require newer versions of zf2/3 --- composer.json | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 2c30bd1f..601d1883 100644 --- a/composer.json +++ b/composer.json @@ -28,8 +28,10 @@ ], "require": { "php": "~5.6 || ~7.0", - "zendframework/zend-mvc": "~2.2 || ^3.0", - "zendframework/zend-servicemanager": "~2.2 || ^3.0", + "zendframework/zend-config": "~2.2", + "zendframework/zend-eventmanager": "^3.0", + "zendframework/zend-mvc": "~2.7.4", + "zendframework/zend-servicemanager": "^3.1", "zfr/rbac": "~1.2" }, "require-dev": { @@ -37,10 +39,10 @@ "zendframework/zend-developer-tools": "~1.1", "zendframework/zend-log": "~2.2", "zendframework/zend-http": "~2.2", - "zendframework/zend-i18n": "~2.2", + "zendframework/zend-i18n": "~2.7.3", "zendframework/zend-serializer": "~2.2", "zendframework/zend-view": "~2.2", - "phpunit/phpunit": "~4.8", + "phpunit/phpunit": "~4.8.26", "squizlabs/php_codesniffer": "1.4.*", "satooshi/php-coveralls": "~0.6", "doctrine/common": "~2.4", From a5ea04f3612175622267a16ff2d603104f3b2efa Mon Sep 17 00:00:00 2001 From: prolic Date: Fri, 8 Jul 2016 18:30:13 +0800 Subject: [PATCH 08/29] allow zf2 and zf3 --- composer.json | 4 +- .../Factory/ControllerGuardFactory.php | 24 ++++++++++-- .../ControllerPermissionsGuardFactory.php | 38 ++++++++++--------- .../ObjectRepositoryRoleProviderFactory.php | 36 +++++++++++------- src/ZfcRbac/Factory/RouteGuardFactory.php | 23 +++++++++-- .../Factory/RoutePermissionsGuardFactory.php | 29 +++++++++----- .../AuthorizationServiceInitializer.php | 2 +- .../AuthorizationServiceDelegatorTest.php | 21 +++++++--- .../ControllerPermissionsGuardFactoryTest.php | 3 +- .../Guard/GuardPluginManagerTest.php | 1 + .../AuthorizationServiceInitializerTest.php | 2 +- .../Util/ServiceManagerFactory.php | 7 ++-- 12 files changed, 129 insertions(+), 61 deletions(-) diff --git a/composer.json b/composer.json index 601d1883..22eb0731 100644 --- a/composer.json +++ b/composer.json @@ -29,9 +29,9 @@ "require": { "php": "~5.6 || ~7.0", "zendframework/zend-config": "~2.2", - "zendframework/zend-eventmanager": "^3.0", + "zendframework/zend-eventmanager": "^2.6.3 || ^3.0", "zendframework/zend-mvc": "~2.7.4", - "zendframework/zend-servicemanager": "^3.1", + "zendframework/zend-servicemanager": "^2.7.6 || ^3.0", "zfr/rbac": "~1.2" }, "require-dev": { diff --git a/src/ZfcRbac/Factory/ControllerGuardFactory.php b/src/ZfcRbac/Factory/ControllerGuardFactory.php index e60a6de5..d591fe72 100644 --- a/src/ZfcRbac/Factory/ControllerGuardFactory.php +++ b/src/ZfcRbac/Factory/ControllerGuardFactory.php @@ -19,7 +19,6 @@ namespace ZfcRbac\Factory; use Interop\Container\ContainerInterface; -use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Guard\ControllerGuard; @@ -29,21 +28,38 @@ * @author Michaël Gallego * @license MIT */ -class ControllerGuardFactory implements FactoryInterface +class ControllerGuardFactory { + /** + * @var array + */ + protected $options = []; + + /** + * {@inheritDoc} + */ + public function __construct(array $options = []) + { + $this->options = $options; + } + /** * {@inheritDoc} * @return ControllerGuard */ - public function __invoke(ContainerInterface $container, $requestedName, array $options = []) + public function __invoke(ContainerInterface $container) { + if (method_exists($container, 'getServiceLocator')) { + $container = $container->getServiceLocator(); + } + /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ $moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions'); /* @var \ZfcRbac\Service\RoleService $roleService */ $roleService = $container->get('ZfcRbac\Service\RoleService'); - $controllerGuard = new ControllerGuard($roleService, $options); + $controllerGuard = new ControllerGuard($roleService, $this->options); $controllerGuard->setProtectionPolicy($moduleOptions->getProtectionPolicy()); return $controllerGuard; diff --git a/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php b/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php index 0a83d3f5..8a874a9a 100644 --- a/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php +++ b/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php @@ -19,10 +19,7 @@ namespace ZfcRbac\Factory; use Interop\Container\ContainerInterface; -use Zend\ServiceManager\FactoryInterface; -use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Guard\ControllerPermissionsGuard; -use ZfcRbac\Guard\RouteGuard; /** * Create a controller guard for checking permissions @@ -30,35 +27,40 @@ * @author JM Lerouxw * @license MIT */ -class ControllerPermissionsGuardFactory implements FactoryInterface +class ControllerPermissionsGuardFactory { + /** + * @var array + */ + protected $options = []; + + /** + * {@inheritDoc} + */ + public function __construct(array $options = []) + { + $this->options = $options; + } + /** * @param ContainerInterface $container - * @param string $requestedName - * @param array $options * @return ControllerPermissionsGuard */ - public function __invoke(ContainerInterface $container, $requestedName, array $options = []) + public function __invoke(ContainerInterface $container) { + if (method_exists($container, 'getServiceLocator')) { + $container = $container->getServiceLocator(); + } + /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ $moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions'); /* @var \ZfcRbac\Service\AuthorizationService $authorizationService */ $authorizationService = $container->get('ZfcRbac\Service\AuthorizationService'); - $guard = new ControllerPermissionsGuard($authorizationService, $options); + $guard = new ControllerPermissionsGuard($authorizationService, $this->options); $guard->setProtectionPolicy($moduleOptions->getProtectionPolicy()); return $guard; } - - - /** - * @param \Zend\ServiceManager\AbstractPluginManager|ServiceLocatorInterface $serviceLocator - * @return RouteGuard - */ - public function createService(ServiceLocatorInterface $serviceLocator) - { - return $this($serviceLocator, ControllerPermissionsGuard::class); - } } diff --git a/src/ZfcRbac/Factory/ObjectRepositoryRoleProviderFactory.php b/src/ZfcRbac/Factory/ObjectRepositoryRoleProviderFactory.php index 1d3fd43f..4c194796 100644 --- a/src/ZfcRbac/Factory/ObjectRepositoryRoleProviderFactory.php +++ b/src/ZfcRbac/Factory/ObjectRepositoryRoleProviderFactory.php @@ -19,7 +19,6 @@ namespace ZfcRbac\Factory; use Interop\Container\ContainerInterface; -use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Exception; use ZfcRbac\Role\ObjectRepositoryRoleProvider; @@ -30,35 +29,46 @@ * @author Michaël Gallego * @license MIT */ -class ObjectRepositoryRoleProviderFactory implements FactoryInterface +class ObjectRepositoryRoleProviderFactory { + /** + * @var array + */ + protected $options = []; + + /** + * {@inheritDoc} + */ + public function __construct(array $options = []) + { + $this->options = $this->options; + } + /** * @param ContainerInterface $container - * @param string $requestedName - * @param array|null $options * @return ObjectRepositoryRoleProvider */ - public function __invoke(ContainerInterface $container, $requestedName, array $options = null) + public function __invoke(ContainerInterface $container) { $objectRepository = null; - if (!isset($options['role_name_property'])) { + if (!isset($this->options['role_name_property'])) { throw new Exception\RuntimeException('The "role_name_property" option is missing'); } - if (isset($options['object_repository'])) { + if (isset($this->options['object_repository'])) { /* @var \Doctrine\Common\Persistence\ObjectRepository $objectRepository */ - $objectRepository = $container->get($options['object_repository']); + $objectRepository = $container->get($this->options['object_repository']); - return new ObjectRepositoryRoleProvider($objectRepository, $options['role_name_property']); + return new ObjectRepositoryRoleProvider($objectRepository, $this->options['role_name_property']); } - if (isset($options['object_manager']) && isset($options['class_name'])) { + if (isset($this->options['object_manager']) && isset($this->options['class_name'])) { /* @var \Doctrine\Common\Persistence\ObjectManager $objectManager */ - $objectManager = $container->get($options['object_manager']); - $objectRepository = $objectManager->getRepository($options['class_name']); + $objectManager = $container->get($this->options['object_manager']); + $objectRepository = $objectManager->getRepository($this->options['class_name']); - return new ObjectRepositoryRoleProvider($objectRepository, $options['role_name_property']); + return new ObjectRepositoryRoleProvider($objectRepository, $this->options['role_name_property']); } throw new Exception\RuntimeException( diff --git a/src/ZfcRbac/Factory/RouteGuardFactory.php b/src/ZfcRbac/Factory/RouteGuardFactory.php index 2212fa37..93027c89 100644 --- a/src/ZfcRbac/Factory/RouteGuardFactory.php +++ b/src/ZfcRbac/Factory/RouteGuardFactory.php @@ -34,21 +34,36 @@ */ class RouteGuardFactory implements FactoryInterface { + /** + * @var array + */ + protected $options = []; + + /** + * {@inheritDoc} + */ + public function __construct(array $options = []) + { + $this->options = $options; + } + /** * @param ContainerInterface $container - * @param string $requestedName - * @param array $options * @return RouteGuard */ - public function __invoke(ContainerInterface $container, $requestedName, array $options = []) + public function __invoke(ContainerInterface $container) { + if (method_exists($container, 'getServiceLocator')) { + $container = $container->getServiceLocator(); + } + /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ $moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions'); /* @var \ZfcRbac\Service\RoleService $roleService */ $roleService = $container->get('ZfcRbac\Service\RoleService'); - $routeGuard = new RouteGuard($roleService, $options); + $routeGuard = new RouteGuard($roleService, $this->options); $routeGuard->setProtectionPolicy($moduleOptions->getProtectionPolicy()); return $routeGuard; diff --git a/src/ZfcRbac/Factory/RoutePermissionsGuardFactory.php b/src/ZfcRbac/Factory/RoutePermissionsGuardFactory.php index 2071fd38..fb17c2c2 100644 --- a/src/ZfcRbac/Factory/RoutePermissionsGuardFactory.php +++ b/src/ZfcRbac/Factory/RoutePermissionsGuardFactory.php @@ -19,10 +19,6 @@ namespace ZfcRbac\Factory; use Interop\Container\ContainerInterface; -use Interop\Container\Exception\ContainerException; -use Zend\ServiceManager\Exception\ServiceNotCreatedException; -use Zend\ServiceManager\Exception\ServiceNotFoundException; -use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Guard\RouteGuard; use ZfcRbac\Guard\RoutePermissionsGuard; @@ -34,23 +30,38 @@ * @author JM Lerouxw * @license MIT */ -class RoutePermissionsGuardFactory implements FactoryInterface +class RoutePermissionsGuardFactory { + /** + * @var array + */ + protected $options = []; + + /** + * {@inheritDoc} + */ + public function __construct(array $options = []) + { + $this->options = $options; + } + /** * @param ContainerInterface $container - * @param string $requestedName - * @param array $options * @return RoutePermissionsGuard */ - public function __invoke(ContainerInterface $container, $requestedName, array $options = []) + public function __invoke(ContainerInterface $container) { + if (method_exists($container, 'getServiceLocator')) { + $container = $container->getServiceLocator(); + } + /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ $moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions'); /* @var \ZfcRbac\Service\AuthorizationService $authorizationService */ $authorizationService = $container->get('ZfcRbac\Service\AuthorizationService'); - $routeGuard = new RoutePermissionsGuard($authorizationService, $options); + $routeGuard = new RoutePermissionsGuard($authorizationService, $this->options); $routeGuard->setProtectionPolicy($moduleOptions->getProtectionPolicy()); return $routeGuard; diff --git a/src/ZfcRbac/Initializer/AuthorizationServiceInitializer.php b/src/ZfcRbac/Initializer/AuthorizationServiceInitializer.php index c3a32e84..70e9a94d 100644 --- a/src/ZfcRbac/Initializer/AuthorizationServiceInitializer.php +++ b/src/ZfcRbac/Initializer/AuthorizationServiceInitializer.php @@ -40,7 +40,7 @@ public function __invoke(ContainerInterface $container, $instance) { if ($instance instanceof AuthorizationServiceAwareInterface) { if ($container instanceof AbstractPluginManager) { - $serviceLocator = $container->getServiceLocator(); + $container = $container->getServiceLocator(); } $authorizationService = $container->get('ZfcRbac\Service\AuthorizationService'); diff --git a/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php b/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php index 97ee413f..61ce9a0b 100644 --- a/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php +++ b/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php @@ -86,6 +86,11 @@ public function testAuthorizationServiceIsInjectedWithDelegator() 'ZfcRbacTest\Initializer\AuthorizationAwareFake' ); + $serviceManager->addDelegator( + 'ZfcRbacTest\AuthorizationAware', + 'ZfcRbac\Factory\AuthorizationServiceDelegatorFactory' + ); + $serviceManager->addDelegator( 'ZfcRbacTest\Initializer\AuthorizationAwareFake', 'ZfcRbac\Factory\AuthorizationServiceDelegatorFactory' @@ -119,11 +124,17 @@ function () { 'ZfcRbac\Factory\AuthorizationServiceDelegatorFactory' ); - $this->setExpectedException( - 'Zend\ServiceManager\Exception\ServiceNotCreatedException', - 'The service ZfcRbacTest\AuthorizationAware must implement AuthorizationServiceAwareInterface.' - ); + $thrown = false; + try { + $serviceManager->get('ZfcRbacTest\AuthorizationAware'); + } catch (\Exception $e) { + $thrown = true; + $this->assertEquals('The service ZfcRbacTest\AuthorizationAware must implement AuthorizationServiceAwareInterface.', $e->getMessage()); + if ($e->getPrevious()) { + $this->assertInstanceOf('ZfcRbac\Exception\RuntimeException', $e->getPrevious()); + } + } - $serviceManager->get('ZfcRbacTest\AuthorizationAware'); + $this->assertTrue($thrown); } } diff --git a/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php index 35c0ea09..d41138b1 100644 --- a/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php @@ -20,6 +20,7 @@ use Zend\ServiceManager\ServiceManager; use ZfcRbac\Factory\ControllerPermissionsGuardFactory; +use ZfcRbac\Guard\ControllerPermissionsGuard; use ZfcRbac\Guard\GuardInterface; use ZfcRbac\Guard\GuardPluginManager; use ZfcRbac\Options\ModuleOptions; @@ -53,7 +54,7 @@ public function testFactory() $pluginManager = new GuardPluginManager($serviceManager); $factory = new ControllerPermissionsGuardFactory(); - $guard = $factory->createService($serviceManager); + $guard = $factory($serviceManager); $this->assertInstanceOf('ZfcRbac\Guard\ControllerPermissionsGuard', $guard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $guard->getProtectionPolicy()); diff --git a/tests/ZfcRbacTest/Guard/GuardPluginManagerTest.php b/tests/ZfcRbacTest/Guard/GuardPluginManagerTest.php index 49d29eb0..91b9369c 100644 --- a/tests/ZfcRbacTest/Guard/GuardPluginManagerTest.php +++ b/tests/ZfcRbacTest/Guard/GuardPluginManagerTest.php @@ -68,6 +68,7 @@ public function guardProvider() /** * @dataProvider guardProvider + * @group by */ public function testCanCreateDefaultGuards($type, $options) { diff --git a/tests/ZfcRbacTest/Initializer/AuthorizationServiceInitializerTest.php b/tests/ZfcRbacTest/Initializer/AuthorizationServiceInitializerTest.php index 0cb1b3bc..c0fdfbcb 100644 --- a/tests/ZfcRbacTest/Initializer/AuthorizationServiceInitializerTest.php +++ b/tests/ZfcRbacTest/Initializer/AuthorizationServiceInitializerTest.php @@ -31,7 +31,7 @@ public function testInitializer() $authServiceClassName = 'ZfcRbac\Service\AuthorizationService'; $initializer = new AuthorizationServiceInitializer(); $instance = new AuthorizationAwareFake(); - $serviceLocator = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface'); + $serviceLocator = $this->getMock('Zend\ServiceManager\ServiceManager'); $authorizationService = $this->getMock('ZfcRbac\Service\AuthorizationService', [], [], '', false); $serviceLocator->expects($this->once()) diff --git a/tests/ZfcRbacTest/Util/ServiceManagerFactory.php b/tests/ZfcRbacTest/Util/ServiceManagerFactory.php index 5caeff9e..0c636ad0 100644 --- a/tests/ZfcRbacTest/Util/ServiceManagerFactory.php +++ b/tests/ZfcRbacTest/Util/ServiceManagerFactory.php @@ -59,10 +59,11 @@ public static function getApplicationConfig() public static function getServiceManager(array $config = null) { $config = $config ?: static::getApplicationConfig(); - $serviceManager = new ServiceManager( - (new ServiceManagerConfig(isset($config['service_manager']) ? $config['service_manager'] : []))->toArray() - ); + $serviceManager = new ServiceManager(); $serviceManager->setService('ApplicationConfig', $config); + $serviceManagerConfig = new ServiceManagerConfig(); + $serviceManagerConfig->configureServiceManager($serviceManager); + $serviceManager->setAllowOverride(true); /* @var $moduleManager \Zend\ModuleManager\ModuleManagerInterface */ $moduleManager = $serviceManager->get('ModuleManager'); From ad351c9bb3c04d3b3de675c7621752788100d604 Mon Sep 17 00:00:00 2001 From: prolic Date: Fri, 8 Jul 2016 18:39:59 +0800 Subject: [PATCH 09/29] fix zf3 usage --- .../Factory/ControllerGuardFactory.php | 2 +- .../ControllerPermissionsGuardFactory.php | 7 +++--- .../ObjectRepositoryRoleProviderFactory.php | 24 +++++++++++-------- src/ZfcRbac/Factory/RouteGuardFactory.php | 8 ++----- .../Factory/RoutePermissionsGuardFactory.php | 2 +- .../AuthorizationServiceDelegatorTest.php | 2 +- 6 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/ZfcRbac/Factory/ControllerGuardFactory.php b/src/ZfcRbac/Factory/ControllerGuardFactory.php index d591fe72..f08d9176 100644 --- a/src/ZfcRbac/Factory/ControllerGuardFactory.php +++ b/src/ZfcRbac/Factory/ControllerGuardFactory.php @@ -49,7 +49,7 @@ public function __construct(array $options = []) */ public function __invoke(ContainerInterface $container) { - if (method_exists($container, 'getServiceLocator')) { + if (! method_exists($container, 'build')) { // servicemanager v3 $container = $container->getServiceLocator(); } diff --git a/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php b/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php index 8a874a9a..9562e0d2 100644 --- a/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php +++ b/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php @@ -46,10 +46,11 @@ public function __construct(array $options = []) * @param ContainerInterface $container * @return ControllerPermissionsGuard */ - public function __invoke(ContainerInterface $container) + public function __invoke(ContainerInterface $container, $resolvedName = ControllerPermissionsGuard::class, $options = []) { - if (method_exists($container, 'getServiceLocator')) { + if (! method_exists($container, 'build')) { // servicemanager v3 $container = $container->getServiceLocator(); + $options = $this->options; } /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ @@ -58,7 +59,7 @@ public function __invoke(ContainerInterface $container) /* @var \ZfcRbac\Service\AuthorizationService $authorizationService */ $authorizationService = $container->get('ZfcRbac\Service\AuthorizationService'); - $guard = new ControllerPermissionsGuard($authorizationService, $this->options); + $guard = new ControllerPermissionsGuard($authorizationService, $options); $guard->setProtectionPolicy($moduleOptions->getProtectionPolicy()); return $guard; diff --git a/src/ZfcRbac/Factory/ObjectRepositoryRoleProviderFactory.php b/src/ZfcRbac/Factory/ObjectRepositoryRoleProviderFactory.php index 4c194796..3af49dbd 100644 --- a/src/ZfcRbac/Factory/ObjectRepositoryRoleProviderFactory.php +++ b/src/ZfcRbac/Factory/ObjectRepositoryRoleProviderFactory.php @@ -41,34 +41,38 @@ class ObjectRepositoryRoleProviderFactory */ public function __construct(array $options = []) { - $this->options = $this->options; + $this->options = $options; } /** * @param ContainerInterface $container * @return ObjectRepositoryRoleProvider */ - public function __invoke(ContainerInterface $container) + public function __invoke(ContainerInterface $container, $resolvedName, $options) { + if (! method_exists($container, 'build')) { // servicemanager v3 + $options = $this->options; + } + $objectRepository = null; - if (!isset($this->options['role_name_property'])) { + if (!isset($options['role_name_property'])) { throw new Exception\RuntimeException('The "role_name_property" option is missing'); } - if (isset($this->options['object_repository'])) { + if (isset($options['object_repository'])) { /* @var \Doctrine\Common\Persistence\ObjectRepository $objectRepository */ - $objectRepository = $container->get($this->options['object_repository']); + $objectRepository = $container->get($options['object_repository']); - return new ObjectRepositoryRoleProvider($objectRepository, $this->options['role_name_property']); + return new ObjectRepositoryRoleProvider($objectRepository, $options['role_name_property']); } - if (isset($this->options['object_manager']) && isset($this->options['class_name'])) { + if (isset($options['object_manager']) && isset($options['class_name'])) { /* @var \Doctrine\Common\Persistence\ObjectManager $objectManager */ - $objectManager = $container->get($this->options['object_manager']); - $objectRepository = $objectManager->getRepository($this->options['class_name']); + $objectManager = $container->get($options['object_manager']); + $objectRepository = $objectManager->getRepository($options['class_name']); - return new ObjectRepositoryRoleProvider($objectRepository, $this->options['role_name_property']); + return new ObjectRepositoryRoleProvider($objectRepository, $options['role_name_property']); } throw new Exception\RuntimeException( diff --git a/src/ZfcRbac/Factory/RouteGuardFactory.php b/src/ZfcRbac/Factory/RouteGuardFactory.php index 93027c89..21c38215 100644 --- a/src/ZfcRbac/Factory/RouteGuardFactory.php +++ b/src/ZfcRbac/Factory/RouteGuardFactory.php @@ -19,10 +19,6 @@ namespace ZfcRbac\Factory; use Interop\Container\ContainerInterface; -use Interop\Container\Exception\ContainerException; -use Zend\ServiceManager\Exception\ServiceNotCreatedException; -use Zend\ServiceManager\Exception\ServiceNotFoundException; -use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Guard\RouteGuard; @@ -32,7 +28,7 @@ * @author Michaël Gallego * @license MIT */ -class RouteGuardFactory implements FactoryInterface +class RouteGuardFactory { /** * @var array @@ -53,7 +49,7 @@ public function __construct(array $options = []) */ public function __invoke(ContainerInterface $container) { - if (method_exists($container, 'getServiceLocator')) { + if (! method_exists($container, 'build')) { // servicemanager v3 $container = $container->getServiceLocator(); } diff --git a/src/ZfcRbac/Factory/RoutePermissionsGuardFactory.php b/src/ZfcRbac/Factory/RoutePermissionsGuardFactory.php index fb17c2c2..929e9c70 100644 --- a/src/ZfcRbac/Factory/RoutePermissionsGuardFactory.php +++ b/src/ZfcRbac/Factory/RoutePermissionsGuardFactory.php @@ -51,7 +51,7 @@ public function __construct(array $options = []) */ public function __invoke(ContainerInterface $container) { - if (method_exists($container, 'getServiceLocator')) { + if (! method_exists($container, 'build')) { // servicemanager v3 $container = $container->getServiceLocator(); } diff --git a/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php b/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php index 61ce9a0b..34f72cbe 100644 --- a/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php +++ b/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php @@ -129,7 +129,7 @@ function () { $serviceManager->get('ZfcRbacTest\AuthorizationAware'); } catch (\Exception $e) { $thrown = true; - $this->assertEquals('The service ZfcRbacTest\AuthorizationAware must implement AuthorizationServiceAwareInterface.', $e->getMessage()); + $this->assertStringEndsWith('The service ZfcRbacTest\AuthorizationAware must implement AuthorizationServiceAwareInterface.', $e->getMessage()); if ($e->getPrevious()) { $this->assertInstanceOf('ZfcRbac\Exception\RuntimeException', $e->getPrevious()); } From 5f55b6cf85036f2e9c38f686704a6a7fa4c246ac Mon Sep 17 00:00:00 2001 From: prolic Date: Fri, 8 Jul 2016 18:43:41 +0800 Subject: [PATCH 10/29] fix usage with zf2 --- src/ZfcRbac/Factory/ObjectRepositoryRoleProviderFactory.php | 1 + tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php | 2 +- .../Factory/ControllerPermissionsGuardFactoryTest.php | 2 +- tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php | 2 +- tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php | 2 +- 5 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/ZfcRbac/Factory/ObjectRepositoryRoleProviderFactory.php b/src/ZfcRbac/Factory/ObjectRepositoryRoleProviderFactory.php index 3af49dbd..08a2708d 100644 --- a/src/ZfcRbac/Factory/ObjectRepositoryRoleProviderFactory.php +++ b/src/ZfcRbac/Factory/ObjectRepositoryRoleProviderFactory.php @@ -51,6 +51,7 @@ public function __construct(array $options = []) public function __invoke(ContainerInterface $container, $resolvedName, $options) { if (! method_exists($container, 'build')) { // servicemanager v3 + $container = $container->getServiceLocator(); $options = $this->options; } diff --git a/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php index 888ab56f..d5bf17c5 100644 --- a/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php @@ -53,7 +53,7 @@ public function testFactory() $pluginManager = new GuardPluginManager($serviceManager); $factory = new ControllerGuardFactory(); - $controllerGuard = $factory->createService($serviceManager); + $controllerGuard = $factory->createService($pluginManager); $this->assertInstanceOf('ZfcRbac\Guard\ControllerGuard', $controllerGuard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $controllerGuard->getProtectionPolicy()); diff --git a/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php index d41138b1..93fe82c6 100644 --- a/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php @@ -54,7 +54,7 @@ public function testFactory() $pluginManager = new GuardPluginManager($serviceManager); $factory = new ControllerPermissionsGuardFactory(); - $guard = $factory($serviceManager); + $guard = $factory($pluginManager); $this->assertInstanceOf('ZfcRbac\Guard\ControllerPermissionsGuard', $guard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $guard->getProtectionPolicy()); diff --git a/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php index f9491383..c2198290 100644 --- a/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php @@ -53,7 +53,7 @@ public function testFactory() $pluginManager = new GuardPluginManager($serviceManager); $factory = new RouteGuardFactory(); - $routeGuard = $factory->createService($serviceManager); + $routeGuard = $factory->createService($pluginManager); $this->assertInstanceOf('ZfcRbac\Guard\RouteGuard', $routeGuard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $routeGuard->getProtectionPolicy()); diff --git a/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php index cf263878..178fc706 100644 --- a/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php @@ -53,7 +53,7 @@ public function testFactory() $pluginManager = new GuardPluginManager($serviceManager); $factory = new RoutePermissionsGuardFactory(); - $routeGuard = $factory->createService($serviceManager); + $routeGuard = $factory->createService($pluginManager); $this->assertInstanceOf('ZfcRbac\Guard\RoutePermissionsGuard', $routeGuard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $routeGuard->getProtectionPolicy()); From 3327195c73e86123030acdf0130904699f966b1b Mon Sep 17 00:00:00 2001 From: prolic Date: Fri, 8 Jul 2016 18:47:09 +0800 Subject: [PATCH 11/29] enable switch for zf3 in tests --- .../Factory/ControllerGuardFactoryTest.php | 11 ++++++++--- .../Factory/ControllerPermissionsGuardFactoryTest.php | 7 ++++++- tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php | 7 ++++++- .../Factory/RoutePermissionsGuardFactoryTest.php | 8 +++++++- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php index d5bf17c5..ef84f222 100644 --- a/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php @@ -53,9 +53,14 @@ public function testFactory() $pluginManager = new GuardPluginManager($serviceManager); $factory = new ControllerGuardFactory(); - $controllerGuard = $factory->createService($pluginManager); - $this->assertInstanceOf('ZfcRbac\Guard\ControllerGuard', $controllerGuard); - $this->assertEquals(GuardInterface::POLICY_ALLOW, $controllerGuard->getProtectionPolicy()); + if (method_exists($serviceManager, 'build')) { // servicemanager v3 + $guard = $factory->createService($serviceManager); + } else { + $guard = $factory->createService($pluginManager); + } + + $this->assertInstanceOf('ZfcRbac\Guard\ControllerGuard', $guard); + $this->assertEquals(GuardInterface::POLICY_ALLOW, $guard->getProtectionPolicy()); } } diff --git a/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php index 93fe82c6..9d1ddffa 100644 --- a/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php @@ -54,7 +54,12 @@ public function testFactory() $pluginManager = new GuardPluginManager($serviceManager); $factory = new ControllerPermissionsGuardFactory(); - $guard = $factory($pluginManager); + + if (method_exists($serviceManager, 'build')) { // servicemanager v3 + $guard = $factory($serviceManager); + } else { + $guard = $factory($pluginManager); + } $this->assertInstanceOf('ZfcRbac\Guard\ControllerPermissionsGuard', $guard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $guard->getProtectionPolicy()); diff --git a/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php index c2198290..bc7e961e 100644 --- a/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php @@ -53,7 +53,12 @@ public function testFactory() $pluginManager = new GuardPluginManager($serviceManager); $factory = new RouteGuardFactory(); - $routeGuard = $factory->createService($pluginManager); + + if (method_exists($serviceManager, 'build')) { // servicemanager v3 + $routeGuard = $factory->createService($serviceManager); + } else { + $routeGuard = $factory->createService($pluginManager); + } $this->assertInstanceOf('ZfcRbac\Guard\RouteGuard', $routeGuard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $routeGuard->getProtectionPolicy()); diff --git a/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php index 178fc706..74ac6ca6 100644 --- a/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php @@ -53,7 +53,13 @@ public function testFactory() $pluginManager = new GuardPluginManager($serviceManager); $factory = new RoutePermissionsGuardFactory(); - $routeGuard = $factory->createService($pluginManager); + + if (method_exists($serviceManager, 'build')) { // servicemanager v3 + $routeGuard = $factory->createService($serviceManager); + } else { + $routeGuard = $factory->createService($pluginManager); + } + $this->assertInstanceOf('ZfcRbac\Guard\RoutePermissionsGuard', $routeGuard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $routeGuard->getProtectionPolicy()); From 149166586ff8549ec17d50574988bf60e1b38b9a Mon Sep 17 00:00:00 2001 From: prolic Date: Fri, 8 Jul 2016 18:49:51 +0800 Subject: [PATCH 12/29] allow zend-mvc 3.0 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 22eb0731..1d415bd1 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "php": "~5.6 || ~7.0", "zendframework/zend-config": "~2.2", "zendframework/zend-eventmanager": "^2.6.3 || ^3.0", - "zendframework/zend-mvc": "~2.7.4", + "zendframework/zend-mvc": "~2.7.4 || ^3.0", "zendframework/zend-servicemanager": "^2.7.6 || ^3.0", "zfr/rbac": "~1.2" }, From 52b2b23634ad550b32cac11760555cf8bd5dadad Mon Sep 17 00:00:00 2001 From: prolic Date: Fri, 8 Jul 2016 18:52:57 +0800 Subject: [PATCH 13/29] php cs issues --- src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php b/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php index 9562e0d2..cb771517 100644 --- a/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php +++ b/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php @@ -46,8 +46,11 @@ public function __construct(array $options = []) * @param ContainerInterface $container * @return ControllerPermissionsGuard */ - public function __invoke(ContainerInterface $container, $resolvedName = ControllerPermissionsGuard::class, $options = []) - { + public function __invoke( + ContainerInterface $container, + $resolvedName = ControllerPermissionsGuard::class, + $options = [] + ) { if (! method_exists($container, 'build')) { // servicemanager v3 $container = $container->getServiceLocator(); $options = $this->options; From 66b84db5516986b705281bd79abd6005f83eadab Mon Sep 17 00:00:00 2001 From: prolic Date: Fri, 8 Jul 2016 19:04:23 +0800 Subject: [PATCH 14/29] update code sniffer --- composer.json | 2 +- src/ZfcRbac/Assertion/AssertionPluginManager.php | 2 +- src/ZfcRbac/Factory/AssertionPluginManagerFactory.php | 2 +- src/ZfcRbac/Options/ModuleOptions.php | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 1d415bd1..792c056a 100644 --- a/composer.json +++ b/composer.json @@ -43,7 +43,7 @@ "zendframework/zend-serializer": "~2.2", "zendframework/zend-view": "~2.2", "phpunit/phpunit": "~4.8.26", - "squizlabs/php_codesniffer": "1.4.*", + "squizlabs/php_codesniffer": "2.6.*", "satooshi/php-coveralls": "~0.6", "doctrine/common": "~2.4", "doctrine/doctrine-module": "~1.1", diff --git a/src/ZfcRbac/Assertion/AssertionPluginManager.php b/src/ZfcRbac/Assertion/AssertionPluginManager.php index 7f9cd05a..e236a22b 100644 --- a/src/ZfcRbac/Assertion/AssertionPluginManager.php +++ b/src/ZfcRbac/Assertion/AssertionPluginManager.php @@ -23,7 +23,7 @@ /** * Plugin manager to create assertions - * + * * @author Aeneas Rekkas * @license MIT * diff --git a/src/ZfcRbac/Factory/AssertionPluginManagerFactory.php b/src/ZfcRbac/Factory/AssertionPluginManagerFactory.php index 2c3f364e..3f120401 100644 --- a/src/ZfcRbac/Factory/AssertionPluginManagerFactory.php +++ b/src/ZfcRbac/Factory/AssertionPluginManagerFactory.php @@ -29,7 +29,7 @@ /** * Factory to create a assertion plugin manager - * + * * @author Aeneas Rekkas * @license MIT */ diff --git a/src/ZfcRbac/Options/ModuleOptions.php b/src/ZfcRbac/Options/ModuleOptions.php index 96eec9d8..648f4107 100644 --- a/src/ZfcRbac/Options/ModuleOptions.php +++ b/src/ZfcRbac/Options/ModuleOptions.php @@ -120,7 +120,7 @@ public function getIdentityProvider() /** * Set the assertions options - * + * * @param array $assertionMap * @return void */ @@ -131,7 +131,7 @@ public function setAssertionMap(array $assertionMap) /** * Get the assertions options - * + * * @return array */ public function getAssertionMap() From aab9b711f5dcb920e790b576b48b4e0ff51aeafd Mon Sep 17 00:00:00 2001 From: prolic Date: Fri, 8 Jul 2016 22:53:54 +0800 Subject: [PATCH 15/29] update ZfcRbacTest/Util/ServiceManagerFactory --- tests/ZfcRbacTest/Util/ServiceManagerFactory.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/ZfcRbacTest/Util/ServiceManagerFactory.php b/tests/ZfcRbacTest/Util/ServiceManagerFactory.php index 0c636ad0..4bd49fac 100644 --- a/tests/ZfcRbacTest/Util/ServiceManagerFactory.php +++ b/tests/ZfcRbacTest/Util/ServiceManagerFactory.php @@ -61,7 +61,9 @@ public static function getServiceManager(array $config = null) $config = $config ?: static::getApplicationConfig(); $serviceManager = new ServiceManager(); $serviceManager->setService('ApplicationConfig', $config); - $serviceManagerConfig = new ServiceManagerConfig(); + $serviceManagerConfig = new ServiceManagerConfig( + isset($config['service_manager']) ? $config['service_manager'] : [] + ); $serviceManagerConfig->configureServiceManager($serviceManager); $serviceManager->setAllowOverride(true); From 4b970dd60195397eb9087e8b064b4ec1621d33c5 Mon Sep 17 00:00:00 2001 From: prolic Date: Sat, 30 Jul 2016 20:37:58 +0800 Subject: [PATCH 16/29] update code again to be 100% backward compatible with zf2 components --- .../Factory/ControllerGuardFactory.php | 21 ++++++----- .../ControllerPermissionsGuardFactory.php | 35 +++++++++++++------ src/ZfcRbac/Factory/GuardsFactory.php | 4 +-- .../Factory/HasRoleViewHelperFactory.php | 2 +- .../Factory/IsGrantedPluginFactory.php | 2 +- .../Factory/IsGrantedViewHelperFactory.php | 2 +- .../ObjectRepositoryRoleProviderFactory.php | 22 +++++++----- src/ZfcRbac/Factory/RouteGuardFactory.php | 23 +++++++----- .../Factory/RoutePermissionsGuardFactory.php | 21 +++++++---- .../AssertionPluginManagerFactoryTest.php | 1 + .../AuthorizationServiceDelegatorTest.php | 22 +++--------- .../Factory/ControllerGuardFactoryTest.php | 14 +++----- .../ControllerPermissionsGuardFactoryTest.php | 11 ++---- .../Factory/GuardPluginManagerFactoryTest.php | 1 + .../ZfcRbacTest/Factory/GuardsFactoryTest.php | 3 ++ .../Factory/HasRoleViewHelperFactoryTest.php | 2 +- .../Factory/IsGrantedPluginFactoryTest.php | 5 +-- .../IsGrantedViewHelperFactoryTest.php | 2 +- ...bjectRepositoryRoleProviderFactoryTest.php | 14 +++++--- .../RoleProviderPluginManagerFactoryTest.php | 1 + .../Factory/RoleServiceFactoryTest.php | 2 +- .../Factory/RouteGuardFactoryTest.php | 10 ++---- .../RoutePermissionsGuardFactoryTest.php | 11 ++---- .../Guard/GuardPluginManagerTest.php | 7 ++-- .../AuthorizationServiceInitializerTest.php | 2 +- .../Role/RoleProviderPluginManagerTest.php | 7 ++-- .../Service/AuthorizationServiceTest.php | 3 +- .../Util/ServiceManagerFactory.php | 10 +++--- 28 files changed, 138 insertions(+), 122 deletions(-) diff --git a/src/ZfcRbac/Factory/ControllerGuardFactory.php b/src/ZfcRbac/Factory/ControllerGuardFactory.php index f08d9176..74630fa7 100644 --- a/src/ZfcRbac/Factory/ControllerGuardFactory.php +++ b/src/ZfcRbac/Factory/ControllerGuardFactory.php @@ -19,6 +19,7 @@ namespace ZfcRbac\Factory; use Interop\Container\ContainerInterface; +use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Guard\ControllerGuard; @@ -28,7 +29,7 @@ * @author Michaël Gallego * @license MIT */ -class ControllerGuardFactory +class ControllerGuardFactory implements FactoryInterface { /** * @var array @@ -39,6 +40,14 @@ class ControllerGuardFactory * {@inheritDoc} */ public function __construct(array $options = []) + { + $this->setCreationOptions($options); + } + + /** + * {@inheritDoc} + */ + public function setCreationOptions(array $options) { $this->options = $options; } @@ -47,19 +56,15 @@ public function __construct(array $options = []) * {@inheritDoc} * @return ControllerGuard */ - public function __invoke(ContainerInterface $container) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { - if (! method_exists($container, 'build')) { // servicemanager v3 - $container = $container->getServiceLocator(); - } - /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ $moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions'); /* @var \ZfcRbac\Service\RoleService $roleService */ $roleService = $container->get('ZfcRbac\Service\RoleService'); - $controllerGuard = new ControllerGuard($roleService, $this->options); + $controllerGuard = new ControllerGuard($roleService, $options); $controllerGuard->setProtectionPolicy($moduleOptions->getProtectionPolicy()); return $controllerGuard; @@ -72,6 +77,6 @@ public function __invoke(ContainerInterface $container) */ public function createService(ServiceLocatorInterface $serviceLocator) { - return $this($serviceLocator, ControllerGuard::class); + return $this($serviceLocator->getServiceLocator(), ControllerGuard::class, $this->options); } } diff --git a/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php b/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php index cb771517..3cc37cb4 100644 --- a/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php +++ b/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php @@ -19,6 +19,8 @@ namespace ZfcRbac\Factory; use Interop\Container\ContainerInterface; +use Zend\ServiceManager\FactoryInterface; +use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Guard\ControllerPermissionsGuard; /** @@ -27,7 +29,7 @@ * @author JM Lerouxw * @license MIT */ -class ControllerPermissionsGuardFactory +class ControllerPermissionsGuardFactory implements FactoryInterface { /** * @var array @@ -38,6 +40,14 @@ class ControllerPermissionsGuardFactory * {@inheritDoc} */ public function __construct(array $options = []) + { + $this->setCreationOptions($options); + } + + /** + * {@inheritDoc} + */ + public function setCreationOptions(array $options) { $this->options = $options; } @@ -46,16 +56,8 @@ public function __construct(array $options = []) * @param ContainerInterface $container * @return ControllerPermissionsGuard */ - public function __invoke( - ContainerInterface $container, - $resolvedName = ControllerPermissionsGuard::class, - $options = [] - ) { - if (! method_exists($container, 'build')) { // servicemanager v3 - $container = $container->getServiceLocator(); - $options = $this->options; - } - + public function __invoke(ContainerInterface $container, $resolvedName, array $options = null) + { /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ $moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions'); @@ -67,4 +69,15 @@ public function __invoke( return $guard; } + + /** + * @param \Zend\ServiceManager\AbstractPluginManager|ServiceLocatorInterface $serviceLocator + * @return ControllerPermissionsGuard + */ + public function createService(ServiceLocatorInterface $serviceLocator) + { + $parentLocator = $serviceLocator->getServiceLocator(); + + return $this($parentLocator, ControllerPermissionsGuard::class, $this->options); + } } diff --git a/src/ZfcRbac/Factory/GuardsFactory.php b/src/ZfcRbac/Factory/GuardsFactory.php index 4771b362..4f0b9340 100644 --- a/src/ZfcRbac/Factory/GuardsFactory.php +++ b/src/ZfcRbac/Factory/GuardsFactory.php @@ -19,11 +19,9 @@ namespace ZfcRbac\Factory; use Interop\Container\ContainerInterface; -use Interop\Container\Exception\ContainerException; -use Zend\ServiceManager\Exception\ServiceNotCreatedException; -use Zend\ServiceManager\Exception\ServiceNotFoundException; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; +use ZfcRbac\Guard\GuardInterface; /** * Create a list of guards diff --git a/src/ZfcRbac/Factory/HasRoleViewHelperFactory.php b/src/ZfcRbac/Factory/HasRoleViewHelperFactory.php index 0978f9d2..b2556a83 100644 --- a/src/ZfcRbac/Factory/HasRoleViewHelperFactory.php +++ b/src/ZfcRbac/Factory/HasRoleViewHelperFactory.php @@ -55,6 +55,6 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o */ public function createService(ServiceLocatorInterface $serviceLocator) { - return $this($serviceLocator, HasRole::class); + return $this($serviceLocator->getServiceLocator(), HasRole::class); } } diff --git a/src/ZfcRbac/Factory/IsGrantedPluginFactory.php b/src/ZfcRbac/Factory/IsGrantedPluginFactory.php index 8f4198b8..2b4aacd5 100644 --- a/src/ZfcRbac/Factory/IsGrantedPluginFactory.php +++ b/src/ZfcRbac/Factory/IsGrantedPluginFactory.php @@ -54,6 +54,6 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o */ public function createService(ServiceLocatorInterface $serviceLocator) { - return $this($serviceLocator, IsGranted::class); + return $this($serviceLocator->getServiceLocator(), IsGranted::class); } } diff --git a/src/ZfcRbac/Factory/IsGrantedViewHelperFactory.php b/src/ZfcRbac/Factory/IsGrantedViewHelperFactory.php index bc0db739..c5301142 100644 --- a/src/ZfcRbac/Factory/IsGrantedViewHelperFactory.php +++ b/src/ZfcRbac/Factory/IsGrantedViewHelperFactory.php @@ -54,6 +54,6 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o */ public function createService(ServiceLocatorInterface $serviceLocator) { - return $this($serviceLocator, IsGranted::class); + return $this($serviceLocator->getServiceLocator(), IsGranted::class); } } diff --git a/src/ZfcRbac/Factory/ObjectRepositoryRoleProviderFactory.php b/src/ZfcRbac/Factory/ObjectRepositoryRoleProviderFactory.php index 08a2708d..68288bb6 100644 --- a/src/ZfcRbac/Factory/ObjectRepositoryRoleProviderFactory.php +++ b/src/ZfcRbac/Factory/ObjectRepositoryRoleProviderFactory.php @@ -19,6 +19,7 @@ namespace ZfcRbac\Factory; use Interop\Container\ContainerInterface; +use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Exception; use ZfcRbac\Role\ObjectRepositoryRoleProvider; @@ -29,7 +30,7 @@ * @author Michaël Gallego * @license MIT */ -class ObjectRepositoryRoleProviderFactory +class ObjectRepositoryRoleProviderFactory implements FactoryInterface { /** * @var array @@ -40,21 +41,26 @@ class ObjectRepositoryRoleProviderFactory * {@inheritDoc} */ public function __construct(array $options = []) + { + $this->setCreationOptions($options); + } + + /** + * {@inheritDoc} + */ + public function setCreationOptions(array $options) { $this->options = $options; } /** * @param ContainerInterface $container + * @param string $requestedName + * @param array|null $options * @return ObjectRepositoryRoleProvider */ - public function __invoke(ContainerInterface $container, $resolvedName, $options) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { - if (! method_exists($container, 'build')) { // servicemanager v3 - $container = $container->getServiceLocator(); - $options = $this->options; - } - $objectRepository = null; if (!isset($options['role_name_property'])) { @@ -88,6 +94,6 @@ public function __invoke(ContainerInterface $container, $resolvedName, $options) */ public function createService(ServiceLocatorInterface $serviceLocator) { - return $this($serviceLocator, ObjectRepositoryRoleProvider::class); + return $this($serviceLocator->getServiceLocator(), ObjectRepositoryRoleProvider::class, $this->options); } } diff --git a/src/ZfcRbac/Factory/RouteGuardFactory.php b/src/ZfcRbac/Factory/RouteGuardFactory.php index 21c38215..05a45cf0 100644 --- a/src/ZfcRbac/Factory/RouteGuardFactory.php +++ b/src/ZfcRbac/Factory/RouteGuardFactory.php @@ -19,6 +19,7 @@ namespace ZfcRbac\Factory; use Interop\Container\ContainerInterface; +use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Guard\RouteGuard; @@ -28,7 +29,7 @@ * @author Michaël Gallego * @license MIT */ -class RouteGuardFactory +class RouteGuardFactory implements FactoryInterface { /** * @var array @@ -39,27 +40,33 @@ class RouteGuardFactory * {@inheritDoc} */ public function __construct(array $options = []) + { + $this->setCreationOptions($options); + } + + /** + * {@inheritDoc} + */ + public function setCreationOptions(array $options) { $this->options = $options; } /** * @param ContainerInterface $container + * @param string $requestedName + * @param array|null $options * @return RouteGuard */ - public function __invoke(ContainerInterface $container) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { - if (! method_exists($container, 'build')) { // servicemanager v3 - $container = $container->getServiceLocator(); - } - /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ $moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions'); /* @var \ZfcRbac\Service\RoleService $roleService */ $roleService = $container->get('ZfcRbac\Service\RoleService'); - $routeGuard = new RouteGuard($roleService, $this->options); + $routeGuard = new RouteGuard($roleService, $options); $routeGuard->setProtectionPolicy($moduleOptions->getProtectionPolicy()); return $routeGuard; @@ -72,6 +79,6 @@ public function __invoke(ContainerInterface $container) */ public function createService(ServiceLocatorInterface $serviceLocator) { - return $this($serviceLocator, RouteGuard::class); + return $this($serviceLocator->getServiceLocator(), RouteGuard::class, $this->options); } } diff --git a/src/ZfcRbac/Factory/RoutePermissionsGuardFactory.php b/src/ZfcRbac/Factory/RoutePermissionsGuardFactory.php index 929e9c70..8314e5d3 100644 --- a/src/ZfcRbac/Factory/RoutePermissionsGuardFactory.php +++ b/src/ZfcRbac/Factory/RoutePermissionsGuardFactory.php @@ -19,6 +19,7 @@ namespace ZfcRbac\Factory; use Interop\Container\ContainerInterface; +use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Guard\RouteGuard; use ZfcRbac\Guard\RoutePermissionsGuard; @@ -30,7 +31,7 @@ * @author JM Lerouxw * @license MIT */ -class RoutePermissionsGuardFactory +class RoutePermissionsGuardFactory implements FactoryInterface { /** * @var array @@ -41,20 +42,26 @@ class RoutePermissionsGuardFactory * {@inheritDoc} */ public function __construct(array $options = []) + { + $this->setCreationOptions($options); + } + + /** + * {@inheritDoc} + */ + public function setCreationOptions(array $options) { $this->options = $options; } /** * @param ContainerInterface $container + * @param string $requestedName + * @param array|null $options * @return RoutePermissionsGuard */ - public function __invoke(ContainerInterface $container) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { - if (! method_exists($container, 'build')) { // servicemanager v3 - $container = $container->getServiceLocator(); - } - /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ $moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions'); @@ -73,6 +80,6 @@ public function __invoke(ContainerInterface $container) */ public function createService(ServiceLocatorInterface $serviceLocator) { - return $this($serviceLocator, RoutePermissionsGuard::class); + return $this($serviceLocator->getServiceLocator(), RoutePermissionsGuard::class); } } diff --git a/tests/ZfcRbacTest/Factory/AssertionPluginManagerFactoryTest.php b/tests/ZfcRbacTest/Factory/AssertionPluginManagerFactoryTest.php index 62d22f66..4d83c21a 100644 --- a/tests/ZfcRbacTest/Factory/AssertionPluginManagerFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/AssertionPluginManagerFactoryTest.php @@ -39,5 +39,6 @@ public function testFactory() $pluginManager = $factory->createService($serviceManager); $this->assertInstanceOf('ZfcRbac\Assertion\AssertionPluginManager', $pluginManager); + $this->assertSame($serviceManager, $pluginManager->getServiceLocator()); } } diff --git a/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php b/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php index 34f72cbe..ca6661dd 100644 --- a/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php +++ b/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php @@ -91,11 +91,6 @@ public function testAuthorizationServiceIsInjectedWithDelegator() 'ZfcRbac\Factory\AuthorizationServiceDelegatorFactory' ); - $serviceManager->addDelegator( - 'ZfcRbacTest\Initializer\AuthorizationAwareFake', - 'ZfcRbac\Factory\AuthorizationServiceDelegatorFactory' - ); - $decoratedInstance = $serviceManager->get('ZfcRbacTest\AuthorizationAware'); $this->assertEquals($authorizationService, $decoratedInstance->getAuthorizationService()); } @@ -124,17 +119,10 @@ function () { 'ZfcRbac\Factory\AuthorizationServiceDelegatorFactory' ); - $thrown = false; - try { - $serviceManager->get('ZfcRbacTest\AuthorizationAware'); - } catch (\Exception $e) { - $thrown = true; - $this->assertStringEndsWith('The service ZfcRbacTest\AuthorizationAware must implement AuthorizationServiceAwareInterface.', $e->getMessage()); - if ($e->getPrevious()) { - $this->assertInstanceOf('ZfcRbac\Exception\RuntimeException', $e->getPrevious()); - } - } - - $this->assertTrue($thrown); + $this->setExpectedException( + 'ZfcRbac\Exception\RuntimeException', + 'The service ZfcRbacTest\AuthorizationAware must implement AuthorizationServiceAwareInterface.' + ); + $serviceManager->get('ZfcRbacTest\AuthorizationAware'); } } diff --git a/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php index ef84f222..850c0001 100644 --- a/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php @@ -50,17 +50,13 @@ public function testFactory() $this->getMock('ZfcRbac\Service\RoleService', [], [], '', false) ); - $pluginManager = new GuardPluginManager($serviceManager); + $pluginManager = new GuardPluginManager(); + $pluginManager->setServiceLocator($serviceManager); $factory = new ControllerGuardFactory(); + $controllerGuard = $factory->createService($pluginManager); - if (method_exists($serviceManager, 'build')) { // servicemanager v3 - $guard = $factory->createService($serviceManager); - } else { - $guard = $factory->createService($pluginManager); - } - - $this->assertInstanceOf('ZfcRbac\Guard\ControllerGuard', $guard); - $this->assertEquals(GuardInterface::POLICY_ALLOW, $guard->getProtectionPolicy()); + $this->assertInstanceOf('ZfcRbac\Guard\ControllerGuard', $controllerGuard); + $this->assertEquals(GuardInterface::POLICY_ALLOW, $controllerGuard->getProtectionPolicy()); } } diff --git a/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php index 9d1ddffa..04674105 100644 --- a/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php @@ -20,7 +20,6 @@ use Zend\ServiceManager\ServiceManager; use ZfcRbac\Factory\ControllerPermissionsGuardFactory; -use ZfcRbac\Guard\ControllerPermissionsGuard; use ZfcRbac\Guard\GuardInterface; use ZfcRbac\Guard\GuardPluginManager; use ZfcRbac\Options\ModuleOptions; @@ -51,15 +50,11 @@ public function testFactory() $this->getMock('ZfcRbac\Service\AuthorizationService', [], [], '', false) ); - $pluginManager = new GuardPluginManager($serviceManager); + $pluginManager = new GuardPluginManager(); + $pluginManager->setServiceLocator($serviceManager); $factory = new ControllerPermissionsGuardFactory(); - - if (method_exists($serviceManager, 'build')) { // servicemanager v3 - $guard = $factory($serviceManager); - } else { - $guard = $factory($pluginManager); - } + $guard = $factory->createService($pluginManager); $this->assertInstanceOf('ZfcRbac\Guard\ControllerPermissionsGuard', $guard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $guard->getProtectionPolicy()); diff --git a/tests/ZfcRbacTest/Factory/GuardPluginManagerFactoryTest.php b/tests/ZfcRbacTest/Factory/GuardPluginManagerFactoryTest.php index 1ef9f2a0..1f7b2c28 100644 --- a/tests/ZfcRbacTest/Factory/GuardPluginManagerFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/GuardPluginManagerFactoryTest.php @@ -39,5 +39,6 @@ public function testFactory() $pluginManager = $factory->createService($serviceManager); $this->assertInstanceOf('ZfcRbac\Guard\GuardPluginManager', $pluginManager); + $this->assertSame($serviceManager, $pluginManager->getServiceLocator()); } } diff --git a/tests/ZfcRbacTest/Factory/GuardsFactoryTest.php b/tests/ZfcRbacTest/Factory/GuardsFactoryTest.php index 494526b4..047f2c5f 100644 --- a/tests/ZfcRbacTest/Factory/GuardsFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/GuardsFactoryTest.php @@ -28,6 +28,9 @@ */ class GuardsFactoryTest extends \PHPUnit_Framework_TestCase { + /** + * @group by + */ public function testFactory() { $moduleOptions = new ModuleOptions([ diff --git a/tests/ZfcRbacTest/Factory/HasRoleViewHelperFactoryTest.php b/tests/ZfcRbacTest/Factory/HasRoleViewHelperFactoryTest.php index 2b61d271..98a8e86e 100644 --- a/tests/ZfcRbacTest/Factory/HasRoleViewHelperFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/HasRoleViewHelperFactoryTest.php @@ -39,7 +39,7 @@ public function testFactory() ); $factory = new HasRoleViewHelperFactory(); - $viewHelper = $factory->createService($serviceManager); + $viewHelper = $factory->createService($pluginManager); $this->assertInstanceOf('ZfcRbac\View\Helper\HasRole', $viewHelper); } diff --git a/tests/ZfcRbacTest/Factory/IsGrantedPluginFactoryTest.php b/tests/ZfcRbacTest/Factory/IsGrantedPluginFactoryTest.php index 503398d8..9095c91b 100644 --- a/tests/ZfcRbacTest/Factory/IsGrantedPluginFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/IsGrantedPluginFactoryTest.php @@ -31,7 +31,8 @@ public function testFactory() { $serviceManager = new ServiceManager(); - $pluginManager = new PluginManager($serviceManager); + $pluginManager = new PluginManager(); + $pluginManager->setServiceLocator($serviceManager); $serviceManager->setService( 'ZfcRbac\Service\AuthorizationService', @@ -39,7 +40,7 @@ public function testFactory() ); $factory = new IsGrantedPluginFactory(); - $isGranted = $factory->createService($serviceManager); + $isGranted = $factory->createService($pluginManager); $this->assertInstanceOf('ZfcRbac\Mvc\Controller\Plugin\IsGranted', $isGranted); } diff --git a/tests/ZfcRbacTest/Factory/IsGrantedViewHelperFactoryTest.php b/tests/ZfcRbacTest/Factory/IsGrantedViewHelperFactoryTest.php index 89ee57a5..b19df9f6 100644 --- a/tests/ZfcRbacTest/Factory/IsGrantedViewHelperFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/IsGrantedViewHelperFactoryTest.php @@ -39,7 +39,7 @@ public function testFactory() ); $factory = new IsGrantedViewHelperFactory(); - $isGranted = $factory->createService($serviceManager); + $isGranted = $factory->createService($pluginManager); $this->assertInstanceOf('ZfcRbac\View\Helper\IsGranted', $isGranted); } diff --git a/tests/ZfcRbacTest/Factory/ObjectRepositoryRoleProviderFactoryTest.php b/tests/ZfcRbacTest/Factory/ObjectRepositoryRoleProviderFactoryTest.php index 6888e967..bd328e02 100644 --- a/tests/ZfcRbacTest/Factory/ObjectRepositoryRoleProviderFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/ObjectRepositoryRoleProviderFactoryTest.php @@ -30,8 +30,10 @@ class ObjectRepositoryRoleProviderFactoryTest extends \PHPUnit_Framework_TestCas { public function testFactoryUsingObjectRepository() { + $pluginManager = new RoleProviderPluginManager(); $serviceManager = new ServiceManager(); - $pluginManager = new RoleProviderPluginManager($serviceManager); + + $pluginManager->setServiceLocator($serviceManager); $options = [ 'role_name_property' => 'name', @@ -46,8 +48,10 @@ public function testFactoryUsingObjectRepository() public function testFactoryUsingObjectManager() { + $pluginManager = new RoleProviderPluginManager(); $serviceManager = new ServiceManager(); - $pluginManager = new RoleProviderPluginManager($serviceManager); + + $pluginManager->setServiceLocator($serviceManager); $options = [ 'role_name_property' => 'name', @@ -73,9 +77,10 @@ public function testFactoryUsingObjectManager() public function testThrowExceptionIfNoRoleNamePropertyIsSet() { try { + $pluginManager = new RoleProviderPluginManager(); $serviceManager = new ServiceManager(); - $pluginManager = new RoleProviderPluginManager($serviceManager); + $pluginManager->setServiceLocator($serviceManager); $pluginManager->get('ZfcRbac\Role\ObjectRepositoryRoleProvider', []); } catch (ServiceNotCreatedException $smException) { while ($e = $smException->getPrevious()) { @@ -97,9 +102,10 @@ public function testThrowExceptionIfNoRoleNamePropertyIsSet() public function testThrowExceptionIfNoObjectManagerNorObjectRepositoryIsSet() { try { + $pluginManager = new RoleProviderPluginManager(); $serviceManager = new ServiceManager(); - $pluginManager = new RoleProviderPluginManager($serviceManager); + $pluginManager->setServiceLocator($serviceManager); $pluginManager->get('ZfcRbac\Role\ObjectRepositoryRoleProvider', [ 'role_name_property' => 'name' ]); diff --git a/tests/ZfcRbacTest/Factory/RoleProviderPluginManagerFactoryTest.php b/tests/ZfcRbacTest/Factory/RoleProviderPluginManagerFactoryTest.php index d99f6f6d..13c490e5 100644 --- a/tests/ZfcRbacTest/Factory/RoleProviderPluginManagerFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/RoleProviderPluginManagerFactoryTest.php @@ -39,5 +39,6 @@ public function testFactory() $pluginManager = $factory->createService($serviceManager); $this->assertInstanceOf('ZfcRbac\Role\RoleProviderPluginManager', $pluginManager); + $this->assertSame($serviceManager, $pluginManager->getServiceLocator()); } } diff --git a/tests/ZfcRbacTest/Factory/RoleServiceFactoryTest.php b/tests/ZfcRbacTest/Factory/RoleServiceFactoryTest.php index f420242d..63eafca8 100644 --- a/tests/ZfcRbacTest/Factory/RoleServiceFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/RoleServiceFactoryTest.php @@ -42,7 +42,7 @@ public function testFactory() $serviceManager = new ServiceManager(); $serviceManager->setService('ZfcRbac\Options\ModuleOptions', $options); - $serviceManager->setService('ZfcRbac\Role\RoleProviderPluginManager', new RoleProviderPluginManager($serviceManager)); + $serviceManager->setService('ZfcRbac\Role\RoleProviderPluginManager', new RoleProviderPluginManager()); $serviceManager->setService( 'ZfcRbac\Identity\AuthenticationProvider', $this->getMock('ZfcRbac\Identity\IdentityProviderInterface') diff --git a/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php index bc7e961e..8063ab49 100644 --- a/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php @@ -50,15 +50,11 @@ public function testFactory() $this->getMock('ZfcRbac\Service\RoleService', [], [], '', false) ); - $pluginManager = new GuardPluginManager($serviceManager); + $pluginManager = new GuardPluginManager(); + $pluginManager->setServiceLocator($serviceManager); $factory = new RouteGuardFactory(); - - if (method_exists($serviceManager, 'build')) { // servicemanager v3 - $routeGuard = $factory->createService($serviceManager); - } else { - $routeGuard = $factory->createService($pluginManager); - } + $routeGuard = $factory->createService($pluginManager); $this->assertInstanceOf('ZfcRbac\Guard\RouteGuard', $routeGuard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $routeGuard->getProtectionPolicy()); diff --git a/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php index 74ac6ca6..6aee9e56 100644 --- a/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php @@ -50,16 +50,11 @@ public function testFactory() $this->getMock('ZfcRbac\Service\AuthorizationService', [], [], '', false) ); - $pluginManager = new GuardPluginManager($serviceManager); + $pluginManager = new GuardPluginManager(); + $pluginManager->setServiceLocator($serviceManager); $factory = new RoutePermissionsGuardFactory(); - - if (method_exists($serviceManager, 'build')) { // servicemanager v3 - $routeGuard = $factory->createService($serviceManager); - } else { - $routeGuard = $factory->createService($pluginManager); - } - + $routeGuard = $factory->createService($pluginManager); $this->assertInstanceOf('ZfcRbac\Guard\RoutePermissionsGuard', $routeGuard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $routeGuard->getProtectionPolicy()); diff --git a/tests/ZfcRbacTest/Guard/GuardPluginManagerTest.php b/tests/ZfcRbacTest/Guard/GuardPluginManagerTest.php index 91b9369c..ff43d263 100644 --- a/tests/ZfcRbacTest/Guard/GuardPluginManagerTest.php +++ b/tests/ZfcRbacTest/Guard/GuardPluginManagerTest.php @@ -18,7 +18,6 @@ namespace ZfcRbacTest\Guard; -use Interop\Container\ContainerInterface; use Zend\ServiceManager\ServiceManager; use ZfcRbac\Guard\GuardPluginManager; use ZfcRbac\Options\ModuleOptions; @@ -68,7 +67,6 @@ public function guardProvider() /** * @dataProvider guardProvider - * @group by */ public function testCanCreateDefaultGuards($type, $options) { @@ -83,7 +81,8 @@ public function testCanCreateDefaultGuards($type, $options) $this->getMock('ZfcRbac\Service\AuthorizationService', [], [], '', false) ); - $pluginManager = new GuardPluginManager($serviceManager); + $pluginManager = new GuardPluginManager(); + $pluginManager->setServiceLocator($serviceManager); $guard = $pluginManager->get($type, $options); @@ -94,7 +93,7 @@ public function testThrowExceptionForInvalidPlugin() { $this->setExpectedException('ZfcRbac\Exception\RuntimeException'); - $pluginManager = new GuardPluginManager($this->getMock(ContainerInterface::class)); + $pluginManager = new GuardPluginManager(); $pluginManager->get('stdClass'); } } diff --git a/tests/ZfcRbacTest/Initializer/AuthorizationServiceInitializerTest.php b/tests/ZfcRbacTest/Initializer/AuthorizationServiceInitializerTest.php index c0fdfbcb..0cb1b3bc 100644 --- a/tests/ZfcRbacTest/Initializer/AuthorizationServiceInitializerTest.php +++ b/tests/ZfcRbacTest/Initializer/AuthorizationServiceInitializerTest.php @@ -31,7 +31,7 @@ public function testInitializer() $authServiceClassName = 'ZfcRbac\Service\AuthorizationService'; $initializer = new AuthorizationServiceInitializer(); $instance = new AuthorizationAwareFake(); - $serviceLocator = $this->getMock('Zend\ServiceManager\ServiceManager'); + $serviceLocator = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface'); $authorizationService = $this->getMock('ZfcRbac\Service\AuthorizationService', [], [], '', false); $serviceLocator->expects($this->once()) diff --git a/tests/ZfcRbacTest/Role/RoleProviderPluginManagerTest.php b/tests/ZfcRbacTest/Role/RoleProviderPluginManagerTest.php index 32452c27..8667acb0 100644 --- a/tests/ZfcRbacTest/Role/RoleProviderPluginManagerTest.php +++ b/tests/ZfcRbacTest/Role/RoleProviderPluginManagerTest.php @@ -18,7 +18,6 @@ namespace ZfcRbacTest\Role; -use Interop\Container\ContainerInterface; use ZfcRbac\Role\RoleProviderPluginManager; /** @@ -29,16 +28,16 @@ class RoleProviderPluginManagerTest extends \PHPUnit_Framework_TestCase public function testValidationOfPluginSucceedsIfRoleProviderInterfaceIsImplemented() { $pluginMock = $this->getMock('ZfcRbac\Role\RoleProviderInterface'); - $pluginManager = new RoleProviderPluginManager($this->getMock(ContainerInterface::class)); + $pluginManager = new RoleProviderPluginManager(); - $this->assertNull($pluginManager->validate($pluginMock)); + $this->assertNull($pluginManager->validatePlugin($pluginMock)); } public function testValidationOfPluginFailsIfRoleProviderInterfaceIsNotImplemented() { $this->setExpectedException('ZfcRbac\Exception\RuntimeException'); - $pluginManager = new RoleProviderPluginManager($this->getMock(ContainerInterface::class)); + $pluginManager = new RoleProviderPluginManager(); $pluginManager->get('stdClass', []); } } diff --git a/tests/ZfcRbacTest/Service/AuthorizationServiceTest.php b/tests/ZfcRbacTest/Service/AuthorizationServiceTest.php index 73e3fb63..1685dc01 100644 --- a/tests/ZfcRbacTest/Service/AuthorizationServiceTest.php +++ b/tests/ZfcRbacTest/Service/AuthorizationServiceTest.php @@ -18,7 +18,6 @@ namespace ZfcRbacTest\Service; -use Interop\Container\ContainerInterface; use Rbac\Rbac; use Rbac\Traversal\Strategy\RecursiveRoleIteratorStrategy; use ZfcRbac\Role\InMemoryRoleProvider; @@ -139,7 +138,7 @@ public function testGranted($role, $permission, $context, $isGranted, $assertion new InMemoryRoleProvider($roleConfig), $rbac->getTraversalStrategy() ); - $assertionPluginManager = new AssertionPluginManager($this->getMock(ContainerInterface::class), $assertionPluginConfig); + $assertionPluginManager = new AssertionPluginManager(new Config($assertionPluginConfig)); $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager); $authorizationService->setAssertions($assertions); diff --git a/tests/ZfcRbacTest/Util/ServiceManagerFactory.php b/tests/ZfcRbacTest/Util/ServiceManagerFactory.php index 4bd49fac..278cad39 100644 --- a/tests/ZfcRbacTest/Util/ServiceManagerFactory.php +++ b/tests/ZfcRbacTest/Util/ServiceManagerFactory.php @@ -59,12 +59,12 @@ public static function getApplicationConfig() public static function getServiceManager(array $config = null) { $config = $config ?: static::getApplicationConfig(); - $serviceManager = new ServiceManager(); - $serviceManager->setService('ApplicationConfig', $config); - $serviceManagerConfig = new ServiceManagerConfig( - isset($config['service_manager']) ? $config['service_manager'] : [] + $serviceManager = new ServiceManager( + new ServiceManagerConfig( + isset($config['service_manager']) ? $config['service_manager'] : [] + ) ); - $serviceManagerConfig->configureServiceManager($serviceManager); + $serviceManager->setService('ApplicationConfig', $config); $serviceManager->setAllowOverride(true); /* @var $moduleManager \Zend\ModuleManager\ModuleManagerInterface */ From 2b1ecb05dc89c233e41b911b0ad4084a0bc3a680 Mon Sep 17 00:00:00 2001 From: prolic Date: Thu, 14 Jul 2016 22:47:15 +0800 Subject: [PATCH 17/29] run all tests with zf3 --- composer.json | 2 +- .../Factory/ControllerGuardFactory.php | 10 +++++++-- .../ControllerPermissionsGuardFactory.php | 10 ++++++--- .../Factory/HasRoleViewHelperFactory.php | 2 +- .../Factory/IsGrantedViewHelperFactory.php | 2 +- .../RoleProviderPluginManagerFactory.php | 1 - src/ZfcRbac/Factory/RouteGuardFactory.php | 4 ++++ .../AssertionPluginManagerFactoryTest.php | 1 - .../AuthorizationServiceDelegatorTest.php | 22 ++++++++++++++----- .../Factory/ControllerGuardFactoryTest.php | 6 ++--- .../ControllerPermissionsGuardFactoryTest.php | 6 ++--- .../Factory/GuardPluginManagerFactoryTest.php | 8 +++---- .../Factory/HasRoleViewHelperFactoryTest.php | 11 ++++++++-- .../Factory/IsGrantedPluginFactoryTest.php | 14 ++++++++---- .../IsGrantedViewHelperFactoryTest.php | 12 ++++++++-- ...bjectRepositoryRoleProviderFactoryTest.php | 14 ++++-------- .../RoleProviderPluginManagerFactoryTest.php | 6 ++--- .../Factory/RoleServiceFactoryTest.php | 2 +- .../Factory/RouteGuardFactoryTest.php | 6 ++--- .../RoutePermissionsGuardFactoryTest.php | 6 ++--- .../Guard/GuardPluginManagerTest.php | 5 ++--- .../Role/RoleProviderPluginManagerTest.php | 5 +++-- .../Service/AuthorizationServiceTest.php | 3 ++- .../Util/ServiceManagerFactory.php | 8 +++---- 24 files changed, 99 insertions(+), 67 deletions(-) diff --git a/composer.json b/composer.json index 792c056a..2f400942 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "zendframework/zend-config": "~2.2", "zendframework/zend-eventmanager": "^2.6.3 || ^3.0", "zendframework/zend-mvc": "~2.7.4 || ^3.0", - "zendframework/zend-servicemanager": "^2.7.6 || ^3.0", + "zendframework/zend-servicemanager": "^2.7.5 || ^3.0.3", "zfr/rbac": "~1.2" }, "require-dev": { diff --git a/src/ZfcRbac/Factory/ControllerGuardFactory.php b/src/ZfcRbac/Factory/ControllerGuardFactory.php index 74630fa7..20d9da51 100644 --- a/src/ZfcRbac/Factory/ControllerGuardFactory.php +++ b/src/ZfcRbac/Factory/ControllerGuardFactory.php @@ -53,11 +53,17 @@ public function setCreationOptions(array $options) } /** - * {@inheritDoc} + * @param ContainerInterface $container + * @param string $requestedName + * @param array|null $options * @return ControllerGuard */ public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { + if (null === $options) { + $options = []; + } + /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ $moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions'); @@ -77,6 +83,6 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o */ public function createService(ServiceLocatorInterface $serviceLocator) { - return $this($serviceLocator->getServiceLocator(), ControllerGuard::class, $this->options); + return $this($serviceLocator, ControllerGuard::class, $this->options); } } diff --git a/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php b/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php index 3cc37cb4..ba37975d 100644 --- a/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php +++ b/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php @@ -54,10 +54,16 @@ public function setCreationOptions(array $options) /** * @param ContainerInterface $container + * @param string $resolvedName + * @param array|null $options * @return ControllerPermissionsGuard */ public function __invoke(ContainerInterface $container, $resolvedName, array $options = null) { + if (null === $options) { + $options = []; + } + /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ $moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions'); @@ -76,8 +82,6 @@ public function __invoke(ContainerInterface $container, $resolvedName, array $op */ public function createService(ServiceLocatorInterface $serviceLocator) { - $parentLocator = $serviceLocator->getServiceLocator(); - - return $this($parentLocator, ControllerPermissionsGuard::class, $this->options); + return $this($serviceLocator, ControllerPermissionsGuard::class, $this->options); } } diff --git a/src/ZfcRbac/Factory/HasRoleViewHelperFactory.php b/src/ZfcRbac/Factory/HasRoleViewHelperFactory.php index b2556a83..0978f9d2 100644 --- a/src/ZfcRbac/Factory/HasRoleViewHelperFactory.php +++ b/src/ZfcRbac/Factory/HasRoleViewHelperFactory.php @@ -55,6 +55,6 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o */ public function createService(ServiceLocatorInterface $serviceLocator) { - return $this($serviceLocator->getServiceLocator(), HasRole::class); + return $this($serviceLocator, HasRole::class); } } diff --git a/src/ZfcRbac/Factory/IsGrantedViewHelperFactory.php b/src/ZfcRbac/Factory/IsGrantedViewHelperFactory.php index c5301142..bc0db739 100644 --- a/src/ZfcRbac/Factory/IsGrantedViewHelperFactory.php +++ b/src/ZfcRbac/Factory/IsGrantedViewHelperFactory.php @@ -54,6 +54,6 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o */ public function createService(ServiceLocatorInterface $serviceLocator) { - return $this($serviceLocator->getServiceLocator(), IsGranted::class); + return $this($serviceLocator, IsGranted::class); } } diff --git a/src/ZfcRbac/Factory/RoleProviderPluginManagerFactory.php b/src/ZfcRbac/Factory/RoleProviderPluginManagerFactory.php index ff1f916a..32580f0c 100644 --- a/src/ZfcRbac/Factory/RoleProviderPluginManagerFactory.php +++ b/src/ZfcRbac/Factory/RoleProviderPluginManagerFactory.php @@ -19,7 +19,6 @@ namespace ZfcRbac\Factory; use Interop\Container\ContainerInterface; -use Zend\ServiceManager\Config; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Role\RoleProviderPluginManager; diff --git a/src/ZfcRbac/Factory/RouteGuardFactory.php b/src/ZfcRbac/Factory/RouteGuardFactory.php index 05a45cf0..d4c4cb5b 100644 --- a/src/ZfcRbac/Factory/RouteGuardFactory.php +++ b/src/ZfcRbac/Factory/RouteGuardFactory.php @@ -60,6 +60,10 @@ public function setCreationOptions(array $options) */ public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { + if (null === $options) { + $options = []; + } + /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ $moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions'); diff --git a/tests/ZfcRbacTest/Factory/AssertionPluginManagerFactoryTest.php b/tests/ZfcRbacTest/Factory/AssertionPluginManagerFactoryTest.php index 4d83c21a..62d22f66 100644 --- a/tests/ZfcRbacTest/Factory/AssertionPluginManagerFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/AssertionPluginManagerFactoryTest.php @@ -39,6 +39,5 @@ public function testFactory() $pluginManager = $factory->createService($serviceManager); $this->assertInstanceOf('ZfcRbac\Assertion\AssertionPluginManager', $pluginManager); - $this->assertSame($serviceManager, $pluginManager->getServiceLocator()); } } diff --git a/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php b/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php index ca6661dd..34f72cbe 100644 --- a/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php +++ b/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php @@ -91,6 +91,11 @@ public function testAuthorizationServiceIsInjectedWithDelegator() 'ZfcRbac\Factory\AuthorizationServiceDelegatorFactory' ); + $serviceManager->addDelegator( + 'ZfcRbacTest\Initializer\AuthorizationAwareFake', + 'ZfcRbac\Factory\AuthorizationServiceDelegatorFactory' + ); + $decoratedInstance = $serviceManager->get('ZfcRbacTest\AuthorizationAware'); $this->assertEquals($authorizationService, $decoratedInstance->getAuthorizationService()); } @@ -119,10 +124,17 @@ function () { 'ZfcRbac\Factory\AuthorizationServiceDelegatorFactory' ); - $this->setExpectedException( - 'ZfcRbac\Exception\RuntimeException', - 'The service ZfcRbacTest\AuthorizationAware must implement AuthorizationServiceAwareInterface.' - ); - $serviceManager->get('ZfcRbacTest\AuthorizationAware'); + $thrown = false; + try { + $serviceManager->get('ZfcRbacTest\AuthorizationAware'); + } catch (\Exception $e) { + $thrown = true; + $this->assertStringEndsWith('The service ZfcRbacTest\AuthorizationAware must implement AuthorizationServiceAwareInterface.', $e->getMessage()); + if ($e->getPrevious()) { + $this->assertInstanceOf('ZfcRbac\Exception\RuntimeException', $e->getPrevious()); + } + } + + $this->assertTrue($thrown); } } diff --git a/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php index 850c0001..152ef70f 100644 --- a/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php @@ -50,11 +50,9 @@ public function testFactory() $this->getMock('ZfcRbac\Service\RoleService', [], [], '', false) ); - $pluginManager = new GuardPluginManager(); - $pluginManager->setServiceLocator($serviceManager); + $pluginManager = new GuardPluginManager($serviceManager); - $factory = new ControllerGuardFactory(); - $controllerGuard = $factory->createService($pluginManager); + $controllerGuard = $pluginManager->get('ZfcRbac\Guard\ControllerGuard'); $this->assertInstanceOf('ZfcRbac\Guard\ControllerGuard', $controllerGuard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $controllerGuard->getProtectionPolicy()); diff --git a/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php index 04674105..d73fbf87 100644 --- a/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php @@ -50,11 +50,9 @@ public function testFactory() $this->getMock('ZfcRbac\Service\AuthorizationService', [], [], '', false) ); - $pluginManager = new GuardPluginManager(); - $pluginManager->setServiceLocator($serviceManager); + $pluginManager = new GuardPluginManager($serviceManager); - $factory = new ControllerPermissionsGuardFactory(); - $guard = $factory->createService($pluginManager); + $guard = $pluginManager->get('ZfcRbac\Guard\ControllerPermissionsGuard'); $this->assertInstanceOf('ZfcRbac\Guard\ControllerPermissionsGuard', $guard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $guard->getProtectionPolicy()); diff --git a/tests/ZfcRbacTest/Factory/GuardPluginManagerFactoryTest.php b/tests/ZfcRbacTest/Factory/GuardPluginManagerFactoryTest.php index 1f7b2c28..226da6a1 100644 --- a/tests/ZfcRbacTest/Factory/GuardPluginManagerFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/GuardPluginManagerFactoryTest.php @@ -20,6 +20,7 @@ use Zend\ServiceManager\ServiceManager; use ZfcRbac\Factory\GuardPluginManagerFactory; +use ZfcRbac\Guard\GuardPluginManager; /** * @covers \ZfcRbac\Factory\GuardPluginManagerFactory @@ -34,11 +35,10 @@ public function testFactory() 'guard_manager' => [] ] ]); + $serviceManager->setFactory(GuardPluginManager::class, GuardPluginManagerFactory::class); - $factory = new GuardPluginManagerFactory(); - $pluginManager = $factory->createService($serviceManager); + $pluginManager = $serviceManager->get(GuardPluginManager::class); - $this->assertInstanceOf('ZfcRbac\Guard\GuardPluginManager', $pluginManager); - $this->assertSame($serviceManager, $pluginManager->getServiceLocator()); + $this->assertInstanceOf(GuardPluginManager::class, $pluginManager); } } diff --git a/tests/ZfcRbacTest/Factory/HasRoleViewHelperFactoryTest.php b/tests/ZfcRbacTest/Factory/HasRoleViewHelperFactoryTest.php index 98a8e86e..4ed9535a 100644 --- a/tests/ZfcRbacTest/Factory/HasRoleViewHelperFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/HasRoleViewHelperFactoryTest.php @@ -32,14 +32,21 @@ public function testFactory() $serviceManager = new ServiceManager(); $pluginManager = new HelperPluginManager($serviceManager); + $pluginManager->configure([ + 'factories' => [ + 'ZfcRbac\View\Helper\HasRole' => 'ZfcRbac\Factory\HasRoleViewHelperFactory' + ], + 'aliases' => [ + 'hasRole' => 'ZfcRbac\View\Helper\HasRole' + ] + ]); $serviceManager->setService( 'ZfcRbac\Service\RoleService', $this->getMock('ZfcRbac\Service\RoleService', [], [], '', false) ); - $factory = new HasRoleViewHelperFactory(); - $viewHelper = $factory->createService($pluginManager); + $viewHelper = $pluginManager->get('hasRole'); $this->assertInstanceOf('ZfcRbac\View\Helper\HasRole', $viewHelper); } diff --git a/tests/ZfcRbacTest/Factory/IsGrantedPluginFactoryTest.php b/tests/ZfcRbacTest/Factory/IsGrantedPluginFactoryTest.php index 9095c91b..b26d5dd2 100644 --- a/tests/ZfcRbacTest/Factory/IsGrantedPluginFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/IsGrantedPluginFactoryTest.php @@ -31,16 +31,22 @@ public function testFactory() { $serviceManager = new ServiceManager(); - $pluginManager = new PluginManager(); - $pluginManager->setServiceLocator($serviceManager); + $pluginManager = new PluginManager($serviceManager); + $pluginManager->configure([ + 'factories' => [ + 'ZfcRbac\Mvc\Controller\Plugin\IsGranted' => 'ZfcRbac\Factory\IsGrantedPluginFactory' + ], + 'aliases' => [ + 'isGranted' => 'ZfcRbac\Mvc\Controller\Plugin\IsGranted' + ] + ]); $serviceManager->setService( 'ZfcRbac\Service\AuthorizationService', $this->getMock('ZfcRbac\Service\AuthorizationServiceInterface') ); - $factory = new IsGrantedPluginFactory(); - $isGranted = $factory->createService($pluginManager); + $isGranted = $pluginManager->get('isGranted'); $this->assertInstanceOf('ZfcRbac\Mvc\Controller\Plugin\IsGranted', $isGranted); } diff --git a/tests/ZfcRbacTest/Factory/IsGrantedViewHelperFactoryTest.php b/tests/ZfcRbacTest/Factory/IsGrantedViewHelperFactoryTest.php index b19df9f6..af2a66af 100644 --- a/tests/ZfcRbacTest/Factory/IsGrantedViewHelperFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/IsGrantedViewHelperFactoryTest.php @@ -32,14 +32,22 @@ public function testFactory() $serviceManager = new ServiceManager(); $pluginManager = new HelperPluginManager($serviceManager); + $pluginManager->configure([ + 'factories' => [ + 'ZfcRbac\View\Helper\IsGranted' => 'ZfcRbac\Factory\IsGrantedViewHelperFactory', + ], + 'aliases' => [ + 'isGranted' => 'ZfcRbac\View\Helper\IsGranted', + ] + ]); $serviceManager->setService( 'ZfcRbac\Service\AuthorizationService', $this->getMock('ZfcRbac\Service\AuthorizationServiceInterface') ); - $factory = new IsGrantedViewHelperFactory(); - $isGranted = $factory->createService($pluginManager); + + $isGranted = $pluginManager->get('isGranted'); $this->assertInstanceOf('ZfcRbac\View\Helper\IsGranted', $isGranted); } diff --git a/tests/ZfcRbacTest/Factory/ObjectRepositoryRoleProviderFactoryTest.php b/tests/ZfcRbacTest/Factory/ObjectRepositoryRoleProviderFactoryTest.php index bd328e02..6888e967 100644 --- a/tests/ZfcRbacTest/Factory/ObjectRepositoryRoleProviderFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/ObjectRepositoryRoleProviderFactoryTest.php @@ -30,10 +30,8 @@ class ObjectRepositoryRoleProviderFactoryTest extends \PHPUnit_Framework_TestCas { public function testFactoryUsingObjectRepository() { - $pluginManager = new RoleProviderPluginManager(); $serviceManager = new ServiceManager(); - - $pluginManager->setServiceLocator($serviceManager); + $pluginManager = new RoleProviderPluginManager($serviceManager); $options = [ 'role_name_property' => 'name', @@ -48,10 +46,8 @@ public function testFactoryUsingObjectRepository() public function testFactoryUsingObjectManager() { - $pluginManager = new RoleProviderPluginManager(); $serviceManager = new ServiceManager(); - - $pluginManager->setServiceLocator($serviceManager); + $pluginManager = new RoleProviderPluginManager($serviceManager); $options = [ 'role_name_property' => 'name', @@ -77,10 +73,9 @@ public function testFactoryUsingObjectManager() public function testThrowExceptionIfNoRoleNamePropertyIsSet() { try { - $pluginManager = new RoleProviderPluginManager(); $serviceManager = new ServiceManager(); + $pluginManager = new RoleProviderPluginManager($serviceManager); - $pluginManager->setServiceLocator($serviceManager); $pluginManager->get('ZfcRbac\Role\ObjectRepositoryRoleProvider', []); } catch (ServiceNotCreatedException $smException) { while ($e = $smException->getPrevious()) { @@ -102,10 +97,9 @@ public function testThrowExceptionIfNoRoleNamePropertyIsSet() public function testThrowExceptionIfNoObjectManagerNorObjectRepositoryIsSet() { try { - $pluginManager = new RoleProviderPluginManager(); $serviceManager = new ServiceManager(); + $pluginManager = new RoleProviderPluginManager($serviceManager); - $pluginManager->setServiceLocator($serviceManager); $pluginManager->get('ZfcRbac\Role\ObjectRepositoryRoleProvider', [ 'role_name_property' => 'name' ]); diff --git a/tests/ZfcRbacTest/Factory/RoleProviderPluginManagerFactoryTest.php b/tests/ZfcRbacTest/Factory/RoleProviderPluginManagerFactoryTest.php index 13c490e5..b33ff1ad 100644 --- a/tests/ZfcRbacTest/Factory/RoleProviderPluginManagerFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/RoleProviderPluginManagerFactoryTest.php @@ -20,6 +20,7 @@ use Zend\ServiceManager\ServiceManager; use ZfcRbac\Factory\RoleProviderPluginManagerFactory; +use ZfcRbac\Role\RoleProviderPluginManager; /** * @covers \ZfcRbac\Factory\RoleProviderPluginManagerFactory @@ -34,11 +35,10 @@ public function testFactory() 'role_provider_manager' => [] ] ]); + $serviceManager->setFactory(RoleProviderPluginManager::class, RoleProviderPluginManagerFactory::class); - $factory = new RoleProviderPluginManagerFactory(); - $pluginManager = $factory->createService($serviceManager); + $pluginManager = $serviceManager->get(RoleProviderPluginManager::class); $this->assertInstanceOf('ZfcRbac\Role\RoleProviderPluginManager', $pluginManager); - $this->assertSame($serviceManager, $pluginManager->getServiceLocator()); } } diff --git a/tests/ZfcRbacTest/Factory/RoleServiceFactoryTest.php b/tests/ZfcRbacTest/Factory/RoleServiceFactoryTest.php index 63eafca8..f420242d 100644 --- a/tests/ZfcRbacTest/Factory/RoleServiceFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/RoleServiceFactoryTest.php @@ -42,7 +42,7 @@ public function testFactory() $serviceManager = new ServiceManager(); $serviceManager->setService('ZfcRbac\Options\ModuleOptions', $options); - $serviceManager->setService('ZfcRbac\Role\RoleProviderPluginManager', new RoleProviderPluginManager()); + $serviceManager->setService('ZfcRbac\Role\RoleProviderPluginManager', new RoleProviderPluginManager($serviceManager)); $serviceManager->setService( 'ZfcRbac\Identity\AuthenticationProvider', $this->getMock('ZfcRbac\Identity\IdentityProviderInterface') diff --git a/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php index 8063ab49..e4cd0284 100644 --- a/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php @@ -50,11 +50,9 @@ public function testFactory() $this->getMock('ZfcRbac\Service\RoleService', [], [], '', false) ); - $pluginManager = new GuardPluginManager(); - $pluginManager->setServiceLocator($serviceManager); + $pluginManager = new GuardPluginManager($serviceManager); - $factory = new RouteGuardFactory(); - $routeGuard = $factory->createService($pluginManager); + $routeGuard = $pluginManager->get('ZfcRbac\Guard\RouteGuard'); $this->assertInstanceOf('ZfcRbac\Guard\RouteGuard', $routeGuard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $routeGuard->getProtectionPolicy()); diff --git a/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php index 6aee9e56..9ad73d47 100644 --- a/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php @@ -50,11 +50,9 @@ public function testFactory() $this->getMock('ZfcRbac\Service\AuthorizationService', [], [], '', false) ); - $pluginManager = new GuardPluginManager(); - $pluginManager->setServiceLocator($serviceManager); + $pluginManager = new GuardPluginManager($serviceManager); - $factory = new RoutePermissionsGuardFactory(); - $routeGuard = $factory->createService($pluginManager); + $routeGuard = $pluginManager->get('ZfcRbac\Guard\RoutePermissionsGuard'); $this->assertInstanceOf('ZfcRbac\Guard\RoutePermissionsGuard', $routeGuard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $routeGuard->getProtectionPolicy()); diff --git a/tests/ZfcRbacTest/Guard/GuardPluginManagerTest.php b/tests/ZfcRbacTest/Guard/GuardPluginManagerTest.php index ff43d263..3fa3c285 100644 --- a/tests/ZfcRbacTest/Guard/GuardPluginManagerTest.php +++ b/tests/ZfcRbacTest/Guard/GuardPluginManagerTest.php @@ -81,8 +81,7 @@ public function testCanCreateDefaultGuards($type, $options) $this->getMock('ZfcRbac\Service\AuthorizationService', [], [], '', false) ); - $pluginManager = new GuardPluginManager(); - $pluginManager->setServiceLocator($serviceManager); + $pluginManager = new GuardPluginManager($serviceManager); $guard = $pluginManager->get($type, $options); @@ -93,7 +92,7 @@ public function testThrowExceptionForInvalidPlugin() { $this->setExpectedException('ZfcRbac\Exception\RuntimeException'); - $pluginManager = new GuardPluginManager(); + $pluginManager = new GuardPluginManager(new ServiceManager()); $pluginManager->get('stdClass'); } } diff --git a/tests/ZfcRbacTest/Role/RoleProviderPluginManagerTest.php b/tests/ZfcRbacTest/Role/RoleProviderPluginManagerTest.php index 8667acb0..452bde3a 100644 --- a/tests/ZfcRbacTest/Role/RoleProviderPluginManagerTest.php +++ b/tests/ZfcRbacTest/Role/RoleProviderPluginManagerTest.php @@ -18,6 +18,7 @@ namespace ZfcRbacTest\Role; +use Zend\ServiceManager\ServiceManager; use ZfcRbac\Role\RoleProviderPluginManager; /** @@ -28,7 +29,7 @@ class RoleProviderPluginManagerTest extends \PHPUnit_Framework_TestCase public function testValidationOfPluginSucceedsIfRoleProviderInterfaceIsImplemented() { $pluginMock = $this->getMock('ZfcRbac\Role\RoleProviderInterface'); - $pluginManager = new RoleProviderPluginManager(); + $pluginManager = new RoleProviderPluginManager(new ServiceManager()); $this->assertNull($pluginManager->validatePlugin($pluginMock)); } @@ -37,7 +38,7 @@ public function testValidationOfPluginFailsIfRoleProviderInterfaceIsNotImplement { $this->setExpectedException('ZfcRbac\Exception\RuntimeException'); - $pluginManager = new RoleProviderPluginManager(); + $pluginManager = new RoleProviderPluginManager(new ServiceManager()); $pluginManager->get('stdClass', []); } } diff --git a/tests/ZfcRbacTest/Service/AuthorizationServiceTest.php b/tests/ZfcRbacTest/Service/AuthorizationServiceTest.php index 1685dc01..bddbbf84 100644 --- a/tests/ZfcRbacTest/Service/AuthorizationServiceTest.php +++ b/tests/ZfcRbacTest/Service/AuthorizationServiceTest.php @@ -20,6 +20,7 @@ use Rbac\Rbac; use Rbac\Traversal\Strategy\RecursiveRoleIteratorStrategy; +use Zend\ServiceManager\ServiceManager; use ZfcRbac\Role\InMemoryRoleProvider; use ZfcRbac\Service\AuthorizationService; use ZfcRbac\Service\RoleService; @@ -138,7 +139,7 @@ public function testGranted($role, $permission, $context, $isGranted, $assertion new InMemoryRoleProvider($roleConfig), $rbac->getTraversalStrategy() ); - $assertionPluginManager = new AssertionPluginManager(new Config($assertionPluginConfig)); + $assertionPluginManager = new AssertionPluginManager(new ServiceManager(), $assertionPluginConfig); $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager); $authorizationService->setAssertions($assertions); diff --git a/tests/ZfcRbacTest/Util/ServiceManagerFactory.php b/tests/ZfcRbacTest/Util/ServiceManagerFactory.php index 278cad39..86cc9954 100644 --- a/tests/ZfcRbacTest/Util/ServiceManagerFactory.php +++ b/tests/ZfcRbacTest/Util/ServiceManagerFactory.php @@ -59,11 +59,11 @@ public static function getApplicationConfig() public static function getServiceManager(array $config = null) { $config = $config ?: static::getApplicationConfig(); - $serviceManager = new ServiceManager( - new ServiceManagerConfig( - isset($config['service_manager']) ? $config['service_manager'] : [] - ) + $serviceManagerConfig = new ServiceManagerConfig( + isset($config['service_manager']) ? $config['service_manager'] : [] ); + $serviceManager = new ServiceManager(); + $serviceManagerConfig->configureServiceManager($serviceManager); $serviceManager->setService('ApplicationConfig', $config); $serviceManager->setAllowOverride(true); From 191b454da3b4b80dfc3ab93d9ee3627a6c61f280 Mon Sep 17 00:00:00 2001 From: prolic Date: Fri, 15 Jul 2016 14:03:46 +0800 Subject: [PATCH 18/29] update tests and srcs --- composer.json | 2 +- .../Factory/ControllerGuardFactory.php | 2 +- .../ControllerPermissionsGuardFactory.php | 2 +- .../Factory/HasRoleViewHelperFactory.php | 2 +- .../Factory/IsGrantedViewHelperFactory.php | 2 +- .../AuthorizationServiceInitializer.php | 4 ---- .../AuthorizationServiceDelegatorTest.php | 18 +++++++--------- .../Factory/ControllerGuardFactoryTest.php | 3 ++- .../ControllerPermissionsGuardFactoryTest.php | 3 ++- .../Factory/GuardPluginManagerFactoryTest.php | 4 ++-- .../ZfcRbacTest/Factory/GuardsFactoryTest.php | 14 ++++++------- .../Factory/HasRoleViewHelperFactoryTest.php | 11 ++-------- .../Factory/IsGrantedPluginFactoryTest.php | 11 ++-------- .../IsGrantedViewHelperFactoryTest.php | 12 ++--------- .../Factory/RedirectStrategyFactoryTest.php | 21 ++++++++++--------- .../RoleProviderPluginManagerFactoryTest.php | 6 +++--- .../Factory/RouteGuardFactoryTest.php | 3 ++- .../RoutePermissionsGuardFactoryTest.php | 3 ++- .../UnauthorizedStrategyFactoryTest.php | 11 +++++----- .../AuthorizationServiceInitializerTest.php | 15 +++++++------ 20 files changed, 63 insertions(+), 86 deletions(-) diff --git a/composer.json b/composer.json index 2f400942..7575f861 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "php": "~5.6 || ~7.0", "zendframework/zend-config": "~2.2", "zendframework/zend-eventmanager": "^2.6.3 || ^3.0", - "zendframework/zend-mvc": "~2.7.4 || ^3.0", + "zendframework/zend-mvc": "~2.7 || ^3.0", "zendframework/zend-servicemanager": "^2.7.5 || ^3.0.3", "zfr/rbac": "~1.2" }, diff --git a/src/ZfcRbac/Factory/ControllerGuardFactory.php b/src/ZfcRbac/Factory/ControllerGuardFactory.php index 20d9da51..a322f62e 100644 --- a/src/ZfcRbac/Factory/ControllerGuardFactory.php +++ b/src/ZfcRbac/Factory/ControllerGuardFactory.php @@ -83,6 +83,6 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o */ public function createService(ServiceLocatorInterface $serviceLocator) { - return $this($serviceLocator, ControllerGuard::class, $this->options); + return $this($serviceLocator->getServiceLocator(), ControllerGuard::class, $this->options); } } diff --git a/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php b/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php index ba37975d..27a57a70 100644 --- a/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php +++ b/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php @@ -82,6 +82,6 @@ public function __invoke(ContainerInterface $container, $resolvedName, array $op */ public function createService(ServiceLocatorInterface $serviceLocator) { - return $this($serviceLocator, ControllerPermissionsGuard::class, $this->options); + return $this($serviceLocator->getServiceLocator(), ControllerPermissionsGuard::class, $this->options); } } diff --git a/src/ZfcRbac/Factory/HasRoleViewHelperFactory.php b/src/ZfcRbac/Factory/HasRoleViewHelperFactory.php index 0978f9d2..b2556a83 100644 --- a/src/ZfcRbac/Factory/HasRoleViewHelperFactory.php +++ b/src/ZfcRbac/Factory/HasRoleViewHelperFactory.php @@ -55,6 +55,6 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o */ public function createService(ServiceLocatorInterface $serviceLocator) { - return $this($serviceLocator, HasRole::class); + return $this($serviceLocator->getServiceLocator(), HasRole::class); } } diff --git a/src/ZfcRbac/Factory/IsGrantedViewHelperFactory.php b/src/ZfcRbac/Factory/IsGrantedViewHelperFactory.php index bc0db739..c5301142 100644 --- a/src/ZfcRbac/Factory/IsGrantedViewHelperFactory.php +++ b/src/ZfcRbac/Factory/IsGrantedViewHelperFactory.php @@ -54,6 +54,6 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o */ public function createService(ServiceLocatorInterface $serviceLocator) { - return $this($serviceLocator, IsGranted::class); + return $this($serviceLocator->getServiceLocator(), IsGranted::class); } } diff --git a/src/ZfcRbac/Initializer/AuthorizationServiceInitializer.php b/src/ZfcRbac/Initializer/AuthorizationServiceInitializer.php index 70e9a94d..6b7c618d 100644 --- a/src/ZfcRbac/Initializer/AuthorizationServiceInitializer.php +++ b/src/ZfcRbac/Initializer/AuthorizationServiceInitializer.php @@ -39,10 +39,6 @@ class AuthorizationServiceInitializer implements InitializerInterface public function __invoke(ContainerInterface $container, $instance) { if ($instance instanceof AuthorizationServiceAwareInterface) { - if ($container instanceof AbstractPluginManager) { - $container = $container->getServiceLocator(); - } - $authorizationService = $container->get('ZfcRbac\Service\AuthorizationService'); $instance->setAuthorizationService($authorizationService); } diff --git a/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php b/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php index 34f72cbe..20e98bd9 100644 --- a/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php +++ b/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php @@ -17,6 +17,8 @@ */ namespace ZfcRbacTest\Factory; +use Interop\Container\ContainerInterface; +use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Factory\AuthorizationServiceDelegatorFactory; use ZfcRbacTest\Initializer\AuthorizationAwareFake; use ZfcRbacTest\Util\ServiceManagerFactory; @@ -32,19 +34,18 @@ public function testDelegatorFactory() { $authServiceClassName = 'ZfcRbac\Service\AuthorizationService'; $delegator = new AuthorizationServiceDelegatorFactory(); - $serviceLocator = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface'); + $serviceLocator = $this->prophesize(ContainerInterface::class); + $serviceLocator->willImplement(ServiceLocatorInterface::class); + $authorizationService = $this->getMock('ZfcRbac\Service\AuthorizationService', [], [], '', false); $callback = function () { return new AuthorizationAwareFake(); }; - $serviceLocator->expects($this->once()) - ->method('get') - ->with($authServiceClassName) - ->will($this->returnValue($authorizationService)); + $serviceLocator->get($authServiceClassName)->willReturn($authorizationService)->shouldBeCalled(); - $decoratedInstance = $delegator->createDelegatorWithName($serviceLocator, 'name', 'requestedName', $callback); + $decoratedInstance = $delegator->createDelegatorWithName($serviceLocator->reveal(), 'name', 'requestedName', $callback); $this->assertEquals($authorizationService, $decoratedInstance->getAuthorizationService()); } @@ -91,11 +92,6 @@ public function testAuthorizationServiceIsInjectedWithDelegator() 'ZfcRbac\Factory\AuthorizationServiceDelegatorFactory' ); - $serviceManager->addDelegator( - 'ZfcRbacTest\Initializer\AuthorizationAwareFake', - 'ZfcRbac\Factory\AuthorizationServiceDelegatorFactory' - ); - $decoratedInstance = $serviceManager->get('ZfcRbacTest\AuthorizationAware'); $this->assertEquals($authorizationService, $decoratedInstance->getAuthorizationService()); } diff --git a/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php index 152ef70f..d5bf17c5 100644 --- a/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php @@ -52,7 +52,8 @@ public function testFactory() $pluginManager = new GuardPluginManager($serviceManager); - $controllerGuard = $pluginManager->get('ZfcRbac\Guard\ControllerGuard'); + $factory = new ControllerGuardFactory(); + $controllerGuard = $factory->createService($pluginManager); $this->assertInstanceOf('ZfcRbac\Guard\ControllerGuard', $controllerGuard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $controllerGuard->getProtectionPolicy()); diff --git a/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php index d73fbf87..1e98300c 100644 --- a/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php @@ -52,7 +52,8 @@ public function testFactory() $pluginManager = new GuardPluginManager($serviceManager); - $guard = $pluginManager->get('ZfcRbac\Guard\ControllerPermissionsGuard'); + $factory = new ControllerPermissionsGuardFactory(); + $guard = $factory->createService($pluginManager); $this->assertInstanceOf('ZfcRbac\Guard\ControllerPermissionsGuard', $guard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $guard->getProtectionPolicy()); diff --git a/tests/ZfcRbacTest/Factory/GuardPluginManagerFactoryTest.php b/tests/ZfcRbacTest/Factory/GuardPluginManagerFactoryTest.php index 226da6a1..01d53efa 100644 --- a/tests/ZfcRbacTest/Factory/GuardPluginManagerFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/GuardPluginManagerFactoryTest.php @@ -35,9 +35,9 @@ public function testFactory() 'guard_manager' => [] ] ]); - $serviceManager->setFactory(GuardPluginManager::class, GuardPluginManagerFactory::class); - $pluginManager = $serviceManager->get(GuardPluginManager::class); + $factory = new GuardPluginManagerFactory(); + $pluginManager = $factory->createService($serviceManager); $this->assertInstanceOf(GuardPluginManager::class, $pluginManager); } diff --git a/tests/ZfcRbacTest/Factory/GuardsFactoryTest.php b/tests/ZfcRbacTest/Factory/GuardsFactoryTest.php index 047f2c5f..f1c494b7 100644 --- a/tests/ZfcRbacTest/Factory/GuardsFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/GuardsFactoryTest.php @@ -28,9 +28,6 @@ */ class GuardsFactoryTest extends \PHPUnit_Framework_TestCase { - /** - * @group by - */ public function testFactory() { $moduleOptions = new ModuleOptions([ @@ -54,9 +51,9 @@ public function testFactory() ] ]); - $serviceManager = new ServiceManager(); - $pluginManager = new GuardPluginManager($serviceManager); + $pluginManager = new GuardPluginManager(); + $serviceManager = new ServiceManager(); $serviceManager->setService('ZfcRbac\Options\ModuleOptions', $moduleOptions); $serviceManager->setService('ZfcRbac\Guard\GuardPluginManager', $pluginManager); $serviceManager->setService( @@ -68,6 +65,8 @@ public function testFactory() $this->getMock('ZfcRbac\Service\AuthorizationServiceInterface', [], [], '', false) ); + $pluginManager->setServiceLocator($serviceManager); + $factory = new GuardsFactory(); $guards = $factory->createService($serviceManager); @@ -86,10 +85,11 @@ public function testReturnArrayIfNoConfig() 'guards' => [] ]); - $serviceManager = new ServiceManager(); - $pluginManager = new GuardPluginManager($serviceManager); + $pluginManager = new GuardPluginManager(); + $serviceManager = new ServiceManager(); $serviceManager->setService('ZfcRbac\Options\ModuleOptions', $moduleOptions); + $pluginManager->setServiceLocator($serviceManager); $factory = new GuardsFactory(); $guards = $factory->createService($serviceManager); diff --git a/tests/ZfcRbacTest/Factory/HasRoleViewHelperFactoryTest.php b/tests/ZfcRbacTest/Factory/HasRoleViewHelperFactoryTest.php index 4ed9535a..98a8e86e 100644 --- a/tests/ZfcRbacTest/Factory/HasRoleViewHelperFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/HasRoleViewHelperFactoryTest.php @@ -32,21 +32,14 @@ public function testFactory() $serviceManager = new ServiceManager(); $pluginManager = new HelperPluginManager($serviceManager); - $pluginManager->configure([ - 'factories' => [ - 'ZfcRbac\View\Helper\HasRole' => 'ZfcRbac\Factory\HasRoleViewHelperFactory' - ], - 'aliases' => [ - 'hasRole' => 'ZfcRbac\View\Helper\HasRole' - ] - ]); $serviceManager->setService( 'ZfcRbac\Service\RoleService', $this->getMock('ZfcRbac\Service\RoleService', [], [], '', false) ); - $viewHelper = $pluginManager->get('hasRole'); + $factory = new HasRoleViewHelperFactory(); + $viewHelper = $factory->createService($pluginManager); $this->assertInstanceOf('ZfcRbac\View\Helper\HasRole', $viewHelper); } diff --git a/tests/ZfcRbacTest/Factory/IsGrantedPluginFactoryTest.php b/tests/ZfcRbacTest/Factory/IsGrantedPluginFactoryTest.php index b26d5dd2..1ebcfd2a 100644 --- a/tests/ZfcRbacTest/Factory/IsGrantedPluginFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/IsGrantedPluginFactoryTest.php @@ -32,21 +32,14 @@ public function testFactory() $serviceManager = new ServiceManager(); $pluginManager = new PluginManager($serviceManager); - $pluginManager->configure([ - 'factories' => [ - 'ZfcRbac\Mvc\Controller\Plugin\IsGranted' => 'ZfcRbac\Factory\IsGrantedPluginFactory' - ], - 'aliases' => [ - 'isGranted' => 'ZfcRbac\Mvc\Controller\Plugin\IsGranted' - ] - ]); $serviceManager->setService( 'ZfcRbac\Service\AuthorizationService', $this->getMock('ZfcRbac\Service\AuthorizationServiceInterface') ); - $isGranted = $pluginManager->get('isGranted'); + $factory = new IsGrantedPluginFactory(); + $isGranted = $factory->createService($pluginManager); $this->assertInstanceOf('ZfcRbac\Mvc\Controller\Plugin\IsGranted', $isGranted); } diff --git a/tests/ZfcRbacTest/Factory/IsGrantedViewHelperFactoryTest.php b/tests/ZfcRbacTest/Factory/IsGrantedViewHelperFactoryTest.php index af2a66af..b19df9f6 100644 --- a/tests/ZfcRbacTest/Factory/IsGrantedViewHelperFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/IsGrantedViewHelperFactoryTest.php @@ -32,22 +32,14 @@ public function testFactory() $serviceManager = new ServiceManager(); $pluginManager = new HelperPluginManager($serviceManager); - $pluginManager->configure([ - 'factories' => [ - 'ZfcRbac\View\Helper\IsGranted' => 'ZfcRbac\Factory\IsGrantedViewHelperFactory', - ], - 'aliases' => [ - 'isGranted' => 'ZfcRbac\View\Helper\IsGranted', - ] - ]); $serviceManager->setService( 'ZfcRbac\Service\AuthorizationService', $this->getMock('ZfcRbac\Service\AuthorizationServiceInterface') ); - - $isGranted = $pluginManager->get('isGranted'); + $factory = new IsGrantedViewHelperFactory(); + $isGranted = $factory->createService($pluginManager); $this->assertInstanceOf('ZfcRbac\View\Helper\IsGranted', $isGranted); } diff --git a/tests/ZfcRbacTest/Factory/RedirectStrategyFactoryTest.php b/tests/ZfcRbacTest/Factory/RedirectStrategyFactoryTest.php index d531b9c9..701da5a8 100644 --- a/tests/ZfcRbacTest/Factory/RedirectStrategyFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/RedirectStrategyFactoryTest.php @@ -18,6 +18,8 @@ namespace ZfcRbacTest\Factory; +use Interop\Container\ContainerInterface; +use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Factory\RedirectStrategyFactory; /** @@ -36,18 +38,17 @@ public function testFactory() $authenticationServiceMock = $this->getMock('Zend\Authentication\AuthenticationService'); - $serviceLocatorMock = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface'); - $serviceLocatorMock->expects($this->at(0)) - ->method('get') - ->with('ZfcRbac\Options\ModuleOptions') - ->will($this->returnValue($moduleOptionsMock)); - $serviceLocatorMock->expects($this->at(1)) - ->method('get') - ->with('Zend\Authentication\AuthenticationService') - ->will($this->returnValue($authenticationServiceMock)); + $serviceLocatorMock = $this->prophesize(ServiceLocatorInterface::class); + $serviceLocatorMock->willImplement(ContainerInterface::class); + $serviceLocatorMock->get('ZfcRbac\Options\ModuleOptions') + ->willReturn($moduleOptionsMock) + ->shouldBeCalled(); + $serviceLocatorMock->get('Zend\Authentication\AuthenticationService') + ->willReturn($authenticationServiceMock) + ->shouldBeCalled(); $factory = new RedirectStrategyFactory(); - $redirectStrategy = $factory->createService($serviceLocatorMock); + $redirectStrategy = $factory->createService($serviceLocatorMock->reveal()); $this->assertInstanceOf('ZfcRbac\View\Strategy\RedirectStrategy', $redirectStrategy); } diff --git a/tests/ZfcRbacTest/Factory/RoleProviderPluginManagerFactoryTest.php b/tests/ZfcRbacTest/Factory/RoleProviderPluginManagerFactoryTest.php index b33ff1ad..8dfa439f 100644 --- a/tests/ZfcRbacTest/Factory/RoleProviderPluginManagerFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/RoleProviderPluginManagerFactoryTest.php @@ -35,10 +35,10 @@ public function testFactory() 'role_provider_manager' => [] ] ]); - $serviceManager->setFactory(RoleProviderPluginManager::class, RoleProviderPluginManagerFactory::class); - $pluginManager = $serviceManager->get(RoleProviderPluginManager::class); + $factory = new RoleProviderPluginManagerFactory(); + $pluginManager = $factory->createService($serviceManager); - $this->assertInstanceOf('ZfcRbac\Role\RoleProviderPluginManager', $pluginManager); + $this->assertInstanceOf(RoleProviderPluginManager::class, $pluginManager); } } diff --git a/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php index e4cd0284..c2198290 100644 --- a/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php @@ -52,7 +52,8 @@ public function testFactory() $pluginManager = new GuardPluginManager($serviceManager); - $routeGuard = $pluginManager->get('ZfcRbac\Guard\RouteGuard'); + $factory = new RouteGuardFactory(); + $routeGuard = $factory->createService($pluginManager); $this->assertInstanceOf('ZfcRbac\Guard\RouteGuard', $routeGuard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $routeGuard->getProtectionPolicy()); diff --git a/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php index 9ad73d47..178fc706 100644 --- a/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php @@ -52,7 +52,8 @@ public function testFactory() $pluginManager = new GuardPluginManager($serviceManager); - $routeGuard = $pluginManager->get('ZfcRbac\Guard\RoutePermissionsGuard'); + $factory = new RoutePermissionsGuardFactory(); + $routeGuard = $factory->createService($pluginManager); $this->assertInstanceOf('ZfcRbac\Guard\RoutePermissionsGuard', $routeGuard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $routeGuard->getProtectionPolicy()); diff --git a/tests/ZfcRbacTest/Factory/UnauthorizedStrategyFactoryTest.php b/tests/ZfcRbacTest/Factory/UnauthorizedStrategyFactoryTest.php index 1efb9b6b..fbc3793e 100644 --- a/tests/ZfcRbacTest/Factory/UnauthorizedStrategyFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/UnauthorizedStrategyFactoryTest.php @@ -18,6 +18,7 @@ namespace ZfcRbacTest\Factory; +use Interop\Container\ContainerInterface; use ZfcRbac\Factory\UnauthorizedStrategyFactory; /** @@ -34,14 +35,12 @@ public function testFactory() ->method('getUnauthorizedStrategy') ->will($this->returnValue($unauthorizedStrategyOptions)); - $serviceLocatorMock = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface'); - $serviceLocatorMock->expects($this->once()) - ->method('get') - ->with('ZfcRbac\Options\ModuleOptions') - ->will($this->returnValue($moduleOptionsMock)); + $serviceLocatorMock = $this->prophesize('Zend\ServiceManager\ServiceLocatorInterface'); + $serviceLocatorMock->willImplement(ContainerInterface::class); + $serviceLocatorMock->get('ZfcRbac\Options\ModuleOptions')->willReturn($moduleOptionsMock)->shouldBeCalled(); $factory = new UnauthorizedStrategyFactory(); - $unauthorizedStrategy = $factory->createService($serviceLocatorMock); + $unauthorizedStrategy = $factory->createService($serviceLocatorMock->reveal()); $this->assertInstanceOf('ZfcRbac\View\Strategy\UnauthorizedStrategy', $unauthorizedStrategy); } diff --git a/tests/ZfcRbacTest/Initializer/AuthorizationServiceInitializerTest.php b/tests/ZfcRbacTest/Initializer/AuthorizationServiceInitializerTest.php index 0cb1b3bc..c58c886f 100644 --- a/tests/ZfcRbacTest/Initializer/AuthorizationServiceInitializerTest.php +++ b/tests/ZfcRbacTest/Initializer/AuthorizationServiceInitializerTest.php @@ -17,6 +17,8 @@ */ namespace ZfcRbacTest\Initializer; +use Interop\Container\ContainerInterface; +use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Initializer\AuthorizationServiceInitializer; /** @@ -31,15 +33,16 @@ public function testInitializer() $authServiceClassName = 'ZfcRbac\Service\AuthorizationService'; $initializer = new AuthorizationServiceInitializer(); $instance = new AuthorizationAwareFake(); - $serviceLocator = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface'); + + $serviceLocator = $this->prophesize(ServiceLocatorInterface::class)->willImplement(ContainerInterface::class); $authorizationService = $this->getMock('ZfcRbac\Service\AuthorizationService', [], [], '', false); - $serviceLocator->expects($this->once()) - ->method('get') - ->with($authServiceClassName) - ->will($this->returnValue($authorizationService)); + $serviceLocator + ->get($authServiceClassName) + ->willReturn($authorizationService) + ->shouldBeCalled(); - $initializer->initialize($instance, $serviceLocator); + $initializer->initialize($instance, $serviceLocator->reveal()); $this->assertEquals($authorizationService, $instance->getAuthorizationService()); } From feb4aba4377eac3573aa130d9883de2f5c6b49ac Mon Sep 17 00:00:00 2001 From: prolic Date: Fri, 15 Jul 2016 14:23:08 +0800 Subject: [PATCH 19/29] add v3 service manager tests --- .../AuthorizationServiceDelegatorTest.php | 41 ++++++++++++++++++- .../Factory/ControllerGuardFactoryTest.php | 40 +++++++++++++++++- .../ControllerPermissionsGuardFactoryTest.php | 40 +++++++++++++++++- .../ZfcRbacTest/Factory/GuardsFactoryTest.php | 11 ++--- .../Factory/HasRoleViewHelperFactoryTest.php | 23 +++++++++++ .../Factory/IsGrantedPluginFactoryTest.php | 22 ++++++++++ .../IsGrantedViewHelperFactoryTest.php | 22 ++++++++++ .../Factory/RouteGuardFactoryTest.php | 40 +++++++++++++++++- .../RoutePermissionsGuardFactoryTest.php | 40 +++++++++++++++++- 9 files changed, 266 insertions(+), 13 deletions(-) diff --git a/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php b/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php index 20e98bd9..23074601 100644 --- a/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php +++ b/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php @@ -34,8 +34,8 @@ public function testDelegatorFactory() { $authServiceClassName = 'ZfcRbac\Service\AuthorizationService'; $delegator = new AuthorizationServiceDelegatorFactory(); - $serviceLocator = $this->prophesize(ContainerInterface::class); - $serviceLocator->willImplement(ServiceLocatorInterface::class); + $serviceLocator = $this->prophesize(ServiceLocatorInterface::class); + $serviceLocator->willImplement(ContainerInterface::class); $authorizationService = $this->getMock('ZfcRbac\Service\AuthorizationService', [], [], '', false); @@ -74,6 +74,10 @@ public function testAuthorizationServiceIsInjectedWithDelegator() { $serviceManager = ServiceManagerFactory::getServiceManager(); + if (method_exists($serviceManager, 'build')) { + $this->markTestSkipped('this test is only vor zend-servicemanager v2'); + } + $serviceManager->setAllowOverride(true); $authorizationService = $this->getMock('ZfcRbac\Service\AuthorizationService', [], [], '', false); $serviceManager->setService( @@ -96,6 +100,39 @@ public function testAuthorizationServiceIsInjectedWithDelegator() $this->assertEquals($authorizationService, $decoratedInstance->getAuthorizationService()); } + /** + * @group by + */ + public function testAuthorizationServiceIsInjectedWithDelegatorV3() + { + $serviceManager = ServiceManagerFactory::getServiceManager(); + + if (! method_exists($serviceManager, 'build')) { + $this->markTestSkipped('this test is only vor zend-servicemanager v3'); + } + + $serviceManager->setAllowOverride(true); + $authorizationService = $this->getMock('ZfcRbac\Service\AuthorizationService', [], [], '', false); + $serviceManager->setService( + 'ZfcRbac\Service\AuthorizationService', + $authorizationService + ); + $serviceManager->setAllowOverride(false); + + $serviceManager->setInvokableClass( + 'ZfcRbacTest\AuthorizationAware', + 'ZfcRbacTest\Initializer\AuthorizationAwareFake' + ); + + $serviceManager->addDelegator( + 'ZfcRbacTest\Initializer\AuthorizationAwareFake', + 'ZfcRbac\Factory\AuthorizationServiceDelegatorFactory' + ); + + $decoratedInstance = $serviceManager->get('ZfcRbacTest\AuthorizationAware'); + $this->assertEquals($authorizationService, $decoratedInstance->getAuthorizationService()); + } + public function testDelegatorThrowExceptionWhenBadInterface() { $serviceManager = ServiceManagerFactory::getServiceManager(); diff --git a/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php index d5bf17c5..36bac5a7 100644 --- a/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/ControllerGuardFactoryTest.php @@ -31,6 +31,12 @@ class ControllerGuardFactoryTest extends \PHPUnit_Framework_TestCase { public function testFactory() { + $serviceManager = new ServiceManager(); + + if (method_exists($serviceManager, 'build')) { + $this->markTestSkipped('this test is only vor zend-servicemanager v2'); + } + $options = new ModuleOptions([ 'identity_provider' => 'ZfcRbac\Identity\AuthenticationProvider', 'guards' => [ @@ -43,7 +49,6 @@ public function testFactory() 'protection_policy' => GuardInterface::POLICY_ALLOW ]); - $serviceManager = new ServiceManager(); $serviceManager->setService('ZfcRbac\Options\ModuleOptions', $options); $serviceManager->setService( 'ZfcRbac\Service\RoleService', @@ -58,4 +63,37 @@ public function testFactory() $this->assertInstanceOf('ZfcRbac\Guard\ControllerGuard', $controllerGuard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $controllerGuard->getProtectionPolicy()); } + + public function testFactoryV3() + { + $serviceManager = new ServiceManager(); + + if (! method_exists($serviceManager, 'build')) { + $this->markTestSkipped('this test is only vor zend-servicemanager v3'); + } + + $options = new ModuleOptions([ + 'identity_provider' => 'ZfcRbac\Identity\AuthenticationProvider', + 'guards' => [ + 'ZfcRbac\Guard\ControllerGuard' => [ + 'controller' => 'MyController', + 'actions' => 'edit', + 'roles' => 'member' + ] + ], + 'protection_policy' => GuardInterface::POLICY_ALLOW + ]); + + $serviceManager->setService('ZfcRbac\Options\ModuleOptions', $options); + $serviceManager->setService( + 'ZfcRbac\Service\RoleService', + $this->getMock('ZfcRbac\Service\RoleService', [], [], '', false) + ); + + $factory = new ControllerGuardFactory(); + $controllerGuard = $factory($serviceManager, GuardPluginManager::class); + + $this->assertInstanceOf('ZfcRbac\Guard\ControllerGuard', $controllerGuard); + $this->assertEquals(GuardInterface::POLICY_ALLOW, $controllerGuard->getProtectionPolicy()); + } } diff --git a/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php index 1e98300c..01bc0cf7 100644 --- a/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/ControllerPermissionsGuardFactoryTest.php @@ -31,6 +31,12 @@ class ControllerPermissionsGuardFactoryTest extends \PHPUnit_Framework_TestCase { public function testFactory() { + $serviceManager = new ServiceManager(); + + if (method_exists($serviceManager, 'build')) { + $this->markTestSkipped('this test is only vor zend-servicemanager v2'); + } + $creationOptions = [ 'route' => 'permission' ]; @@ -43,7 +49,6 @@ public function testFactory() 'protection_policy' => GuardInterface::POLICY_ALLOW, ]); - $serviceManager = new ServiceManager(); $serviceManager->setService('ZfcRbac\Options\ModuleOptions', $options); $serviceManager->setService( 'ZfcRbac\Service\AuthorizationService', @@ -58,4 +63,37 @@ public function testFactory() $this->assertInstanceOf('ZfcRbac\Guard\ControllerPermissionsGuard', $guard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $guard->getProtectionPolicy()); } + + public function testFactoryV3() + { + $serviceManager = new ServiceManager(); + + if (!method_exists($serviceManager, 'build')) { + $this->markTestSkipped('this test is only vor zend-servicemanager v3'); + } + + $creationOptions = [ + 'route' => 'permission' + ]; + + $options = new ModuleOptions([ + 'identity_provider' => 'ZfcRbac\Identity\AuthenticationProvider', + 'guards' => [ + 'ZfcRbac\Guard\ControllerPermissionsGuard' => $creationOptions + ], + 'protection_policy' => GuardInterface::POLICY_ALLOW, + ]); + + $serviceManager->setService('ZfcRbac\Options\ModuleOptions', $options); + $serviceManager->setService( + 'ZfcRbac\Service\AuthorizationService', + $this->getMock('ZfcRbac\Service\AuthorizationService', [], [], '', false) + ); + + $factory = new ControllerPermissionsGuardFactory(); + $guard = $factory($serviceManager, GuardPluginManager::class); + + $this->assertInstanceOf('ZfcRbac\Guard\ControllerPermissionsGuard', $guard); + $this->assertEquals(GuardInterface::POLICY_ALLOW, $guard->getProtectionPolicy()); + } } diff --git a/tests/ZfcRbacTest/Factory/GuardsFactoryTest.php b/tests/ZfcRbacTest/Factory/GuardsFactoryTest.php index f1c494b7..494526b4 100644 --- a/tests/ZfcRbacTest/Factory/GuardsFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/GuardsFactoryTest.php @@ -51,9 +51,9 @@ public function testFactory() ] ]); - $pluginManager = new GuardPluginManager(); - $serviceManager = new ServiceManager(); + $pluginManager = new GuardPluginManager($serviceManager); + $serviceManager->setService('ZfcRbac\Options\ModuleOptions', $moduleOptions); $serviceManager->setService('ZfcRbac\Guard\GuardPluginManager', $pluginManager); $serviceManager->setService( @@ -65,8 +65,6 @@ public function testFactory() $this->getMock('ZfcRbac\Service\AuthorizationServiceInterface', [], [], '', false) ); - $pluginManager->setServiceLocator($serviceManager); - $factory = new GuardsFactory(); $guards = $factory->createService($serviceManager); @@ -85,11 +83,10 @@ public function testReturnArrayIfNoConfig() 'guards' => [] ]); - $pluginManager = new GuardPluginManager(); - $serviceManager = new ServiceManager(); + $pluginManager = new GuardPluginManager($serviceManager); + $serviceManager->setService('ZfcRbac\Options\ModuleOptions', $moduleOptions); - $pluginManager->setServiceLocator($serviceManager); $factory = new GuardsFactory(); $guards = $factory->createService($serviceManager); diff --git a/tests/ZfcRbacTest/Factory/HasRoleViewHelperFactoryTest.php b/tests/ZfcRbacTest/Factory/HasRoleViewHelperFactoryTest.php index 98a8e86e..840b92a5 100644 --- a/tests/ZfcRbacTest/Factory/HasRoleViewHelperFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/HasRoleViewHelperFactoryTest.php @@ -31,6 +31,10 @@ public function testFactory() { $serviceManager = new ServiceManager(); + if (method_exists($serviceManager, 'build')) { + $this->markTestSkipped('this test is only vor zend-servicemanager v2'); + } + $pluginManager = new HelperPluginManager($serviceManager); $serviceManager->setService( @@ -43,4 +47,23 @@ public function testFactory() $this->assertInstanceOf('ZfcRbac\View\Helper\HasRole', $viewHelper); } + + public function testFactoryV3() + { + $serviceManager = new ServiceManager(); + + if (!method_exists($serviceManager, 'build')) { + $this->markTestSkipped('this test is only vor zend-servicemanager v3'); + } + + $serviceManager->setService( + 'ZfcRbac\Service\RoleService', + $this->getMock('ZfcRbac\Service\RoleService', [], [], '', false) + ); + + $factory = new HasRoleViewHelperFactory(); + $viewHelper = $factory($serviceManager, 'ZfcRbac\View\Helper\HasRole'); + + $this->assertInstanceOf('ZfcRbac\View\Helper\HasRole', $viewHelper); + } } diff --git a/tests/ZfcRbacTest/Factory/IsGrantedPluginFactoryTest.php b/tests/ZfcRbacTest/Factory/IsGrantedPluginFactoryTest.php index 1ebcfd2a..39feee01 100644 --- a/tests/ZfcRbacTest/Factory/IsGrantedPluginFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/IsGrantedPluginFactoryTest.php @@ -31,6 +31,10 @@ public function testFactory() { $serviceManager = new ServiceManager(); + if (method_exists($serviceManager, 'build')) { + $this->markTestSkipped('this test is only vor zend-servicemanager v2'); + } + $pluginManager = new PluginManager($serviceManager); $serviceManager->setService( @@ -43,4 +47,22 @@ public function testFactory() $this->assertInstanceOf('ZfcRbac\Mvc\Controller\Plugin\IsGranted', $isGranted); } + + public function testFactoryV3() + { + $serviceManager = new ServiceManager(); + + if (! method_exists($serviceManager, 'build')) { + $this->markTestSkipped('this test is only vor zend-servicemanager v3'); + } + $serviceManager->setService( + 'ZfcRbac\Service\AuthorizationService', + $this->getMock('ZfcRbac\Service\AuthorizationServiceInterface') + ); + + $factory = new IsGrantedPluginFactory(); + $isGranted = $factory($serviceManager, 'ZfcRbac\Mvc\Controller\Plugin\IsGranted'); + + $this->assertInstanceOf('ZfcRbac\Mvc\Controller\Plugin\IsGranted', $isGranted); + } } diff --git a/tests/ZfcRbacTest/Factory/IsGrantedViewHelperFactoryTest.php b/tests/ZfcRbacTest/Factory/IsGrantedViewHelperFactoryTest.php index b19df9f6..e7ae7a1c 100644 --- a/tests/ZfcRbacTest/Factory/IsGrantedViewHelperFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/IsGrantedViewHelperFactoryTest.php @@ -31,6 +31,10 @@ public function testFactory() { $serviceManager = new ServiceManager(); + if (method_exists($serviceManager, 'build')) { + $this->markTestSkipped('this test is only vor zend-servicemanager v2'); + } + $pluginManager = new HelperPluginManager($serviceManager); $serviceManager->setService( @@ -43,4 +47,22 @@ public function testFactory() $this->assertInstanceOf('ZfcRbac\View\Helper\IsGranted', $isGranted); } + + public function testFactoryV3() + { + $serviceManager = new ServiceManager(); + + if (! method_exists($serviceManager, 'build')) { + $this->markTestSkipped('this test is only vor zend-servicemanager v3'); + } + $serviceManager->setService( + 'ZfcRbac\Service\AuthorizationService', + $this->getMock('ZfcRbac\Service\AuthorizationServiceInterface') + ); + + $factory = new IsGrantedViewHelperFactory(); + $isGranted = $factory($serviceManager, 'ZfcRbac\View\Helper\IsGranted'); + + $this->assertInstanceOf('ZfcRbac\View\Helper\IsGranted', $isGranted); + } } diff --git a/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php index c2198290..7cade6a3 100644 --- a/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/RouteGuardFactoryTest.php @@ -31,6 +31,12 @@ class RouteGuardFactoryTest extends \PHPUnit_Framework_TestCase { public function testFactory() { + $serviceManager = new ServiceManager(); + + if (method_exists($serviceManager, 'build')) { + $this->markTestSkipped('this test is only vor zend-servicemanager v2'); + } + $creationOptions = [ 'route' => 'role' ]; @@ -43,7 +49,6 @@ public function testFactory() 'protection_policy' => GuardInterface::POLICY_ALLOW, ]); - $serviceManager = new ServiceManager(); $serviceManager->setService('ZfcRbac\Options\ModuleOptions', $options); $serviceManager->setService( 'ZfcRbac\Service\RoleService', @@ -58,4 +63,37 @@ public function testFactory() $this->assertInstanceOf('ZfcRbac\Guard\RouteGuard', $routeGuard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $routeGuard->getProtectionPolicy()); } + + public function testFactoryV3() + { + $serviceManager = new ServiceManager(); + + if (!method_exists($serviceManager, 'build')) { + $this->markTestSkipped('this test is only vor zend-servicemanager v3'); + } + + $creationOptions = [ + 'route' => 'role' + ]; + + $options = new ModuleOptions([ + 'identity_provider' => 'ZfcRbac\Identity\AuthenticationProvider', + 'guards' => [ + 'ZfcRbac\Guard\RouteGuard' => $creationOptions + ], + 'protection_policy' => GuardInterface::POLICY_ALLOW, + ]); + + $serviceManager->setService('ZfcRbac\Options\ModuleOptions', $options); + $serviceManager->setService( + 'ZfcRbac\Service\RoleService', + $this->getMock('ZfcRbac\Service\RoleService', [], [], '', false) + ); + + $factory = new RouteGuardFactory(); + $routeGuard = $factory($serviceManager, 'ZfcRbac\Guard\RouteGuard'); + + $this->assertInstanceOf('ZfcRbac\Guard\RouteGuard', $routeGuard); + $this->assertEquals(GuardInterface::POLICY_ALLOW, $routeGuard->getProtectionPolicy()); + } } diff --git a/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php b/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php index 178fc706..1328c8d2 100644 --- a/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/RoutePermissionsGuardFactoryTest.php @@ -31,6 +31,12 @@ class RoutePermissionsGuardFactoryTest extends \PHPUnit_Framework_TestCase { public function testFactory() { + $serviceManager = new ServiceManager(); + + if (method_exists($serviceManager, 'build')) { + $this->markTestSkipped('this test is only vor zend-servicemanager v2'); + } + $creationOptions = [ 'route' => 'role' ]; @@ -43,7 +49,6 @@ public function testFactory() 'protection_policy' => GuardInterface::POLICY_ALLOW, ]); - $serviceManager = new ServiceManager(); $serviceManager->setService('ZfcRbac\Options\ModuleOptions', $options); $serviceManager->setService( 'ZfcRbac\Service\AuthorizationService', @@ -58,4 +63,37 @@ public function testFactory() $this->assertInstanceOf('ZfcRbac\Guard\RoutePermissionsGuard', $routeGuard); $this->assertEquals(GuardInterface::POLICY_ALLOW, $routeGuard->getProtectionPolicy()); } + + public function testFactoryV3() + { + $serviceManager = new ServiceManager(); + + if (! method_exists($serviceManager, 'build')) { + $this->markTestSkipped('this test is only vor zend-servicemanager v3'); + } + + $creationOptions = [ + 'route' => 'role' + ]; + + $options = new ModuleOptions([ + 'identity_provider' => 'ZfcRbac\Identity\AuthenticationProvider', + 'guards' => [ + 'ZfcRbac\Guard\RoutePermissionsGuard' => $creationOptions + ], + 'protection_policy' => GuardInterface::POLICY_ALLOW, + ]); + + $serviceManager->setService('ZfcRbac\Options\ModuleOptions', $options); + $serviceManager->setService( + 'ZfcRbac\Service\AuthorizationService', + $this->getMock('ZfcRbac\Service\AuthorizationService', [], [], '', false) + ); + + $factory = new RoutePermissionsGuardFactory(); + $routeGuard = $factory($serviceManager, 'ZfcRbac\Guard\RoutePermissionsGuard'); + + $this->assertInstanceOf('ZfcRbac\Guard\RoutePermissionsGuard', $routeGuard); + $this->assertEquals(GuardInterface::POLICY_ALLOW, $routeGuard->getProtectionPolicy()); + } } From 03fca8e702031ca6ef08adfcb0ed96a6136bdcf1 Mon Sep 17 00:00:00 2001 From: prolic Date: Fri, 15 Jul 2016 14:24:18 +0800 Subject: [PATCH 20/29] remove test group --- .../ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php b/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php index 23074601..044d3c3f 100644 --- a/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php +++ b/tests/ZfcRbacTest/Factory/AuthorizationServiceDelegatorTest.php @@ -100,9 +100,6 @@ public function testAuthorizationServiceIsInjectedWithDelegator() $this->assertEquals($authorizationService, $decoratedInstance->getAuthorizationService()); } - /** - * @group by - */ public function testAuthorizationServiceIsInjectedWithDelegatorV3() { $serviceManager = ServiceManagerFactory::getServiceManager(); From 2d856b884fe73ae431f24c9783d42d85a221d30a Mon Sep 17 00:00:00 2001 From: prolic Date: Fri, 15 Jul 2016 14:27:20 +0800 Subject: [PATCH 21/29] apply php cs fixes --- src/ZfcRbac/Collector/RbacCollector.php | 1 - src/ZfcRbac/Factory/AssertionPluginManagerFactory.php | 3 --- src/ZfcRbac/Factory/HasRoleViewHelperFactory.php | 3 --- src/ZfcRbac/Factory/IsGrantedPluginFactory.php | 3 --- src/ZfcRbac/Factory/IsGrantedViewHelperFactory.php | 3 --- src/ZfcRbac/Guard/RouteGuard.php | 1 - src/ZfcRbac/Identity/AuthenticationIdentityProvider.php | 1 - src/ZfcRbac/Initializer/AuthorizationServiceInitializer.php | 1 - src/ZfcRbac/Service/RoleService.php | 2 -- tests/ZfcRbacTest/Asset/MockRoleWithPermissionMethod.php | 2 +- tests/ZfcRbacTest/Asset/MockRoleWithPermissionProperty.php | 2 +- tests/ZfcRbacTest/Collector/RbacCollectorTest.php | 3 --- tests/ZfcRbacTest/Factory/ModuleOptionsFactoryTest.php | 1 - .../Factory/ObjectRepositoryRoleProviderFactoryTest.php | 1 - tests/ZfcRbacTest/Factory/RedirectStrategyFactoryTest.php | 1 - tests/ZfcRbacTest/Factory/UnauthorizedStrategyFactoryTest.php | 1 - tests/ZfcRbacTest/Guard/ProtectionPolicyTraitTest.php | 1 + tests/ZfcRbacTest/Initializer/AuthorizationAwareFake.php | 2 +- tests/ZfcRbacTest/Role/RoleProviderPluginManagerTest.php | 1 - tests/ZfcRbacTest/Service/AuthorizationServiceTest.php | 3 +-- tests/ZfcRbacTest/View/Strategy/RedirectStrategyTest.php | 2 +- 21 files changed, 6 insertions(+), 32 deletions(-) diff --git a/src/ZfcRbac/Collector/RbacCollector.php b/src/ZfcRbac/Collector/RbacCollector.php index 918e82e5..85b74168 100644 --- a/src/ZfcRbac/Collector/RbacCollector.php +++ b/src/ZfcRbac/Collector/RbacCollector.php @@ -232,6 +232,5 @@ public function unserialize($serialized) $this->collectedRoles = $collection['roles']; $this->collectedPermissions = $collection['permissions']; $this->collectedOptions = $collection['options']; - } } diff --git a/src/ZfcRbac/Factory/AssertionPluginManagerFactory.php b/src/ZfcRbac/Factory/AssertionPluginManagerFactory.php index 3f120401..d757e053 100644 --- a/src/ZfcRbac/Factory/AssertionPluginManagerFactory.php +++ b/src/ZfcRbac/Factory/AssertionPluginManagerFactory.php @@ -19,10 +19,7 @@ namespace ZfcRbac\Factory; use Interop\Container\ContainerInterface; -use Interop\Container\Exception\ContainerException; use Zend\ServiceManager\Config; -use Zend\ServiceManager\Exception\ServiceNotCreatedException; -use Zend\ServiceManager\Exception\ServiceNotFoundException; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Assertion\AssertionPluginManager; diff --git a/src/ZfcRbac/Factory/HasRoleViewHelperFactory.php b/src/ZfcRbac/Factory/HasRoleViewHelperFactory.php index b2556a83..5c2e1b4e 100644 --- a/src/ZfcRbac/Factory/HasRoleViewHelperFactory.php +++ b/src/ZfcRbac/Factory/HasRoleViewHelperFactory.php @@ -19,9 +19,6 @@ namespace ZfcRbac\Factory; use Interop\Container\ContainerInterface; -use Interop\Container\Exception\ContainerException; -use Zend\ServiceManager\Exception\ServiceNotCreatedException; -use Zend\ServiceManager\Exception\ServiceNotFoundException; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Service\RoleService; diff --git a/src/ZfcRbac/Factory/IsGrantedPluginFactory.php b/src/ZfcRbac/Factory/IsGrantedPluginFactory.php index 2b4aacd5..b2edf41e 100644 --- a/src/ZfcRbac/Factory/IsGrantedPluginFactory.php +++ b/src/ZfcRbac/Factory/IsGrantedPluginFactory.php @@ -19,9 +19,6 @@ namespace ZfcRbac\Factory; use Interop\Container\ContainerInterface; -use Interop\Container\Exception\ContainerException; -use Zend\ServiceManager\Exception\ServiceNotCreatedException; -use Zend\ServiceManager\Exception\ServiceNotFoundException; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Mvc\Controller\Plugin\IsGranted; diff --git a/src/ZfcRbac/Factory/IsGrantedViewHelperFactory.php b/src/ZfcRbac/Factory/IsGrantedViewHelperFactory.php index c5301142..c4119f1e 100644 --- a/src/ZfcRbac/Factory/IsGrantedViewHelperFactory.php +++ b/src/ZfcRbac/Factory/IsGrantedViewHelperFactory.php @@ -19,9 +19,6 @@ namespace ZfcRbac\Factory; use Interop\Container\ContainerInterface; -use Interop\Container\Exception\ContainerException; -use Zend\ServiceManager\Exception\ServiceNotCreatedException; -use Zend\ServiceManager\Exception\ServiceNotFoundException; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\View\Helper\IsGranted; diff --git a/src/ZfcRbac/Guard/RouteGuard.php b/src/ZfcRbac/Guard/RouteGuard.php index a8bdd5b3..69466dce 100644 --- a/src/ZfcRbac/Guard/RouteGuard.php +++ b/src/ZfcRbac/Guard/RouteGuard.php @@ -19,7 +19,6 @@ namespace ZfcRbac\Guard; use Zend\Mvc\MvcEvent; -use ZfcRbac\Exception; use ZfcRbac\Service\RoleService; /** diff --git a/src/ZfcRbac/Identity/AuthenticationIdentityProvider.php b/src/ZfcRbac/Identity/AuthenticationIdentityProvider.php index 5f469630..1673189b 100644 --- a/src/ZfcRbac/Identity/AuthenticationIdentityProvider.php +++ b/src/ZfcRbac/Identity/AuthenticationIdentityProvider.php @@ -19,7 +19,6 @@ namespace ZfcRbac\Identity; use Zend\Authentication\AuthenticationServiceInterface; -use ZfcRbac\Exception; /** * This provider uses the Zend authentication service to fetch the identity diff --git a/src/ZfcRbac/Initializer/AuthorizationServiceInitializer.php b/src/ZfcRbac/Initializer/AuthorizationServiceInitializer.php index 6b7c618d..778ce174 100644 --- a/src/ZfcRbac/Initializer/AuthorizationServiceInitializer.php +++ b/src/ZfcRbac/Initializer/AuthorizationServiceInitializer.php @@ -19,7 +19,6 @@ namespace ZfcRbac\Initializer; use Interop\Container\ContainerInterface; -use Zend\ServiceManager\AbstractPluginManager; use Zend\ServiceManager\InitializerInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Service\AuthorizationServiceAwareInterface; diff --git a/src/ZfcRbac/Service/RoleService.php b/src/ZfcRbac/Service/RoleService.php index 0a23a555..8aef9b64 100644 --- a/src/ZfcRbac/Service/RoleService.php +++ b/src/ZfcRbac/Service/RoleService.php @@ -18,9 +18,7 @@ namespace ZfcRbac\Service; -use Rbac\Role\HierarchicalRoleInterface; use Rbac\Role\RoleInterface; -use RecursiveIteratorIterator; use Traversable; use ZfcRbac\Exception; use ZfcRbac\Identity\IdentityInterface; diff --git a/tests/ZfcRbacTest/Asset/MockRoleWithPermissionMethod.php b/tests/ZfcRbacTest/Asset/MockRoleWithPermissionMethod.php index 376d352a..199e7458 100644 --- a/tests/ZfcRbacTest/Asset/MockRoleWithPermissionMethod.php +++ b/tests/ZfcRbacTest/Asset/MockRoleWithPermissionMethod.php @@ -19,4 +19,4 @@ public function hasPermission($permission) { return false; } -} \ No newline at end of file +} diff --git a/tests/ZfcRbacTest/Asset/MockRoleWithPermissionProperty.php b/tests/ZfcRbacTest/Asset/MockRoleWithPermissionProperty.php index 44522bbb..a4e25f70 100644 --- a/tests/ZfcRbacTest/Asset/MockRoleWithPermissionProperty.php +++ b/tests/ZfcRbacTest/Asset/MockRoleWithPermissionProperty.php @@ -16,4 +16,4 @@ public function hasPermission($permission) { return false; } -} \ No newline at end of file +} diff --git a/tests/ZfcRbacTest/Collector/RbacCollectorTest.php b/tests/ZfcRbacTest/Collector/RbacCollectorTest.php index 47aa5b21..42434471 100644 --- a/tests/ZfcRbacTest/Collector/RbacCollectorTest.php +++ b/tests/ZfcRbacTest/Collector/RbacCollectorTest.php @@ -20,13 +20,11 @@ use Rbac\Role\RoleInterface; use Zend\Mvc\MvcEvent; -use Zend\Permissions\Rbac\Rbac; use Zend\Permissions\Rbac\Role; use ZfcRbac\Collector\RbacCollector; use ZfcRbac\Guard\GuardInterface; use ZfcRbac\Options\ModuleOptions; use ZfcRbac\Role\InMemoryRoleProvider; -use ZfcRbac\Role\RoleProviderInterface; use ZfcRbac\Service\RoleService; use Rbac\Traversal\Strategy\RecursiveRoleIteratorStrategy; use ZfcRbacTest\Asset\MockRoleWithPermissionMethod; @@ -89,7 +87,6 @@ public function testUnserializeThrowsInvalidArgumentException() $serialized = serialize($unserialized); $collector->unserialize($serialized); - } diff --git a/tests/ZfcRbacTest/Factory/ModuleOptionsFactoryTest.php b/tests/ZfcRbacTest/Factory/ModuleOptionsFactoryTest.php index 4e2e5bf3..10b8baa4 100644 --- a/tests/ZfcRbacTest/Factory/ModuleOptionsFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/ModuleOptionsFactoryTest.php @@ -39,4 +39,3 @@ public function testFactory() $this->assertInstanceOf('ZfcRbac\Options\ModuleOptions', $options); } } - \ No newline at end of file diff --git a/tests/ZfcRbacTest/Factory/ObjectRepositoryRoleProviderFactoryTest.php b/tests/ZfcRbacTest/Factory/ObjectRepositoryRoleProviderFactoryTest.php index 6888e967..4093970c 100644 --- a/tests/ZfcRbacTest/Factory/ObjectRepositoryRoleProviderFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/ObjectRepositoryRoleProviderFactoryTest.php @@ -104,7 +104,6 @@ public function testThrowExceptionIfNoObjectManagerNorObjectRepositoryIsSet() 'role_name_property' => 'name' ]); } catch (ServiceNotCreatedException $smException) { - while ($e = $smException->getPrevious()) { if ($e instanceof RuntimeException) { return true; diff --git a/tests/ZfcRbacTest/Factory/RedirectStrategyFactoryTest.php b/tests/ZfcRbacTest/Factory/RedirectStrategyFactoryTest.php index 701da5a8..b871534b 100644 --- a/tests/ZfcRbacTest/Factory/RedirectStrategyFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/RedirectStrategyFactoryTest.php @@ -53,4 +53,3 @@ public function testFactory() $this->assertInstanceOf('ZfcRbac\View\Strategy\RedirectStrategy', $redirectStrategy); } } - \ No newline at end of file diff --git a/tests/ZfcRbacTest/Factory/UnauthorizedStrategyFactoryTest.php b/tests/ZfcRbacTest/Factory/UnauthorizedStrategyFactoryTest.php index fbc3793e..564ebf5f 100644 --- a/tests/ZfcRbacTest/Factory/UnauthorizedStrategyFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/UnauthorizedStrategyFactoryTest.php @@ -45,4 +45,3 @@ public function testFactory() $this->assertInstanceOf('ZfcRbac\View\Strategy\UnauthorizedStrategy', $unauthorizedStrategy); } } - \ No newline at end of file diff --git a/tests/ZfcRbacTest/Guard/ProtectionPolicyTraitTest.php b/tests/ZfcRbacTest/Guard/ProtectionPolicyTraitTest.php index 800f917e..a8ab1f50 100644 --- a/tests/ZfcRbacTest/Guard/ProtectionPolicyTraitTest.php +++ b/tests/ZfcRbacTest/Guard/ProtectionPolicyTraitTest.php @@ -17,6 +17,7 @@ */ namespace ZfcRbacTest\Guard; + use ZfcRbac\Guard\GuardInterface; /** diff --git a/tests/ZfcRbacTest/Initializer/AuthorizationAwareFake.php b/tests/ZfcRbacTest/Initializer/AuthorizationAwareFake.php index 59757a47..bdc19c03 100644 --- a/tests/ZfcRbacTest/Initializer/AuthorizationAwareFake.php +++ b/tests/ZfcRbacTest/Initializer/AuthorizationAwareFake.php @@ -27,6 +27,6 @@ */ class AuthorizationAwareFake implements AuthorizationServiceAwareInterface { - + use \ZfcRbac\Service\AuthorizationServiceAwareTrait; } diff --git a/tests/ZfcRbacTest/Role/RoleProviderPluginManagerTest.php b/tests/ZfcRbacTest/Role/RoleProviderPluginManagerTest.php index 452bde3a..36d7c69b 100644 --- a/tests/ZfcRbacTest/Role/RoleProviderPluginManagerTest.php +++ b/tests/ZfcRbacTest/Role/RoleProviderPluginManagerTest.php @@ -42,4 +42,3 @@ public function testValidationOfPluginFailsIfRoleProviderInterfaceIsNotImplement $pluginManager->get('stdClass', []); } } - \ No newline at end of file diff --git a/tests/ZfcRbacTest/Service/AuthorizationServiceTest.php b/tests/ZfcRbacTest/Service/AuthorizationServiceTest.php index bddbbf84..d8721dd4 100644 --- a/tests/ZfcRbacTest/Service/AuthorizationServiceTest.php +++ b/tests/ZfcRbacTest/Service/AuthorizationServiceTest.php @@ -26,7 +26,6 @@ use ZfcRbac\Service\RoleService; use ZfcRbacTest\Asset\SimpleAssertion; use ZfcRbac\Assertion\AssertionPluginManager; -use Zend\ServiceManager\Config; /** * @covers \ZfcRbac\Service\AuthorizationService @@ -103,7 +102,7 @@ public function grantedProvider() /** * @dataProvider grantedProvider */ - public function testGranted($role, $permission, $context, $isGranted, $assertions = array()) + public function testGranted($role, $permission, $context, $isGranted, $assertions = []) { $roleConfig = [ 'admin' => [ diff --git a/tests/ZfcRbacTest/View/Strategy/RedirectStrategyTest.php b/tests/ZfcRbacTest/View/Strategy/RedirectStrategyTest.php index b80ec508..d3c2ef06 100644 --- a/tests/ZfcRbacTest/View/Strategy/RedirectStrategyTest.php +++ b/tests/ZfcRbacTest/View/Strategy/RedirectStrategyTest.php @@ -121,7 +121,7 @@ public function testCanRedirectWhenConnected() $this->assertEquals(302, $mvcEvent->getResponse()->getStatusCode()); $this->assertEquals('/home', $mvcEvent->getResponse()->getHeaders()->get('Location')->getFieldValue()); } - + public function testWontRedirectWhenConnectedAndOptionDisabled() { $response = new HttpResponse(); From 284ba810620d354d82af6e2b93fa9016285cfed2 Mon Sep 17 00:00:00 2001 From: prolic Date: Fri, 15 Jul 2016 14:36:22 +0800 Subject: [PATCH 22/29] some last cleanups --- config/module.config.php | 5 +---- src/ZfcRbac/Guard/AbstractGuard.php | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/config/module.config.php b/config/module.config.php index 17783041..dcf52fd8 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -18,10 +18,6 @@ return [ 'service_manager' => [ - 'invokables' => [ - 'ZfcRbac\Collector\RbacCollector' => 'ZfcRbac\Collector\RbacCollector', - ], - 'factories' => [ /* Factories that do not map to a class */ 'ZfcRbac\Guards' => 'ZfcRbac\Factory\GuardsFactory', @@ -29,6 +25,7 @@ /* Factories that map to a class */ 'Rbac\Rbac' => 'ZfcRbac\Factory\RbacFactory', 'ZfcRbac\Assertion\AssertionPluginManager' => 'ZfcRbac\Factory\AssertionPluginManagerFactory', + 'ZfcRbac\Collector\RbacCollector' => \Zend\ServiceManager\Factory\InvokableFactory::class, 'ZfcRbac\Guard\GuardPluginManager' => 'ZfcRbac\Factory\GuardPluginManagerFactory', 'ZfcRbac\Identity\AuthenticationIdentityProvider' => 'ZfcRbac\Factory\AuthenticationIdentityProviderFactory', 'ZfcRbac\Options\ModuleOptions' => 'ZfcRbac\Factory\ModuleOptionsFactory', diff --git a/src/ZfcRbac/Guard/AbstractGuard.php b/src/ZfcRbac/Guard/AbstractGuard.php index 33796c4a..cdeecb2a 100644 --- a/src/ZfcRbac/Guard/AbstractGuard.php +++ b/src/ZfcRbac/Guard/AbstractGuard.php @@ -46,7 +46,7 @@ abstract class AbstractGuard implements GuardInterface /** * {@inheritDoc} */ - public function attach(EventManagerInterface $events, $priority = self::EVENT_PRIORITY) + public function attach(EventManagerInterface $events, $priority = AbstractGuard::EVENT_PRIORITY) { $this->listeners[] = $events->attach(static::EVENT_NAME, [$this, 'onResult'], $priority); } From 78943924b521a2dbd72180ef071b226cdaac8cf5 Mon Sep 17 00:00:00 2001 From: Sascha-Oliver Prolic Date: Fri, 15 Jul 2016 15:50:42 +0800 Subject: [PATCH 23/29] update doctrine orm module in composer json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 7575f861..34336f03 100644 --- a/composer.json +++ b/composer.json @@ -47,7 +47,7 @@ "satooshi/php-coveralls": "~0.6", "doctrine/common": "~2.4", "doctrine/doctrine-module": "~1.1", - "doctrine/doctrine-orm-module": "~0.11" + "doctrine/doctrine-orm-module": "^1.0" }, "suggest": { "zendframework/zend-developer-tools": "if you want to show information about the roles", From 4dc3eb097b4a009c841d82aa3a1fb5b2f8065959 Mon Sep 17 00:00:00 2001 From: Sascha-Oliver Prolic Date: Fri, 15 Jul 2016 15:52:04 +0800 Subject: [PATCH 24/29] update AuthorizationServiceInitializer --- src/ZfcRbac/Initializer/AuthorizationServiceInitializer.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ZfcRbac/Initializer/AuthorizationServiceInitializer.php b/src/ZfcRbac/Initializer/AuthorizationServiceInitializer.php index 778ce174..7e0b60f6 100644 --- a/src/ZfcRbac/Initializer/AuthorizationServiceInitializer.php +++ b/src/ZfcRbac/Initializer/AuthorizationServiceInitializer.php @@ -48,6 +48,10 @@ public function __invoke(ContainerInterface $container, $instance) */ public function initialize($instance, ServiceLocatorInterface $serviceLocator) { + if ($serviceLocator instanceof AbstractPluginManager) { + $serviceLocator = $serviceLocator->getServiceLocator(); + } + $this($serviceLocator, $instance); } } From 9e97ce306f4b3d1fbdcc3a3db416523446e59e95 Mon Sep 17 00:00:00 2001 From: Atanas Vasilev Date: Fri, 22 Jul 2016 15:05:03 +0300 Subject: [PATCH 25/29] Fixes #340 - EventManager::trigger() causes an event instance of the wrong type to be passed to registered listeners --- src/ZfcRbac/Guard/AbstractGuard.php | 8 +++----- tests/ZfcRbacTest/Guard/ControllerGuardTest.php | 7 ++++--- .../Guard/ControllerPermissionsGuardTest.php | 7 ++++--- tests/ZfcRbacTest/Guard/RouteGuardTest.php | 7 ++++--- tests/ZfcRbacTest/Guard/RoutePermissionsGuardTest.php | 10 ++++++---- .../ZfcRbacTest/View/Strategy/RedirectStrategyTest.php | 2 +- 6 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/ZfcRbac/Guard/AbstractGuard.php b/src/ZfcRbac/Guard/AbstractGuard.php index cdeecb2a..b015ae9c 100644 --- a/src/ZfcRbac/Guard/AbstractGuard.php +++ b/src/ZfcRbac/Guard/AbstractGuard.php @@ -69,10 +69,8 @@ public function onResult(MvcEvent $event) )); $event->stopPropagation(true); - - $application = $event->getApplication(); - $eventManager = $application->getEventManager(); - - $eventManager->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $event); + $event->setName(MvcEvent::EVENT_DISPATCH_ERROR); + $target = $event->getTarget(); + $target->getEventManager()->triggerEvent($event); } } diff --git a/tests/ZfcRbacTest/Guard/ControllerGuardTest.php b/tests/ZfcRbacTest/Guard/ControllerGuardTest.php index 3d3fa5ae..fe153f9b 100644 --- a/tests/ZfcRbacTest/Guard/ControllerGuardTest.php +++ b/tests/ZfcRbacTest/Guard/ControllerGuardTest.php @@ -19,7 +19,7 @@ namespace ZfcRbacTest\Guard; use Zend\Mvc\MvcEvent; -use Zend\Mvc\Router\RouteMatch; +use Zend\Router\RouteMatch; use ZfcRbac\Guard\ControllerGuard; use ZfcRbac\Guard\GuardInterface; use ZfcRbac\Role\InMemoryRoleProvider; @@ -489,14 +489,15 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() ->will($this->returnValue($eventManager)); $eventManager->expects($this->once()) - ->method('trigger') - ->with(MvcEvent::EVENT_DISPATCH_ERROR); + ->method('triggerEvent') + ->with($event); $routeMatch->setParam('controller', 'MyController'); $routeMatch->setParam('action', 'delete'); $event->setRouteMatch($routeMatch); $event->setApplication($application); + $event->setTarget($application); $identityProvider = $this->getMock('ZfcRbac\Identity\IdentityProviderInterface'); $identityProvider->expects($this->any()) diff --git a/tests/ZfcRbacTest/Guard/ControllerPermissionsGuardTest.php b/tests/ZfcRbacTest/Guard/ControllerPermissionsGuardTest.php index 8b7f9cb5..ff7184f7 100644 --- a/tests/ZfcRbacTest/Guard/ControllerPermissionsGuardTest.php +++ b/tests/ZfcRbacTest/Guard/ControllerPermissionsGuardTest.php @@ -19,7 +19,7 @@ namespace ZfcRbacTest\Guard; use Zend\Mvc\MvcEvent; -use Zend\Mvc\Router\RouteMatch; +use Zend\Router\RouteMatch; use ZfcRbac\Guard\ControllerGuard; use ZfcRbac\Guard\ControllerPermissionsGuard; use ZfcRbac\Guard\GuardInterface; @@ -476,14 +476,15 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() ->will($this->returnValue($eventManager)); $eventManager->expects($this->once()) - ->method('trigger') - ->with(MvcEvent::EVENT_DISPATCH_ERROR); + ->method('triggerEvent') + ->with($event); $routeMatch->setParam('controller', 'MyController'); $routeMatch->setParam('action', 'delete'); $event->setRouteMatch($routeMatch); $event->setApplication($application); + $event->setTarget($application); $identityProvider = $this->getMock('ZfcRbac\Identity\IdentityProviderInterface'); $identityProvider->expects($this->any()) diff --git a/tests/ZfcRbacTest/Guard/RouteGuardTest.php b/tests/ZfcRbacTest/Guard/RouteGuardTest.php index 28442509..9002752b 100644 --- a/tests/ZfcRbacTest/Guard/RouteGuardTest.php +++ b/tests/ZfcRbacTest/Guard/RouteGuardTest.php @@ -19,7 +19,7 @@ namespace ZfcRbacTest\Guard; use Zend\Mvc\MvcEvent; -use Zend\Mvc\Router\RouteMatch; +use Zend\Router\RouteMatch; use ZfcRbac\Guard\ControllerGuard; use ZfcRbac\Guard\GuardInterface; use ZfcRbac\Guard\RouteGuard; @@ -439,12 +439,13 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() ->will($this->returnValue($eventManager)); $eventManager->expects($this->once()) - ->method('trigger') - ->with(MvcEvent::EVENT_DISPATCH_ERROR); + ->method('triggerEvent') + ->with($event); $routeMatch->setMatchedRouteName('adminRoute'); $event->setRouteMatch($routeMatch); $event->setApplication($application); + $event->setTarget($application); $identityProvider = $this->getMock('ZfcRbac\Identity\IdentityProviderInterface'); $identityProvider->expects($this->any()) diff --git a/tests/ZfcRbacTest/Guard/RoutePermissionsGuardTest.php b/tests/ZfcRbacTest/Guard/RoutePermissionsGuardTest.php index 4420546e..506178c8 100644 --- a/tests/ZfcRbacTest/Guard/RoutePermissionsGuardTest.php +++ b/tests/ZfcRbacTest/Guard/RoutePermissionsGuardTest.php @@ -18,7 +18,7 @@ namespace ZfcRbacTest\Guard; use Zend\Mvc\MvcEvent; -use Zend\Mvc\Router\RouteMatch; +use Zend\Router\RouteMatch; use ZfcRbac\Guard\ControllerGuard; use ZfcRbac\Guard\GuardInterface; use ZfcRbac\Guard\RouteGuard; @@ -410,9 +410,6 @@ public function testProperlyFillEventOnAuthorization() public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() { $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface'); - $eventManager->expects($this->once()) - ->method('trigger') - ->with(MvcEvent::EVENT_DISPATCH_ERROR); $application = $this->getMock('Zend\Mvc\Application', [], [], '', false); $application->expects($this->once()) @@ -425,6 +422,11 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() $event = new MvcEvent(); $event->setRouteMatch($routeMatch); $event->setApplication($application); + $event->setTarget($application); + + $eventManager->expects($this->once()) + ->method('triggerEvent') + ->with($event); $authorizationService = $this->getMock('ZfcRbac\Service\AuthorizationServiceInterface', [], [], '', false); $authorizationService->expects($this->once()) diff --git a/tests/ZfcRbacTest/View/Strategy/RedirectStrategyTest.php b/tests/ZfcRbacTest/View/Strategy/RedirectStrategyTest.php index d3c2ef06..84482da5 100644 --- a/tests/ZfcRbacTest/View/Strategy/RedirectStrategyTest.php +++ b/tests/ZfcRbacTest/View/Strategy/RedirectStrategyTest.php @@ -22,7 +22,7 @@ use Zend\Http\Request as HttpRequest; use Zend\Http\Response as HttpResponse; use Zend\Mvc\MvcEvent; -use Zend\Mvc\Router\Http\TreeRouteStack; +use Zend\Router\Http\TreeRouteStack; use ZfcRbac\Exception\UnauthorizedException; use ZfcRbac\Options\RedirectStrategyOptions; use ZfcRbac\View\Strategy\RedirectStrategy; From 1c8dc2539dc9b1f447ecfcb4f37144b88e687a3a Mon Sep 17 00:00:00 2001 From: Atanas Vasilev Date: Mon, 25 Jul 2016 11:07:36 +0300 Subject: [PATCH 26/29] make test modifications related to #340, zf2- and zf3-compatible --- .../ZfcRbacTest/Guard/ControllerGuardTest.php | 29 ++++++++++++------- .../Guard/ControllerPermissionsGuardTest.php | 29 ++++++++++++------- tests/ZfcRbacTest/Guard/RouteGuardTest.php | 13 +++++++-- .../Guard/RoutePermissionsGuardTest.php | 13 +++++++-- .../View/Strategy/RedirectStrategyTest.php | 2 +- 5 files changed, 59 insertions(+), 27 deletions(-) diff --git a/tests/ZfcRbacTest/Guard/ControllerGuardTest.php b/tests/ZfcRbacTest/Guard/ControllerGuardTest.php index fe153f9b..304f37e6 100644 --- a/tests/ZfcRbacTest/Guard/ControllerGuardTest.php +++ b/tests/ZfcRbacTest/Guard/ControllerGuardTest.php @@ -19,6 +19,7 @@ namespace ZfcRbacTest\Guard; use Zend\Mvc\MvcEvent; +use Zend\Mvc\Router\RouteMatch as V2RouteMatch; use Zend\Router\RouteMatch; use ZfcRbac\Guard\ControllerGuard; use ZfcRbac\Guard\GuardInterface; @@ -410,9 +411,10 @@ public function testControllerGranted( $protectionPolicy ) { $event = new MvcEvent(); - $routeMatch = new RouteMatch([]); - $routeMatch->setParam('controller', $controller); - $routeMatch->setParam('action', $action); + $routeMatch = $this->createRouteMatch([ + 'controller' => $controller, + 'action' => $action, + ]); $event->setRouteMatch($routeMatch); @@ -436,7 +438,10 @@ public function testControllerGranted( public function testProperlyFillEventOnAuthorization() { $event = new MvcEvent(); - $routeMatch = new RouteMatch([]); + $routeMatch = $this->createRouteMatch([ + 'controller' => 'MyController', + 'action' => 'edit', + ]); $application = $this->getMock('Zend\Mvc\Application', [], [], '', false); $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface'); @@ -445,8 +450,6 @@ public function testProperlyFillEventOnAuthorization() ->method('getEventManager') ->will($this->returnValue($eventManager)); - $routeMatch->setParam('controller', 'MyController'); - $routeMatch->setParam('action', 'edit'); $event->setRouteMatch($routeMatch); $event->setApplication($application); @@ -479,7 +482,10 @@ public function testProperlyFillEventOnAuthorization() public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() { $event = new MvcEvent(); - $routeMatch = new RouteMatch([]); + $routeMatch = $this->createRouteMatch([ + 'controller' => 'MyController', + 'action' => 'delete', + ]); $application = $this->getMock('Zend\Mvc\Application', [], [], '', false); $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface'); @@ -492,9 +498,6 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() ->method('triggerEvent') ->with($event); - $routeMatch->setParam('controller', 'MyController'); - $routeMatch->setParam('action', 'delete'); - $event->setRouteMatch($routeMatch); $event->setApplication($application); $event->setTarget($application); @@ -522,4 +525,10 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() $this->assertEquals(ControllerGuard::GUARD_UNAUTHORIZED, $event->getError()); $this->assertInstanceOf('ZfcRbac\Exception\UnauthorizedException', $event->getParam('exception')); } + + public function createRouteMatch(array $params = []) + { + $class = class_exists(V2RouteMatch::class) ? V2RouteMatch::class : RouteMatch::class; + return new $class($params); + } } diff --git a/tests/ZfcRbacTest/Guard/ControllerPermissionsGuardTest.php b/tests/ZfcRbacTest/Guard/ControllerPermissionsGuardTest.php index ff7184f7..ba5cb4ed 100644 --- a/tests/ZfcRbacTest/Guard/ControllerPermissionsGuardTest.php +++ b/tests/ZfcRbacTest/Guard/ControllerPermissionsGuardTest.php @@ -19,6 +19,7 @@ namespace ZfcRbacTest\Guard; use Zend\Mvc\MvcEvent; +use Zend\Mvc\Router\RouteMatch as V2RouteMatch; use Zend\Router\RouteMatch; use ZfcRbac\Guard\ControllerGuard; use ZfcRbac\Guard\ControllerPermissionsGuard; @@ -400,9 +401,10 @@ public function testControllerGranted( $isGranted, $protectionPolicy ) { - $routeMatch = new RouteMatch([]); - $routeMatch->setParam('controller', $controller); - $routeMatch->setParam('action', $action); + $routeMatch = $this->createRouteMatch([ + 'controller' => $controller, + 'action' => $action, + ]); $authorizationService = $this->getMockAuthorizationService(); $authorizationService->expects($this->any()) @@ -421,7 +423,10 @@ public function testControllerGranted( public function testProperlyFillEventOnAuthorization() { $event = new MvcEvent(); - $routeMatch = new RouteMatch([]); + $routeMatch = $this->createRouteMatch([ + 'controller' => 'MyController', + 'action' => 'edit', + ]); $application = $this->getMock('Zend\Mvc\Application', [], [], '', false); $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface'); @@ -430,8 +435,6 @@ public function testProperlyFillEventOnAuthorization() ->method('getEventManager') ->will($this->returnValue($eventManager)); - $routeMatch->setParam('controller', 'MyController'); - $routeMatch->setParam('action', 'edit'); $event->setRouteMatch($routeMatch); $event->setApplication($application); @@ -466,7 +469,10 @@ public function testProperlyFillEventOnAuthorization() public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() { $event = new MvcEvent(); - $routeMatch = new RouteMatch([]); + $routeMatch = $this->createRouteMatch([ + 'controller' => 'MyController', + 'action' => 'delete', + ]); $application = $this->getMock('Zend\Mvc\Application', [], [], '', false); $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface'); @@ -479,9 +485,6 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() ->method('triggerEvent') ->with($event); - $routeMatch->setParam('controller', 'MyController'); - $routeMatch->setParam('action', 'delete'); - $event->setRouteMatch($routeMatch); $event->setApplication($application); $event->setTarget($application); @@ -511,4 +514,10 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() $this->assertEquals(ControllerGuard::GUARD_UNAUTHORIZED, $event->getError()); $this->assertInstanceOf('ZfcRbac\Exception\UnauthorizedException', $event->getParam('exception')); } + + public function createRouteMatch(array $params = []) + { + $class = class_exists(V2RouteMatch::class) ? V2RouteMatch::class : RouteMatch::class; + return new $class($params); + } } diff --git a/tests/ZfcRbacTest/Guard/RouteGuardTest.php b/tests/ZfcRbacTest/Guard/RouteGuardTest.php index 9002752b..da671158 100644 --- a/tests/ZfcRbacTest/Guard/RouteGuardTest.php +++ b/tests/ZfcRbacTest/Guard/RouteGuardTest.php @@ -19,6 +19,7 @@ namespace ZfcRbacTest\Guard; use Zend\Mvc\MvcEvent; +use Zend\Mvc\Router\RouteMatch as V2RouteMatch; use Zend\Router\RouteMatch; use ZfcRbac\Guard\ControllerGuard; use ZfcRbac\Guard\GuardInterface; @@ -367,7 +368,7 @@ public function testRouteGranted( $protectionPolicy ) { $event = new MvcEvent(); - $routeMatch = new RouteMatch([]); + $routeMatch = $this->createRouteMatch(); $routeMatch->setMatchedRouteName($matchedRouteName); $event->setRouteMatch($routeMatch); @@ -392,7 +393,7 @@ public function testRouteGranted( public function testProperlyFillEventOnAuthorization() { $event = new MvcEvent(); - $routeMatch = new RouteMatch([]); + $routeMatch = $this->createRouteMatch(); $application = $this->getMock('Zend\Mvc\Application', [], [], '', false); $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface'); @@ -429,7 +430,7 @@ public function testProperlyFillEventOnAuthorization() public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() { $event = new MvcEvent(); - $routeMatch = new RouteMatch([]); + $routeMatch = $this->createRouteMatch(); $application = $this->getMock('Zend\Mvc\Application', [], [], '', false); $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface'); @@ -465,4 +466,10 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() $this->assertEquals(RouteGuard::GUARD_UNAUTHORIZED, $event->getError()); $this->assertInstanceOf('ZfcRbac\Exception\UnauthorizedException', $event->getParam('exception')); } + + public function createRouteMatch(array $params = []) + { + $class = class_exists(V2RouteMatch::class) ? V2RouteMatch::class : RouteMatch::class; + return new $class($params); + } } diff --git a/tests/ZfcRbacTest/Guard/RoutePermissionsGuardTest.php b/tests/ZfcRbacTest/Guard/RoutePermissionsGuardTest.php index 506178c8..a616eccc 100644 --- a/tests/ZfcRbacTest/Guard/RoutePermissionsGuardTest.php +++ b/tests/ZfcRbacTest/Guard/RoutePermissionsGuardTest.php @@ -18,6 +18,7 @@ namespace ZfcRbacTest\Guard; use Zend\Mvc\MvcEvent; +use Zend\Mvc\Router\RouteMatch as V2RouteMatch; use Zend\Router\RouteMatch; use ZfcRbac\Guard\ControllerGuard; use ZfcRbac\Guard\GuardInterface; @@ -359,7 +360,7 @@ public function testRoutePermissionGranted( $isGranted, $protectionPolicy ) { - $routeMatch = new RouteMatch([]); + $routeMatch = $this->createRouteMatch(); $routeMatch->setMatchedRouteName($matchedRouteName); $event = new MvcEvent(); @@ -385,7 +386,7 @@ public function testProperlyFillEventOnAuthorization() ->method('getEventManager') ->will($this->returnValue($eventManager)); - $routeMatch = new RouteMatch([]); + $routeMatch = $this->createRouteMatch(); $routeMatch->setMatchedRouteName('adminRoute'); $event = new MvcEvent(); @@ -416,7 +417,7 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() ->method('getEventManager') ->will($this->returnValue($eventManager)); - $routeMatch = new RouteMatch([]); + $routeMatch = $this->createRouteMatch(); $routeMatch->setMatchedRouteName('adminRoute'); $event = new MvcEvent(); @@ -443,4 +444,10 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() $this->assertEquals(RouteGuard::GUARD_UNAUTHORIZED, $event->getError()); $this->assertInstanceOf('ZfcRbac\Exception\UnauthorizedException', $event->getParam('exception')); } + + public function createRouteMatch(array $params = []) + { + $class = class_exists(V2RouteMatch::class) ? V2RouteMatch::class : RouteMatch::class; + return new $class($params); + } } diff --git a/tests/ZfcRbacTest/View/Strategy/RedirectStrategyTest.php b/tests/ZfcRbacTest/View/Strategy/RedirectStrategyTest.php index 84482da5..d3c2ef06 100644 --- a/tests/ZfcRbacTest/View/Strategy/RedirectStrategyTest.php +++ b/tests/ZfcRbacTest/View/Strategy/RedirectStrategyTest.php @@ -22,7 +22,7 @@ use Zend\Http\Request as HttpRequest; use Zend\Http\Response as HttpResponse; use Zend\Mvc\MvcEvent; -use Zend\Router\Http\TreeRouteStack; +use Zend\Mvc\Router\Http\TreeRouteStack; use ZfcRbac\Exception\UnauthorizedException; use ZfcRbac\Options\RedirectStrategyOptions; use ZfcRbac\View\Strategy\RedirectStrategy; From 079e13531230d79a13c689fa960ebc3c0165f575 Mon Sep 17 00:00:00 2001 From: Sascha-Oliver Prolic Date: Mon, 25 Jul 2016 16:43:39 +0800 Subject: [PATCH 27/29] Revert "Fix for #340" --- src/ZfcRbac/Guard/AbstractGuard.php | 8 +++-- .../ZfcRbacTest/Guard/ControllerGuardTest.php | 36 +++++++------------ .../Guard/ControllerPermissionsGuardTest.php | 36 +++++++------------ tests/ZfcRbacTest/Guard/RouteGuardTest.php | 20 ++++------- .../Guard/RoutePermissionsGuardTest.php | 23 ++++-------- 5 files changed, 44 insertions(+), 79 deletions(-) diff --git a/src/ZfcRbac/Guard/AbstractGuard.php b/src/ZfcRbac/Guard/AbstractGuard.php index b015ae9c..cdeecb2a 100644 --- a/src/ZfcRbac/Guard/AbstractGuard.php +++ b/src/ZfcRbac/Guard/AbstractGuard.php @@ -69,8 +69,10 @@ public function onResult(MvcEvent $event) )); $event->stopPropagation(true); - $event->setName(MvcEvent::EVENT_DISPATCH_ERROR); - $target = $event->getTarget(); - $target->getEventManager()->triggerEvent($event); + + $application = $event->getApplication(); + $eventManager = $application->getEventManager(); + + $eventManager->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $event); } } diff --git a/tests/ZfcRbacTest/Guard/ControllerGuardTest.php b/tests/ZfcRbacTest/Guard/ControllerGuardTest.php index 304f37e6..3d3fa5ae 100644 --- a/tests/ZfcRbacTest/Guard/ControllerGuardTest.php +++ b/tests/ZfcRbacTest/Guard/ControllerGuardTest.php @@ -19,8 +19,7 @@ namespace ZfcRbacTest\Guard; use Zend\Mvc\MvcEvent; -use Zend\Mvc\Router\RouteMatch as V2RouteMatch; -use Zend\Router\RouteMatch; +use Zend\Mvc\Router\RouteMatch; use ZfcRbac\Guard\ControllerGuard; use ZfcRbac\Guard\GuardInterface; use ZfcRbac\Role\InMemoryRoleProvider; @@ -411,10 +410,9 @@ public function testControllerGranted( $protectionPolicy ) { $event = new MvcEvent(); - $routeMatch = $this->createRouteMatch([ - 'controller' => $controller, - 'action' => $action, - ]); + $routeMatch = new RouteMatch([]); + $routeMatch->setParam('controller', $controller); + $routeMatch->setParam('action', $action); $event->setRouteMatch($routeMatch); @@ -438,10 +436,7 @@ public function testControllerGranted( public function testProperlyFillEventOnAuthorization() { $event = new MvcEvent(); - $routeMatch = $this->createRouteMatch([ - 'controller' => 'MyController', - 'action' => 'edit', - ]); + $routeMatch = new RouteMatch([]); $application = $this->getMock('Zend\Mvc\Application', [], [], '', false); $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface'); @@ -450,6 +445,8 @@ public function testProperlyFillEventOnAuthorization() ->method('getEventManager') ->will($this->returnValue($eventManager)); + $routeMatch->setParam('controller', 'MyController'); + $routeMatch->setParam('action', 'edit'); $event->setRouteMatch($routeMatch); $event->setApplication($application); @@ -482,10 +479,7 @@ public function testProperlyFillEventOnAuthorization() public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() { $event = new MvcEvent(); - $routeMatch = $this->createRouteMatch([ - 'controller' => 'MyController', - 'action' => 'delete', - ]); + $routeMatch = new RouteMatch([]); $application = $this->getMock('Zend\Mvc\Application', [], [], '', false); $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface'); @@ -495,12 +489,14 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() ->will($this->returnValue($eventManager)); $eventManager->expects($this->once()) - ->method('triggerEvent') - ->with($event); + ->method('trigger') + ->with(MvcEvent::EVENT_DISPATCH_ERROR); + + $routeMatch->setParam('controller', 'MyController'); + $routeMatch->setParam('action', 'delete'); $event->setRouteMatch($routeMatch); $event->setApplication($application); - $event->setTarget($application); $identityProvider = $this->getMock('ZfcRbac\Identity\IdentityProviderInterface'); $identityProvider->expects($this->any()) @@ -525,10 +521,4 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() $this->assertEquals(ControllerGuard::GUARD_UNAUTHORIZED, $event->getError()); $this->assertInstanceOf('ZfcRbac\Exception\UnauthorizedException', $event->getParam('exception')); } - - public function createRouteMatch(array $params = []) - { - $class = class_exists(V2RouteMatch::class) ? V2RouteMatch::class : RouteMatch::class; - return new $class($params); - } } diff --git a/tests/ZfcRbacTest/Guard/ControllerPermissionsGuardTest.php b/tests/ZfcRbacTest/Guard/ControllerPermissionsGuardTest.php index ba5cb4ed..8b7f9cb5 100644 --- a/tests/ZfcRbacTest/Guard/ControllerPermissionsGuardTest.php +++ b/tests/ZfcRbacTest/Guard/ControllerPermissionsGuardTest.php @@ -19,8 +19,7 @@ namespace ZfcRbacTest\Guard; use Zend\Mvc\MvcEvent; -use Zend\Mvc\Router\RouteMatch as V2RouteMatch; -use Zend\Router\RouteMatch; +use Zend\Mvc\Router\RouteMatch; use ZfcRbac\Guard\ControllerGuard; use ZfcRbac\Guard\ControllerPermissionsGuard; use ZfcRbac\Guard\GuardInterface; @@ -401,10 +400,9 @@ public function testControllerGranted( $isGranted, $protectionPolicy ) { - $routeMatch = $this->createRouteMatch([ - 'controller' => $controller, - 'action' => $action, - ]); + $routeMatch = new RouteMatch([]); + $routeMatch->setParam('controller', $controller); + $routeMatch->setParam('action', $action); $authorizationService = $this->getMockAuthorizationService(); $authorizationService->expects($this->any()) @@ -423,10 +421,7 @@ public function testControllerGranted( public function testProperlyFillEventOnAuthorization() { $event = new MvcEvent(); - $routeMatch = $this->createRouteMatch([ - 'controller' => 'MyController', - 'action' => 'edit', - ]); + $routeMatch = new RouteMatch([]); $application = $this->getMock('Zend\Mvc\Application', [], [], '', false); $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface'); @@ -435,6 +430,8 @@ public function testProperlyFillEventOnAuthorization() ->method('getEventManager') ->will($this->returnValue($eventManager)); + $routeMatch->setParam('controller', 'MyController'); + $routeMatch->setParam('action', 'edit'); $event->setRouteMatch($routeMatch); $event->setApplication($application); @@ -469,10 +466,7 @@ public function testProperlyFillEventOnAuthorization() public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() { $event = new MvcEvent(); - $routeMatch = $this->createRouteMatch([ - 'controller' => 'MyController', - 'action' => 'delete', - ]); + $routeMatch = new RouteMatch([]); $application = $this->getMock('Zend\Mvc\Application', [], [], '', false); $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface'); @@ -482,12 +476,14 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() ->will($this->returnValue($eventManager)); $eventManager->expects($this->once()) - ->method('triggerEvent') - ->with($event); + ->method('trigger') + ->with(MvcEvent::EVENT_DISPATCH_ERROR); + + $routeMatch->setParam('controller', 'MyController'); + $routeMatch->setParam('action', 'delete'); $event->setRouteMatch($routeMatch); $event->setApplication($application); - $event->setTarget($application); $identityProvider = $this->getMock('ZfcRbac\Identity\IdentityProviderInterface'); $identityProvider->expects($this->any()) @@ -514,10 +510,4 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() $this->assertEquals(ControllerGuard::GUARD_UNAUTHORIZED, $event->getError()); $this->assertInstanceOf('ZfcRbac\Exception\UnauthorizedException', $event->getParam('exception')); } - - public function createRouteMatch(array $params = []) - { - $class = class_exists(V2RouteMatch::class) ? V2RouteMatch::class : RouteMatch::class; - return new $class($params); - } } diff --git a/tests/ZfcRbacTest/Guard/RouteGuardTest.php b/tests/ZfcRbacTest/Guard/RouteGuardTest.php index da671158..28442509 100644 --- a/tests/ZfcRbacTest/Guard/RouteGuardTest.php +++ b/tests/ZfcRbacTest/Guard/RouteGuardTest.php @@ -19,8 +19,7 @@ namespace ZfcRbacTest\Guard; use Zend\Mvc\MvcEvent; -use Zend\Mvc\Router\RouteMatch as V2RouteMatch; -use Zend\Router\RouteMatch; +use Zend\Mvc\Router\RouteMatch; use ZfcRbac\Guard\ControllerGuard; use ZfcRbac\Guard\GuardInterface; use ZfcRbac\Guard\RouteGuard; @@ -368,7 +367,7 @@ public function testRouteGranted( $protectionPolicy ) { $event = new MvcEvent(); - $routeMatch = $this->createRouteMatch(); + $routeMatch = new RouteMatch([]); $routeMatch->setMatchedRouteName($matchedRouteName); $event->setRouteMatch($routeMatch); @@ -393,7 +392,7 @@ public function testRouteGranted( public function testProperlyFillEventOnAuthorization() { $event = new MvcEvent(); - $routeMatch = $this->createRouteMatch(); + $routeMatch = new RouteMatch([]); $application = $this->getMock('Zend\Mvc\Application', [], [], '', false); $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface'); @@ -430,7 +429,7 @@ public function testProperlyFillEventOnAuthorization() public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() { $event = new MvcEvent(); - $routeMatch = $this->createRouteMatch(); + $routeMatch = new RouteMatch([]); $application = $this->getMock('Zend\Mvc\Application', [], [], '', false); $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface'); @@ -440,13 +439,12 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() ->will($this->returnValue($eventManager)); $eventManager->expects($this->once()) - ->method('triggerEvent') - ->with($event); + ->method('trigger') + ->with(MvcEvent::EVENT_DISPATCH_ERROR); $routeMatch->setMatchedRouteName('adminRoute'); $event->setRouteMatch($routeMatch); $event->setApplication($application); - $event->setTarget($application); $identityProvider = $this->getMock('ZfcRbac\Identity\IdentityProviderInterface'); $identityProvider->expects($this->any()) @@ -466,10 +464,4 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() $this->assertEquals(RouteGuard::GUARD_UNAUTHORIZED, $event->getError()); $this->assertInstanceOf('ZfcRbac\Exception\UnauthorizedException', $event->getParam('exception')); } - - public function createRouteMatch(array $params = []) - { - $class = class_exists(V2RouteMatch::class) ? V2RouteMatch::class : RouteMatch::class; - return new $class($params); - } } diff --git a/tests/ZfcRbacTest/Guard/RoutePermissionsGuardTest.php b/tests/ZfcRbacTest/Guard/RoutePermissionsGuardTest.php index a616eccc..4420546e 100644 --- a/tests/ZfcRbacTest/Guard/RoutePermissionsGuardTest.php +++ b/tests/ZfcRbacTest/Guard/RoutePermissionsGuardTest.php @@ -18,8 +18,7 @@ namespace ZfcRbacTest\Guard; use Zend\Mvc\MvcEvent; -use Zend\Mvc\Router\RouteMatch as V2RouteMatch; -use Zend\Router\RouteMatch; +use Zend\Mvc\Router\RouteMatch; use ZfcRbac\Guard\ControllerGuard; use ZfcRbac\Guard\GuardInterface; use ZfcRbac\Guard\RouteGuard; @@ -360,7 +359,7 @@ public function testRoutePermissionGranted( $isGranted, $protectionPolicy ) { - $routeMatch = $this->createRouteMatch(); + $routeMatch = new RouteMatch([]); $routeMatch->setMatchedRouteName($matchedRouteName); $event = new MvcEvent(); @@ -386,7 +385,7 @@ public function testProperlyFillEventOnAuthorization() ->method('getEventManager') ->will($this->returnValue($eventManager)); - $routeMatch = $this->createRouteMatch(); + $routeMatch = new RouteMatch([]); $routeMatch->setMatchedRouteName('adminRoute'); $event = new MvcEvent(); @@ -411,23 +410,21 @@ public function testProperlyFillEventOnAuthorization() public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() { $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface'); + $eventManager->expects($this->once()) + ->method('trigger') + ->with(MvcEvent::EVENT_DISPATCH_ERROR); $application = $this->getMock('Zend\Mvc\Application', [], [], '', false); $application->expects($this->once()) ->method('getEventManager') ->will($this->returnValue($eventManager)); - $routeMatch = $this->createRouteMatch(); + $routeMatch = new RouteMatch([]); $routeMatch->setMatchedRouteName('adminRoute'); $event = new MvcEvent(); $event->setRouteMatch($routeMatch); $event->setApplication($application); - $event->setTarget($application); - - $eventManager->expects($this->once()) - ->method('triggerEvent') - ->with($event); $authorizationService = $this->getMock('ZfcRbac\Service\AuthorizationServiceInterface', [], [], '', false); $authorizationService->expects($this->once()) @@ -444,10 +441,4 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() $this->assertEquals(RouteGuard::GUARD_UNAUTHORIZED, $event->getError()); $this->assertInstanceOf('ZfcRbac\Exception\UnauthorizedException', $event->getParam('exception')); } - - public function createRouteMatch(array $params = []) - { - $class = class_exists(V2RouteMatch::class) ? V2RouteMatch::class : RouteMatch::class; - return new $class($params); - } } From f7e401a69be828d16d4c9b4e5cb241e382778de4 Mon Sep 17 00:00:00 2001 From: prolic Date: Mon, 25 Jul 2016 22:22:27 +0800 Subject: [PATCH 28/29] switch triggerEvent/trigger methods depending on zf2/zf3 --- src/ZfcRbac/Guard/AbstractGuard.php | 9 +++- .../ZfcRbacTest/Guard/ControllerGuardTest.php | 40 ++++++++++++----- .../Guard/ControllerPermissionsGuardTest.php | 43 +++++++++++++------ tests/ZfcRbacTest/Guard/RouteGuardTest.php | 24 ++++++++--- .../Guard/RoutePermissionsGuardTest.php | 25 ++++++++--- 5 files changed, 103 insertions(+), 38 deletions(-) diff --git a/src/ZfcRbac/Guard/AbstractGuard.php b/src/ZfcRbac/Guard/AbstractGuard.php index cdeecb2a..3e36fa06 100644 --- a/src/ZfcRbac/Guard/AbstractGuard.php +++ b/src/ZfcRbac/Guard/AbstractGuard.php @@ -73,6 +73,13 @@ public function onResult(MvcEvent $event) $application = $event->getApplication(); $eventManager = $application->getEventManager(); - $eventManager->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $event); + if (method_exists($eventManager, 'triggerEvent')) { + // ZF3 EventManager + $event->setName(MvcEvent::EVENT_DISPATCH_ERROR); + $eventManager->triggerEvent($event); + } else { + // ZF2 EventManager + $eventManager->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $event); + } } } diff --git a/tests/ZfcRbacTest/Guard/ControllerGuardTest.php b/tests/ZfcRbacTest/Guard/ControllerGuardTest.php index 3d3fa5ae..41234894 100644 --- a/tests/ZfcRbacTest/Guard/ControllerGuardTest.php +++ b/tests/ZfcRbacTest/Guard/ControllerGuardTest.php @@ -19,7 +19,8 @@ namespace ZfcRbacTest\Guard; use Zend\Mvc\MvcEvent; -use Zend\Mvc\Router\RouteMatch; +use Zend\Mvc\Router\RouteMatch as V2RouteMatch; +use Zend\Router\RouteMatch; use ZfcRbac\Guard\ControllerGuard; use ZfcRbac\Guard\GuardInterface; use ZfcRbac\Role\InMemoryRoleProvider; @@ -410,9 +411,10 @@ public function testControllerGranted( $protectionPolicy ) { $event = new MvcEvent(); - $routeMatch = new RouteMatch([]); - $routeMatch->setParam('controller', $controller); - $routeMatch->setParam('action', $action); + $routeMatch = $this->createRouteMatch([ + 'controller' => $controller, + 'action' => $action, + ]); $event->setRouteMatch($routeMatch); @@ -436,7 +438,10 @@ public function testControllerGranted( public function testProperlyFillEventOnAuthorization() { $event = new MvcEvent(); - $routeMatch = new RouteMatch([]); + $routeMatch = $this->createRouteMatch([ + 'controller' => 'MyController', + 'action' => 'edit', + ]); $application = $this->getMock('Zend\Mvc\Application', [], [], '', false); $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface'); @@ -445,8 +450,6 @@ public function testProperlyFillEventOnAuthorization() ->method('getEventManager') ->will($this->returnValue($eventManager)); - $routeMatch->setParam('controller', 'MyController'); - $routeMatch->setParam('action', 'edit'); $event->setRouteMatch($routeMatch); $event->setApplication($application); @@ -479,7 +482,10 @@ public function testProperlyFillEventOnAuthorization() public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() { $event = new MvcEvent(); - $routeMatch = new RouteMatch([]); + $routeMatch = $this->createRouteMatch([ + 'controller' => 'MyController', + 'action' => 'delete', + ]); $application = $this->getMock('Zend\Mvc\Application', [], [], '', false); $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface'); @@ -488,9 +494,15 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() ->method('getEventManager') ->will($this->returnValue($eventManager)); - $eventManager->expects($this->once()) - ->method('trigger') - ->with(MvcEvent::EVENT_DISPATCH_ERROR); + if (method_exists($eventManager, 'triggerEvent')) { + $eventManager->expects($this->once()) + ->method('triggerEvent') + ->with($event); + } else { + $eventManager->expects($this->once()) + ->method('trigger') + ->with(MvcEvent::EVENT_DISPATCH_ERROR); + } $routeMatch->setParam('controller', 'MyController'); $routeMatch->setParam('action', 'delete'); @@ -521,4 +533,10 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() $this->assertEquals(ControllerGuard::GUARD_UNAUTHORIZED, $event->getError()); $this->assertInstanceOf('ZfcRbac\Exception\UnauthorizedException', $event->getParam('exception')); } + + public function createRouteMatch(array $params = []) + { + $class = class_exists(V2RouteMatch::class) ? V2RouteMatch::class : RouteMatch::class; + return new $class($params); + } } diff --git a/tests/ZfcRbacTest/Guard/ControllerPermissionsGuardTest.php b/tests/ZfcRbacTest/Guard/ControllerPermissionsGuardTest.php index 8b7f9cb5..618882d4 100644 --- a/tests/ZfcRbacTest/Guard/ControllerPermissionsGuardTest.php +++ b/tests/ZfcRbacTest/Guard/ControllerPermissionsGuardTest.php @@ -19,7 +19,8 @@ namespace ZfcRbacTest\Guard; use Zend\Mvc\MvcEvent; -use Zend\Mvc\Router\RouteMatch; +use Zend\Mvc\Router\RouteMatch as V2RouteMatch; +use Zend\Router\RouteMatch; use ZfcRbac\Guard\ControllerGuard; use ZfcRbac\Guard\ControllerPermissionsGuard; use ZfcRbac\Guard\GuardInterface; @@ -400,9 +401,10 @@ public function testControllerGranted( $isGranted, $protectionPolicy ) { - $routeMatch = new RouteMatch([]); - $routeMatch->setParam('controller', $controller); - $routeMatch->setParam('action', $action); + $routeMatch = $this->createRouteMatch([ + 'controller' => $controller, + 'action' => $action, + ]); $authorizationService = $this->getMockAuthorizationService(); $authorizationService->expects($this->any()) @@ -421,7 +423,10 @@ public function testControllerGranted( public function testProperlyFillEventOnAuthorization() { $event = new MvcEvent(); - $routeMatch = new RouteMatch([]); + $routeMatch = $this->createRouteMatch([ + 'controller' => 'MyController', + 'action' => 'edit', + ]); $application = $this->getMock('Zend\Mvc\Application', [], [], '', false); $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface'); @@ -430,8 +435,6 @@ public function testProperlyFillEventOnAuthorization() ->method('getEventManager') ->will($this->returnValue($eventManager)); - $routeMatch->setParam('controller', 'MyController'); - $routeMatch->setParam('action', 'edit'); $event->setRouteMatch($routeMatch); $event->setApplication($application); @@ -466,7 +469,10 @@ public function testProperlyFillEventOnAuthorization() public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() { $event = new MvcEvent(); - $routeMatch = new RouteMatch([]); + $routeMatch = $this->createRouteMatch([ + 'controller' => 'MyController', + 'action' => 'delete', + ]); $application = $this->getMock('Zend\Mvc\Application', [], [], '', false); $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface'); @@ -475,12 +481,15 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() ->method('getEventManager') ->will($this->returnValue($eventManager)); - $eventManager->expects($this->once()) - ->method('trigger') - ->with(MvcEvent::EVENT_DISPATCH_ERROR); - - $routeMatch->setParam('controller', 'MyController'); - $routeMatch->setParam('action', 'delete'); + if (method_exists($eventManager, 'triggerEvent')) { + $eventManager->expects($this->once()) + ->method('triggerEvent') + ->with($event); + } else { + $eventManager->expects($this->once()) + ->method('trigger') + ->with(MvcEvent::EVENT_DISPATCH_ERROR); + } $event->setRouteMatch($routeMatch); $event->setApplication($application); @@ -510,4 +519,10 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() $this->assertEquals(ControllerGuard::GUARD_UNAUTHORIZED, $event->getError()); $this->assertInstanceOf('ZfcRbac\Exception\UnauthorizedException', $event->getParam('exception')); } + + public function createRouteMatch(array $params = []) + { + $class = class_exists(V2RouteMatch::class) ? V2RouteMatch::class : RouteMatch::class; + return new $class($params); + } } diff --git a/tests/ZfcRbacTest/Guard/RouteGuardTest.php b/tests/ZfcRbacTest/Guard/RouteGuardTest.php index 28442509..6371d0de 100644 --- a/tests/ZfcRbacTest/Guard/RouteGuardTest.php +++ b/tests/ZfcRbacTest/Guard/RouteGuardTest.php @@ -367,7 +367,7 @@ public function testRouteGranted( $protectionPolicy ) { $event = new MvcEvent(); - $routeMatch = new RouteMatch([]); + $routeMatch = $this->createRouteMatch(); $routeMatch->setMatchedRouteName($matchedRouteName); $event->setRouteMatch($routeMatch); @@ -392,7 +392,7 @@ public function testRouteGranted( public function testProperlyFillEventOnAuthorization() { $event = new MvcEvent(); - $routeMatch = new RouteMatch([]); + $routeMatch = $this->createRouteMatch(); $application = $this->getMock('Zend\Mvc\Application', [], [], '', false); $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface'); @@ -429,7 +429,7 @@ public function testProperlyFillEventOnAuthorization() public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() { $event = new MvcEvent(); - $routeMatch = new RouteMatch([]); + $routeMatch = $this->createRouteMatch(); $application = $this->getMock('Zend\Mvc\Application', [], [], '', false); $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface'); @@ -438,9 +438,15 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() ->method('getEventManager') ->will($this->returnValue($eventManager)); - $eventManager->expects($this->once()) - ->method('trigger') - ->with(MvcEvent::EVENT_DISPATCH_ERROR); + if (method_exists($eventManager, 'triggerEvent')) { + $eventManager->expects($this->once()) + ->method('triggerEvent') + ->with($event); + } else { + $eventManager->expects($this->once()) + ->method('trigger') + ->with(MvcEvent::EVENT_DISPATCH_ERROR); + } $routeMatch->setMatchedRouteName('adminRoute'); $event->setRouteMatch($routeMatch); @@ -464,4 +470,10 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() $this->assertEquals(RouteGuard::GUARD_UNAUTHORIZED, $event->getError()); $this->assertInstanceOf('ZfcRbac\Exception\UnauthorizedException', $event->getParam('exception')); } + + public function createRouteMatch(array $params = []) + { + $class = class_exists(V2RouteMatch::class) ? V2RouteMatch::class : RouteMatch::class; + return new $class($params); + } } diff --git a/tests/ZfcRbacTest/Guard/RoutePermissionsGuardTest.php b/tests/ZfcRbacTest/Guard/RoutePermissionsGuardTest.php index 4420546e..a952ede6 100644 --- a/tests/ZfcRbacTest/Guard/RoutePermissionsGuardTest.php +++ b/tests/ZfcRbacTest/Guard/RoutePermissionsGuardTest.php @@ -359,7 +359,7 @@ public function testRoutePermissionGranted( $isGranted, $protectionPolicy ) { - $routeMatch = new RouteMatch([]); + $routeMatch = $this->createRouteMatch(); $routeMatch->setMatchedRouteName($matchedRouteName); $event = new MvcEvent(); @@ -385,7 +385,7 @@ public function testProperlyFillEventOnAuthorization() ->method('getEventManager') ->will($this->returnValue($eventManager)); - $routeMatch = new RouteMatch([]); + $routeMatch = $this->createRouteMatch(); $routeMatch->setMatchedRouteName('adminRoute'); $event = new MvcEvent(); @@ -410,22 +410,29 @@ public function testProperlyFillEventOnAuthorization() public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() { $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface'); - $eventManager->expects($this->once()) - ->method('trigger') - ->with(MvcEvent::EVENT_DISPATCH_ERROR); $application = $this->getMock('Zend\Mvc\Application', [], [], '', false); $application->expects($this->once()) ->method('getEventManager') ->will($this->returnValue($eventManager)); - $routeMatch = new RouteMatch([]); + $routeMatch = $this->createRouteMatch(); $routeMatch->setMatchedRouteName('adminRoute'); $event = new MvcEvent(); $event->setRouteMatch($routeMatch); $event->setApplication($application); + if (method_exists($eventManager, 'triggerEvent')) { + $eventManager->expects($this->once()) + ->method('triggerEvent') + ->with($event); + } else { + $eventManager->expects($this->once()) + ->method('trigger') + ->with(MvcEvent::EVENT_DISPATCH_ERROR); + } + $authorizationService = $this->getMock('ZfcRbac\Service\AuthorizationServiceInterface', [], [], '', false); $authorizationService->expects($this->once()) ->method('isGranted') @@ -441,4 +448,10 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() $this->assertEquals(RouteGuard::GUARD_UNAUTHORIZED, $event->getError()); $this->assertInstanceOf('ZfcRbac\Exception\UnauthorizedException', $event->getParam('exception')); } + + public function createRouteMatch(array $params = []) + { + $class = class_exists(V2RouteMatch::class) ? V2RouteMatch::class : RouteMatch::class; + return new $class($params); + } } From 4afa493ca2d00ee1dacf93694fec5bb400e4193b Mon Sep 17 00:00:00 2001 From: Sascha-Oliver Prolic Date: Thu, 11 Aug 2016 20:56:16 +0800 Subject: [PATCH 29/29] fix route permissions guard factory --- src/ZfcRbac/Factory/RoutePermissionsGuardFactory.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ZfcRbac/Factory/RoutePermissionsGuardFactory.php b/src/ZfcRbac/Factory/RoutePermissionsGuardFactory.php index 8314e5d3..9a98c794 100644 --- a/src/ZfcRbac/Factory/RoutePermissionsGuardFactory.php +++ b/src/ZfcRbac/Factory/RoutePermissionsGuardFactory.php @@ -62,13 +62,17 @@ public function setCreationOptions(array $options) */ public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { + if (null === $options) { + $options = []; + } + /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ $moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions'); /* @var \ZfcRbac\Service\AuthorizationService $authorizationService */ $authorizationService = $container->get('ZfcRbac\Service\AuthorizationService'); - $routeGuard = new RoutePermissionsGuard($authorizationService, $this->options); + $routeGuard = new RoutePermissionsGuard($authorizationService, $options); $routeGuard->setProtectionPolicy($moduleOptions->getProtectionPolicy()); return $routeGuard; @@ -80,6 +84,6 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o */ public function createService(ServiceLocatorInterface $serviceLocator) { - return $this($serviceLocator->getServiceLocator(), RoutePermissionsGuard::class); + return $this($serviceLocator->getServiceLocator(), RoutePermissionsGuard::class, $this->options); } }