From efe3ef466f9e2ce29c5385b127495e95960c0462 Mon Sep 17 00:00:00 2001 From: Dennis Riehle Date: Fri, 24 Sep 2021 18:38:34 +0200 Subject: [PATCH 1/2] fixed wrong config path introduced by renaming files in e7039c13cb39684b82c1cb9fc788e7baf804cd36 --- src/Module.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Module.php b/src/Module.php index a0172a9..61083c6 100644 --- a/src/Module.php +++ b/src/Module.php @@ -82,6 +82,6 @@ public function getControllerPluginConfig() */ public function getConfig() { - return include __DIR__ . '/../../config/module.config.php'; + return include __DIR__ . '/../config/module.config.php'; } } From b821e2e98517a5ac9bc4c33bb1bde2baa4729d0a Mon Sep 17 00:00:00 2001 From: Dennis Riehle Date: Fri, 24 Sep 2021 19:15:43 +0200 Subject: [PATCH 2/2] moved config into single place and added unit tests for configuration and bootstrapping --- config/module.config.php | 10 ++++ src/Module.php | 32 +----------- test/ModuleTest.php | 105 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 31 deletions(-) create mode 100644 test/ModuleTest.php diff --git a/config/module.config.php b/config/module.config.php index e46aeea..bf9c4e8 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -86,6 +86,11 @@ Service\AuthorizeAwareServiceInitializer::class ], ], + 'controller_plugins' => [ + 'factories' => [ + 'isAllowed' => Controller\Plugin\IsAllowedFactory::class + ], + ], 'view_manager' => [ 'template_map' => [ 'error/403' => __DIR__ . '/../view/error/403.phtml', @@ -93,6 +98,11 @@ => __DIR__ . '/../view/laminas-developer-tools/toolbar/bjy-authorize-role.phtml', ], ], + 'view_helpers' => [ + 'factories' => [ + 'isAllowed' => View\Helper\IsAllowedFactory::class, + ], + ], 'laminas-developer-tools' => [ 'profiler' => [ 'collectors' => [ diff --git a/src/Module.php b/src/Module.php index 61083c6..cc5e1fe 100644 --- a/src/Module.php +++ b/src/Module.php @@ -2,15 +2,11 @@ namespace BjyAuthorize; -use BjyAuthorize\Controller\Plugin; use BjyAuthorize\Guard\AbstractGuard; -use BjyAuthorize\View\Helper; use BjyAuthorize\View\UnauthorizedStrategy; use Laminas\EventManager\EventInterface; use Laminas\ModuleManager\Feature\BootstrapListenerInterface; use Laminas\ModuleManager\Feature\ConfigProviderInterface; -use Laminas\ModuleManager\Feature\ControllerPluginProviderInterface; -use Laminas\ModuleManager\Feature\ViewHelperProviderInterface; use Laminas\ServiceManager\ServiceManager; /** @@ -20,9 +16,7 @@ */ class Module implements BootstrapListenerInterface, - ConfigProviderInterface, - ControllerPluginProviderInterface, - ViewHelperProviderInterface + ConfigProviderInterface { /** * {@inheritDoc} @@ -53,30 +47,6 @@ public function onBootstrap(EventInterface $event) $strategy->attach($app->getEventManager()); } - /** - * {@inheritDoc} - */ - public function getViewHelperConfig() - { - return [ - 'factories' => [ - 'isAllowed' => Helper\IsAllowedFactory::class, - ], - ]; - } - - /** - * {@inheritDoc} - */ - public function getControllerPluginConfig() - { - return [ - 'factories' => [ - 'isAllowed' => Plugin\IsAllowedFactory::class - ], - ]; - } - /** * {@inheritDoc} */ diff --git a/test/ModuleTest.php b/test/ModuleTest.php new file mode 100644 index 0000000..88092c1 --- /dev/null +++ b/test/ModuleTest.php @@ -0,0 +1,105 @@ +getConfig(); + + $this->assertIsArray($config); + $this->assertArrayHasKey('bjyauthorize', $config); + $this->assertArrayHasKey('service_manager', $config); + $this->assertArrayHasKey('controller_plugins', $config); + $this->assertArrayHasKey('view_manager', $config); + $this->assertArrayHasKey('view_helpers', $config); + $this->assertArrayHasKey('laminas-developer-tools', $config); + } + + public function testIfBoostrapRegistersGuardsAndStrategy() + { + $module = new Module(); + $event = $this->getMockedBootstrapEvent(); + + $module->onBootstrap($event); + } + + protected function getMockedBootstrapEvent() + { + $guard1 = $this->getMockBuilder(Controller::class) + ->disableOriginalConstructor() + ->getMock(); + + $guard1->expects($this->once()) + ->method('attach'); + + $guard2 = $this->getMockBuilder(Route::class) + ->disableOriginalConstructor() + ->getMock(); + + $guard2->expects($this->once()) + ->method('attach'); + + $strategy = $this->getMockBuilder(UnauthorizedStrategy::class) + ->disableOriginalConstructor() + ->getMock(); + + $strategy->expects($this->once()) + ->method('attach'); + + $serviceManager = $this->getMockBuilder(ServiceManager::class) + ->getMock(); + + $serviceManager->expects($this->any()) + ->method('get') + ->will($this->returnCallback(function(string $name) use ($guard1, $guard2, $strategy) { + switch ($name) { + case 'BjyAuthorize\Config': + return ['unauthorized_strategy' => 'my_unauthorized_strategy']; + case 'my_unauthorized_strategy': + return $strategy; + case 'BjyAuthorize\Guards': + return [$guard1, $guard2]; + default: + throw new \InvalidArgumentException('Invalid service call.'); + } + })); + + $eventManager = $this->getMockBuilder(EventManager::class) + ->getMock(); + + $app = $this->getMockBuilder(Application::class) + ->disableOriginalConstructor() + ->getMock(); + + $app->expects($this->any()) + ->method('getServiceManager') + ->willReturn($serviceManager); + + $app->expects($this->any()) + ->method('getEventManager') + ->willReturn($eventManager); + + $event = $this->getMockBuilder(MvcEvent::class) + ->disableOriginalConstructor() + ->getMock(); + + $event->expects($this->any()) + ->method('getTarget') + ->willReturn($app); + + return $event; + } +}