Skip to content
Permalink
Browse files

Merge pull request #78 from ergebnis/feature/slide

Enhancement: Fail early when field definitions reference unavailable fields
  • Loading branch information
localheinz committed Mar 24, 2020
2 parents f352466 + 740162f commit b261249cffe42d8944988016f4cca657934870be
Showing with 31 additions and 21 deletions.
  1. +21 −13 src/EntityDef.php
  2. +10 −8 test/Unit/FixtureFactoryTest.php
@@ -42,25 +42,33 @@ public function __construct(ORM\Mapping\ClassMetadata $classMetadata, array $fie
$this->fieldDefinitions = [];
$this->configuration = $configuration;

foreach ($fieldDefinitions as $fieldName => $fieldDefinition) {
if (!$this->classMetadata->hasField($fieldName) && !$this->classMetadata->hasAssociation($fieldName)) {
throw new \Exception(\sprintf(
'No such field in %s: %s',
$this->getClassName(),
$fieldName
));
}
$fieldNames = \array_merge(
\array_keys($this->classMetadata->fieldMappings),
\array_keys($this->classMetadata->associationMappings),
\array_keys($this->classMetadata->embeddedClasses)
);

$extraFieldNames = \array_diff(
\array_keys($fieldDefinitions),
$fieldNames
);

if ([] !== $extraFieldNames) {
\natsort($extraFieldNames);

throw new \Exception(\sprintf(
'No such fields in %s: "%s"',
$this->getClassName(),
\implode('", "', $extraFieldNames)
));
}

foreach ($fieldDefinitions as $fieldName => $fieldDefinition) {
$this->fieldDefinitions[$fieldName] = $this->normalizeFieldDefinition($fieldDefinition);
}

$defaultEntity = $this->classMetadata->newInstance();

$fieldNames = \array_merge(
$this->classMetadata->getFieldNames(),
$this->classMetadata->getAssociationNames()
);

foreach ($fieldNames as $fieldName) {
if (\array_key_exists($fieldName, $this->fieldDefinitions)) {
continue;
@@ -76,21 +76,23 @@ public function testDefineEntityThrowsExceptionWhenClassNameDoesNotReferenceAnEn
$fixtureFactory->defineEntity($className);
}

public function testDefineEntityThrowsExceptionWhenUsingFieldNameThatDoesNotExistInEntity(): void
public function testDefineEntityThrowsExceptionWhenUsingFieldNamesThatDoNotExistInEntity(): void
{
$fieldName = 'pieType';

$fixtureFactory = new FixtureFactory(self::createEntityManager());

$this->expectException(\Exception::class);
$this->expectExceptionMessage(\sprintf(
'No such field in %s: %s',
Fixture\FixtureFactory\Entity\Spaceship::class,
$fieldName
'No such fields in %s: "diameter", "foo1", "foo3", "foo20", "pieType"',
Fixture\FixtureFactory\Entity\Commander::class
));

$fixtureFactory->defineEntity(Fixture\FixtureFactory\Entity\Spaceship::class, [
$fieldName => 'blueberry',
$fixtureFactory->defineEntity(Fixture\FixtureFactory\Entity\Commander::class, [
'diameter' => '30cm',
'foo1' => 'bar',
'foo20' => 'baz',
'foo3' => 'qux',
'name' => new Fixture\FixtureFactory\Entity\Name(),
'pieType' => 'blueberry',
]);
}

0 comments on commit b261249

Please sign in to comment.
You can’t perform that action at this time.