diff --git a/src/Provider/Doctrine/FixtureFactory.php b/src/Provider/Doctrine/FixtureFactory.php index 5a3c52a..330075c 100644 --- a/src/Provider/Doctrine/FixtureFactory.php +++ b/src/Provider/Doctrine/FixtureFactory.php @@ -4,6 +4,7 @@ use Doctrine\ORM\EntityManager; use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\ORM\Mapping; use Exception; /** @@ -84,12 +85,17 @@ public function get($name, array $fieldOverrides = []) throw EntityDefinitionUnavailable::for($name); } + /** @var EntityDef $def */ $def = $this->entityDefs[$name]; + $config = $def->getConfig(); $this->checkFieldOverrides($def, $fieldOverrides); - $ent = $def->getEntityMetadata()->newInstance(); + /** @var Mapping\ClassMetadata $entityMetadata */ + $entityMetadata = $def->getEntityMetadata(); + + $ent = $entityMetadata->newInstance(); $fieldValues = []; foreach ($def->getFieldDefs() as $fieldName => $fieldDef) { $fieldValues[$fieldName] = array_key_exists($fieldName, $fieldOverrides) @@ -105,7 +111,7 @@ public function get($name, array $fieldOverrides = []) $config['afterCreate']($ent, $fieldValues); } - if ($this->persist) { + if ($this->persist && false === $entityMetadata->isEmbeddedClass) { $this->em->persist($ent); } diff --git a/tests/Provider/Doctrine/Fixtures/PersistingTest.php b/tests/Provider/Doctrine/Fixtures/PersistingTest.php index 01df511..fc406be 100644 --- a/tests/Provider/Doctrine/Fixtures/PersistingTest.php +++ b/tests/Provider/Doctrine/Fixtures/PersistingTest.php @@ -1,6 +1,9 @@ getQuery(); $this->assertEmpty($q->getResult()); } + + /** + * @test + */ + public function doesNotPersistEmbeddableWhenAutomaticPersistingIsTurnedOn() + { + $mappingClasses = [ + Mapping\Embeddable::class, + Mapping\Embedded::class, + ]; + + foreach ($mappingClasses as $mappingClass) { + if (!class_exists($mappingClass)) { + $this->markTestSkipped('Doctrine Embeddable feature not available'); + } + } + + $this->factory->defineEntity('Name', [ + 'first' => FieldDef::sequence(static function () { + $values = [ + null, + 'Doe', + 'Smith', + ]; + + return $values[array_rand($values)]; + }), + 'last' => FieldDef::sequence(static function () { + $values = [ + null, + 'Jane', + 'John', + ]; + + return $values[array_rand($values)]; + }), + ]); + + $this->factory->defineEntity('Commander', [ + 'name' => FieldDef::reference('Name'), + ]); + + $this->factory->persistOnGet(); + + /** @var TestEntity\Commander $commander */ + $commander = $this->factory->get('Commander'); + + $this->assertInstanceOf(TestEntity\Commander::class, $commander); + $this->assertInstanceOf(TestEntity\Name::class, $commander->name()); + + $this->em->flush(); + } } diff --git a/tests/Provider/Doctrine/Fixtures/TestEntity/Commander.php b/tests/Provider/Doctrine/Fixtures/TestEntity/Commander.php new file mode 100644 index 0000000..3a48c5e --- /dev/null +++ b/tests/Provider/Doctrine/Fixtures/TestEntity/Commander.php @@ -0,0 +1,46 @@ +name = new Name(); + } + + public function id(): string + { + return $this->id; + } + + public function name(): Name + { + return $this->name; + } +} diff --git a/tests/Provider/Doctrine/Fixtures/TestEntity/Name.php b/tests/Provider/Doctrine/Fixtures/TestEntity/Name.php new file mode 100644 index 0000000..8f0ba89 --- /dev/null +++ b/tests/Provider/Doctrine/Fixtures/TestEntity/Name.php @@ -0,0 +1,43 @@ +first; + } + + public function last(): ?string + { + return $this->last; + } +}