Skip to content

Commit

Permalink
Added assertion_manager config to Module options
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Richer [email protected] <[email protected]>
  • Loading branch information
visto9259 committed Aug 20, 2024
1 parent 98bbb70 commit da6e600
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 11 deletions.
4 changes: 2 additions & 2 deletions config/lmcrbac.global.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ return [
* 'AssertOwnerCanEditArticle' => \My\Namespace\AssertOwnerCanEditArticleFactory::class
* ],
*/
'assertion_map' => [
],
// 'assertion_map' => [],
// 'assertion_manager' => [],
],
];
6 changes: 4 additions & 2 deletions src/Assertion/AssertionPluginManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lmc\Rbac\Assertion;

use Laminas\ServiceManager\Factory\FactoryInterface;
use Lmc\Rbac\Options\ModuleOptions;
use Psr\Container\ContainerInterface;

class AssertionPluginManagerFactory implements FactoryInterface
Expand All @@ -13,8 +14,9 @@ class AssertionPluginManagerFactory implements FactoryInterface
*/
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): AssertionPluginManager
{
$config = $container->get('config')['lmc_rbac']['assertion_manager'];
/** @var ModuleOptions $config */
$config = $container->get(ModuleOptions::class);

return new AssertionPluginManager($container, $config);
return new AssertionPluginManager($container, $config->getAssertionManager());
}
}
25 changes: 25 additions & 0 deletions src/Options/ModuleOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ class ModuleOptions extends AbstractOptions
InMemoryRoleProvider::class => [],
];

/**
* Assertion plugin manager configuration
*
* @var array
*/
protected array $assertionManager = [];

/**
* Constructor
*
Expand Down Expand Up @@ -114,4 +121,22 @@ public function getRoleProvider(): array
{
return $this->roleProvider;
}

/**
* Set the configuration for the assertion plugin manager
*
* @param array $assertionManager
*/
public function setAssertionManager(array $assertionManager): void
{
$this->assertionManager = $assertionManager;
}

/**
* @return array
*/
public function getAssertionManager(): array
{
return $this->assertionManager;
}
}
9 changes: 8 additions & 1 deletion src/Options/ModuleOptionsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

namespace Lmc\Rbac\Options;

use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
use Psr\Container\ContainerInterface;

/**
Expand All @@ -30,6 +31,12 @@ class ModuleOptionsFactory
{
public function __invoke(ContainerInterface $container): ModuleOptions
{
return new ModuleOptions($container->get('config')['lmc_rbac']);
$config = $container->get('config');

if (!isset($config['lmc_rbac']) || !is_array($config['lmc_rbac'])) {
throw new ServiceNotCreatedException('No lmc_rbac config found.');
}

return new ModuleOptions($config['lmc_rbac']);
}
}
14 changes: 8 additions & 6 deletions test/Assertion/AssertionPluginManagerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,23 @@
use Laminas\ServiceManager\ServiceManager;
use Lmc\Rbac\Assertion\AssertionPluginManager;
use Lmc\Rbac\Assertion\AssertionPluginManagerFactory;
use Lmc\Rbac\Options\ModuleOptions;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;

#[CoversClass(AssertionPluginManagerFactory::class)]
class AssertionPluginManagerFactoryTest extends TestCase
{
public function testFactory(): void
{
$serviceManager = new ServiceManager();
$serviceManager->setService('config', [
'lmc_rbac' => [
$moduleOptions = new ModuleOptions(
[
'assertion_manager' => [],
],
]);

]
);
$serviceManager = new ServiceManager();
$serviceManager->setService(ModuleOptions::class, $moduleOptions);
$factory = new AssertionPluginManagerFactory();
$pluginManager = $factory($serviceManager, AssertionPluginManager::class);

Expand Down
18 changes: 18 additions & 0 deletions test/Options/ModuleOptionsFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

namespace LmcTest\Rbac\Options;

use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
use Laminas\ServiceManager\ServiceManager;
use Lmc\Rbac\Options\ModuleOptions;
use Lmc\Rbac\Options\ModuleOptionsFactory;
Expand All @@ -42,4 +43,21 @@ public function testFactory(): void

$this->assertInstanceOf(ModuleOptions::class, $options);
}
public function testNoConfig(): void
{
$factory = new ModuleOptionsFactory();
$serviceManager = new ServiceManager();

$config = [];
$serviceManager->setService('config', $config);

$this->expectException(ServiceNotCreatedException::class);
$factory($serviceManager);

$config['lmc_rbac'] = 'foo';
$serviceManager->setService('config', $config);

$this->expectException(ServiceNotCreatedException::class);
$factory($serviceManager);
}
}
5 changes: 5 additions & 0 deletions test/Options/ModuleOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function testAssertModuleDefaultOptions(): void
$this->assertIsArray($moduleOptions->getRoleProvider());
$this->assertIsArray($moduleOptions->getAssertionMap());
$this->assertEquals(InMemoryRoleProvider::class, key($moduleOptions->getRoleProvider()));
$this->assertIsArray($moduleOptions->getAssertionManager());
}

public function testSettersAndGetters(): void
Expand All @@ -50,10 +51,14 @@ public function testSettersAndGetters(): void
'foo' => 'bar',
],
'identity_provider' => 'foo',
'assertion_manager' => [
'factories' => [],
],
]);

$this->assertEquals('unknown', $moduleOptions->getGuestRole());
$this->assertEquals([], $moduleOptions->getRoleProvider());
$this->assertEquals(['foo' => 'bar'], $moduleOptions->getAssertionMap());
$this->assertEquals(['factories' => []], $moduleOptions->getAssertionManager());
}
}

0 comments on commit da6e600

Please sign in to comment.