diff --git a/src/Assertion/AssertionContainer.php b/src/Assertion/AssertionContainer.php index fd85584..6f04d44 100644 --- a/src/Assertion/AssertionContainer.php +++ b/src/Assertion/AssertionContainer.php @@ -26,9 +26,9 @@ /** * Plugin manager to create assertions * - * @author Aeneas Rekkas - * @licence MIT * @deprecated Use AssertionPluginManager + * + * @licence MIT * @codeCoverageIgnore */ final class AssertionContainer extends AbstractPluginManager implements AssertionContainerInterface @@ -36,9 +36,9 @@ final class AssertionContainer extends AbstractPluginManager implements Assertio protected $instanceOf = AssertionInterface::class; /** - * @inheritdoc + * @inheritDoc */ - public function get($name, array $options = null): AssertionInterface + public function get($name, ?array $options = null): AssertionInterface { return parent::get($name); } diff --git a/src/Assertion/AssertionContainerFactory.php b/src/Assertion/AssertionContainerFactory.php index 71dac6e..bc7604d 100644 --- a/src/Assertion/AssertionContainerFactory.php +++ b/src/Assertion/AssertionContainerFactory.php @@ -26,9 +26,9 @@ /** * Factory to create a assertion plugin manager * - * @author Aeneas Rekkas - * @licence MIT * @deprecated Use AssertionPluginManagerFactory + * + * @licence MIT * @codeCoverageIgnore */ class AssertionContainerFactory diff --git a/src/Assertion/AssertionInterface.php b/src/Assertion/AssertionInterface.php index 2b46733..e9f2137 100644 --- a/src/Assertion/AssertionInterface.php +++ b/src/Assertion/AssertionInterface.php @@ -27,16 +27,13 @@ /** * Interface that you can implement for dynamic assertions * - * @author Michaël Gallego - * @author Aeneas Rekkas - * @author Daniel Gimenes * @licence MIT */ interface AssertionInterface { public function assert( PermissionInterface|string $permission, - IdentityInterface $identity = null, + ?IdentityInterface $identity = null, mixed $context = null ): bool; } diff --git a/src/Assertion/AssertionPluginManager.php b/src/Assertion/AssertionPluginManager.php index cbb8b93..e4bc38b 100644 --- a/src/Assertion/AssertionPluginManager.php +++ b/src/Assertion/AssertionPluginManager.php @@ -6,13 +6,13 @@ use Laminas\ServiceManager\AbstractPluginManager; use Laminas\ServiceManager\Exception\InvalidServiceException; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; + +use function sprintf; class AssertionPluginManager extends AbstractPluginManager implements AssertionPluginManagerInterface { - /** - * @param mixed $instance - * @return void - */ public function validate(mixed $instance): void { if ($instance instanceof AssertionInterface) { @@ -20,18 +20,17 @@ public function validate(mixed $instance): void } throw new InvalidServiceException(sprintf( 'Assertions must implement "Lmc\Rbac\Assertion\AssertionInterface", but "%s" was given', - get_class($instance) + $instance::class )); } /** - * @param $name + * @param string $name * @param array|null $options - * @return AssertionInterface - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ - public function get($name, array $options = null): AssertionInterface + public function get($name, ?array $options = null): AssertionInterface { return parent::get($name, $options); } diff --git a/src/Assertion/AssertionSet.php b/src/Assertion/AssertionSet.php index bf61320..abfc42f 100644 --- a/src/Assertion/AssertionSet.php +++ b/src/Assertion/AssertionSet.php @@ -25,23 +25,28 @@ use Lmc\Rbac\Identity\IdentityInterface; use Lmc\Rbac\Permission\PermissionInterface; +use function count; +use function gettype; +use function is_array; +use function is_callable; +use function is_object; +use function is_string; +use function sprintf; + class AssertionSet implements AssertionInterface { /** * Condition constants */ - public const CONDITION_OR = 'condition_or'; + public const CONDITION_OR = 'condition_or'; public const CONDITION_AND = 'condition_and'; - /** - * @var array - */ + /** @var array */ private array $assertions; - private $condition = self::CONDITION_AND; + private string $condition = self::CONDITION_AND; /** - * @param AssertionPluginManagerInterface $assertionPluginManager * @param array $assertions */ public function __construct( @@ -49,8 +54,10 @@ public function __construct( array $assertions ) { if (isset($assertions['condition'])) { - if ($assertions['condition'] !== AssertionSet::CONDITION_AND - && $assertions['condition'] !== AssertionSet::CONDITION_OR) { + if ( + $assertions['condition'] !== self::CONDITION_AND + && $assertions['condition'] !== self::CONDITION_OR + ) { throw new Exception\InvalidArgumentException('Invalid assertion condition given.'); } @@ -62,8 +69,11 @@ public function __construct( $this->assertions = $assertions; } - public function assert(PermissionInterface|string $permission, IdentityInterface $identity = null, $context = null): bool - { + public function assert( + PermissionInterface|string $permission, + ?IdentityInterface $identity = null, + mixed $context = null + ): bool { if (empty($this->assertions)) { return false; } @@ -85,23 +95,23 @@ public function assert(PermissionInterface|string $permission, IdentityInterface break; case is_array($assertion): $this->assertions[$index] = $assertion = new AssertionSet($this->assertionPluginManager, $assertion); - $asserted = $assertion->assert($permission, $identity, $context); + $asserted = $assertion->assert($permission, $identity, $context); break; default: throw new Exception\InvalidArgumentException(sprintf( 'Assertion must be callable, string, array or implement Lmc\Rbac\Assertion\AssertionInterface, "%s" given', - is_object($assertion) ? get_class($assertion) : gettype($assertion) + is_object($assertion) ? $assertion::class : gettype($assertion) )); } switch ($this->condition) { - case AssertionSet::CONDITION_AND: + case self::CONDITION_AND: if (false === $asserted) { return false; } break; - case AssertionSet::CONDITION_OR: + case self::CONDITION_OR: if (true === $asserted) { return true; } @@ -111,7 +121,7 @@ public function assert(PermissionInterface|string $permission, IdentityInterface $assertedCount++; } - if (AssertionSet::CONDITION_AND === $this->condition && count($this->assertions) === $assertedCount) { + if (self::CONDITION_AND === $this->condition && count($this->assertions) === $assertedCount) { return true; } diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index aaa85df..6d5ebc0 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -21,6 +21,22 @@ namespace Lmc\Rbac; +use Laminas\ServiceManager\Factory\InvokableFactory; +use Lmc\Rbac\Assertion\AssertionPluginManager; +use Lmc\Rbac\Assertion\AssertionPluginManagerFactory; +use Lmc\Rbac\Assertion\AssertionPluginManagerInterface; +use Lmc\Rbac\Options\ModuleOptions; +use Lmc\Rbac\Options\ModuleOptionsFactory; +use Lmc\Rbac\Rbac; +use Lmc\Rbac\Role\InMemoryRoleProvider; +use Lmc\Rbac\Role\InMemoryRoleProviderFactory; +use Lmc\Rbac\Role\ObjectRepositoryRoleProvider; +use Lmc\Rbac\Role\ObjectRepositoryRoleProviderFactory; +use Lmc\Rbac\Service\AuthorizationServiceFactory; +use Lmc\Rbac\Service\AuthorizationServiceInterface; +use Lmc\Rbac\Service\RoleServiceFactory; +use Lmc\Rbac\Service\RoleServiceInterface; + /** * The configuration provider for the LmcRbac module * @@ -32,24 +48,24 @@ public function __invoke(): array { return [ 'dependencies' => $this->getDependencyConfig(), - 'lmc_rbac' => $this->getModuleConfig(), + 'lmc_rbac' => $this->getModuleConfig(), ]; } public function getDependencyConfig(): array { return [ - 'aliases' => [ - \Lmc\Rbac\Assertion\AssertionPluginManagerInterface::class => \Lmc\Rbac\Assertion\AssertionPluginManager::class, + 'aliases' => [ + AssertionPluginManagerInterface::class => AssertionPluginManager::class, ], 'factories' => [ - \Lmc\Rbac\Assertion\AssertionPluginManager::class => \Lmc\Rbac\Assertion\AssertionPluginManagerFactory::class, - \Lmc\Rbac\Options\ModuleOptions::class => \Lmc\Rbac\Options\ModuleOptionsFactory::class, - \Lmc\Rbac\Role\InMemoryRoleProvider::class => \Lmc\Rbac\Role\InMemoryRoleProviderFactory::class, - \Lmc\Rbac\Role\ObjectRepositoryRoleProvider::class => \Lmc\Rbac\Role\ObjectRepositoryRoleProviderFactory::class, - \Lmc\Rbac\Service\AuthorizationServiceInterface::class => \Lmc\Rbac\Service\AuthorizationServiceFactory::class, - \Lmc\Rbac\Service\RoleServiceInterface::class => \Lmc\Rbac\Service\RoleServiceFactory::class, - \Lmc\Rbac\Rbac::class => \Laminas\ServiceManager\Factory\InvokableFactory::class, + AssertionPluginManager::class => AssertionPluginManagerFactory::class, + ModuleOptions::class => ModuleOptionsFactory::class, + InMemoryRoleProvider::class => InMemoryRoleProviderFactory::class, + ObjectRepositoryRoleProvider::class => ObjectRepositoryRoleProviderFactory::class, + AuthorizationServiceInterface::class => AuthorizationServiceFactory::class, + RoleServiceInterface::class => RoleServiceFactory::class, + Rbac::class => InvokableFactory::class, ], ]; } diff --git a/src/Exception/ExceptionInterface.php b/src/Exception/ExceptionInterface.php index 1530467..059f7c9 100644 --- a/src/Exception/ExceptionInterface.php +++ b/src/Exception/ExceptionInterface.php @@ -24,8 +24,6 @@ /** * Base exception interface for LmcRbac * - * @author Michaël Gallego - * @licence MIT */ interface ExceptionInterface { diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php index 9bfb483..b52fe9f 100644 --- a/src/Exception/InvalidArgumentException.php +++ b/src/Exception/InvalidArgumentException.php @@ -25,9 +25,6 @@ /** * InvalidArgumentException - * - * @author Michaël Gallego - * @licence MIT */ class InvalidArgumentException extends BaseInvalidArgumentException implements ExceptionInterface { diff --git a/src/Exception/RoleNotFoundException.php b/src/Exception/RoleNotFoundException.php index ae10677..38e3d4f 100644 --- a/src/Exception/RoleNotFoundException.php +++ b/src/Exception/RoleNotFoundException.php @@ -25,14 +25,9 @@ /** * Exception that is thrown when a role cannot be found (for instance from a provider) - * - * @author Michaël Gallego - * @licence MIT */ class RoleNotFoundException extends BaseRuntimeException implements ExceptionInterface { - /** - * @var string - */ + /** @var string */ protected $message = 'No role could be found'; } diff --git a/src/Exception/RuntimeException.php b/src/Exception/RuntimeException.php index 788ee2f..78a4b80 100644 --- a/src/Exception/RuntimeException.php +++ b/src/Exception/RuntimeException.php @@ -26,8 +26,6 @@ /** * RuntimeException * - * @author Michaël Gallego - * @licence MIT */ class RuntimeException extends BaseRuntimeException implements ExceptionInterface { diff --git a/src/Identity/IdentityInterface.php b/src/Identity/IdentityInterface.php index 5327b09..831b6aa 100644 --- a/src/Identity/IdentityInterface.php +++ b/src/Identity/IdentityInterface.php @@ -26,8 +26,6 @@ /** * Interface for an identity * - * @author Michaël Gallego - * @licence MIT */ interface IdentityInterface { diff --git a/src/Module.php b/src/Module.php index 8148ee6..011c1c5 100644 --- a/src/Module.php +++ b/src/Module.php @@ -29,10 +29,9 @@ final class Module public function getConfig(): array { $provider = new ConfigProvider(); - return [ 'service_manager' => $provider->getDependencyConfig(), - 'lmc_rbac' => $provider->getModuleConfig(), + 'lmc_rbac' => $provider->getModuleConfig(), ]; } } diff --git a/src/Options/ModuleOptions.php b/src/Options/ModuleOptions.php index 5144c5f..18a18ba 100644 --- a/src/Options/ModuleOptions.php +++ b/src/Options/ModuleOptions.php @@ -22,19 +22,16 @@ namespace Lmc\Rbac\Options; use Laminas\Stdlib\AbstractOptions; +use Lmc\Rbac\Role\InMemoryRoleProvider; /** * Options for LmcRbac module * - * @author Michaël Gallego - * @licence MIT */ class ModuleOptions extends AbstractOptions { /** * Guest role (used when no identity is found) - * - * @var string */ protected string $guestRole = 'guest'; @@ -52,7 +49,7 @@ class ModuleOptions extends AbstractOptions * @var array */ protected array $roleProvider = [ - 'Lmc\Rbac\Role\InMemoryRoleProvider' => [], + InMemoryRoleProvider::class => [], ]; /** @@ -71,7 +68,6 @@ public function __construct($options = null) * Set the assertions options * * @param array $assertionMap - * @return void */ public function setAssertionMap(array $assertionMap): void { @@ -90,9 +86,6 @@ public function getAssertionMap(): array /** * Set the guest role (used when no identity is found) - * - * @param string $guestRole - * @return void */ public function setGuestRole(string $guestRole): void { @@ -101,8 +94,6 @@ public function setGuestRole(string $guestRole): void /** * Get the guest role (used when no identity is found) - * - * @return string */ public function getGuestRole(): string { diff --git a/src/Options/ModuleOptionsFactory.php b/src/Options/ModuleOptionsFactory.php index d905f59..e44d456 100644 --- a/src/Options/ModuleOptionsFactory.php +++ b/src/Options/ModuleOptionsFactory.php @@ -21,14 +21,10 @@ namespace Lmc\Rbac\Options; -use Lmc\Rbac\Options\ModuleOptions; use Psr\Container\ContainerInterface; /** * Factory for the module options - * - * @author Michaël Gallego - * @licence MIT */ class ModuleOptionsFactory { diff --git a/src/Permission/PermissionInterface.php b/src/Permission/PermissionInterface.php index 21ad8b7..bebabee 100644 --- a/src/Permission/PermissionInterface.php +++ b/src/Permission/PermissionInterface.php @@ -1,17 +1,17 @@ */ interface PermissionInterface { /** * Get the permission name - * @return string */ public function __toString(): string; } diff --git a/src/Rbac.php b/src/Rbac.php index ac95aab..2e8ebc5 100644 --- a/src/Rbac.php +++ b/src/Rbac.php @@ -26,6 +26,8 @@ use Lmc\Rbac\Role\RoleInterface; use Traversable; +use function iterator_to_array; + /** * Rbac object. It is used to check a permission against roles */ @@ -33,10 +35,6 @@ class Rbac implements RbacInterface { /** * Determines if access is granted by checking the roles for permission. - * - * @param RoleInterface|iterable $roles - * @param string|PermissionInterface $permission - * @return bool */ public function isGranted(RoleInterface|iterable $roles, string|PermissionInterface $permission): bool { @@ -49,7 +47,7 @@ public function isGranted(RoleInterface|iterable $roles, string|PermissionInterf } foreach ($this->flattenRoles($roles) as $role) { - /* @var RoleInterface $role */ + /** @var RoleInterface $role */ if ($role->hasPermission($permission)) { return true; } diff --git a/src/RbacInterface.php b/src/RbacInterface.php index 7d289b3..9f9df40 100644 --- a/src/RbacInterface.php +++ b/src/RbacInterface.php @@ -1,20 +1,16 @@ - * @licence MIT */ final class InMemoryRoleProvider implements RoleProviderInterface { - /** - * @var array - */ + /** @var array */ private array $rolesConfig = []; /** @@ -55,7 +50,7 @@ public function __construct(array $rolesConfig) } /** - * @inheritdoc + * @inheritDoc */ public function getRoles(iterable $roleNames): iterable { @@ -71,7 +66,7 @@ public function getRoles(iterable $roleNames): iterable $roleConfig = $this->rolesConfig[$roleName]; if (isset($roleConfig['children'])) { - $role = new Role($roleName); + $role = new Role($roleName); $childRoles = (array) $roleConfig['children']; foreach ($this->getRoles($childRoles) as $childRole) { diff --git a/src/Role/InMemoryRoleProviderFactory.php b/src/Role/InMemoryRoleProviderFactory.php index c809c53..ea19bcd 100644 --- a/src/Role/InMemoryRoleProviderFactory.php +++ b/src/Role/InMemoryRoleProviderFactory.php @@ -28,8 +28,6 @@ /** * Factory used to create an in memory role provider * - * @author Vytautas Stankus - * @licence MIT */ class InMemoryRoleProviderFactory { diff --git a/src/Role/ObjectRepositoryRoleProvider.php b/src/Role/ObjectRepositoryRoleProvider.php index 2ed8a49..0b71482 100644 --- a/src/Role/ObjectRepositoryRoleProvider.php +++ b/src/Role/ObjectRepositoryRoleProvider.php @@ -24,27 +24,23 @@ use Doctrine\Persistence\ObjectRepository; use Lmc\Rbac\Exception\RoleNotFoundException; +use function array_diff; +use function count; +use function implode; +use function sprintf; + /** * Role provider that uses Doctrine object repository to fetch roles - * - * @author Michaël Gallego - * @licence MIT */ final class ObjectRepositoryRoleProvider implements RoleProviderInterface { - /** - * @var ObjectRepository - */ + /** @var ObjectRepository */ private $objectRepository; - /** - * @var string - */ + /** @var string */ private $roleNameProperty; - /** - * @var array - */ + /** @var array */ private $roleCache = []; public function __construct(ObjectRepository $objectRepository, string $roleNameProperty) diff --git a/src/Role/ObjectRepositoryRoleProviderFactory.php b/src/Role/ObjectRepositoryRoleProviderFactory.php index 1a23ea8..e4fde12 100644 --- a/src/Role/ObjectRepositoryRoleProviderFactory.php +++ b/src/Role/ObjectRepositoryRoleProviderFactory.php @@ -28,16 +28,13 @@ /** * Factory used to create an object repository role provider - * - * @author Michaël Gallego - * @licence MIT */ class ObjectRepositoryRoleProviderFactory { public function __invoke(ContainerInterface $container): ObjectRepositoryRoleProvider { $moduleOptions = $container->get(ModuleOptions::class); - $options = $moduleOptions->getRoleProvider()[ObjectRepositoryRoleProvider::class] ?? []; + $options = $moduleOptions->getRoleProvider()[ObjectRepositoryRoleProvider::class] ?? []; if (! isset($options['role_name_property'])) { throw new Exception\RuntimeException('The "role_name_property" option is missing'); @@ -50,7 +47,7 @@ public function __invoke(ContainerInterface $container): ObjectRepositoryRolePro } if (isset($options['object_manager'], $options['class_name'])) { - $objectManager = $container->get($options['object_manager']); + $objectManager = $container->get($options['object_manager']); $objectRepository = $objectManager->getRepository($options['class_name']); return new ObjectRepositoryRoleProvider($objectRepository, $options['role_name_property']); diff --git a/src/Role/Role.php b/src/Role/Role.php index 874702f..2425d4f 100644 --- a/src/Role/Role.php +++ b/src/Role/Role.php @@ -29,19 +29,12 @@ */ class Role implements RoleInterface { - /** - * @var string - */ private string $name; - /** - * @var string[] - */ + /** @var string[] */ private array $permissions = []; - /** - * @var array|RoleInterface[] - */ + /** @var array|RoleInterface[] */ private array $children = []; public function __construct(string $name) diff --git a/src/Role/RoleProviderInterface.php b/src/Role/RoleProviderInterface.php index 8a7d23f..d2e0e2e 100644 --- a/src/Role/RoleProviderInterface.php +++ b/src/Role/RoleProviderInterface.php @@ -27,8 +27,6 @@ * Data can come from anywhere. LmcRbac is bundled with two providers that allow to load roles from database * or from memory * - * @author Michaël Gallego - * @licence MIT */ interface RoleProviderInterface { diff --git a/src/Service/AuthorizationService.php b/src/Service/AuthorizationService.php index 1697412..bef3881 100644 --- a/src/Service/AuthorizationService.php +++ b/src/Service/AuthorizationService.php @@ -28,12 +28,12 @@ use Lmc\Rbac\Permission\PermissionInterface; use Lmc\Rbac\RbacInterface; +use function array_merge; +use function is_array; + /** * Authorization service is a simple service that internally uses Rbac to check if identity is * granted a permission - * - * @author Michaël Gallego - * @licence MIT */ class AuthorizationService implements AuthorizationServiceInterface { @@ -43,9 +43,7 @@ class AuthorizationService implements AuthorizationServiceInterface private AssertionPluginManagerInterface $assertionPluginManager; - /** - * @var array - */ + /** @var array */ private array $assertions; public function __construct( @@ -54,17 +52,16 @@ public function __construct( AssertionPluginManagerInterface $assertionPluginManager, array $assertions = [] ) { - $this->rbac = $rbac; - $this->roleService = $roleService; + $this->rbac = $rbac; + $this->roleService = $roleService; $this->assertionPluginManager = $assertionPluginManager; - $this->assertions = $assertions; + $this->assertions = $assertions; } /** * Set assertions, either merging or replacing (default) + * * @param array $assertions - * @param bool $merge - * @return void */ public function setAssertions(array $assertions, bool $merge = false): void { @@ -75,19 +72,16 @@ public function setAssertions(array $assertions, bool $merge = false): void /** * Set assertion for a given permission - * @param PermissionInterface|string $permission - * @param AssertionInterface|callable|string $assertion - * @return void */ - public function setAssertion(PermissionInterface|string $permission, AssertionInterface|callable|string $assertion): void - { + public function setAssertion( + PermissionInterface|string $permission, + AssertionInterface|callable|string $assertion + ): void { $this->assertions[(string) $permission] = $assertion; } /** * Check if there are assertions for the permission - * @param PermissionInterface|string $permission - * @return bool */ public function hasAssertion(PermissionInterface|string $permission): bool { @@ -96,6 +90,7 @@ public function hasAssertion(PermissionInterface|string $permission): bool /** * Get the assertions + * * @return array */ public function getAssertions(): array @@ -105,16 +100,17 @@ public function getAssertions(): array /** * Get the assertions for the given permission - * @param PermissionInterface|string $permission - * @return AssertionInterface|callable|string|null */ public function getAssertion(PermissionInterface|string $permission): AssertionInterface|callable|string|null { - return $this->hasAssertion(($permission)) ? $this->assertions[(string) $permission] : null; + return $this->hasAssertion($permission) ? $this->assertions[(string) $permission] : null; } - public function isGranted(IdentityInterface|null $identity, string|PermissionInterface $permission, mixed $context = null): bool - { + public function isGranted( + IdentityInterface|null $identity, + string|PermissionInterface $permission, + mixed $context = null + ): bool { $roles = $this->roleService->getIdentityRoles($identity, $context); if (empty($roles)) { @@ -129,7 +125,7 @@ public function isGranted(IdentityInterface|null $identity, string|PermissionInt return true; } - if (\is_array($this->assertions[(string) $permission])) { + if (is_array($this->assertions[(string) $permission])) { $permissionAssertions = $this->assertions[(string) $permission]; } else { $permissionAssertions = [$this->assertions[(string) $permission]]; diff --git a/src/Service/AuthorizationServiceAwareInterface.php b/src/Service/AuthorizationServiceAwareInterface.php index ba6e8c3..b63afae 100644 --- a/src/Service/AuthorizationServiceAwareInterface.php +++ b/src/Service/AuthorizationServiceAwareInterface.php @@ -18,18 +18,10 @@ namespace Lmc\Rbac\Service; -/** - * @author Eric Richer - * - */ - interface AuthorizationServiceAwareInterface { /** * Set the AuthorizationService - * - * @param AuthorizationServiceInterface $authorizationService - * @return void */ public function setAuthorizationService(AuthorizationServiceInterface $authorizationService): void; } diff --git a/src/Service/AuthorizationServiceAwareTrait.php b/src/Service/AuthorizationServiceAwareTrait.php index b7df369..ba101ff 100644 --- a/src/Service/AuthorizationServiceAwareTrait.php +++ b/src/Service/AuthorizationServiceAwareTrait.php @@ -1,48 +1,40 @@ - * - */ +namespace Lmc\Rbac\Service; trait AuthorizationServiceAwareTrait { - /** - * @var AuthorizationServiceInterface|null - */ protected ?AuthorizationServiceInterface $authorizationService = null; /** * Set the AuthorizationService - * @param AuthorizationServiceInterface $authorizationService - * @return void */ - public function setAuthorizationService(AuthorizationServiceInterface $authorizationService):void + public function setAuthorizationService(AuthorizationServiceInterface $authorizationService): void { $this->authorizationService = $authorizationService; } /** * Get the AuthorizationService - * @return AuthorizationServiceInterface|null */ public function getAuthorizationService(): ?AuthorizationServiceInterface { diff --git a/src/Service/AuthorizationServiceDelegatorFactory.php b/src/Service/AuthorizationServiceDelegatorFactory.php index b5dfaee..d2999fb 100644 --- a/src/Service/AuthorizationServiceDelegatorFactory.php +++ b/src/Service/AuthorizationServiceDelegatorFactory.php @@ -1,4 +1,5 @@ - * - */ +namespace Lmc\Rbac\Service; use Laminas\ServiceManager\Exception\ServiceNotCreatedException; use Laminas\ServiceManager\Factory\DelegatorFactoryInterface; use Psr\Container\ContainerInterface; +use function call_user_func; + class AuthorizationServiceDelegatorFactory implements DelegatorFactoryInterface { - /** * @inheritDoc */ - public function __invoke(ContainerInterface $container, $name, callable $callback, ?array $options = null): AuthorizationServiceAwareInterface - { + public function __invoke( + ContainerInterface $container, + $name, + callable $callback, + ?array $options = null + ): AuthorizationServiceAwareInterface { $instance = call_user_func($callback); - if (! $instance instanceof AuthorizationServiceAwareInterface) { throw new ServiceNotCreatedException("The service $name must implement Laminas\Authorization\Service\AuthorizationServiceAwareInterface"); } diff --git a/src/Service/AuthorizationServiceFactory.php b/src/Service/AuthorizationServiceFactory.php index 8381d34..536d799 100644 --- a/src/Service/AuthorizationServiceFactory.php +++ b/src/Service/AuthorizationServiceFactory.php @@ -1,5 +1,4 @@ - * @licence MIT */ class AuthorizationServiceFactory { diff --git a/src/Service/AuthorizationServiceInterface.php b/src/Service/AuthorizationServiceInterface.php index 1fdec84..1855a82 100644 --- a/src/Service/AuthorizationServiceInterface.php +++ b/src/Service/AuthorizationServiceInterface.php @@ -27,55 +27,45 @@ /** * Minimal interface for an authorization service - * - * @author Michaël Gallego - * @licence MIT */ interface AuthorizationServiceInterface { /** * Check if the permission is granted to the current identity - * - * @param IdentityInterface|null $identity - * @param PermissionInterface|string $permission - * @param mixed|null $context - * @return bool */ - public function isGranted(?IdentityInterface $identity, PermissionInterface|string $permission, mixed $context = null): bool; + public function isGranted( + ?IdentityInterface $identity, + PermissionInterface|string $permission, + mixed $context = null + ): bool; /** * Set assertions, either merging or replacing (default) - * @param array $assertions - * @param bool $merge - * @return void */ public function setAssertions(array $assertions, bool $merge = false): void; /** * Set assertion for a given permission - * @param PermissionInterface|string $permission - * @param AssertionInterface|callable|string $assertion - * @return void */ - public function setAssertion(PermissionInterface|string $permission, AssertionInterface|callable|string $assertion): void; + public function setAssertion( + PermissionInterface|string $permission, + AssertionInterface|callable|string $assertion + ): void; /** * Check if there are assertions for the permission - * @param PermissionInterface|string $permission - * @return bool */ public function hasAssertion(PermissionInterface|string $permission): bool; /** * Get the assertions + * * @return array */ public function getAssertions(): array; /** * Get the assertions for the given permission - * @param PermissionInterface|string $permission - * @return AssertionInterface|callable|string|null */ public function getAssertion(PermissionInterface|string $permission): AssertionInterface|callable|string|null; } diff --git a/src/Service/RoleService.php b/src/Service/RoleService.php index a0dd4e3..284b65c 100644 --- a/src/Service/RoleService.php +++ b/src/Service/RoleService.php @@ -26,31 +26,23 @@ use Lmc\Rbac\Role\RoleProviderInterface; use Traversable; +use function array_merge; + /** * Role service - * - * @author Michaël Gallego - * @licence MIT */ class RoleService implements RoleServiceInterface { protected RoleProviderInterface $roleProvider; - protected string $guestRole = ''; - - public function __construct( - RoleProviderInterface $roleProvider, - string $guestRole - ) { + public function __construct(RoleProviderInterface $roleProvider, string $guestRole) + { $this->roleProvider = $roleProvider; - $this->guestRole = $guestRole; + $this->guestRole = $guestRole; } /** * Set the guest role - * - * @param string $guestRole - * @return void */ public function setGuestRole(string $guestRole): void { @@ -59,8 +51,6 @@ public function setGuestRole(string $guestRole): void /** * Get the guest role - * - * @return string */ public function getGuestRole(): string { @@ -70,11 +60,9 @@ public function getGuestRole(): string /** * Get the identity roles from the current identity, applying some more logic * - * @param IdentityInterface|null $identity - * @param mixed|null $context * @return RoleInterface[] */ - public function getIdentityRoles(IdentityInterface $identity = null, mixed $context = null): iterable + public function getIdentityRoles(?IdentityInterface $identity = null, mixed $context = null): iterable { // If no identity is provided, get the guest role if (null === $identity) { @@ -93,10 +81,9 @@ public function getIdentityRoles(IdentityInterface $identity = null, mixed $cont protected function convertRoles(iterable $roles): iterable { $collectedRoles = []; - $toCollect = []; - + $toCollect = []; foreach ($roles as $role) { - // If it's already a RoleInterface, nothing to do as a RoleInterface contains everything already + // If it's already a RoleInterface, nothing to do as a RoleInterface contains everything already if ($role instanceof RoleInterface) { $collectedRoles[] = $role; continue; diff --git a/src/Service/RoleServiceFactory.php b/src/Service/RoleServiceFactory.php index 7e86215..41cf89d 100644 --- a/src/Service/RoleServiceFactory.php +++ b/src/Service/RoleServiceFactory.php @@ -1,5 +1,4 @@ - * @licence MIT */ class RoleServiceFactory { diff --git a/src/Service/RoleServiceInterface.php b/src/Service/RoleServiceInterface.php index 7385a78..ccc549b 100644 --- a/src/Service/RoleServiceInterface.php +++ b/src/Service/RoleServiceInterface.php @@ -26,19 +26,13 @@ /** * Role service - * - * @author Michaël Gallego - * @licence MIT */ interface RoleServiceInterface { /** * Get the identity roles from the current identity, applying some more logic * - * @param null|IdentityInterface $identity - * @param mixed|null $context * @return RoleInterface[] */ - public function getIdentityRoles(IdentityInterface $identity = null, mixed $context = null): iterable; - + public function getIdentityRoles(?IdentityInterface $identity = null, mixed $context = null): iterable; } diff --git a/test/Assertion/AssertionPluginManagerFactoryTest.php b/test/Assertion/AssertionPluginManagerFactoryTest.php index 45fc05a..9e48b8f 100644 --- a/test/Assertion/AssertionPluginManagerFactoryTest.php +++ b/test/Assertion/AssertionPluginManagerFactoryTest.php @@ -22,14 +22,12 @@ namespace LmcTest\Rbac\Assertion; use Laminas\ServiceManager\ServiceManager; -use Lmc\Rbac\Assertion\AssertionContainer; -use Lmc\Rbac\Assertion\AssertionContainerFactory; use Lmc\Rbac\Assertion\AssertionPluginManager; use Lmc\Rbac\Assertion\AssertionPluginManagerFactory; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; -#[CoversClass('\Lmc\Rbac\Assertion\AssertionPluginManagerFactory')] +#[CoversClass(AssertionPluginManagerFactory::class)] class AssertionPluginManagerFactoryTest extends TestCase { public function testFactory(): void @@ -41,7 +39,7 @@ public function testFactory(): void ], ]); - $factory = new AssertionPluginManagerFactory(); + $factory = new AssertionPluginManagerFactory(); $pluginManager = $factory($serviceManager, AssertionPluginManager::class); $this->assertInstanceOf(AssertionPluginManager::class, $pluginManager); diff --git a/test/Assertion/AssertionPluginManagerTest.php b/test/Assertion/AssertionPluginManagerTest.php index ac5feea..eb022ae 100644 --- a/test/Assertion/AssertionPluginManagerTest.php +++ b/test/Assertion/AssertionPluginManagerTest.php @@ -21,7 +21,6 @@ namespace LmcTest\Rbac\Assertion; -use Interop\Container\ContainerInterface; use Laminas\ServiceManager\Exception\InvalidServiceException; use Laminas\ServiceManager\Factory\InvokableFactory; use Lmc\Rbac\Assertion\AssertionInterface; @@ -29,14 +28,16 @@ use LmcTest\Rbac\Asset\SimpleAssertion; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; +use Psr\Container\ContainerInterface; +use stdClass; -#[CoversClass('\Lmc\Rbac\Assertion\AssertionPluginManager')] +#[CoversClass(AssertionPluginManager::class)] class AssertionPluginManagerTest extends TestCase { public function testValidationOfPluginSucceedsIfAssertionInterfaceIsImplemented() { $containerMock = $this->createMock(ContainerInterface::class); - $container = new AssertionPluginManager($containerMock, [ + $container = new AssertionPluginManager($containerMock, [ 'factories' => [ SimpleAssertion::class => InvokableFactory::class, ], @@ -48,13 +49,13 @@ public function testValidationOfPluginSucceedsIfAssertionInterfaceIsImplemented( public function testValidationOfPluginFailsIfAssertionInterfaceIsNotImplemented() { $containerMock = $this->createMock(ContainerInterface::class); - $container = new AssertionPluginManager($containerMock, [ + $container = new AssertionPluginManager($containerMock, [ 'factories' => [ - \stdClass::class => InvokableFactory::class, + stdClass::class => InvokableFactory::class, ], ]); $this->expectException(InvalidServiceException::class); - $instance = $container->get(\stdClass::class); + $instance = $container->get(stdClass::class); } } diff --git a/test/Assertion/AssertionSetTest.php b/test/Assertion/AssertionSetTest.php index e021ee3..30b7de8 100644 --- a/test/Assertion/AssertionSetTest.php +++ b/test/Assertion/AssertionSetTest.php @@ -1,6 +1,7 @@ getMockBuilder(AssertionPluginManagerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionContainer, []); + $assertionSet = new AssertionSet($assertionContainer, []); $this->assertInstanceOf(AssertionInterface::class, $assertionSet); } @@ -46,7 +48,7 @@ public function testImplementsAssertionInterface() public function testWhenNoAssertionsArePresentTheAssertionWillFail() { $assertionContainer = $this->getMockBuilder(AssertionPluginManagerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionContainer, []); + $assertionSet = new AssertionSet($assertionContainer, []); $this->assertFalse($assertionSet->assert('foo')); } @@ -54,7 +56,7 @@ public function testWhenNoAssertionsArePresentTheAssertionWillFail() public function testAcceptsAnAndCondition() { $assertionContainer = $this->getMockBuilder(AssertionPluginManagerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionContainer, ['condition' => AssertionSet::CONDITION_AND]); + $assertionSet = new AssertionSet($assertionContainer, ['condition' => AssertionSet::CONDITION_AND]); $this->assertFalse($assertionSet->assert('foo')); } @@ -62,7 +64,7 @@ public function testAcceptsAnAndCondition() public function testAcceptsAnOrCondition() { $assertionContainer = $this->getMockBuilder(AssertionPluginManagerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionContainer, ['condition' => AssertionSet::CONDITION_OR]); + $assertionSet = new AssertionSet($assertionContainer, ['condition' => AssertionSet::CONDITION_OR]); $this->assertFalse($assertionSet->assert('foo')); } @@ -81,7 +83,7 @@ public function testWhenNoConditionIsGivenAndIsUsed() $barAssertion = new SimpleAssertion(false); $assertionContainer = $this->getMockBuilder(AssertionPluginManagerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionContainer, ['fooFactory', 'barFactory']); + $assertionSet = new AssertionSet($assertionContainer, ['fooFactory', 'barFactory']); $matcher = $this->exactly(2); $assertionContainer->expects($matcher) @@ -105,7 +107,7 @@ public function testAndConditionWillBreakEarlyWithFailure() $barAssertion = new SimpleAssertion(true); $assertionContainer = $this->getMockBuilder(AssertionPluginManagerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionContainer, ['fooFactory', 'barFactory', 'condition' => AssertionSet::CONDITION_AND]); + $assertionSet = new AssertionSet($assertionContainer, ['fooFactory', 'barFactory', 'condition' => AssertionSet::CONDITION_AND]); $assertionContainer->expects($this->once())->method('get')->with('fooFactory')->willReturn($fooAssertion); @@ -121,7 +123,7 @@ public function testOrConditionWillBreakEarlyWithSuccess() $barAssertion = new SimpleAssertion(false); $assertionContainer = $this->getMockBuilder(AssertionPluginManagerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionContainer, ['fooFactory', 'barFactory', 'condition' => AssertionSet::CONDITION_OR]); + $assertionSet = new AssertionSet($assertionContainer, ['fooFactory', 'barFactory', 'condition' => AssertionSet::CONDITION_OR]); $assertionContainer->expects($this->once())->method('get')->with('fooFactory')->willReturn($fooAssertion); @@ -136,7 +138,7 @@ public function testAssertionsAsStringsAreCached() $fooAssertion = new SimpleAssertion(true); $assertionContainer = $this->getMockBuilder(AssertionPluginManagerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionContainer, ['fooFactory']); + $assertionSet = new AssertionSet($assertionContainer, ['fooFactory']); $assertionContainer->expects($this->once())->method('get')->with('fooFactory')->willReturn($fooAssertion); @@ -152,7 +154,7 @@ public function testUsesAssertionsAsStrings() $fooAssertion = new SimpleAssertion(true); $assertionContainer = $this->getMockBuilder(AssertionPluginManagerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionContainer, ['fooFactory']); + $assertionSet = new AssertionSet($assertionContainer, ['fooFactory']); $assertionContainer->expects($this->once())->method('get')->with('fooFactory')->willReturn($fooAssertion); @@ -166,7 +168,7 @@ public function testUsesAssertionsAsInstances() $fooAssertion = new SimpleAssertion(true); $assertionContainer = $this->getMockBuilder(AssertionPluginManagerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionContainer, [$fooAssertion]); + $assertionSet = new AssertionSet($assertionContainer, [$fooAssertion]); $this->assertTrue($assertionSet->assert('permission')); @@ -175,15 +177,15 @@ public function testUsesAssertionsAsInstances() public function testUsesAssertionsAsCallables() { - $called = false; - $fooAssertion = function ($permission, IdentityInterface $identity = null, $context = null) use (&$called) { + $called = false; + $fooAssertion = function ($permission, ?IdentityInterface $identity = null, $context = null) use (&$called) { $called = true; return true; }; $assertionContainer = $this->getMockBuilder(AssertionPluginManagerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionContainer, [$fooAssertion]); + $assertionSet = new AssertionSet($assertionContainer, [$fooAssertion]); $this->assertTrue($assertionSet->assert('permission')); @@ -196,7 +198,7 @@ public function testUsesAssertionsAsArrays() $barAssertion = new SimpleAssertion(true); $assertionContainer = $this->getMockBuilder(AssertionPluginManagerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionContainer, ['fooFactory', ['barFactory']]); + $assertionSet = new AssertionSet($assertionContainer, ['fooFactory', ['barFactory']]); $assertionContainer->expects($this->exactly(2)) ->method('get') @@ -213,10 +215,10 @@ public function testUsesAssertionsAsArrays() public function testThrowExceptionForInvalidAssertion() { - $fooAssertion = new \stdClass(); + $fooAssertion = new stdClass(); $assertionContainer = $this->getMockBuilder(AssertionPluginManagerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionContainer, [$fooAssertion]); + $assertionSet = new AssertionSet($assertionContainer, [$fooAssertion]); $this->expectException(InvalidArgumentException::class); $this->assertTrue($assertionSet->assert('permission')); @@ -226,7 +228,7 @@ public function testThrowExceptionForInvalidAssertion() public function testMatrix(array $assertions, bool $expectedResult, array $assertionCalledCount) { $assertionContainer = $this->getMockBuilder(AssertionPluginManagerInterface::class)->getMock(); - $assertionSet = new AssertionSet($assertionContainer, $assertions); + $assertionSet = new AssertionSet($assertionContainer, $assertions); $this->assertSame($expectedResult, $assertionSet->assert('permission')); diff --git a/test/Asset/DummyAuthorizationServiceClass.php b/test/Asset/DummyAuthorizationServiceClass.php index 776ab6c..88111fd 100644 --- a/test/Asset/DummyAuthorizationServiceClass.php +++ b/test/Asset/DummyAuthorizationServiceClass.php @@ -1,26 +1,24 @@ - */ +namespace LmcTest\Rbac\Asset; use Lmc\Rbac\Service\AuthorizationServiceAwareInterface; use Lmc\Rbac\Service\AuthorizationServiceAwareTrait; diff --git a/test/Asset/Identity.php b/test/Asset/Identity.php index 94025c6..930662c 100644 --- a/test/Asset/Identity.php +++ b/test/Asset/Identity.php @@ -25,9 +25,7 @@ class Identity implements IdentityInterface { - /** - * @var array - */ + /** @var array */ private array $roles; public function __construct(array $roles = []) diff --git a/test/Asset/Permission.php b/test/Asset/Permission.php index 4710270..76cdc6e 100644 --- a/test/Asset/Permission.php +++ b/test/Asset/Permission.php @@ -1,6 +1,5 @@ willAssert = $willAssert; } - public function assert(string|PermissionInterface $permission, IdentityInterface $identity = null, $context = null): bool + public function assert(string|PermissionInterface $permission, ?IdentityInterface $identity = null, $context = null): bool { $this->called++; diff --git a/test/ConfigProviderTest.php b/test/ConfigProviderTest.php index 5583314..09df2d3 100644 --- a/test/ConfigProviderTest.php +++ b/test/ConfigProviderTest.php @@ -19,30 +19,45 @@ declare(strict_types=1); -namespace LmcTest; +namespace LmcTest\Rbac; +use Laminas\ServiceManager\Factory\InvokableFactory; +use Lmc\Rbac\Assertion\AssertionPluginManager; +use Lmc\Rbac\Assertion\AssertionPluginManagerFactory; +use Lmc\Rbac\Assertion\AssertionPluginManagerInterface; use Lmc\Rbac\ConfigProvider; +use Lmc\Rbac\Options\ModuleOptions; +use Lmc\Rbac\Options\ModuleOptionsFactory; +use Lmc\Rbac\Rbac; +use Lmc\Rbac\Role\InMemoryRoleProvider; +use Lmc\Rbac\Role\InMemoryRoleProviderFactory; +use Lmc\Rbac\Role\ObjectRepositoryRoleProvider; +use Lmc\Rbac\Role\ObjectRepositoryRoleProviderFactory; +use Lmc\Rbac\Service\AuthorizationServiceFactory; +use Lmc\Rbac\Service\AuthorizationServiceInterface; +use Lmc\Rbac\Service\RoleServiceFactory; +use Lmc\Rbac\Service\RoleServiceInterface; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; -#[CoversClass('Lmc\Rbac\ConfigProvider')] +#[CoversClass(ConfigProvider::class)] class ConfigProviderTest extends TestCase { public function testProvidesExpectedConfiguration() { $provider = new ConfigProvider(); $expected = [ - 'aliases' => [ - \Lmc\Rbac\Assertion\AssertionPluginManagerInterface::class => \Lmc\Rbac\Assertion\AssertionPluginManager::class, + 'aliases' => [ + AssertionPluginManagerInterface::class => AssertionPluginManager::class, ], 'factories' => [ - \Lmc\Rbac\Assertion\AssertionPluginManager::class => \Lmc\Rbac\Assertion\AssertionPluginManagerFactory::class, - \Lmc\Rbac\Options\ModuleOptions::class => \Lmc\Rbac\Options\ModuleOptionsFactory::class, - \Lmc\Rbac\Role\InMemoryRoleProvider::class => \Lmc\Rbac\Role\InMemoryRoleProviderFactory::class, - \Lmc\Rbac\Role\ObjectRepositoryRoleProvider::class => \Lmc\Rbac\Role\ObjectRepositoryRoleProviderFactory::class, - \Lmc\Rbac\Service\AuthorizationServiceInterface::class => \Lmc\Rbac\Service\AuthorizationServiceFactory::class, - \Lmc\Rbac\Service\RoleServiceInterface::class => \Lmc\Rbac\Service\RoleServiceFactory::class, - \Lmc\Rbac\Rbac::class => \Laminas\ServiceManager\Factory\InvokableFactory::class, + AssertionPluginManager::class => AssertionPluginManagerFactory::class, + ModuleOptions::class => ModuleOptionsFactory::class, + InMemoryRoleProvider::class => InMemoryRoleProviderFactory::class, + ObjectRepositoryRoleProvider::class => ObjectRepositoryRoleProviderFactory::class, + AuthorizationServiceInterface::class => AuthorizationServiceFactory::class, + RoleServiceInterface::class => RoleServiceFactory::class, + Rbac::class => InvokableFactory::class, ], ]; $this->assertEquals($expected, $provider->getDependencyConfig()); @@ -62,7 +77,7 @@ public function testInvocationProvidesDependencyConfiguration() $provider = new ConfigProvider(); $expected = [ 'dependencies' => $provider->getDependencyConfig(), - 'lmc_rbac' => $provider->getModuleConfig(), + 'lmc_rbac' => $provider->getModuleConfig(), ]; $this->assertEquals($expected, $provider()); } diff --git a/test/ModuleTest.php b/test/ModuleTest.php index d887b57..92a2c38 100644 --- a/test/ModuleTest.php +++ b/test/ModuleTest.php @@ -19,23 +19,23 @@ declare(strict_types=1); -namespace LmcTest; +namespace LmcTest\Rbac; use Lmc\Rbac\ConfigProvider; use Lmc\Rbac\Module; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; -#[CoversClass('\Lmc\Rbac\Module')] +#[CoversClass(Module::class)] class ModuleTest extends TestCase { public function testProvidesExpectedConfiguration() { $provider = new ConfigProvider(); - $module = new Module(); + $module = new Module(); $expected = [ 'service_manager' => $provider->getDependencyConfig(), - 'lmc_rbac' => $provider->getModuleConfig(), + 'lmc_rbac' => $provider->getModuleConfig(), ]; $this->assertEquals($expected, $module->getConfig()); } diff --git a/test/Options/ModuleOptionsFactoryTest.php b/test/Options/ModuleOptionsFactoryTest.php index 96fd050..6da1f25 100644 --- a/test/Options/ModuleOptionsFactoryTest.php +++ b/test/Options/ModuleOptionsFactoryTest.php @@ -22,12 +22,12 @@ namespace LmcTest\Rbac\Options; use Laminas\ServiceManager\ServiceManager; -use Lmc\Rbac\Options\ModuleOptionsFactory; use Lmc\Rbac\Options\ModuleOptions; +use Lmc\Rbac\Options\ModuleOptionsFactory; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; -#[CoversClass('\Lmc\Rbac\Options\ModuleOptionsFactory')] +#[CoversClass(ModuleOptionsFactory::class)] class ModuleOptionsFactoryTest extends TestCase { public function testFactory(): void diff --git a/test/Options/ModuleOptionsTest.php b/test/Options/ModuleOptionsTest.php index ea4fe50..4f5fb3d 100644 --- a/test/Options/ModuleOptionsTest.php +++ b/test/Options/ModuleOptionsTest.php @@ -22,28 +22,31 @@ namespace LmcTest\Rbac\Options; use Lmc\Rbac\Options\ModuleOptions; +use Lmc\Rbac\Role\InMemoryRoleProvider; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; -#[CoversClass('\Lmc\Rbac\Options\ModuleOptions')] +use function key; + +#[CoversClass(ModuleOptions::class)] class ModuleOptionsTest extends TestCase { public function testAssertModuleDefaultOptions(): void { - $moduleOptions = new \Lmc\Rbac\Options\ModuleOptions(); + $moduleOptions = new ModuleOptions(); $this->assertEquals('guest', $moduleOptions->getGuestRole()); $this->assertIsArray($moduleOptions->getRoleProvider()); $this->assertIsArray($moduleOptions->getAssertionMap()); - $this->assertEquals('Lmc\Rbac\Role\InMemoryRoleProvider', key($moduleOptions->getRoleProvider())); + $this->assertEquals(InMemoryRoleProvider::class, key($moduleOptions->getRoleProvider())); } public function testSettersAndGetters(): void { $moduleOptions = new ModuleOptions([ - 'guest_role' => 'unknown', - 'role_provider' => [], - 'assertion_map' => [ + 'guest_role' => 'unknown', + 'role_provider' => [], + 'assertion_map' => [ 'foo' => 'bar', ], 'identity_provider' => 'foo', diff --git a/test/Rbac/RbacTest.php b/test/Rbac/RbacTest.php index 19a6326..5dcf822 100644 --- a/test/Rbac/RbacTest.php +++ b/test/Rbac/RbacTest.php @@ -21,12 +21,13 @@ namespace LmcTest\Rbac\Rbac; +use ArrayIterator; use Lmc\Rbac\Rbac; use Lmc\Rbac\Role\Role; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; -#[CoversClass('\Lmc\Rbac\Rbac')] +#[CoversClass(Rbac::class)] class RbacTest extends TestCase { public function testCanConvertSingleRole(): void @@ -53,7 +54,7 @@ public function testCanCheckMultipleRolesWithMatchingPermission(): void $role2->addPermission('permission'); $roles = [$role1, $role2]; - $rbac = new Rbac(); + $rbac = new Rbac(); $this->assertTrue($rbac->isGranted($roles, 'permission')); } @@ -64,7 +65,7 @@ public function testReturnFalseIfNoRoleHasPermission(): void $role2 = new Role('Bar'); $roles = [$role1, $role2]; - $rbac = new \Lmc\Rbac\Rbac(); + $rbac = new Rbac(); $this->assertFalse($rbac->isGranted($roles, 'permission')); } @@ -77,7 +78,7 @@ public function testCanCheckHierarchicalRole(): void $parentRole = new Role('Foo'); $parentRole->addChild($childRole); - $rbac = new \Lmc\Rbac\Rbac(); + $rbac = new Rbac(); $this->assertTrue($rbac->isGranted($parentRole, 'permission')); } @@ -101,8 +102,8 @@ public function testCanCheckTraversableAsRolesList(): void $role2 = new Role('Bar'); $role2->addPermission('permission'); - $roles = new \ArrayIterator([$role1, $role2]); - $rbac = new Rbac(); + $roles = new ArrayIterator([$role1, $role2]); + $rbac = new Rbac(); $this->assertTrue($rbac->isGranted($roles, 'permission')); } diff --git a/test/Role/InMemoryRoleProviderFactoryTest.php b/test/Role/InMemoryRoleProviderFactoryTest.php index aec269f..2482bb6 100644 --- a/test/Role/InMemoryRoleProviderFactoryTest.php +++ b/test/Role/InMemoryRoleProviderFactoryTest.php @@ -22,13 +22,13 @@ namespace LmcTest\Rbac\Role; use Laminas\ServiceManager\ServiceManager; -use Lmc\Rbac\Role\InMemoryRoleProviderFactory; use Lmc\Rbac\Options\ModuleOptions; use Lmc\Rbac\Role\InMemoryRoleProvider; +use Lmc\Rbac\Role\InMemoryRoleProviderFactory; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; -#[CoversClass('\Lmc\Rbac\Role\InMemoryRoleProviderFactory')] +#[CoversClass(InMemoryRoleProviderFactory::class)] class InMemoryRoleProviderFactoryTest extends TestCase { public function testFactoryUsingObjectRepository(): void @@ -37,12 +37,12 @@ public function testFactoryUsingObjectRepository(): void $container->setService(ModuleOptions::class, new ModuleOptions([ 'role_provider' => [ InMemoryRoleProvider::class => [ - 'admin' => [ - 'children' => ['member'], + 'admin' => [ + 'children' => ['member'], 'permissions' => ['delete'], ], 'member' => [ - 'children' => ['guest'], + 'children' => ['guest'], 'permissions' => ['write'], ], 'guest', diff --git a/test/Role/InMemoryRoleProviderTest.php b/test/Role/InMemoryRoleProviderTest.php index f34166e..9d889d8 100644 --- a/test/Role/InMemoryRoleProviderTest.php +++ b/test/Role/InMemoryRoleProviderTest.php @@ -26,21 +26,21 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; -#[CoversClass('\Lmc\Rbac\Role\InMemoryRoleProvider')] +#[CoversClass(InMemoryRoleProvider::class)] class InMemoryRoleProviderTest extends TestCase { public function testInMemoryProvider(): void { $inMemoryProvider = new InMemoryRoleProvider([ - 'admin' => [ - 'children' => ['member'], + 'admin' => [ + 'children' => ['member'], 'permissions' => ['delete'], ], 'member' => [ - 'children' => ['guest'], + 'children' => ['guest'], 'permissions' => ['write'], ], - 'mrx' => [ + 'mrx' => [ 'permissions' => ['write', 'delete'], ], 'guest', @@ -66,7 +66,6 @@ public function testInMemoryProvider(): void // Test guest role $guestRole = $roles[2]; $this->assertInstanceOf(RoleInterface::class, $guestRole); -// $this->assertNotInstanceOf(HierarchicalRoleInterface::class, $guestRole); $this->assertEquals('guest', $guestRole->getName()); $this->assertFalse($guestRole->hasPermission('write')); $this->assertFalse($guestRole->hasPermission('delete')); @@ -74,7 +73,6 @@ public function testInMemoryProvider(): void // Test mrx role $guestRole = $roles[3]; $this->assertInstanceOf(RoleInterface::class, $guestRole); -// $this->assertNotInstanceOf(HierarchicalRoleInterface::class, $guestRole); $this->assertEquals('mrx', $guestRole->getName()); $this->assertTrue($guestRole->hasPermission('write')); $this->assertTrue($guestRole->hasPermission('delete')); diff --git a/test/Role/ObjectRepositoryRoleProviderFactoryTest.php b/test/Role/ObjectRepositoryRoleProviderFactoryTest.php index bb6ede0..0fb220a 100644 --- a/test/Role/ObjectRepositoryRoleProviderFactoryTest.php +++ b/test/Role/ObjectRepositoryRoleProviderFactoryTest.php @@ -24,14 +24,14 @@ use Doctrine\Persistence\ObjectManager; use Doctrine\Persistence\ObjectRepository; use Laminas\ServiceManager\ServiceManager; -use Lmc\Rbac\Role\ObjectRepositoryRoleProviderFactory; use Lmc\Rbac\Exception\RuntimeException; use Lmc\Rbac\Options\ModuleOptions; use Lmc\Rbac\Role\ObjectRepositoryRoleProvider; +use Lmc\Rbac\Role\ObjectRepositoryRoleProviderFactory; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; -#[CoversClass('\Lmc\Rbac\Role\ObjectRepositoryRoleProviderFactory')] +#[CoversClass(ObjectRepositoryRoleProviderFactory::class)] class ObjectRepositoryRoleProviderFactoryTest extends TestCase { public function testFactoryUsingObjectRepository(): void @@ -41,7 +41,7 @@ public function testFactoryUsingObjectRepository(): void 'role_provider' => [ ObjectRepositoryRoleProvider::class => [ 'role_name_property' => 'name', - 'object_repository' => 'RoleObjectRepository', + 'object_repository' => 'RoleObjectRepository', ], ], ])); @@ -58,8 +58,8 @@ public function testFactoryUsingObjectManager(): void 'role_provider' => [ ObjectRepositoryRoleProvider::class => [ 'role_name_property' => 'name', - 'object_manager' => 'ObjectManager', - 'class_name' => 'Role', + 'object_manager' => 'ObjectManager', + 'class_name' => 'Role', ], ], ])); diff --git a/test/Role/ObjectRepositoryRoleProviderTest.php b/test/Role/ObjectRepositoryRoleProviderTest.php index 827377d..470e2c5 100644 --- a/test/Role/ObjectRepositoryRoleProviderTest.php +++ b/test/Role/ObjectRepositoryRoleProviderTest.php @@ -21,12 +21,15 @@ namespace LmcTest\Rbac\Role; +use Doctrine\DBAL\Driver\PDO\SQLite\Driver; use Doctrine\DBAL\DriverManager; use Doctrine\ORM\EntityManager; use Doctrine\ORM\ORMSetup; use Doctrine\ORM\Tools\SchemaTool; use Doctrine\Persistence\ObjectManager; use Doctrine\Persistence\ObjectRepository; +use Generator; +use Lmc\Rbac\Exception\RoleNotFoundException; use Lmc\Rbac\Role\ObjectRepositoryRoleProvider; use Lmc\Rbac\Role\Role; use Lmc\Rbac\Role\RoleInterface; @@ -34,21 +37,23 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; -#[CoversClass('\Lmc\Rbac\Role\ObjectRepositoryRoleProvider')] +use function count; +use function is_array; + +#[CoversClass(ObjectRepositoryRoleProvider::class)] class ObjectRepositoryRoleProviderTest extends TestCase { - public static function roleProvider(): array { return [ 'one-role-flat' => [ - 'rolesConfig' => [ + 'rolesConfig' => [ 'admin', ], 'rolesToCheck' => ['admin'], ], - '2-roles-flat' => [ - 'rolesConfig' => [ + '2-roles-flat' => [ + 'rolesConfig' => [ 'admin', 'member', ], @@ -60,9 +65,9 @@ public static function roleProvider(): array public function testObjectRepositoryProviderGetRoles(): void { $objectRepository = $this->createMock(ObjectRepository::class); - $memberRole = new Role('member'); - $provider = new ObjectRepositoryRoleProvider($objectRepository, 'name'); - $result = [$memberRole]; + $memberRole = new Role('member'); + $provider = new ObjectRepositoryRoleProvider($objectRepository, 'name'); + $result = [$memberRole]; $objectRepository->expects($this->once())->method('findBy')->willReturn($result); @@ -72,9 +77,9 @@ public function testObjectRepositoryProviderGetRoles(): void public function testRoleCacheOnConsecutiveCalls(): void { $objectRepository = $this->createMock(ObjectRepository::class); - $memberRole = new Role('member'); - $provider = new ObjectRepositoryRoleProvider($objectRepository, 'name'); - $result = [$memberRole]; + $memberRole = new Role('member'); + $provider = new ObjectRepositoryRoleProvider($objectRepository, 'name'); + $result = [$memberRole]; // note exactly once, consecutive call come from cache $objectRepository->expects($this->exactly(1))->method('findBy')->willReturn($result); @@ -86,9 +91,9 @@ public function testRoleCacheOnConsecutiveCalls(): void public function testClearRoleCache(): void { $objectRepository = $this->createMock(ObjectRepository::class); - $memberRole = new Role('member'); - $provider = new ObjectRepositoryRoleProvider($objectRepository, 'name'); - $result = [$memberRole]; + $memberRole = new Role('member'); + $provider = new ObjectRepositoryRoleProvider($objectRepository, 'name'); + $result = [$memberRole]; // note exactly twice, as cache is cleared $objectRepository->expects($this->exactly(2))->method('findBy')->willReturn($result); @@ -101,13 +106,13 @@ public function testClearRoleCache(): void public function testThrowExceptionIfAskedRoleIsNotFound(): void { $objectRepository = $this->createMock(ObjectRepository::class); - $memberRole = new Role('member'); - $provider = new ObjectRepositoryRoleProvider($objectRepository, 'name'); - $result = [$memberRole]; + $memberRole = new Role('member'); + $provider = new ObjectRepositoryRoleProvider($objectRepository, 'name'); + $result = [$memberRole]; $objectRepository->expects($this->once())->method('findBy')->willReturn($result); - $this->expectException('Lmc\Rbac\Exception\RoleNotFoundException'); + $this->expectException(RoleNotFoundException::class); $this->expectExceptionMessage('Some roles were asked but could not be loaded from database: guest, admin'); $provider->getRoles(['guest', 'admin', 'member']); @@ -132,7 +137,7 @@ public function testObjectRepositoryProviderForFlatRole(array $rolesConfig, arra } $objectManager->flush(); - $objectRepository = $objectManager->getRepository('LmcTest\Rbac\Asset\Role'); + $objectRepository = $objectManager->getRepository(\LmcTest\Rbac\Asset\Role::class); $objectRepositoryRoleProvider = new ObjectRepositoryRoleProvider($objectRepository, 'name'); $roles = $objectRepositoryRoleProvider->getRoles($rolesToCheck); @@ -160,7 +165,7 @@ public function testObjectRepositoryProviderForFlatRoleWithPermissions() $objectManager->persist($adminRole); $objectManager->flush(); - $objectRepository = $objectManager->getRepository('LmcTest\Rbac\Asset\Role'); + $objectRepository = $objectManager->getRepository(\LmcTest\Rbac\Asset\Role::class); $objectRepositoryRoleProvider = new ObjectRepositoryRoleProvider($objectRepository, 'name'); @@ -170,7 +175,7 @@ public function testObjectRepositoryProviderForFlatRoleWithPermissions() $this->assertCount(1, $roles); $this->assertIsArray($roles); - $this->assertInstanceOf('Lmc\Rbac\Role\RoleInterface', $roles[0]); + $this->assertInstanceOf(RoleInterface::class, $roles[0]); $this->assertEquals('admin', $roles[0]->getName()); $this->assertTrue($roles[0]->hasPermission('manage')); $this->assertTrue($roles[0]->hasPermission('read')); @@ -196,7 +201,7 @@ public function testObjectRepositoryProviderForHierarchicalRole() $objectManager->flush(); - $objectRepository = $objectManager->getRepository('LmcTest\Rbac\Asset\Role'); + $objectRepository = $objectManager->getRepository(\LmcTest\Rbac\Asset\Role::class); $objectRepositoryRoleProvider = new ObjectRepositoryRoleProvider($objectRepository, 'name'); @@ -206,13 +211,13 @@ public function testObjectRepositoryProviderForHierarchicalRole() $this->assertCount(1, $roles); $this->assertIsArray($roles); - $this->assertInstanceOf('Lmc\Rbac\Role\RoleInterface', $roles[0]); + $this->assertInstanceOf(RoleInterface::class, $roles[0]); $this->assertEquals('admin', $roles[0]->getName()); $childRolesString = ''; foreach ($this->flattenRoles($roles[0]->getChildren()) as $childRole) { - $this->assertInstanceOf('Lmc\Rbac\Role\RoleInterface', $childRole); + $this->assertInstanceOf(RoleInterface::class, $childRole); $childRolesString .= $childRole->getName(); } @@ -221,15 +226,15 @@ public function testObjectRepositoryProviderForHierarchicalRole() private function getObjectManager(): ObjectManager|EntityManager { - $config = ORMSetup::createAttributeMetadataConfiguration( + $config = ORMSetup::createAttributeMetadataConfiguration( paths: [__DIR__ . '/../Asset'], isDevMode: true ); - $connection = DriverManager::getConnection([ - 'driverClass' => 'Doctrine\DBAL\Driver\PDO\SQLite\Driver', - 'path' => null, - 'memory' => true, - 'dbname' => 'test', + $connection = DriverManager::getConnection([ + 'driverClass' => Driver::class, + 'path' => null, + 'memory' => true, + 'dbname' => 'test', ], $config); $entityManager = new EntityManager($connection, $config); @@ -240,7 +245,7 @@ private function getObjectManager(): ObjectManager|EntityManager return $objectManager; } - private function flattenRoles(iterable $roles): \Generator + private function flattenRoles(iterable $roles): Generator { foreach ($roles as $role) { yield $role; diff --git a/test/Role/RoleTest.php b/test/Role/RoleTest.php index 37be4bc..225258d 100644 --- a/test/Role/RoleTest.php +++ b/test/Role/RoleTest.php @@ -26,7 +26,7 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; -#[CoversClass('\Lmc\Rbac\Role\Role')] +#[CoversClass(Role::class)] class RoleTest extends TestCase { public function testSetNameByConstructor(): void @@ -73,12 +73,11 @@ public function testHasPermission(): void $role->addChild($childRole); $this->assertTrue($role->hasPermission('bar')); $this->assertFalse($role->hasPermission('baz')); - } public function testCanAddChild(): void { - $role = new Role('role'); + $role = new Role('role'); $child = new Role('child'); $role->addChild($child); @@ -99,7 +98,7 @@ public function testHasChildren(): void public function testCanGetChildren(): void { - $role = new Role('role'); + $role = new Role('role'); $child1 = new Role('child 1'); $child2 = new Role('child 2'); diff --git a/test/Service/AuthorizationServiceAwareTraitTest.php b/test/Service/AuthorizationServiceAwareTraitTest.php index 0c1bc94..b0b2312 100644 --- a/test/Service/AuthorizationServiceAwareTraitTest.php +++ b/test/Service/AuthorizationServiceAwareTraitTest.php @@ -1,38 +1,37 @@ - */ +namespace LmcTest\Rbac\Service; +use Lmc\Rbac\Service\AuthorizationServiceAwareTrait; use Lmc\Rbac\Service\AuthorizationServiceInterface; use LmcTest\Rbac\Asset\DummyAuthorizationServiceClass; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; -#[CoversClass('Lmc\Rbac\Service\AuthorizationServiceAwareTrait')] +#[CoversClass(AuthorizationServiceAwareTrait::class)] class AuthorizationServiceAwareTraitTest extends TestCase { public function testAuthorizationServiceAwareTrait() { - $class = new DummyAuthorizationServiceClass(); + $class = new DummyAuthorizationServiceClass(); $authorizationService = $this->createMock(AuthorizationServiceInterface::class); $class->setAuthorizationService($authorizationService); $this->assertSame($authorizationService, $class->getAuthorizationService()); diff --git a/test/Service/AuthorizationServiceDelegatorFactoryTest.php b/test/Service/AuthorizationServiceDelegatorFactoryTest.php index 9c3305f..e6c8b67 100644 --- a/test/Service/AuthorizationServiceDelegatorFactoryTest.php +++ b/test/Service/AuthorizationServiceDelegatorFactoryTest.php @@ -1,27 +1,25 @@ - */ - use Laminas\ServiceManager\Exception\ServiceNotCreatedException; use Lmc\Rbac\Service\AuthorizationServiceDelegatorFactory; use Lmc\Rbac\Service\AuthorizationServiceInterface; @@ -30,8 +28,9 @@ use PHPUnit\Framework\TestCase; use Prophecy\PhpUnit\ProphecyTrait; use Psr\Container\ContainerInterface; +use stdClass; -#[CoversClass('\Lmc\Rbac\Service\AuthorizationServiceDelegatorFactory')] +#[CoversClass(AuthorizationServiceDelegatorFactory::class)] class AuthorizationServiceDelegatorFactoryTest extends TestCase { use ProphecyTrait; @@ -39,32 +38,26 @@ class AuthorizationServiceDelegatorFactoryTest extends TestCase public function testDelegatorFactory(): void { $authorizationService = $this->createMock(AuthorizationServiceInterface::class); - $container = $this->prophesize(ContainerInterface::class); - - $callback = function () { + $container = $this->prophesize(ContainerInterface::class); + $callback = function () { return new DummyAuthorizationServiceClass(); }; - $container->get(AuthorizationServiceInterface::class)->willReturn($authorizationService)->shouldBeCalled(); - $delegatorFactory = new AuthorizationServiceDelegatorFactory(); - $instance = $delegatorFactory($container->reveal(), DummyAuthorizationServiceClass::class, $callback ); + $instance = $delegatorFactory($container->reveal(), DummyAuthorizationServiceClass::class, $callback); $this->assertInstanceOf(DummyAuthorizationServiceClass::class, $instance); } public function testDelegatorFactoryException(): void { $authorizationService = $this->createMock(AuthorizationServiceInterface::class); - $container = $this->prophesize(ContainerInterface::class); - - $callback = function () { - return new \StdClass(); + $container = $this->prophesize(ContainerInterface::class); + $callback = function () { + return new stdClass(); }; - $delegatorFactory = new AuthorizationServiceDelegatorFactory(); - + $delegatorFactory = new AuthorizationServiceDelegatorFactory(); $container->get(AuthorizationServiceInterface::class)->willReturn($authorizationService)->shouldNotBeCalled(); $this->expectException(ServiceNotCreatedException::class); - - $instance = $delegatorFactory($container->reveal(), DummyAuthorizationServiceClass::class, $callback ); + $instance = $delegatorFactory($container->reveal(), DummyAuthorizationServiceClass::class, $callback); } } diff --git a/test/Service/AuthorizationServiceFactoryTest.php b/test/Service/AuthorizationServiceFactoryTest.php index d337f86..0934766 100644 --- a/test/Service/AuthorizationServiceFactoryTest.php +++ b/test/Service/AuthorizationServiceFactoryTest.php @@ -21,20 +21,19 @@ namespace LmcTest\Rbac\Service; -use Lmc\Rbac\Assertion\AssertionContainerInterface; use Lmc\Rbac\Assertion\AssertionPluginManager; use Lmc\Rbac\Assertion\AssertionPluginManagerInterface; -use Lmc\Rbac\Service\AuthorizationServiceFactory; use Lmc\Rbac\Options\ModuleOptions; use Lmc\Rbac\Rbac; use Lmc\Rbac\Service\AuthorizationService; +use Lmc\Rbac\Service\AuthorizationServiceFactory; use Lmc\Rbac\Service\RoleServiceInterface; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use Prophecy\PhpUnit\ProphecyTrait; use Psr\Container\ContainerInterface; -#[CoversClass('\Lmc\Rbac\Service\AuthorizationServiceFactory')] +#[CoversClass(AuthorizationServiceFactory::class)] class AuthorizationServiceFactoryTest extends TestCase { use ProphecyTrait; @@ -47,7 +46,7 @@ public function testCanCreateAuthorizationService(): void $container->get(AssertionPluginManagerInterface::class)->willReturn($this->createMock(AssertionPluginManager::class)); $container->get(Rbac::class)->willReturn(new Rbac()); - $factory = new AuthorizationServiceFactory(); + $factory = new AuthorizationServiceFactory(); $authorizationService = $factory($container->reveal()); $this->assertInstanceOf(AuthorizationService::class, $authorizationService); diff --git a/test/Service/AuthorizationServiceTest.php b/test/Service/AuthorizationServiceTest.php index 29b2e52..8cfa12d 100644 --- a/test/Service/AuthorizationServiceTest.php +++ b/test/Service/AuthorizationServiceTest.php @@ -40,8 +40,11 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; +use stdClass; -#[CoversClass('Lmc\Rbac\Service\AuthorizationService')] +use function array_merge; + +#[CoversClass(AuthorizationService::class)] class AuthorizationServiceTest extends TestCase { public static function grantedProvider(): array @@ -125,7 +128,7 @@ public static function grantedProvider(): array 'true_assertion', 'condition' => AssertionSet::CONDITION_AND, ], - 'sleep' => 'false_assertion', + 'sleep' => 'false_assertion', ], ], @@ -160,30 +163,30 @@ public static function grantedProvider(): array public function testGranted($role, $permission, $context, bool $isGranted, array $assertions = []): void { $roleConfig = [ - 'admin' => [ - 'children' => ['member'], + 'admin' => [ + 'children' => ['member'], 'permissions' => ['delete'], ], 'member' => [ - 'children' => ['guest'], + 'children' => ['guest'], 'permissions' => ['write'], ], - 'guest' => [ + 'guest' => [ 'permissions' => ['read'], ], ]; $assertionPluginConfig = [ 'services' => [ - 'true_assertion' => new SimpleAssertion(true), + 'true_assertion' => new SimpleAssertion(true), 'false_assertion' => new SimpleAssertion(false), ], ]; - $identity = new Identity((array) $role); - $roleService = new RoleService(new InMemoryRoleProvider($roleConfig), 'guest'); + $identity = new Identity((array) $role); + $roleService = new RoleService(new InMemoryRoleProvider($roleConfig), 'guest'); $assertionPluginManager = new AssertionPluginManager(new ServiceManager(), $assertionPluginConfig); - $authorizationService = new AuthorizationService(new Rbac(), $roleService, $assertionPluginManager, $assertions); + $authorizationService = new AuthorizationService(new Rbac(), $roleService, $assertionPluginManager, $assertions); $this->assertEquals($isGranted, $authorizationService->isGranted($identity, $permission, $context)); } @@ -224,7 +227,7 @@ public function testReturnsFalseForIdentityWithoutRoles(): void public function testReturnsTrueForIdentityWhenHasPermissionButNoAssertionsExists(): void { - $role = new Role('admin'); + $role = new Role('admin'); $identity = new Identity([$role]); $roleService = $this->getMockBuilder(RoleServiceInterface::class)->getMock(); @@ -243,8 +246,8 @@ public function testReturnsTrueForIdentityWhenHasPermissionButNoAssertionsExists public function testUsesAssertionsAsInstances(): void { - $role = new Role('admin'); - $identity = new Identity([$role]); + $role = new Role('admin'); + $identity = new Identity([$role]); $assertion = new SimpleAssertion(); $roleService = $this->getMockBuilder(RoleServiceInterface::class)->getMock(); @@ -265,8 +268,8 @@ public function testUsesAssertionsAsInstances(): void public function testUsesAssertionsAsStrings(): void { - $role = new Role('admin'); - $identity = new Identity([$role]); + $role = new Role('admin'); + $identity = new Identity([$role]); $assertion = new SimpleAssertion(); $roleService = $this->getMockBuilder(RoleServiceInterface::class)->getMock(); @@ -287,7 +290,7 @@ public function testUsesAssertionsAsStrings(): void public function testUsesAssertionsAsCallable(): void { - $role = new Role('admin'); + $role = new Role('admin'); $identity = new Identity([$role]); $roleService = $this->getMockBuilder(RoleServiceInterface::class)->getMock(); @@ -306,7 +309,7 @@ public function testUsesAssertionsAsCallable(): void $roleService, $assertionPluginManager, [ - 'foo' => function ($permission, IdentityInterface $identity = null, $context = null) use (&$called) { + 'foo' => function ($permission, ?IdentityInterface $identity = null, $context = null) use (&$called) { $called = true; return false; @@ -321,7 +324,7 @@ public function testUsesAssertionsAsCallable(): void public function testUsesAssertionsAsArrays(): void { - $role = new Role('admin'); + $role = new Role('admin'); $identity = new Identity([$role]); $roleService = $this->getMockBuilder(RoleServiceInterface::class)->getMock(); @@ -338,12 +341,12 @@ public function testUsesAssertionsAsArrays(): void $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager, [ 'foo' => [ - function ($permission, IdentityInterface $identity = null, $context = null) use (&$called1) { + function ($permission, ?IdentityInterface $identity = null, $context = null) use (&$called1) { $called1 = true; return true; }, - function ($permission, IdentityInterface $identity = null, $context = null) use (&$called2) { + function ($permission, ?IdentityInterface $identity = null, $context = null) use (&$called2) { $called2 = true; return false; @@ -368,7 +371,7 @@ public function testThrowExceptionForInvalidAssertion(): void $roleService->expects($this->once())->method('getIdentityRoles')->willreturn([$role]); $assertionPluginManager = $this->getMockBuilder(AssertionPluginManagerInterface::class)->disableOriginalConstructor()->getMock(); - $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager, ['foo' => new \stdClass()]); + $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager, ['foo' => new stdClass()]); $this->expectException(InvalidArgumentException::class); @@ -378,12 +381,12 @@ public function testThrowExceptionForInvalidAssertion(): void public function testContextIsPassedToRoleService(): void { $identity = new Identity([]); - $context = 'context'; + $context = 'context'; - $rbac = $this->getMockBuilder(RbacInterface::class)->disableOriginalConstructor()->getMock(); - $roleService = $this->getMockBuilder(RoleServiceInterface::class)->getMock(); + $rbac = $this->getMockBuilder(RbacInterface::class)->disableOriginalConstructor()->getMock(); + $roleService = $this->getMockBuilder(RoleServiceInterface::class)->getMock(); $assertionPluginManager = $this->getMockBuilder(AssertionPluginManagerInterface::class)->getMock(); - $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager); + $authorizationService = new AuthorizationService($rbac, $roleService, $assertionPluginManager); $roleService->expects($this->once())->method('getIdentityRoles')->with($identity, $context)->willReturn([]); $authorizationService->isGranted($identity, 'foo', $context); @@ -391,7 +394,7 @@ public function testContextIsPassedToRoleService(): void public function testGetAssertions(): void { - $assertions = [ + $assertions = [ 'foo' => 'foo', ]; $authorizationService = $this->createAuthorizationService($assertions); @@ -402,7 +405,7 @@ public function testGetAssertions(): void public function testHasAssertion(): void { - $assertions = [ + $assertions = [ 'foo' => 'foo', ]; $authorizationService = $this->createAuthorizationService($assertions); @@ -412,10 +415,10 @@ public function testHasAssertion(): void public function testSetAssertions(): void { - $assertions = [ + $assertions = [ 'foo' => 'foo', ]; - $newAssertions = [ + $newAssertions = [ 'bar' => 'bar', ]; $authorizationService = $this->createAuthorizationService($assertions); diff --git a/test/Service/RoleServiceFactoryTest.php b/test/Service/RoleServiceFactoryTest.php index 2bf23ac..483237f 100644 --- a/test/Service/RoleServiceFactoryTest.php +++ b/test/Service/RoleServiceFactoryTest.php @@ -21,6 +21,7 @@ namespace LmcTest\Rbac\Service; +use Laminas\ServiceManager\Exception\ServiceNotCreatedException; use Laminas\ServiceManager\ServiceManager; use Lmc\Rbac\Options\ModuleOptions; use Lmc\Rbac\Role\InMemoryRoleProvider; @@ -28,16 +29,17 @@ use Lmc\Rbac\Service\RoleServiceFactory; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; +use stdClass; -#[CoversClass('\Lmc\Rbac\Service\RoleServiceFactory')] +#[CoversClass(RoleServiceFactory::class)] class RoleServiceFactoryTest extends TestCase { public function testCanCreateRoleService(): void { $options = new ModuleOptions([ - 'guest_role' => 'guest', + 'guest_role' => 'guest', 'role_provider' => [ - \Lmc\Rbac\Role\InMemoryRoleProvider::class => [ + InMemoryRoleProvider::class => [ 'foo', ], ], @@ -45,29 +47,31 @@ public function testCanCreateRoleService(): void $container = new ServiceManager([ 'services' => [ - ModuleOptions::class => $options, + ModuleOptions::class => $options, InMemoryRoleProvider::class => new InMemoryRoleProvider([]), ], ]); - $factory = new RoleServiceFactory(); + $factory = new RoleServiceFactory(); $roleService = $factory($container, RoleService::class); - $this->assertInstanceOf(\Lmc\Rbac\Service\RoleService::class, $roleService); + $this->assertInstanceOf(RoleService::class, $roleService); } public function testThrowExceptionIfNoRoleProvider(): void { - $this->expectException(\Laminas\ServiceManager\Exception\ServiceNotCreatedException::class); + $this->expectException(ServiceNotCreatedException::class); $options = new ModuleOptions([ - 'guest_role' => 'guest', + 'guest_role' => 'guest', 'role_provider' => [], ]); - $container = new ServiceManager(['services' => [ - ModuleOptions::class => $options, - ]]); + $container = new ServiceManager([ + 'services' => [ + ModuleOptions::class => $options, + ], + ]); $factory = new RoleServiceFactory(); $factory($container); @@ -75,21 +79,23 @@ public function testThrowExceptionIfNoRoleProvider(): void public function testThrowExceptionIfInvalidRoleProvider(): void { - $this->expectException(\Laminas\ServiceManager\Exception\ServiceNotCreatedException::class); + $this->expectException(ServiceNotCreatedException::class); $options = new ModuleOptions([ - 'guest_role' => 'guest', + 'guest_role' => 'guest', 'role_provider' => [ 'InvalidRoleProvider' => [], ], ]); - $container = new ServiceManager(['services' => [ - ModuleOptions::class => $options, - 'InvalidRoleProvider' => function () { - return new \stdClass(); - } - ]]); + $container = new ServiceManager([ + 'services' => [ + ModuleOptions::class => $options, + 'InvalidRoleProvider' => function () { + return new stdClass(); + }, + ], + ]); $factory = new RoleServiceFactory(); $factory($container); diff --git a/test/Service/RoleServiceTest.php b/test/Service/RoleServiceTest.php index e67253d..96bd620 100644 --- a/test/Service/RoleServiceTest.php +++ b/test/Service/RoleServiceTest.php @@ -21,6 +21,7 @@ namespace LmcTest\Rbac\Service; +use ArrayIterator; use Lmc\Rbac\Identity\IdentityInterface; use Lmc\Rbac\Role\InMemoryRoleProvider; use Lmc\Rbac\Role\Role; @@ -33,7 +34,7 @@ use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; -#[CoversClass('\Lmc\Rbac\Service\RoleService')] +#[CoversClass(RoleService::class)] class RoleServiceTest extends TestCase { use ProphecyTrait; @@ -63,8 +64,8 @@ public function testReturnGuestRoleIfNullIdentityIsGiven(): void public function testReturnTraversableRolesFromIdentityGiven(): void { $roleService = new RoleService(new InMemoryRoleProvider([]), 'guest'); - $identity = $this->prophesize(IdentityInterface::class); - $identity->getRoles()->willReturn($roles = new \ArrayIterator(['first', 'second', 'third'])); + $identity = $this->prophesize(IdentityInterface::class); + $identity->getRoles()->willReturn($roles = new ArrayIterator(['first', 'second', 'third'])); $result = $roleService->getIdentityRoles($identity->reveal()); @@ -81,8 +82,8 @@ public function testWillNotInvokeRoleProviderIfAllRolesCollected(): void $roleProvider->getRoles(Argument::any())->shouldNotBeCalled(); $roleService = new RoleService($roleProvider->reveal(), 'guest'); - $roles = [new Role('first'), new Role('second'), new Role('third')]; - $identity = new Identity($roles); + $roles = [new Role('first'), new Role('second'), new Role('third')]; + $identity = new Identity($roles); $result = $roleService->getIdentityRoles($identity); @@ -94,11 +95,11 @@ public function testWillNotInvokeRoleProviderIfAllRolesCollected(): void public function testWillCollectRolesOnlyIfRequired(): void { $roleProvider = $this->prophesize(RoleProviderInterface::class); - $roles = [new Role('first'), new Role('second'), 'third']; + $roles = [new Role('first'), new Role('second'), 'third']; $roleProvider->getRoles(['third'])->shouldBeCalled()->willReturn([new Role('third')]); $roleService = new RoleService($roleProvider->reveal(), 'guest'); - $identity = new Identity($roles); + $identity = new Identity($roles); $result = $roleService->getIdentityRoles($identity);