From eb720440d60741518f35d0dc34cd5007f47af393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Mon, 13 Jul 2020 18:41:57 +0200 Subject: [PATCH] Fix: Add tests for persistOnGet() --- test/Integration/FixtureFactoryTest.php | 161 +++++++++++++++++++++--- 1 file changed, 143 insertions(+), 18 deletions(-) diff --git a/test/Integration/FixtureFactoryTest.php b/test/Integration/FixtureFactoryTest.php index 348eb767..e0651706 100644 --- a/test/Integration/FixtureFactoryTest.php +++ b/test/Integration/FixtureFactoryTest.php @@ -13,6 +13,7 @@ namespace Ergebnis\FactoryBot\Test\Integration; +use Ergebnis\FactoryBot\Count; use Ergebnis\FactoryBot\FieldDefinition; use Ergebnis\FactoryBot\FixtureFactory; use Ergebnis\FactoryBot\Test\Fixture; @@ -30,7 +31,7 @@ */ final class FixtureFactoryTest extends AbstractTestCase { - public function testCreateOnePersistsEntityWhenPersistOnGetHasBeenTurnedOn(): void + public function testCreateOneDoesNotPersistEntityByDefault(): void { $entityManager = self::entityManager(); $faker = self::faker(); @@ -40,22 +41,55 @@ public function testCreateOnePersistsEntityWhenPersistOnGetHasBeenTurnedOn(): vo $faker ); + $name = $faker->word; + $fixtureFactory->define(Fixture\FixtureFactory\Entity\Organization::class, [ - 'name' => $faker->word, + 'name' => $name, + ]); + + $fixtureFactory->createOne(Fixture\FixtureFactory\Entity\Organization::class); + + $entityManager->flush(); + $entityManager->clear(); + + $organization = $entityManager->getRepository(Fixture\FixtureFactory\Entity\Organization::class)->findOneBy([ + 'name' => $name, + ]); + + self::assertNull($organization); + } + + public function testCreateOnePersistsEntityWhenPersistOnGetHasBeenEnabled(): void + { + $entityManager = self::entityManager(); + $faker = self::faker(); + + $fixtureFactory = new FixtureFactory( + $entityManager, + $faker + ); + + $name = $faker->word; + + $fixtureFactory->define(Fixture\FixtureFactory\Entity\Organization::class, [ + 'name' => $name, ]); $fixtureFactory->persistOnGet(); - /** @var Fixture\FixtureFactory\Entity\Organization $organization */ - $organization = $fixtureFactory->createOne(Fixture\FixtureFactory\Entity\Organization::class); + $fixtureFactory->createOne(Fixture\FixtureFactory\Entity\Organization::class); $entityManager->flush(); + $entityManager->clear(); + + $organization = $entityManager->getRepository(Fixture\FixtureFactory\Entity\Organization::class)->findOneBy([ + 'name' => $name, + ]); - self::assertNotNull($organization->id()); - self::assertSame($organization, $entityManager->find(Fixture\FixtureFactory\Entity\Organization::class, $organization->id())); + self::assertInstanceOf(Fixture\FixtureFactory\Entity\Organization::class, $organization); } - public function testCreateOneDoesNotNotPersistEntityByDefault(): void + public function testCreateOneDoesNotPersistEntityWhenPersistOnGetHasBeenDisabled(): void { $entityManager = self::entityManager(); $faker = self::faker(); @@ -65,26 +99,28 @@ public function testCreateOneDoesNotNotPersistEntityByDefault(): void $faker ); + $name = $faker->word; + $fixtureFactory->define(Fixture\FixtureFactory\Entity\Organization::class, [ - 'name' => $faker->word, + 'name' => $name, ]); - /** @var Fixture\FixtureFactory\Entity\Organization $organization */ - $organization = $fixtureFactory->createOne(Fixture\FixtureFactory\Entity\Organization::class); + $fixtureFactory->persistOnGet(); + $fixtureFactory->persistOnGet(false); - $entityManager->flush(); + $fixtureFactory->createOne(Fixture\FixtureFactory\Entity\Organization::class); - self::assertNull($organization->id()); + $entityManager->flush(); + $entityManager->clear(); - $query = $entityManager->createQueryBuilder() - ->select('organization') - ->from(Fixture\FixtureFactory\Entity\Organization::class, 'organization') - ->getQuery(); + $organization = $entityManager->getRepository(Fixture\FixtureFactory\Entity\Organization::class)->findOneBy([ + 'name' => $name, + ]); - self::assertEmpty($query->getResult()); + self::assertNull($organization); } - public function testCreateOneDoesNotPersistEmbeddablesWhenPersistOnGetHasBeenTurnedOn(): void + public function testCreateOneDoesNotPersistEmbeddablesWhenPersistOnGetHasBeenEnabled(): void { $entityManager = self::entityManager(); $faker = self::faker(); @@ -113,4 +149,93 @@ public function testCreateOneDoesNotPersistEmbeddablesWhenPersistOnGetHasBeenTur $this->addToAssertionCount(1); } + + public function testCreateManyDoesNotPersistEntitiesByDefault(): void + { + $entityManager = self::entityManager(); + + $fixtureFactory = new FixtureFactory( + $entityManager, + self::faker() + ); + + $fixtureFactory->define(Fixture\FixtureFactory\Entity\Organization::class, [ + 'name' => FieldDefinition::sequence('name-%d'), + ]); + + $fixtureFactory->createMany( + Fixture\FixtureFactory\Entity\Organization::class, + Count::exact(5) + ); + + $entityManager->flush(); + $entityManager->clear(); + + $organizations = $entityManager->getRepository(Fixture\FixtureFactory\Entity\Organization::class)->findAll(); + + self::assertEmpty($organizations); + } + + public function testCreateManyPersistsEntitiesWhenPersistOnGetHasBeenEnabled(): void + { + $entityManager = self::entityManager(); + $faker = self::faker(); + + $fixtureFactory = new FixtureFactory( + $entityManager, + $faker + ); + + $fixtureFactory->define(Fixture\FixtureFactory\Entity\Organization::class, [ + 'name' => FieldDefinition::sequence('name-%d'), + ]); + + $fixtureFactory->persistOnGet(); + + $value = $faker->numberBetween(1, 5); + + $fixtureFactory->createMany( + Fixture\FixtureFactory\Entity\Organization::class, + Count::exact($value) + ); + + $entityManager->flush(); + $entityManager->clear(); + + $organizations = $entityManager->getRepository(Fixture\FixtureFactory\Entity\Organization::class)->findAll(); + + self::assertCount($value, $organizations); + } + + public function testCreateManyDoesNotPersistEntitiesWhenPersistOnGetHasBeenDisabled(): void + { + $entityManager = self::entityManager(); + $faker = self::faker(); + + $fixtureFactory = new FixtureFactory( + $entityManager, + $faker + ); + + $fixtureFactory->define(Fixture\FixtureFactory\Entity\Organization::class, [ + 'name' => FieldDefinition::sequence('name-%d'), + ]); + + $fixtureFactory->persistOnGet(); + $fixtureFactory->persistOnGet(false); + + $value = $faker->numberBetween(1, 5); + + $fixtureFactory->createMany( + Fixture\FixtureFactory\Entity\Organization::class, + Count::exact($value) + ); + + $entityManager->flush(); + $entityManager->clear(); + + $organizations = $entityManager->getRepository(Fixture\FixtureFactory\Entity\Organization::class)->findAll(); + + self::assertEmpty($organizations); + } }