Skip to content

Commit 7877805

Browse files
committed
configValidator to check entity class is unique; unit test
1 parent 7968237 commit 7877805

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

Config/ConfigValidator.php

+26-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Mdiyakov\DoctrineSolrBundle\Exception\ClientConfigException;
66
use Mdiyakov\DoctrineSolrBundle\Exception\ConfigFieldException;
7+
use Mdiyakov\DoctrineSolrBundle\Exception\EntityClassConfigException;
78
use Mdiyakov\DoctrineSolrBundle\Exception\FilterConfigException;
89
use Mdiyakov\DoctrineSolrBundle\Exception\RequiredFieldException;
910
use Mdiyakov\DoctrineSolrBundle\Exception\SchemaConfigException;
@@ -12,10 +13,15 @@
1213
class ConfigValidator
1314
{
1415
/**
15-
* @var array
16+
* @var string[]
1617
*/
1718
private $discriminatorValues = [];
1819

20+
/**
21+
* @var string[]
22+
*/
23+
private $entityClasses = [];
24+
1925
/**
2026
* @param string[][] $entityConfig
2127
* @param string[][] $schemes
@@ -33,6 +39,7 @@ public function validate($entityConfig, $schemes, $filters, $clients)
3339

3440
$schemaConfig = $schemes[$entityConfig['schema']];
3541
$this->checkEntityContainRequiredFields($entityConfig['class'], $schemaConfig);
42+
$this->checkClassesForUnique($entityConfig['class']);
3643
$this->checkConfigFields($entityConfig, $schemaConfig);
3744
$this->checkFilters($entityConfig, $filters);
3845
$this->checkClients($schemaConfig, $clients);
@@ -219,4 +226,22 @@ private function checkEntityHasField($entityClass, $entityFieldName)
219226
);
220227
}
221228
}
229+
230+
/**
231+
* @param $entityClass
232+
* @throws EntityClassConfigException
233+
*/
234+
private function checkClassesForUnique($entityClass)
235+
{
236+
if (!in_array($entityClass, $this->entityClasses)) {
237+
$this->entityClasses[] = $entityClass;
238+
} else {
239+
throw new EntityClassConfigException(
240+
sprintf(
241+
'It seems entity class "%s" has been configured more than once inside "indexed_entities" section. You can not have different config for the single entity class',
242+
$entityClass
243+
)
244+
);
245+
}
246+
}
222247
}

Tests/Config/ConfigValidatorTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ public function testSchemaNotDefined()
8585
$this->runConfigTest('schema_not_defined');
8686
}
8787

88+
/**
89+
* @expectedException \Mdiyakov\DoctrineSolrBundle\Exception\EntityClassConfigException
90+
* @expectedExceptionMessage It seems entity class "Mdiyakov\DoctrineSolrBundle\Tests\Config\Entity\MyEntity" has been configured more than once inside "indexed_entities" section. You can not have different config for the single entity class
91+
*/
92+
public function testEntityClassDoubled()
93+
{
94+
$this->runConfigTest('entity_class_doubled');
95+
}
96+
97+
8898
/**
8999
* @expectedException \Mdiyakov\DoctrineSolrBundle\Exception\ConfigFieldException
90100
* @expectedExceptionMessage "my_enitity_type" discriminator value has already been used.

Tests/Config/config_fixtures.yml

+22
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,26 @@ discriminator_config_not_unique:
162162
fields:
163163
- { entity_field_name: 'id', document_field_name: 'd_id', field_type: int, entity_primary_key: true }
164164
filters: []
165+
solarium_clients: []
166+
167+
entity_class_doubled:
168+
indexed_entities:
169+
my_entity:
170+
class: Mdiyakov\DoctrineSolrBundle\Tests\Config\Entity\MyEntity
171+
schema: my_schema
172+
config:
173+
- { name: type, value: page }
174+
my_entity_2:
175+
class: Mdiyakov\DoctrineSolrBundle\Tests\Config\Entity\MyEntity
176+
schema: my_schema
177+
config:
178+
- { name: type, value: page }
179+
schemes:
180+
my_schema:
181+
document_unique_field: { name: 'uid' }
182+
config_entity_fields:
183+
- { config_field_name: 'type', document_field_name: 'type', discriminator: true, priority: 50 }
184+
fields:
185+
- { entity_field_name: 'id', document_field_name: 'd_id', field_type: int, entity_primary_key: true }
186+
filters: []
165187
solarium_clients: []

0 commit comments

Comments
 (0)