Skip to content

Commit

Permalink
Merge branch 'MalteGerth/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
adamlundrigan committed Jul 7, 2014
2 parents 85d5462 + ab574e4 commit acc0446
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 12 deletions.
8 changes: 0 additions & 8 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,9 @@
namespace LdcUserProfile;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\Mvc\MvcEvent;

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()
{
Expand Down
8 changes: 8 additions & 0 deletions config/ldc-user-profile.global.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

);

/**
Expand Down
2 changes: 2 additions & 0 deletions demo/ExtensionModule/src/ExtensionModule/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

class Extension extends AbstractExtension
{

protected $session;

public function getName()
{
Expand Down
4 changes: 3 additions & 1 deletion src/LdcUserProfile/Controller/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/LdcUserProfile/Extensions/ZfcUser/ZfcUserForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(RegistrationOptionsInterface $registrationOptions)
* Set Registration Options
*
* @param RegistrationOptionsInterface $registrationOptions
* @return Register
* @return ZfcUserForm
*/
public function setRegistrationOptions(RegistrationOptionsInterface $registrationOptions)
{
Expand Down
7 changes: 7 additions & 0 deletions src/LdcUserProfile/Extensions/ZfcUser/ZfcUserInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions src/LdcUserProfile/Options/ModuleOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ class ModuleOptions extends AbstractOptions
*/
protected $validationGroupOverrides = array();

/**
* Registered extensions
*
* @var array
*/
protected $registeredExtensions = array();

public function getIsEnabled()
{
return $this->isEnabled;
Expand Down Expand Up @@ -76,4 +83,16 @@ public function setValidationGroupOverrides(array $vg)
return $this;
}

public function getRegisteredExtensions()
{
return $this->registeredExtensions;
}

public function setRegisteredExtensions(array $ext)
{
$this->registeredExtensions = $ext;

return $this;
}

}
10 changes: 10 additions & 0 deletions src/LdcUserProfile/Service/ProfileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
20 changes: 19 additions & 1 deletion src/LdcUserProfile/Service/ProfileServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace LdcUserProfile\Service;

use LdcUserProfile\Extensions\AbstractExtension;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

Expand All @@ -20,7 +21,24 @@ 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
/* @var \LdcUserProfile\Options\ModuleOptions $moduleOptions */
$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) {
$service->unregisterExtension($extensionName);
continue;
}

$extension = $serviceLocator->get($extensionName);
if ($extension instanceof AbstractExtension) {
$service->registerExtension($extension);
}
}

return $service;
}
Expand Down
18 changes: 18 additions & 0 deletions tests/LdcUserProfileTest/Options/ModuleOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
46 changes: 45 additions & 1 deletion tests/LdcUserProfileTest/Service/ProfileServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@

namespace LdcUserProfileTest\Service;

use LdcUserProfile\Service\ProfileService;
use LdcUserProfile\Options\ModuleOptions;

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);
Expand All @@ -26,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));
}
}
20 changes: 20 additions & 0 deletions tests/LdcUserProfileTest/Service/ProfileServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit acc0446

Please sign in to comment.