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

Commit

Permalink
Merge branch 'hotfix/filters-service-config'
Browse files Browse the repository at this point in the history
Close #56
  • Loading branch information
weierophinney committed May 17, 2017
2 parents c4bb581 + a0404b2 commit 2c29e8b
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 2 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#56](https://github.com/zendframework/zend-filter/pull/56) fixes how the
`FilterPluginManagerFactory` factory initializes the plugin manager instance,
ensuring it is injecting the relevant configuration from the `config` service
and thus seeding it with configured translator loader services. This means
that the `filters` configuration will now be honored in non-zend-mvc contexts.

## 2.7.1 - 2016-04-18

Expand Down
26 changes: 25 additions & 1 deletion src/FilterPluginManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Zend\Filter;

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

Expand All @@ -27,7 +28,30 @@ class FilterPluginManagerFactory implements FactoryInterface
*/
public function __invoke(ContainerInterface $container, $name, array $options = null)
{
return new FilterPluginManager($container, $options ?: []);
$pluginManager = new FilterPluginManager($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 filters configuration, nothing more to do
if (! isset($config['filters']) || ! is_array($config['filters'])) {
return $pluginManager;
}

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

return $pluginManager;
}

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

use Interop\Container\ContainerInterface;
use PHPUnit_Framework_TestCase as TestCase;
use Zend\Filter\Boolean;
use Zend\Filter\FilterInterface;
use Zend\Filter\FilterPluginManager;
use Zend\Filter\FilterPluginManagerFactory;
use Zend\ServiceManager\ServiceLocatorInterface;
Expand Down Expand Up @@ -73,4 +75,99 @@ public function testFactoryConfiguresPluginManagerUnderServiceManagerV2()
$filters = $factory->createService($container->reveal());
$this->assertSame($filter, $filters->get('test'));
}

public function testConfiguresFilterServicesWhenFound()
{
$filter = $this->prophesize(FilterInterface::class)->reveal();
$config = [
'filters' => [
'aliases' => [
'test' => Boolean::class,
],
'factories' => [
'test-too' => function ($container) use ($filter) {
return $filter;
},
],
],
];

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

$container->has('ServiceListener')->willReturn(false);
$container->has('config')->willReturn(true);
$container->get('config')->willReturn($config);

$factory = new FilterPluginManagerFactory();
$filters = $factory($container->reveal(), 'FilterManager');

$this->assertInstanceOf(FilterPluginManager::class, $filters);
$this->assertTrue($filters->has('test'));
$this->assertInstanceOf(Boolean::class, $filters->get('test'));
$this->assertTrue($filters->has('test-too'));
$this->assertSame($filter, $filters->get('test-too'));
}

public function testDoesNotConfigureFilterServicesWhenServiceListenerPresent()
{
$filter = $this->prophesize(FilterInterface::class)->reveal();
$config = [
'filters' => [
'aliases' => [
'test' => Boolean::class,
],
'factories' => [
'test-too' => function ($container) use ($filter) {
return $filter;
},
],
],
];

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

$container->has('ServiceListener')->willReturn(true);
$container->has('config')->shouldNotBeCalled();
$container->get('config')->shouldNotBeCalled();

$factory = new FilterPluginManagerFactory();
$filters = $factory($container->reveal(), 'FilterManager');

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

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

$container->has('ServiceListener')->willReturn(false);
$container->has('config')->willReturn(false);
$container->get('config')->shouldNotBeCalled();

$factory = new FilterPluginManagerFactory();
$filters = $factory($container->reveal(), 'FilterManager');

$this->assertInstanceOf(FilterPluginManager::class, $filters);
}

public function testDoesNotConfigureFilterServicesWhenConfigServiceDoesNotContainFiltersConfig()
{
$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']);

$factory = new FilterPluginManagerFactory();
$filters = $factory($container->reveal(), 'FilterManager');

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

0 comments on commit 2c29e8b

Please sign in to comment.