Skip to content

Commit

Permalink
Minor implementation changes + unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adamlundrigan committed Jul 7, 2014
1 parent 2321a00 commit ab574e4
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/LdcUserProfile/Options/ModuleOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
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
21 changes: 7 additions & 14 deletions src/LdcUserProfile/Service/ProfileServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
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');
}
}
45 changes: 44 additions & 1 deletion tests/LdcUserProfileTest/Service/ProfileServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace LdcUserProfileTest\Service;

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

class ProfileServiceFactoryTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -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));
}
}
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 ab574e4

Please sign in to comment.