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 a0172a9..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,35 +47,11 @@ 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} */ public function getConfig() { - return include __DIR__ . '/../../config/module.config.php'; + return include __DIR__ . '/../config/module.config.php'; } } 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; + } +}