diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 7633616..f14c842 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -3,12 +3,8 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./vendor/autoload.php" colors="true" - stopOnFailure="false" - processIsolation="false" - backupGlobals="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" - displayDetailsOnTestsThatTriggerDeprecations="false" > ./test @@ -16,7 +12,7 @@ - ./src + ./src diff --git a/src/Exception/UnauthorizedException.php b/src/Exception/UnauthorizedException.php deleted file mode 100644 index 69ce3fd..0000000 --- a/src/Exception/UnauthorizedException.php +++ /dev/null @@ -1,38 +0,0 @@ - - * @licence MIT - */ -class UnauthorizedException extends BaseRuntimeException implements UnauthorizedExceptionInterface -{ - /** - * @var string - */ - protected $message = 'You are not authorized to access this resource'; -} diff --git a/src/Exception/UnauthorizedExceptionInterface.php b/src/Exception/UnauthorizedExceptionInterface.php deleted file mode 100644 index ee0077c..0000000 --- a/src/Exception/UnauthorizedExceptionInterface.php +++ /dev/null @@ -1,32 +0,0 @@ - - * @licence MIT - */ -interface UnauthorizedExceptionInterface extends ExceptionInterface -{ -} diff --git a/src/Service/RoleServiceFactory.php b/src/Service/RoleServiceFactory.php index cfaae74..7e86215 100644 --- a/src/Service/RoleServiceFactory.php +++ b/src/Service/RoleServiceFactory.php @@ -23,6 +23,7 @@ use Laminas\ServiceManager\Exception\ServiceNotCreatedException; use Lmc\Rbac\Options\ModuleOptions; +use Lmc\Rbac\Role\RoleProviderInterface; use Psr\Container\ContainerInterface; /** @@ -44,9 +45,13 @@ public function __invoke(ContainerInterface $container): RoleService } $roleProviderName = key($roleProvider); + $roleProvider = $container->get($roleProviderName); + if (! $roleProvider instanceof RoleProviderInterface) { + throw new ServiceNotCreatedException(sprintf('Class %s does not implement LmcRbac\Role\RoleProviderInterface', $roleProviderName)); + } return new RoleService( - $container->get($roleProviderName), + $roleProvider, $moduleOptions->getGuestRole() ); } diff --git a/test/ConfigProviderTest.php b/test/ConfigProviderTest.php index bc56e82..5583314 100644 --- a/test/ConfigProviderTest.php +++ b/test/ConfigProviderTest.php @@ -19,7 +19,7 @@ declare(strict_types=1); -namespace LmcRbacTest; +namespace LmcTest; use Lmc\Rbac\ConfigProvider; use PHPUnit\Framework\Attributes\CoversClass; diff --git a/test/ModuleTest.php b/test/ModuleTest.php index 4df59bc..d887b57 100644 --- a/test/ModuleTest.php +++ b/test/ModuleTest.php @@ -19,7 +19,7 @@ declare(strict_types=1); -namespace LmcRbacTest; +namespace LmcTest; use Lmc\Rbac\ConfigProvider; use Lmc\Rbac\Module; diff --git a/test/Service/RoleServiceFactoryTest.php b/test/Service/RoleServiceFactoryTest.php index 309576f..2bf23ac 100644 --- a/test/Service/RoleServiceFactoryTest.php +++ b/test/Service/RoleServiceFactoryTest.php @@ -72,4 +72,26 @@ public function testThrowExceptionIfNoRoleProvider(): void $factory = new RoleServiceFactory(); $factory($container); } + + public function testThrowExceptionIfInvalidRoleProvider(): void + { + $this->expectException(\Laminas\ServiceManager\Exception\ServiceNotCreatedException::class); + + $options = new ModuleOptions([ + 'guest_role' => 'guest', + 'role_provider' => [ + 'InvalidRoleProvider' => [], + ], + ]); + + $container = new ServiceManager(['services' => [ + ModuleOptions::class => $options, + 'InvalidRoleProvider' => function () { + return new \stdClass(); + } + ]]); + + $factory = new RoleServiceFactory(); + $factory($container); + } }