diff --git a/.travis.yml b/.travis.yml index 796ebed..e84ec3c 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 a2f5ecf..2cf8a0b 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, 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/composer.json b/composer.json index b5bf481..34336f0 100644 --- a/composer.json +++ b/composer.json @@ -27,20 +27,27 @@ } ], "require": { - "php": ">=5.4", - "zendframework/zend-mvc": "~2.2", - "zendframework/zend-servicemanager": "~2.2", + "php": "~5.6 || ~7.0", + "zendframework/zend-config": "~2.2", + "zendframework/zend-eventmanager": "^2.6.3 || ^3.0", + "zendframework/zend-mvc": "~2.7 || ^3.0", + "zendframework/zend-servicemanager": "^2.7.5 || ^3.0.3", "zfr/rbac": "~1.2" }, "require-dev": { - "zendframework/zendframework": "~2.2", - "zendframework/zend-developer-tools": "dev-master", - "phpunit/phpunit": "~3.7", - "squizlabs/php_codesniffer": "1.4.*", + "zendframework/zend-authentication": "~2.2", + "zendframework/zend-developer-tools": "~1.1", + "zendframework/zend-log": "~2.2", + "zendframework/zend-http": "~2.2", + "zendframework/zend-i18n": "~2.7.3", + "zendframework/zend-serializer": "~2.2", + "zendframework/zend-view": "~2.2", + "phpunit/phpunit": "~4.8.26", + "squizlabs/php_codesniffer": "2.6.*", "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": "^1.0" }, "suggest": { "zendframework/zend-developer-tools": "if you want to show information about the roles", diff --git a/config/module.config.php b/config/module.config.php index 1778304..dcf52fd 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/Assertion/AssertionPluginManager.php b/src/ZfcRbac/Assertion/AssertionPluginManager.php index b5b156e..e236a22 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 * @@ -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/Collector/RbacCollector.php b/src/ZfcRbac/Collector/RbacCollector.php index 918e82e..85b7416 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 301eb6f..d757e05 100644 --- a/src/ZfcRbac/Factory/AssertionPluginManagerFactory.php +++ b/src/ZfcRbac/Factory/AssertionPluginManagerFactory.php @@ -18,6 +18,7 @@ namespace ZfcRbac\Factory; +use Interop\Container\ContainerInterface; use Zend\ServiceManager\Config; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; @@ -25,23 +26,31 @@ /** * Factory to create a assertion plugin manager - * + * * @author Aeneas Rekkas * @license MIT */ 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 04cbaec..604ba1f 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 feed797..d57a1f8 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 2f3300f..4d0164c 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 1fbfa4a..a322f62 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,13 +29,21 @@ * @author Michaël Gallego * @license MIT */ -class ControllerGuardFactory implements FactoryInterface, MutableCreationOptionsInterface +class ControllerGuardFactory implements FactoryInterface { /** * @var array */ protected $options = []; + /** + * {@inheritDoc} + */ + public function __construct(array $options = []) + { + $this->setCreationOptions($options); + } + /** * {@inheritDoc} */ @@ -45,22 +53,36 @@ public function setCreationOptions(array $options) } /** - * {@inheritDoc} + * @param ContainerInterface $container + * @param string $requestedName + * @param array|null $options * @return ControllerGuard */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { - $parentLocator = $serviceLocator->getServiceLocator(); + if (null === $options) { + $options = []; + } /* @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->getServiceLocator(), ControllerGuard::class, $this->options); + } } diff --git a/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php b/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php index 6e80fd2..27a57a7 100644 --- a/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php +++ b/src/ZfcRbac/Factory/ControllerPermissionsGuardFactory.php @@ -18,11 +18,10 @@ 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; /** * Create a controller guard for checking permissions @@ -30,13 +29,21 @@ * @author JM Lerouxw * @license MIT */ -class ControllerPermissionsGuardFactory implements FactoryInterface, MutableCreationOptionsInterface +class ControllerPermissionsGuardFactory implements FactoryInterface { /** * @var array */ protected $options = []; + /** + * {@inheritDoc} + */ + public function __construct(array $options = []) + { + $this->setCreationOptions($options); + } + /** * {@inheritDoc} */ @@ -46,22 +53,35 @@ public function setCreationOptions(array $options) } /** - * @param \Zend\ServiceManager\AbstractPluginManager|ServiceLocatorInterface $serviceLocator - * @return RouteGuard + * @param ContainerInterface $container + * @param string $resolvedName + * @param array|null $options + * @return ControllerPermissionsGuard */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $resolvedName, array $options = null) { - $parentLocator = $serviceLocator->getServiceLocator(); + if (null === $options) { + $options = []; + } /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ - $moduleOptions = $parentLocator->get('ZfcRbac\Options\ModuleOptions'); + $moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions'); /* @var \ZfcRbac\Service\AuthorizationService $authorizationService */ - $authorizationService = $parentLocator->get('ZfcRbac\Service\AuthorizationService'); + $authorizationService = $container->get('ZfcRbac\Service\AuthorizationService'); - $guard = new ControllerPermissionsGuard($authorizationService, $this->options); + $guard = new ControllerPermissionsGuard($authorizationService, $options); $guard->setProtectionPolicy($moduleOptions->getProtectionPolicy()); return $guard; } + + /** + * @param \Zend\ServiceManager\AbstractPluginManager|ServiceLocatorInterface $serviceLocator + * @return ControllerPermissionsGuard + */ + public function createService(ServiceLocatorInterface $serviceLocator) + { + return $this($serviceLocator->getServiceLocator(), ControllerPermissionsGuard::class, $this->options); + } } diff --git a/src/ZfcRbac/Factory/GuardPluginManagerFactory.php b/src/ZfcRbac/Factory/GuardPluginManagerFactory.php index 4b067b6..cb24e00 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 29bb781..4f0b934 100644 --- a/src/ZfcRbac/Factory/GuardsFactory.php +++ b/src/ZfcRbac/Factory/GuardsFactory.php @@ -18,8 +18,10 @@ namespace ZfcRbac\Factory; +use Interop\Container\ContainerInterface; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; +use ZfcRbac\Guard\GuardInterface; /** * Create a list of guards @@ -30,13 +32,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 +48,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 +57,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 387e751..5c2e1b4 100644 --- a/src/ZfcRbac/Factory/HasRoleViewHelperFactory.php +++ b/src/ZfcRbac/Factory/HasRoleViewHelperFactory.php @@ -18,6 +18,7 @@ namespace ZfcRbac\Factory; +use Interop\Container\ContainerInterface; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Service\RoleService; @@ -32,14 +33,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->getServiceLocator(), HasRole::class); + } } diff --git a/src/ZfcRbac/Factory/IsGrantedPluginFactory.php b/src/ZfcRbac/Factory/IsGrantedPluginFactory.php index 531d518..b2edf41 100644 --- a/src/ZfcRbac/Factory/IsGrantedPluginFactory.php +++ b/src/ZfcRbac/Factory/IsGrantedPluginFactory.php @@ -18,6 +18,7 @@ namespace ZfcRbac\Factory; +use Interop\Container\ContainerInterface; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Mvc\Controller\Plugin\IsGranted; @@ -31,14 +32,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->getServiceLocator(), IsGranted::class); + } } diff --git a/src/ZfcRbac/Factory/IsGrantedViewHelperFactory.php b/src/ZfcRbac/Factory/IsGrantedViewHelperFactory.php index ba0fa34..c4119f1 100644 --- a/src/ZfcRbac/Factory/IsGrantedViewHelperFactory.php +++ b/src/ZfcRbac/Factory/IsGrantedViewHelperFactory.php @@ -18,6 +18,7 @@ namespace ZfcRbac\Factory; +use Interop\Container\ContainerInterface; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\View\Helper\IsGranted; @@ -31,14 +32,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->getServiceLocator(), IsGranted::class); + } } diff --git a/src/ZfcRbac/Factory/ModuleOptionsFactory.php b/src/ZfcRbac/Factory/ModuleOptionsFactory.php index bfc1b71..a1c1f13 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 5a19e30..68288bb 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,13 +30,21 @@ * @author Michaël Gallego * @license MIT */ -class ObjectRepositoryRoleProviderFactory implements FactoryInterface, MutableCreationOptionsInterface +class ObjectRepositoryRoleProviderFactory implements FactoryInterface { /** * @var array */ protected $options = []; + /** + * {@inheritDoc} + */ + public function __construct(array $options = []) + { + $this->setCreationOptions($options); + } + /** * {@inheritDoc} */ @@ -46,31 +54,32 @@ public function setCreationOptions(array $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 +87,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->getServiceLocator(), ObjectRepositoryRoleProvider::class, $this->options); + } } diff --git a/src/ZfcRbac/Factory/RbacFactory.php b/src/ZfcRbac/Factory/RbacFactory.php index 8cf5088..cbd5e3f 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 455e621..e9e2836 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 58aada1..32580f0 100644 --- a/src/ZfcRbac/Factory/RoleProviderPluginManagerFactory.php +++ b/src/ZfcRbac/Factory/RoleProviderPluginManagerFactory.php @@ -18,7 +18,7 @@ namespace ZfcRbac\Factory; -use Zend\ServiceManager\Config; +use Interop\Container\ContainerInterface; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Role\RoleProviderPluginManager; @@ -32,16 +32,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 b3626d0..6eae608 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 3a92663..d4c4cb5 100644 --- a/src/ZfcRbac/Factory/RouteGuardFactory.php +++ b/src/ZfcRbac/Factory/RouteGuardFactory.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\RouteGuard; @@ -29,13 +29,21 @@ * @author Michaël Gallego * @license MIT */ -class RouteGuardFactory implements FactoryInterface, MutableCreationOptionsInterface +class RouteGuardFactory implements FactoryInterface { /** * @var array */ protected $options = []; + /** + * {@inheritDoc} + */ + public function __construct(array $options = []) + { + $this->setCreationOptions($options); + } + /** * {@inheritDoc} */ @@ -45,22 +53,36 @@ public function setCreationOptions(array $options) } /** - * {@inheritDoc} + * @param ContainerInterface $container + * @param string $requestedName + * @param array|null $options * @return RouteGuard */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { - $parentLocator = $serviceLocator->getServiceLocator(); + if (null === $options) { + $options = []; + } /* @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->getServiceLocator(), RouteGuard::class, $this->options); + } } diff --git a/src/ZfcRbac/Factory/RoutePermissionsGuardFactory.php b/src/ZfcRbac/Factory/RoutePermissionsGuardFactory.php index c6fdc25..9a98c79 100644 --- a/src/ZfcRbac/Factory/RoutePermissionsGuardFactory.php +++ b/src/ZfcRbac/Factory/RoutePermissionsGuardFactory.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\RouteGuard; use ZfcRbac\Guard\RoutePermissionsGuard; @@ -31,13 +31,21 @@ * @author JM Lerouxw * @license MIT */ -class RoutePermissionsGuardFactory implements FactoryInterface, MutableCreationOptionsInterface +class RoutePermissionsGuardFactory implements FactoryInterface { /** * @var array */ protected $options = []; + /** + * {@inheritDoc} + */ + public function __construct(array $options = []) + { + $this->setCreationOptions($options); + } + /** * {@inheritDoc} */ @@ -47,22 +55,35 @@ public function setCreationOptions(array $options) } /** - * @param \Zend\ServiceManager\AbstractPluginManager|ServiceLocatorInterface $serviceLocator - * @return RouteGuard + * @param ContainerInterface $container + * @param string $requestedName + * @param array|null $options + * @return RoutePermissionsGuard */ - public function createService(ServiceLocatorInterface $serviceLocator) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { - $parentLocator = $serviceLocator->getServiceLocator(); + if (null === $options) { + $options = []; + } /* @var \ZfcRbac\Options\ModuleOptions $moduleOptions */ - $moduleOptions = $parentLocator->get('ZfcRbac\Options\ModuleOptions'); + $moduleOptions = $container->get('ZfcRbac\Options\ModuleOptions'); /* @var \ZfcRbac\Service\AuthorizationService $authorizationService */ - $authorizationService = $parentLocator->get('ZfcRbac\Service\AuthorizationService'); + $authorizationService = $container->get('ZfcRbac\Service\AuthorizationService'); - $routeGuard = new RoutePermissionsGuard($authorizationService, $this->options); + $routeGuard = new RoutePermissionsGuard($authorizationService, $options); $routeGuard->setProtectionPolicy($moduleOptions->getProtectionPolicy()); return $routeGuard; } + + /** + * @param \Zend\ServiceManager\AbstractPluginManager|ServiceLocatorInterface $serviceLocator + * @return RouteGuard + */ + public function createService(ServiceLocatorInterface $serviceLocator) + { + return $this($serviceLocator->getServiceLocator(), RoutePermissionsGuard::class, $this->options); + } } diff --git a/src/ZfcRbac/Factory/UnauthorizedStrategyFactory.php b/src/ZfcRbac/Factory/UnauthorizedStrategyFactory.php index 699dd23..3176358 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 4eba2d7..3e36fa0 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 = AbstractGuard::EVENT_PRIORITY) { - $this->listeners[] = $events->attach(static::EVENT_NAME, [$this, 'onResult'], static::EVENT_PRIORITY); + $this->listeners[] = $events->attach(static::EVENT_NAME, [$this, 'onResult'], $priority); } /** @@ -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/src/ZfcRbac/Guard/GuardPluginManager.php b/src/ZfcRbac/Guard/GuardPluginManager.php index 0a51048..2f54b8a 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/Guard/RouteGuard.php b/src/ZfcRbac/Guard/RouteGuard.php index a8bdd5b..69466dc 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 5f46963..1673189 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 dcaa358..7e0b60f 100644 --- a/src/ZfcRbac/Initializer/AuthorizationServiceInitializer.php +++ b/src/ZfcRbac/Initializer/AuthorizationServiceInitializer.php @@ -18,7 +18,7 @@ namespace ZfcRbac\Initializer; -use Zend\ServiceManager\AbstractPluginManager; +use Interop\Container\ContainerInterface; use Zend\ServiceManager\InitializerInterface; use Zend\ServiceManager\ServiceLocatorInterface; use ZfcRbac\Service\AuthorizationServiceAwareInterface; @@ -32,17 +32,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(); - } - - $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) + { + if ($serviceLocator instanceof AbstractPluginManager) { + $serviceLocator = $serviceLocator->getServiceLocator(); + } + + $this($serviceLocator, $instance); + } } diff --git a/src/ZfcRbac/Module.php b/src/ZfcRbac/Module.php index 372b044..255dbb3 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/Options/ModuleOptions.php b/src/ZfcRbac/Options/ModuleOptions.php index 96eec9d..648f410 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() diff --git a/src/ZfcRbac/Role/RoleProviderPluginManager.php b/src/ZfcRbac/Role/RoleProviderPluginManager.php index d8f995f..02e2d5f 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/Service/RoleService.php b/src/ZfcRbac/Service/RoleService.php index 0a23a55..8aef9b6 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/src/ZfcRbac/View/Strategy/AbstractStrategy.php b/src/ZfcRbac/View/Strategy/AbstractStrategy.php index a245de2..8eda603 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/Asset/MockRoleWithPermissionMethod.php b/tests/ZfcRbacTest/Asset/MockRoleWithPermissionMethod.php index 376d352..199e745 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 44522bb..a4e25f7 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 47aa5b2..4243447 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/AssertionPluginManagerFactoryTest.php b/tests/ZfcRbacTest/Factory/AssertionPluginManagerFactoryTest.php index 4d83c21..62d22f6 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 ca6661d..044d3c3 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(ServiceLocatorInterface::class); + $serviceLocator->willImplement(ContainerInterface::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()); } @@ -73,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( @@ -95,6 +100,36 @@ public function testAuthorizationServiceIsInjectedWithDelegator() $this->assertEquals($authorizationService, $decoratedInstance->getAuthorizationService()); } + 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(); @@ -119,10 +154,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 850c000..36bac5a 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,15 +49,13 @@ public function testFactory() 'protection_policy' => GuardInterface::POLICY_ALLOW ]); - $serviceManager = new ServiceManager(); $serviceManager->setService('ZfcRbac\Options\ModuleOptions', $options); $serviceManager->setService( 'ZfcRbac\Service\RoleService', $this->getMock('ZfcRbac\Service\RoleService', [], [], '', false) ); - $pluginManager = new GuardPluginManager(); - $pluginManager->setServiceLocator($serviceManager); + $pluginManager = new GuardPluginManager($serviceManager); $factory = new ControllerGuardFactory(); $controllerGuard = $factory->createService($pluginManager); @@ -59,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 0467410..01bc0cf 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,15 +49,13 @@ public function testFactory() 'protection_policy' => GuardInterface::POLICY_ALLOW, ]); - $serviceManager = new ServiceManager(); $serviceManager->setService('ZfcRbac\Options\ModuleOptions', $options); $serviceManager->setService( 'ZfcRbac\Service\AuthorizationService', $this->getMock('ZfcRbac\Service\AuthorizationService', [], [], '', false) ); - $pluginManager = new GuardPluginManager(); - $pluginManager->setServiceLocator($serviceManager); + $pluginManager = new GuardPluginManager($serviceManager); $factory = new ControllerPermissionsGuardFactory(); $guard = $factory->createService($pluginManager); @@ -59,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/GuardPluginManagerFactoryTest.php b/tests/ZfcRbacTest/Factory/GuardPluginManagerFactoryTest.php index 1f7b2c2..01d53ef 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 @@ -38,7 +39,6 @@ public function testFactory() $factory = new GuardPluginManagerFactory(); $pluginManager = $factory->createService($serviceManager); - $this->assertInstanceOf('ZfcRbac\Guard\GuardPluginManager', $pluginManager); - $this->assertSame($serviceManager, $pluginManager->getServiceLocator()); + $this->assertInstanceOf(GuardPluginManager::class, $pluginManager); } } diff --git a/tests/ZfcRbacTest/Factory/GuardsFactoryTest.php b/tests/ZfcRbacTest/Factory/GuardsFactoryTest.php index f1c494b..494526b 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 c2a052e..840b92a 100644 --- a/tests/ZfcRbacTest/Factory/HasRoleViewHelperFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/HasRoleViewHelperFactoryTest.php @@ -31,8 +31,11 @@ public function testFactory() { $serviceManager = new ServiceManager(); - $pluginManager = new HelperPluginManager(); - $pluginManager->setServiceLocator($serviceManager); + if (method_exists($serviceManager, 'build')) { + $this->markTestSkipped('this test is only vor zend-servicemanager v2'); + } + + $pluginManager = new HelperPluginManager($serviceManager); $serviceManager->setService( 'ZfcRbac\Service\RoleService', @@ -44,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 9095c91..39feee0 100644 --- a/tests/ZfcRbacTest/Factory/IsGrantedPluginFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/IsGrantedPluginFactoryTest.php @@ -31,8 +31,11 @@ public function testFactory() { $serviceManager = new ServiceManager(); - $pluginManager = new PluginManager(); - $pluginManager->setServiceLocator($serviceManager); + if (method_exists($serviceManager, 'build')) { + $this->markTestSkipped('this test is only vor zend-servicemanager v2'); + } + + $pluginManager = new PluginManager($serviceManager); $serviceManager->setService( 'ZfcRbac\Service\AuthorizationService', @@ -44,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 aa638d3..e7ae7a1 100644 --- a/tests/ZfcRbacTest/Factory/IsGrantedViewHelperFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/IsGrantedViewHelperFactoryTest.php @@ -31,8 +31,11 @@ public function testFactory() { $serviceManager = new ServiceManager(); - $pluginManager = new HelperPluginManager(); - $pluginManager->setServiceLocator($serviceManager); + if (method_exists($serviceManager, 'build')) { + $this->markTestSkipped('this test is only vor zend-servicemanager v2'); + } + + $pluginManager = new HelperPluginManager($serviceManager); $serviceManager->setService( 'ZfcRbac\Service\AuthorizationService', @@ -44,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/ModuleOptionsFactoryTest.php b/tests/ZfcRbacTest/Factory/ModuleOptionsFactoryTest.php index 4e2e5bf..10b8baa 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 bd328e0..4093970 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,15 +97,13 @@ 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' ]); } 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 d531b9c..b871534 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,20 +38,18 @@ 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); } } - \ No newline at end of file diff --git a/tests/ZfcRbacTest/Factory/RoleProviderPluginManagerFactoryTest.php b/tests/ZfcRbacTest/Factory/RoleProviderPluginManagerFactoryTest.php index 13c490e..8dfa439 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 @@ -38,7 +39,6 @@ public function testFactory() $factory = new RoleProviderPluginManagerFactory(); $pluginManager = $factory->createService($serviceManager); - $this->assertInstanceOf('ZfcRbac\Role\RoleProviderPluginManager', $pluginManager); - $this->assertSame($serviceManager, $pluginManager->getServiceLocator()); + $this->assertInstanceOf(RoleProviderPluginManager::class, $pluginManager); } } diff --git a/tests/ZfcRbacTest/Factory/RoleServiceFactoryTest.php b/tests/ZfcRbacTest/Factory/RoleServiceFactoryTest.php index 63eafca..f420242 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 8063ab4..7cade6a 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,15 +49,13 @@ public function testFactory() 'protection_policy' => GuardInterface::POLICY_ALLOW, ]); - $serviceManager = new ServiceManager(); $serviceManager->setService('ZfcRbac\Options\ModuleOptions', $options); $serviceManager->setService( 'ZfcRbac\Service\RoleService', $this->getMock('ZfcRbac\Service\RoleService', [], [], '', false) ); - $pluginManager = new GuardPluginManager(); - $pluginManager->setServiceLocator($serviceManager); + $pluginManager = new GuardPluginManager($serviceManager); $factory = new RouteGuardFactory(); $routeGuard = $factory->createService($pluginManager); @@ -59,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 6aee9e5..1328c8d 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,15 +49,13 @@ public function testFactory() 'protection_policy' => GuardInterface::POLICY_ALLOW, ]); - $serviceManager = new ServiceManager(); $serviceManager->setService('ZfcRbac\Options\ModuleOptions', $options); $serviceManager->setService( 'ZfcRbac\Service\AuthorizationService', $this->getMock('ZfcRbac\Service\AuthorizationService', [], [], '', false) ); - $pluginManager = new GuardPluginManager(); - $pluginManager->setServiceLocator($serviceManager); + $pluginManager = new GuardPluginManager($serviceManager); $factory = new RoutePermissionsGuardFactory(); $routeGuard = $factory->createService($pluginManager); @@ -59,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()); + } } diff --git a/tests/ZfcRbacTest/Factory/UnauthorizedStrategyFactoryTest.php b/tests/ZfcRbacTest/Factory/UnauthorizedStrategyFactoryTest.php index 1efb9b6..564ebf5 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,16 +35,13 @@ 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); } } - \ No newline at end of file diff --git a/tests/ZfcRbacTest/Guard/ControllerGuardTest.php b/tests/ZfcRbacTest/Guard/ControllerGuardTest.php index 3d3fa5a..4123489 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 8b7f9cb..618882d 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/GuardPluginManagerTest.php b/tests/ZfcRbacTest/Guard/GuardPluginManagerTest.php index ff43d26..3fa3c28 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/Guard/ProtectionPolicyTraitTest.php b/tests/ZfcRbacTest/Guard/ProtectionPolicyTraitTest.php index 800f917..a8ab1f5 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/Guard/RouteGuardTest.php b/tests/ZfcRbacTest/Guard/RouteGuardTest.php index 2844250..6371d0d 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 4420546..a952ede 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); + } } diff --git a/tests/ZfcRbacTest/Initializer/AuthorizationAwareFake.php b/tests/ZfcRbacTest/Initializer/AuthorizationAwareFake.php index 59757a4..bdc19c0 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/Initializer/AuthorizationServiceInitializerTest.php b/tests/ZfcRbacTest/Initializer/AuthorizationServiceInitializerTest.php index 0cb1b3b..c58c886 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()); } diff --git a/tests/ZfcRbacTest/Role/RoleProviderPluginManagerTest.php b/tests/ZfcRbacTest/Role/RoleProviderPluginManagerTest.php index 8667acb..36d7c69 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,8 +38,7 @@ public function testValidationOfPluginFailsIfRoleProviderInterfaceIsNotImplement { $this->setExpectedException('ZfcRbac\Exception\RuntimeException'); - $pluginManager = new RoleProviderPluginManager(); + $pluginManager = new RoleProviderPluginManager(new ServiceManager()); $pluginManager->get('stdClass', []); } } - \ No newline at end of file diff --git a/tests/ZfcRbacTest/Service/AuthorizationServiceTest.php b/tests/ZfcRbacTest/Service/AuthorizationServiceTest.php index 1685dc0..d8721dd 100644 --- a/tests/ZfcRbacTest/Service/AuthorizationServiceTest.php +++ b/tests/ZfcRbacTest/Service/AuthorizationServiceTest.php @@ -20,12 +20,12 @@ use Rbac\Rbac; use Rbac\Traversal\Strategy\RecursiveRoleIteratorStrategy; +use Zend\ServiceManager\ServiceManager; use ZfcRbac\Role\InMemoryRoleProvider; use ZfcRbac\Service\AuthorizationService; use ZfcRbac\Service\RoleService; use ZfcRbacTest\Asset\SimpleAssertion; use ZfcRbac\Assertion\AssertionPluginManager; -use Zend\ServiceManager\Config; /** * @covers \ZfcRbac\Service\AuthorizationService @@ -102,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' => [ @@ -138,7 +138,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 964f19a..86cc995 100644 --- a/tests/ZfcRbacTest/Util/ServiceManagerFactory.php +++ b/tests/ZfcRbacTest/Util/ServiceManagerFactory.php @@ -59,12 +59,13 @@ 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); /* @var $moduleManager \Zend\ModuleManager\ModuleManagerInterface */ $moduleManager = $serviceManager->get('ModuleManager'); diff --git a/tests/ZfcRbacTest/View/Strategy/RedirectStrategyTest.php b/tests/ZfcRbacTest/View/Strategy/RedirectStrategyTest.php index b80ec50..d3c2ef0 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();