Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Split persistOnGet() #311

Merged
merged 1 commit into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ For a full diff see [`0.1.0...main`][0.1.0...main].

* Renamed `InvalidDefinition::fromClassNameAndException()` to `InvalidDefinition::throwsExceptionDuringInstantiation()` ([#300]), by [@localheinz]
* Renamed `Number` to `Count` ([#309]), by [@localheinz]
* Split `FixtureFactory::persistOnGet()` into `FixtureFactory::persistAfterCreate()` and `FixtureFactory::doNotPersistAfterCreate()` ([#311]), by [@localheinz]

### Fixed

Expand Down Expand Up @@ -148,5 +149,6 @@ For a full diff see [`fa9c564...0.1.0`][fa9c564...0.1.0].
[#301]: https://github.com/ergebnis/factory-bot/pull/301
[#302]: https://github.com/ergebnis/factory-bot/pull/302
[#309]: https://github.com/ergebnis/factory-bot/pull/309
[#311]: https://github.com/ergebnis/factory-bot/pull/311

[@localheinz]: https://github.com/localheinz
22 changes: 13 additions & 9 deletions src/FixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ final class FixtureFactory
/**
* @var bool
*/
private $persist = false;
private $persistAfterCreate = false;

public function __construct(ORM\EntityManagerInterface $entityManager, Generator $faker)
{
Expand Down Expand Up @@ -218,7 +218,7 @@ public function createOne(string $className, array $fieldDefinitionOverrides = [
$this->faker
);

if ($this->persist && false === $classMetadata->isEmbeddedClass) {
if ($this->persistAfterCreate && false === $classMetadata->isEmbeddedClass) {
$this->entityManager->persist($entity);
}

Expand Down Expand Up @@ -261,15 +261,19 @@ public function createMany(string $className, Count $count, array $fieldDefiniti
}

/**
* Sets whether `get()` should automatically persist the entity it creates.
* By default it does not. In any case, you still need to call
* flush() yourself.
*
* @param bool $enabled
* Enable persisting of entities after creation.
*/
public function persistAfterCreate(): void
{
$this->persistAfterCreate = true;
}

/**
* Disable persisting of entities after creation.
*/
public function persistOnGet(bool $enabled = true): void
public function doNotPersistAfterCreate(): void
{
$this->persist = $enabled;
$this->persistAfterCreate = false;
}

/**
Expand Down
24 changes: 12 additions & 12 deletions test/Integration/FixtureFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function testCreateOneDoesNotPersistEntityByDefault(): void
self::assertNull($organization);
}

public function testCreateOnePersistsEntityWhenPersistOnGetHasBeenEnabled(): void
public function testCreateOnePersistsEntityWhenPersistAfterCreateHasBeenEnabled(): void
{
$entityManager = self::entityManager();
$faker = self::faker();
Expand All @@ -75,7 +75,7 @@ public function testCreateOnePersistsEntityWhenPersistOnGetHasBeenEnabled(): voi
'name' => $name,
]);

$fixtureFactory->persistOnGet();
$fixtureFactory->persistAfterCreate();

$fixtureFactory->createOne(Fixture\FixtureFactory\Entity\Organization::class);

Expand All @@ -89,7 +89,7 @@ public function testCreateOnePersistsEntityWhenPersistOnGetHasBeenEnabled(): voi
self::assertInstanceOf(Fixture\FixtureFactory\Entity\Organization::class, $organization);
}

public function testCreateOneDoesNotPersistEntityWhenPersistOnGetHasBeenDisabled(): void
public function testCreateOneDoesNotPersistEntityWhenPersistAfterCreateHasBeenDisabled(): void
{
$entityManager = self::entityManager();
$faker = self::faker();
Expand All @@ -105,8 +105,8 @@ public function testCreateOneDoesNotPersistEntityWhenPersistOnGetHasBeenDisabled
'name' => $name,
]);

$fixtureFactory->persistOnGet();
$fixtureFactory->persistOnGet(false);
$fixtureFactory->persistAfterCreate();
$fixtureFactory->doNotPersistAfterCreate();

$fixtureFactory->createOne(Fixture\FixtureFactory\Entity\Organization::class);

Expand All @@ -120,7 +120,7 @@ public function testCreateOneDoesNotPersistEntityWhenPersistOnGetHasBeenDisabled
self::assertNull($organization);
}

public function testCreateOneDoesNotPersistEmbeddablesWhenPersistOnGetHasBeenEnabled(): void
public function testCreateOneDoesNotPersistEmbeddablesWhenPersistAfterCreateHasBeenEnabled(): void
{
$entityManager = self::entityManager();
$faker = self::faker();
Expand All @@ -141,7 +141,7 @@ public function testCreateOneDoesNotPersistEmbeddablesWhenPersistOnGetHasBeenEna
'login' => $faker->userName,
]);

$fixtureFactory->persistOnGet();
$fixtureFactory->persistAfterCreate();

$fixtureFactory->createOne(Fixture\FixtureFactory\Entity\User::class);

Expand Down Expand Up @@ -176,7 +176,7 @@ public function testCreateManyDoesNotPersistEntitiesByDefault(): void
self::assertEmpty($organizations);
}

public function testCreateManyPersistsEntitiesWhenPersistOnGetHasBeenEnabled(): void
public function testCreateManyPersistsEntitiesWhenPersistAfterCreateHasBeenEnabled(): void
{
$entityManager = self::entityManager();
$faker = self::faker();
Expand All @@ -190,7 +190,7 @@ public function testCreateManyPersistsEntitiesWhenPersistOnGetHasBeenEnabled():
'name' => FieldDefinition::sequence('name-%d'),
]);

$fixtureFactory->persistOnGet();
$fixtureFactory->persistAfterCreate();

$value = $faker->numberBetween(1, 5);

Expand All @@ -207,7 +207,7 @@ public function testCreateManyPersistsEntitiesWhenPersistOnGetHasBeenEnabled():
self::assertCount($value, $organizations);
}

public function testCreateManyDoesNotPersistEntitiesWhenPersistOnGetHasBeenDisabled(): void
public function testCreateManyDoesNotPersistEntitiesWhenPersistAfterCreateHasBeenDisabled(): void
{
$entityManager = self::entityManager();
$faker = self::faker();
Expand All @@ -221,8 +221,8 @@ public function testCreateManyDoesNotPersistEntitiesWhenPersistOnGetHasBeenDisab
'name' => FieldDefinition::sequence('name-%d'),
]);

$fixtureFactory->persistOnGet();
$fixtureFactory->persistOnGet(false);
$fixtureFactory->persistAfterCreate();
$fixtureFactory->doNotPersistAfterCreate();

$value = $faker->numberBetween(1, 5);

Expand Down