Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Ensure validators config is honored in non-zend-mvc contexts #168

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/ValidatorPluginManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Zend\Validator;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Config;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

Expand All @@ -27,7 +28,30 @@ class ValidatorPluginManagerFactory implements FactoryInterface
*/
public function __invoke(ContainerInterface $container, $name, array $options = null)
{
return new ValidatorPluginManager($container, $options ?: []);
$pluginManager = new ValidatorPluginManager($container, $options ?: []);

// If this is in a zend-mvc application, the ServiceListener will inject
// merged configuration during bootstrap.
if ($container->has('ServiceListener')) {
return $pluginManager;
}

// If we do not have a config service, nothing more to do
if (! $container->has('config')) {
return $pluginManager;
}

$config = $container->get('config');

// If we do not have validators configuration, nothing more to do
if (! isset($config['validators']) || ! is_array($config['validators'])) {
return $pluginManager;
}

// Wire service configuration for validators
(new Config($config['validators']))->configureServiceManager($pluginManager);

return $pluginManager;
}

/**
Expand Down
100 changes: 100 additions & 0 deletions test/ValidatorPluginManagerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Interop\Container\ContainerInterface;
use PHPUnit\Framework\TestCase;
use Zend\Validator\Digits;
use Zend\Validator\ValidatorInterface;
use Zend\Validator\ValidatorPluginManager;
use Zend\Validator\ValidatorPluginManagerFactory;
Expand Down Expand Up @@ -70,4 +71,103 @@ public function testFactoryConfiguresPluginManagerUnderServiceManagerV2()
$validators = $factory->createService($container->reveal());
$this->assertSame($validator, $validators->get('test'));
}

public function testConfiguresValidatorServicesWhenFound()
{
$validator = $this->prophesize(ValidatorInterface::class)->reveal();
$config = [
'validators' => [
'aliases' => [
'test' => Digits::class,
],
'factories' => [
'test-too' => function ($container) use ($validator) {
return $validator;
},
],
],
];

$container = $this->prophesize(ServiceLocatorInterface::class);
$container->willImplement(ContainerInterface::class);

$container->has('ServiceListener')->willReturn(false);
$container->has('config')->willReturn(true);
$container->get('config')->willReturn($config);
$container->has('MvcTranslator')->willReturn(false); // necessary due to default initializers

$factory = new ValidatorPluginManagerFactory();
$validators = $factory($container->reveal(), 'ValidatorManager');

$this->assertInstanceOf(ValidatorPluginManager::class, $validators);
$this->assertTrue($validators->has('test'));
$this->assertInstanceOf(Digits::class, $validators->get('test'));
$this->assertTrue($validators->has('test-too'));
$this->assertSame($validator, $validators->get('test-too'));
}

public function testDoesNotConfigureValidatorServicesWhenServiceListenerPresent()
{
$validator = $this->prophesize(ValidatorInterface::class)->reveal();
$config = [
'validators' => [
'aliases' => [
'test' => Digits::class,
],
'factories' => [
'test-too' => function ($container) use ($validator) {
return $validator;
},
],
],
];

$container = $this->prophesize(ServiceLocatorInterface::class);
$container->willImplement(ContainerInterface::class);

$container->has('ServiceListener')->willReturn(true);
$container->has('config')->shouldNotBeCalled();
$container->get('config')->shouldNotBeCalled();
$container->has('MvcTranslator')->willReturn(false); // necessary due to default initializers

$factory = new ValidatorPluginManagerFactory();
$validators = $factory($container->reveal(), 'ValidatorManager');

$this->assertInstanceOf(ValidatorPluginManager::class, $validators);
$this->assertFalse($validators->has('test'));
$this->assertFalse($validators->has('test-too'));
}

public function testDoesNotConfigureValidatorServicesWhenConfigServiceNotPresent()
{
$container = $this->prophesize(ServiceLocatorInterface::class);
$container->willImplement(ContainerInterface::class);

$container->has('ServiceListener')->willReturn(false);
$container->has('config')->willReturn(false);
$container->get('config')->shouldNotBeCalled();
$container->has('MvcTranslator')->willReturn(false); // necessary due to default initializers

$factory = new ValidatorPluginManagerFactory();
$validators = $factory($container->reveal(), 'ValidatorManager');

$this->assertInstanceOf(ValidatorPluginManager::class, $validators);
}

public function testDoesNotConfigureValidatorServicesWhenConfigServiceDoesNotContainValidatorsConfig()
{
$container = $this->prophesize(ServiceLocatorInterface::class);
$container->willImplement(ContainerInterface::class);

$container->has('ServiceListener')->willReturn(false);
$container->has('config')->willReturn(true);
$container->get('config')->willReturn(['foo' => 'bar']);
$container->has('MvcTranslator')->willReturn(false); // necessary due to default initializers

$factory = new ValidatorPluginManagerFactory();
$validators = $factory($container->reveal(), 'ValidatorManager');

$this->assertInstanceOf(ValidatorPluginManager::class, $validators);
$this->assertFalse($validators->has('foo'));
}
}