Skip to content
Permalink
Browse files

Merge pull request #79 from ergebnis/feature/embeddable

Fix: Populate referenced embeddables
  • Loading branch information
localheinz committed Mar 24, 2020
2 parents b261249 + 012bcc5 commit adaa3e09a76a4948b25cbd772ac4b9b0c36ebd9f
Showing with 84 additions and 3 deletions.
  1. +1 −1 .github/workflows/integrate.yaml
  2. +5 −0 CHANGELOG.md
  3. +1 −1 Makefile
  4. +6 −1 src/EntityDef.php
  5. +71 −0 test/Unit/FixtureFactoryTest.php
@@ -10,7 +10,7 @@ on: # yamllint disable-line rule:truthy

env:
MIN_COVERED_MSI: 94
MIN_MSI: 86
MIN_MSI: 87
REQUIRED_PHP_EXTENSIONS: "mbstring"

jobs:
@@ -24,6 +24,10 @@ For a full diff see [`fa9c564...master`][fa9c564...master].
* Marked all classes as `final` ([#33]), by [@localheinz]
* Marked `EntityDef` as internal ([#49]), by [@localheinz]

### Fixed

* Populated embeddables and disallowed referencing fields using dot notation ([#79]), by [@localheinz]

[fa9c564...master]: https://github.com/ergebnis/factory-bot/compare/fa9c564...master

[#1]: https://github.com/ergebnis/factory-bot/pull/1
@@ -36,5 +40,6 @@ For a full diff see [`fa9c564...master`][fa9c564...master].
[#24]: https://github.com/ergebnis/factory-bot/pull/24
[#33]: https://github.com/ergebnis/factory-bot/pull/33
[#49]: https://github.com/ergebnis/factory-bot/pull/49
[#79]: https://github.com/ergebnis/factory-bot/pull/79

[@localheinz]: https://github.com/localheinz
@@ -1,5 +1,5 @@
MIN_COVERED_MSI:=94
MIN_MSI:=86
MIN_MSI:=87

.PHONY: it
it: coding-standards dependency-analysis static-code-analysis doctrine tests ## Runs the coding-standards, dependency-analysis, static-code-analysis, doctrine, and tests targets
@@ -42,12 +42,17 @@ public function __construct(ORM\Mapping\ClassMetadata $classMetadata, array $fie
$this->fieldDefinitions = [];
$this->configuration = $configuration;

$fieldNames = \array_merge(
/** @var string[] $allFieldNames */
$allFieldNames = \array_merge(
\array_keys($this->classMetadata->fieldMappings),
\array_keys($this->classMetadata->associationMappings),
\array_keys($this->classMetadata->embeddedClasses)
);

$fieldNames = \array_filter($allFieldNames, static function (string $fieldName): bool {
return false === \strpos($fieldName, '.');
});

$extraFieldNames = \array_diff(
\array_keys($fieldDefinitions),
$fieldNames
@@ -19,6 +19,7 @@
use Ergebnis\FactoryBot\FieldDef;
use Ergebnis\FactoryBot\FixtureFactory;
use Ergebnis\FactoryBot\Test\Fixture;
use Ergebnis\Test\Util\Helper;

/**
* @internal
@@ -31,6 +32,8 @@
*/
final class FixtureFactoryTest extends AbstractTestCase
{
use Helper;

public function testDefineEntityThrowsExceptionWhenDefinitionHasAlreadyBeenProvidedForEntity(): void
{
$fixtureFactory = new FixtureFactory(self::createEntityManager());
@@ -139,6 +142,74 @@ public function testThrowsWhenTryingToGiveNonexistentFieldsWhileConstructing():
]);
}

public function testDefineEntityAllowsDefiningAndReferencingEmbeddables(): void
{
$faker = self::faker()->unique();

$firstName = $faker->firstName;
$lastName = $faker->lastName;

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

$fixtureFactory->defineEntity(Fixture\FixtureFactory\Entity\Name::class, [
'first' => $firstName,
'last' => $lastName,
]);

$fixtureFactory->defineEntity(Fixture\FixtureFactory\Entity\Commander::class, [
'name' => FieldDef::reference(Fixture\FixtureFactory\Entity\Name::class),
]);

$commander = $fixtureFactory->get(Fixture\FixtureFactory\Entity\Commander::class);

self::assertInstanceOf(Fixture\FixtureFactory\Entity\Commander::class, $commander);

$name = $commander->name();

self::assertInstanceOf(Fixture\FixtureFactory\Entity\Name::class, $name);

self::assertSame($firstName, $name->first());
self::assertSame($lastName, $name->last());
}

public function testDefineEntityThrowsExceptionWhenReferencingFieldsOfEmbeddablesUsingDotNotation(): void
{
$faker = self::faker()->unique();

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

$this->expectException(\Exception::class);
$this->expectExceptionMessage(\sprintf(
'No such fields in %s: "name.first", "name.last"',
Fixture\FixtureFactory\Entity\Commander::class
));

$fixtureFactory->defineEntity(Fixture\FixtureFactory\Entity\Commander::class, [
'name.first' => $faker->firstName,
'name.last' => $faker->lastName,
]);
}

public function testGetEntityThrowsExceptionWhenReferencingFieldsOfEmbeddablesUsingDotNotation(): void
{
$faker = self::faker()->unique();

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

$fixtureFactory->defineEntity(Fixture\FixtureFactory\Entity\Commander::class);

$this->expectException(\Exception::class);
$this->expectExceptionMessage(\sprintf(
'Field(s) not in %s: \'name.first\', \'name.last\'',
Fixture\FixtureFactory\Entity\Commander::class
));

$fixtureFactory->get(Fixture\FixtureFactory\Entity\Commander::class, [
'name.first' => $faker->firstName,
'name.last' => $faker->lastName,
]);
}

public function testThrowsWhenTryingToGetLessThanOneInstance(): void
{
$fixtureFactory = new FixtureFactory(self::createEntityManager());

0 comments on commit adaa3e0

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