From c390f86ebcb1199010047b9f0b4cb120d675d20b Mon Sep 17 00:00:00 2001 From: Malte Gerth Date: Fri, 4 Jul 2014 08:44:39 +0200 Subject: [PATCH 1/5] moved un-/register new extensions to the config files #6 --- Module.php | 7 ----- config/ldc-user-profile.global.php.dist | 8 ++++++ src/LdcUserProfile/Options/ModuleOptions.php | 19 ++++++++++++++ .../Service/ProfileServiceFactory.php | 26 ++++++++++++++++++- 4 files changed, 52 insertions(+), 8 deletions(-) diff --git a/Module.php b/Module.php index 6b99ee0..2742822 100644 --- a/Module.php +++ b/Module.php @@ -14,13 +14,6 @@ class Module implements AutoloaderProviderInterface { - public function onBootstrap(MvcEvent $e) - { - $sm = $e->getApplication()->getServiceManager(); - $sm->get('ldc-user-profile_service')->registerExtension( - $sm->get('ldc-user-profile_extension_zfcuser') - ); - } public function getAutoloaderConfig() { diff --git a/config/ldc-user-profile.global.php.dist b/config/ldc-user-profile.global.php.dist index b8b349e..493842a 100644 --- a/config/ldc-user-profile.global.php.dist +++ b/config/ldc-user-profile.global.php.dist @@ -22,6 +22,14 @@ $settings = array( */ 'validation_group_override' => array(), + /** + * Register extensions by adding them with their service manager key and + * TRUE as value. Unregister an extension by setting the value to FALSE. + */ + 'registered_extensions' => array( + 'ldc-user-profile_extension_zfcuser' => true, + ) + ); /** diff --git a/src/LdcUserProfile/Options/ModuleOptions.php b/src/LdcUserProfile/Options/ModuleOptions.php index 1b303e9..bfe6d3d 100644 --- a/src/LdcUserProfile/Options/ModuleOptions.php +++ b/src/LdcUserProfile/Options/ModuleOptions.php @@ -35,6 +35,13 @@ class ModuleOptions extends AbstractOptions */ protected $validationGroupOverrides = array(); + /** + * Registered extensions + * + * @var array + */ + protected $registeredExtensions = array(); + public function getIsEnabled() { return $this->isEnabled; @@ -76,4 +83,16 @@ public function setValidationGroupOverrides(array $vg) return $this; } + public function getRegisteredExtensions() + { + return $this->registeredExtensions; + } + + public function setRegisteredExtensions($registeredExtensions) + { + $this->registeredExtensions = $registeredExtensions; + + return $this; + } + } diff --git a/src/LdcUserProfile/Service/ProfileServiceFactory.php b/src/LdcUserProfile/Service/ProfileServiceFactory.php index c2c1a80..4b9120c 100644 --- a/src/LdcUserProfile/Service/ProfileServiceFactory.php +++ b/src/LdcUserProfile/Service/ProfileServiceFactory.php @@ -9,6 +9,7 @@ namespace LdcUserProfile\Service; +use LdcUserProfile\Extensions\AbstractExtension; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; @@ -20,8 +21,31 @@ class ProfileServiceFactory implements FactoryInterface public function createService(ServiceLocatorInterface $serviceLocator) { $service = new ProfileService(); - $service->setModuleOptions($serviceLocator->get('ldc-user-profile_module_options')); + + // Get the module options + $moduleOptions = $serviceLocator->get('ldc-user-profile_module_options'); + $service->setModuleOptions($moduleOptions); + + // Register/Unregister the active/inactive extensions + foreach ($moduleOptions->getRegisteredExtensions() as $extensionName => $isActive) { + if ($isActive) { + $this->registerExtension($extensionName, $service, $serviceLocator); + } else { + $service->unregisterExtension($extensionName); + } + } return $service; } + + protected function registerExtension( + $extensionName, + ProfileService $service, + ServiceLocatorInterface $serviceLocator + ) { + $extension = $serviceLocator->get($extensionName); + if ($extension instanceof AbstractExtension) { + $service->registerExtension($extension); + } + } } From eeec86ccc03e5bb2eaa7b660ed4071e5eb23e0a0 Mon Sep 17 00:00:00 2001 From: Malte Gerth Date: Fri, 4 Jul 2014 09:13:38 +0200 Subject: [PATCH 2/5] fixed failing test due to last commit --- tests/LdcUserProfileTest/Service/ProfileServiceFactoryTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/LdcUserProfileTest/Service/ProfileServiceFactoryTest.php b/tests/LdcUserProfileTest/Service/ProfileServiceFactoryTest.php index 175dac7..2819cf5 100644 --- a/tests/LdcUserProfileTest/Service/ProfileServiceFactoryTest.php +++ b/tests/LdcUserProfileTest/Service/ProfileServiceFactoryTest.php @@ -16,6 +16,7 @@ class ProfileServiceFactoryTest extends \PHPUnit_Framework_TestCase public function testCreateService() { $mockModuleOptions = \Mockery::mock('LdcUserProfile\Options\ModuleOptions'); + $mockModuleOptions->shouldReceive('getRegisteredExtensions')->andReturn(array()); $serviceManager = new \Zend\ServiceManager\ServiceManager(); $serviceManager->setService('ldc-user-profile_module_options', $mockModuleOptions); From cc15ef826e4feff2e412fb05e39881db79d61875 Mon Sep 17 00:00:00 2001 From: Malte Gerth Date: Fri, 4 Jul 2014 12:21:28 +0200 Subject: [PATCH 3/5] fixed some minor errors reported by scrutinizer --- Module.php | 1 - src/LdcUserProfile/Controller/ProfileController.php | 4 +++- src/LdcUserProfile/Extensions/ZfcUser/ZfcUserForm.php | 2 +- .../Extensions/ZfcUser/ZfcUserInputFilter.php | 7 +++++++ src/LdcUserProfile/Service/ProfileServiceFactory.php | 1 + 5 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Module.php b/Module.php index 2742822..d12b851 100644 --- a/Module.php +++ b/Module.php @@ -10,7 +10,6 @@ namespace LdcUserProfile; use Zend\ModuleManager\Feature\AutoloaderProviderInterface; -use Zend\Mvc\MvcEvent; class Module implements AutoloaderProviderInterface { diff --git a/src/LdcUserProfile/Controller/ProfileController.php b/src/LdcUserProfile/Controller/ProfileController.php index 80a417b..96d3a0e 100644 --- a/src/LdcUserProfile/Controller/ProfileController.php +++ b/src/LdcUserProfile/Controller/ProfileController.php @@ -12,10 +12,12 @@ use Zend\Mvc\Controller\AbstractActionController; use LdcUserProfile\Options\ModuleOptions; use LdcUserProfile\Service\ProfileService; -use Zend\Form\Form; use Zend\View\Model\ViewModel; use Zend\Http\Response; +/** + * @method \ZfcUser\Controller\Plugin\ZfcUserAuthentication zfcUserAuthentication() + */ class ProfileController extends AbstractActionController { protected $moduleOptions; diff --git a/src/LdcUserProfile/Extensions/ZfcUser/ZfcUserForm.php b/src/LdcUserProfile/Extensions/ZfcUser/ZfcUserForm.php index 9ef7d90..ada7e74 100644 --- a/src/LdcUserProfile/Extensions/ZfcUser/ZfcUserForm.php +++ b/src/LdcUserProfile/Extensions/ZfcUser/ZfcUserForm.php @@ -26,7 +26,7 @@ public function __construct(RegistrationOptionsInterface $registrationOptions) * Set Registration Options * * @param RegistrationOptionsInterface $registrationOptions - * @return Register + * @return ZfcUserForm */ public function setRegistrationOptions(RegistrationOptionsInterface $registrationOptions) { diff --git a/src/LdcUserProfile/Extensions/ZfcUser/ZfcUserInputFilter.php b/src/LdcUserProfile/Extensions/ZfcUser/ZfcUserInputFilter.php index 9ae365a..eae295c 100644 --- a/src/LdcUserProfile/Extensions/ZfcUser/ZfcUserInputFilter.php +++ b/src/LdcUserProfile/Extensions/ZfcUser/ZfcUserInputFilter.php @@ -14,6 +14,13 @@ class ZfcUserInputFilter extends RegisterFilter { + + /** + * Constructor + * + * @param Validator\NoOtherRecordExists $emailValidator + * @param Validator\NoOtherRecordExists $usernameValidator + */ public function __construct($emailValidator, $usernameValidator, RegistrationOptionsInterface $options) { parent::__construct($emailValidator, $usernameValidator, $options); diff --git a/src/LdcUserProfile/Service/ProfileServiceFactory.php b/src/LdcUserProfile/Service/ProfileServiceFactory.php index 4b9120c..92b4b6c 100644 --- a/src/LdcUserProfile/Service/ProfileServiceFactory.php +++ b/src/LdcUserProfile/Service/ProfileServiceFactory.php @@ -23,6 +23,7 @@ public function createService(ServiceLocatorInterface $serviceLocator) $service = new ProfileService(); // Get the module options + /* @var \LdcUserProfile\Options\ModuleOptions $moduleOptions */ $moduleOptions = $serviceLocator->get('ldc-user-profile_module_options'); $service->setModuleOptions($moduleOptions); From 2321a0091ee50740fa18be32db549df22d327bb9 Mon Sep 17 00:00:00 2001 From: Malte Gerth Date: Fri, 4 Jul 2014 12:33:04 +0200 Subject: [PATCH 4/5] fixed uninitialized variable --- demo/ExtensionModule/src/ExtensionModule/Extension.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/demo/ExtensionModule/src/ExtensionModule/Extension.php b/demo/ExtensionModule/src/ExtensionModule/Extension.php index 830b19f..a7504d6 100644 --- a/demo/ExtensionModule/src/ExtensionModule/Extension.php +++ b/demo/ExtensionModule/src/ExtensionModule/Extension.php @@ -13,6 +13,8 @@ class Extension extends AbstractExtension { + + protected $session; public function getName() { From ab574e494e29753fc902c6ff70ec7ff243cb32d7 Mon Sep 17 00:00:00 2001 From: Adam Lundrigan Date: Mon, 7 Jul 2014 09:36:09 -0230 Subject: [PATCH 5/5] Minor implementation changes + unit tests --- src/LdcUserProfile/Options/ModuleOptions.php | 4 +- src/LdcUserProfile/Service/ProfileService.php | 10 +++++ .../Service/ProfileServiceFactory.php | 21 +++------ .../Options/ModuleOptionsTest.php | 18 ++++++++ .../Service/ProfileServiceFactoryTest.php | 45 ++++++++++++++++++- .../Service/ProfileServiceTest.php | 20 +++++++++ 6 files changed, 101 insertions(+), 17 deletions(-) diff --git a/src/LdcUserProfile/Options/ModuleOptions.php b/src/LdcUserProfile/Options/ModuleOptions.php index bfe6d3d..364fe97 100644 --- a/src/LdcUserProfile/Options/ModuleOptions.php +++ b/src/LdcUserProfile/Options/ModuleOptions.php @@ -88,9 +88,9 @@ public function getRegisteredExtensions() return $this->registeredExtensions; } - public function setRegisteredExtensions($registeredExtensions) + public function setRegisteredExtensions(array $ext) { - $this->registeredExtensions = $registeredExtensions; + $this->registeredExtensions = $ext; return $this; } diff --git a/src/LdcUserProfile/Service/ProfileService.php b/src/LdcUserProfile/Service/ProfileService.php index 37d6354..8306039 100644 --- a/src/LdcUserProfile/Service/ProfileService.php +++ b/src/LdcUserProfile/Service/ProfileService.php @@ -55,6 +55,16 @@ public function getExtensions() return $this->extensions; } + public function hasExtension($nameOrInstance) + { + return array_key_Exists( + $nameOrInstance instanceof AbstractExtension + ? $nameOrInstance->getName() + : (string) $nameOrInstance, + $this->extensions + ); + } + public function constructFormForUser(UserInterface $user) { $form = clone $this->getFormPrototype(); diff --git a/src/LdcUserProfile/Service/ProfileServiceFactory.php b/src/LdcUserProfile/Service/ProfileServiceFactory.php index 92b4b6c..f2126c0 100644 --- a/src/LdcUserProfile/Service/ProfileServiceFactory.php +++ b/src/LdcUserProfile/Service/ProfileServiceFactory.php @@ -29,24 +29,17 @@ public function createService(ServiceLocatorInterface $serviceLocator) // Register/Unregister the active/inactive extensions foreach ($moduleOptions->getRegisteredExtensions() as $extensionName => $isActive) { - if ($isActive) { - $this->registerExtension($extensionName, $service, $serviceLocator); - } else { + if (! $isActive) { $service->unregisterExtension($extensionName); + continue; + } + + $extension = $serviceLocator->get($extensionName); + if ($extension instanceof AbstractExtension) { + $service->registerExtension($extension); } } return $service; } - - protected function registerExtension( - $extensionName, - ProfileService $service, - ServiceLocatorInterface $serviceLocator - ) { - $extension = $serviceLocator->get($extensionName); - if ($extension instanceof AbstractExtension) { - $service->registerExtension($extension); - } - } } diff --git a/tests/LdcUserProfileTest/Options/ModuleOptionsTest.php b/tests/LdcUserProfileTest/Options/ModuleOptionsTest.php index 9ee2cba..a6c90e3 100644 --- a/tests/LdcUserProfileTest/Options/ModuleOptionsTest.php +++ b/tests/LdcUserProfileTest/Options/ModuleOptionsTest.php @@ -48,4 +48,22 @@ public function testGetSetValidationGroupOverrides() $this->assertEquals($data, $this->options->getValidationGroupOverrides()); } + public function testSetValidationGroupOverridesRequiresArray() + { + $this->setExpectedException('PHPUnit_Framework_Error'); + $this->options->setValidationGroupOverrides('foo'); + } + + public function testGetSetRegisteredExtensions() + { + $data = array('foo', 'bar'); + $this->options->setRegisteredExtensions($data); + $this->assertEquals($data, $this->options->getRegisteredExtensions()); + } + + public function testSetRegisteredExtensionsRequiresArray() + { + $this->setExpectedException('PHPUnit_Framework_Error'); + $this->options->setRegisteredExtensions('foo'); + } } diff --git a/tests/LdcUserProfileTest/Service/ProfileServiceFactoryTest.php b/tests/LdcUserProfileTest/Service/ProfileServiceFactoryTest.php index 2819cf5..56d470a 100644 --- a/tests/LdcUserProfileTest/Service/ProfileServiceFactoryTest.php +++ b/tests/LdcUserProfileTest/Service/ProfileServiceFactoryTest.php @@ -9,7 +9,7 @@ namespace LdcUserProfileTest\Service; -use LdcUserProfile\Service\ProfileService; +use LdcUserProfile\Options\ModuleOptions; class ProfileServiceFactoryTest extends \PHPUnit_Framework_TestCase { @@ -27,4 +27,47 @@ public function testCreateService() $this->assertInstanceOf('LdcUserProfile\Service\ProfileService', $svc); $this->assertSame($mockModuleOptions, $svc->getModuleOptions()); } + + public function testCreateServiceWithRegisteredExtension() + { + $moduleOptions = new ModuleOptions(); + $moduleOptions->setRegisteredExtensions(array('foo' => true)); + + $mockExtension = \Mockery::mock('LdcUserProfile\Extensions\AbstractExtension'); + $mockExtension->shouldReceive('getName')->andReturn('foo'); + + $serviceManager = new \Zend\ServiceManager\ServiceManager(); + $serviceManager->setService('ldc-user-profile_module_options', $moduleOptions); + $serviceManager->setService('foo', $mockExtension); + + $factory = new \LdcUserProfile\Service\ProfileServiceFactory(); + $svc = $factory->createService($serviceManager); + + $this->assertInstanceOf('LdcUserProfile\Service\ProfileService', $svc); + $this->assertTrue($svc->hasExtension('foo')); + $this->assertTrue($svc->hasExtension($mockExtension)); + + $extensions = $svc->getExtensions(); + $this->assertSame($mockExtension, $extensions['foo']); + } + + public function testCreateServiceWithUnregisterExtension() + { + $moduleOptions = new ModuleOptions(); + $moduleOptions->setRegisteredExtensions(array('foo' => false)); + + $mockExtension = \Mockery::mock('LdcUserProfile\Extensions\AbstractExtension'); + $mockExtension->shouldReceive('getName')->andReturn('foo'); + + $serviceManager = new \Zend\ServiceManager\ServiceManager(); + $serviceManager->setService('ldc-user-profile_module_options', $moduleOptions); + //$serviceManager->setService('foo', $mockExtension); + + $factory = new \LdcUserProfile\Service\ProfileServiceFactory(); + $svc = $factory->createService($serviceManager); + + $this->assertInstanceOf('LdcUserProfile\Service\ProfileService', $svc); + $this->assertFalse($svc->hasExtension('foo')); + $this->assertFalse($svc->hasExtension($mockExtension)); + } } diff --git a/tests/LdcUserProfileTest/Service/ProfileServiceTest.php b/tests/LdcUserProfileTest/Service/ProfileServiceTest.php index 1c27433..fbdc911 100644 --- a/tests/LdcUserProfileTest/Service/ProfileServiceTest.php +++ b/tests/LdcUserProfileTest/Service/ProfileServiceTest.php @@ -46,6 +46,12 @@ public function testRegisterExtensionRejectsInvalidExtension() $this->service->registerExtension(new \stdClass()); } + public function testRegisterExtensionRejectsNullExtension() + { + $this->setExpectedException('PHPUnit_Framework_Error'); + $this->service->registerExtension(null); + } + public function testUnregisterExtensionByInstance() { $ext = $this->testRegisterExtension(); @@ -62,6 +68,20 @@ public function testUnregisterExtensionByName() $this->assertArrayNotHasKey('testext', $this->service->getExtensions()); } + public function testHasExtensionByName() + { + $ext = $this->testRegisterExtension(); + + $this->assertTrue($this->service->hasExtension($ext->getName())); + } + + public function testHasExtensionByInstance() + { + $ext = $this->testRegisterExtension(); + + $this->assertTrue($this->service->hasExtension($ext)); + } + public function testSaveCallsSaveOnEachRegsiteredExtension() { $payload = new \stdClass();