Skip to content

Commit c5c1e05

Browse files
committed
Merge pull request #41 from martiis/master
Unique filter names validation in configuration.
2 parents 845aad8 + 2e1a87a commit c5c1e05

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

DependencyInjection/ONGRFilterManagerExtension.php

+26
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace ONGR\FilterManagerBundle\DependencyInjection;
1313

14+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1415
use Symfony\Component\DependencyInjection\ContainerBuilder;
1516
use Symfony\Component\Config\FileLocator;
1617
use Symfony\Component\DependencyInjection\Definition;
@@ -51,6 +52,7 @@ private function addFilters(array $config, ContainerBuilder $container)
5152
}
5253

5354
$filterMap = $container->getParameter('ongr_filter_manager.filter_map');
55+
$this->validateFilterNames($config['filters']);
5456

5557
foreach ($config['filters'] as $type => $filters) {
5658
foreach ($filters as $name => $filter) {
@@ -82,6 +84,30 @@ private function addFilters(array $config, ContainerBuilder $container)
8284
}
8385
}
8486

87+
/**
88+
* Checks if filter names are valid.
89+
*
90+
* @param array $filters Filters to validate.
91+
*
92+
* @throws InvalidConfigurationException
93+
*/
94+
private function validateFilterNames(array $filters)
95+
{
96+
$existing = [];
97+
98+
foreach ($filters as $type => $filters) {
99+
foreach ($filters as $name => $data) {
100+
if (in_array($name, $existing)) {
101+
throw new InvalidConfigurationException(
102+
"Found duplicate filter name `{$name}` in `{$type}` filter"
103+
);
104+
}
105+
106+
$existing[] = $name;
107+
}
108+
}
109+
}
110+
85111
/**
86112
* Adds filters managers based on configuration.
87113
*

Tests/Unit/DependencyInjection/ONGRFilterManagerExtensionTest.php

+30-2
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,35 @@ public function testWithEmptyConfiguration()
167167
}
168168
}
169169

170+
/**
171+
* Tests if exception is thrown when filter names are duplicated.
172+
*
173+
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
174+
* @expectedExceptionMessage Found duplicate filter name `test_sorting` in `match` filter
175+
*/
176+
public function testConfigWithDuplicateNameException()
177+
{
178+
$config = array_replace_recursive(
179+
$this->getDummyConfig(),
180+
[
181+
'ongr_filter_manager' => [
182+
'filters' => [
183+
'match' => [
184+
'test_sorting' => [
185+
'request_field' => 'test_q',
186+
'field' => 'test_title',
187+
],
188+
],
189+
],
190+
],
191+
]
192+
);
193+
194+
$container = new ContainerBuilder();
195+
$extension = new ONGRFilterManagerExtension();
196+
$extension->load($config, $container);
197+
}
198+
170199
/**
171200
* Returns dummy configuration for testing.
172201
*
@@ -188,8 +217,7 @@ protected function getDummyConfig($relations = [])
188217
],
189218
'filters' => [
190219
'sort' => [
191-
'test_sorting' =>
192-
[
220+
'test_sorting' => [
193221
'relations' => $relations,
194222
'request_field' => 'sort',
195223
'choices' => [

0 commit comments

Comments
 (0)