Skip to content

Commit

Permalink
Merge pull request #374 from ergebnis/feature/persisting
Browse files Browse the repository at this point in the history
Enhancement: Replace persistAfterCreate() and doNotPersistAfterCreate() with persisting()
  • Loading branch information
ergebnis-bot committed Aug 5, 2020
2 parents 3a98c1a + a358f76 commit abb051d
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 105 deletions.
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## Unreleased

For a full diff see [`0.2.1...main`][0.2.1...main].
For a full diff see [`0.3.0...main`][0.3.0...main].

## [`0.3.0`][0.3.0]

For a full diff see [`0.2.1...0.3.0`][0.2.1...0.3.0].

### Added

Expand All @@ -17,6 +21,7 @@ For a full diff see [`0.2.1...main`][0.2.1...main].

* Moved resolution of `Count` to `FixtureFactory` ([#351]), by [@localheinz]
* Extracted `DefaultStrategy` for resolving field values and count ([#353]), by [@localheinz]
* Replaced `FixtureFactory::persistAfterCreate()` and `FixtureFactory::doNotPersistAfterCreate()` with `FixtureFactory::persisting()`, a mutator that returns a persisting `FixtureFactory` ([#374]), by [@localheinz]

## [`0.2.1`][0.2.1]

Expand Down Expand Up @@ -117,11 +122,13 @@ For a full diff see [`fa9c564...0.1.0`][fa9c564...0.1.0].
[0.1.0]: https://github.com/ergebnis/factory-bot/releases/tag/0.1.0
[0.2.0]: https://github.com/ergebnis/factory-bot/releases/tag/0.2.0
[0.2.1]: https://github.com/ergebnis/factory-bot/releases/tag/0.2.1
[0.3.0]: https://github.com/ergebnis/factory-bot/releases/tag/0.3.0

[fa9c564...0.1.0]: https://github.com/ergebnis/factory-bot/compare/fa9c564...0.1.0
[0.1.0...0.2.0]: https://github.com/ergebnis/factory-bot/compare/0.1.0...0.2.0
[0.2.0...0.2.1]: https://github.com/ergebnis/factory-bot/compare/0.2.0...0.2.1
[0.2.1...main]: https://github.com/ergebnis/factory-bot/compare/0.2.1...main
[0.2.1...0.3.0]: https://github.com/ergebnis/factory-bot/compare/0.2.1...0.3.0
[0.3.0...main]: https://github.com/ergebnis/factory-bot/compare/0.3.0...main

[#1]: https://github.com/ergebnis/factory-bot/pull/1
[#3]: https://github.com/ergebnis/factory-bot/pull/3
Expand Down Expand Up @@ -190,5 +197,6 @@ For a full diff see [`fa9c564...0.1.0`][fa9c564...0.1.0].
[#353]: https://github.com/ergebnis/factory-bot/pull/353
[#365]: https://github.com/ergebnis/factory-bot/pull/365
[#369]: https://github.com/ergebnis/factory-bot/pull/369
[#374]: https://github.com/ergebnis/factory-bot/pull/374

[@localheinz]: https://github.com/localheinz
21 changes: 2 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1052,38 +1052,21 @@ Also see [Creating entity definitions](#creating-entity-definitions).

When the fixture factory creates entities, the fixture factory does not persist them by default.

#### Enabling persistence

You can activate the automatic persistence of entities by invoking `FixtureFactory::persistAfterCreate()`.
To create a fixture factory that persists entities out of an available fixture factory, invoke `persisting()`:

```php
<?php

use Ergebnis\FactoryBot;

/** @var FactoryBot\FixtureFactory $fixtureFactory */
$fixtureFactory->persistAfterCreate();
$persistingFixtureFactory = $fixtureFactory->persisting();
```

After this point, the fixture factory will automatically persist every entity it creates.

:exclamation: You need to flush the entity manager yourself.

#### Disabling persistence

If you have previously activated the automatic persistence of entities, you can disable it by invoking `FixtureFactory::doNotPersistAfterCreate()`.

```php
<?php

use Ergebnis\FactoryBot;

/** @var FactoryBot\FixtureFactory $fixtureFactory */
$fixtureFactory->doNotPersistAfterCreate();
```

After this point, the fixture factory will not automatically persist any entity it creates.

### Flushing entities

The fixture factory will not flush the entity manager - you need to flush it yourself.
Expand Down
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,6 @@ parameters:

-
message: "#^Call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertInstanceOf\\(\\) with 'Ergebnis\\\\\\\\FactoryBot\\\\\\\\FixtureFactory' and Ergebnis\\\\FactoryBot\\\\FixtureFactory will always evaluate to true\\.$#"
count: 1
count: 2
path: test/Unit/FixtureFactoryTest.php

3 changes: 2 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,13 @@
<code>$name</code>
<code>$fieldValues['name']</code>
</MixedArgument>
<RedundantCondition occurrences="5">
<RedundantCondition occurrences="6">
<code>assertNotNull</code>
<code>assertNotNull</code>
<code>assertInstanceOf</code>
<code>assertInstanceOf</code>
<code>assertInstanceOf</code>
<code>assertInstanceOf</code>
</RedundantCondition>
<RedundantConditionGivenDocblockType occurrences="5">
<code>assertInstanceOf</code>
Expand Down
18 changes: 8 additions & 10 deletions src/FixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,19 +332,17 @@ public function withOptional(): self
}

/**
* Enables persisting of entities after creation.
* Returns a fixture factory that persists entities after creation.
*
* @return self
*/
public function persistAfterCreate(): void
public function persisting(): self
{
$this->persistAfterCreate = true;
}
$instance = clone $this;

/**
* Disables persisting of entities after creation.
*/
public function doNotPersistAfterCreate(): void
{
$this->persistAfterCreate = false;
$instance->persistAfterCreate = true;

return $instance;
}

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

public function testCreateOnePersistsEntityWhenPersistAfterCreateHasBeenEnabled(): void
public function testCreateOnePersistsEntityWhenFixtureFactoryIsPersisting(): void
{
$entityManager = self::entityManager();
$faker = self::faker();
Expand All @@ -82,9 +82,9 @@ public function testCreateOnePersistsEntityWhenPersistAfterCreateHasBeenEnabled(
'name' => $name,
]);

$fixtureFactory->persistAfterCreate();
$persistingFixtureFactory = $fixtureFactory->persisting();

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

$entityManager->flush();
$entityManager->clear();
Expand All @@ -96,38 +96,7 @@ public function testCreateOnePersistsEntityWhenPersistAfterCreateHasBeenEnabled(
self::assertInstanceOf(Entity\Organization::class, $organization);
}

public function testCreateOneDoesNotPersistEntityWhenPersistAfterCreateHasBeenDisabled(): void
{
$entityManager = self::entityManager();
$faker = self::faker();

$fixtureFactory = new FixtureFactory(
$entityManager,
$faker
);

$name = $faker->word;

$fixtureFactory->define(Entity\Organization::class, [
'name' => $name,
]);

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

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

$entityManager->flush();
$entityManager->clear();

$organization = $entityManager->getRepository(Entity\Organization::class)->findOneBy([
'name' => $name,
]);

self::assertNull($organization);
}

public function testCreateOneDoesNotPersistEmbeddablesWhenPersistAfterCreateHasBeenEnabled(): void
public function testCreateOneDoesNotPersistEmbeddablesWhenFixtureFactoryIsPersisting(): void
{
$entityManager = self::entityManager();
$faker = self::faker();
Expand All @@ -151,9 +120,9 @@ public function testCreateOneDoesNotPersistEmbeddablesWhenPersistAfterCreateHasB
'login' => $faker->userName,
]);

$fixtureFactory->persistAfterCreate();
$persistingFixtureFactory = $fixtureFactory->persisting();

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

$entityManager->flush();

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

public function testCreateManyPersistsEntitiesWhenPersistAfterCreateHasBeenEnabled(): void
public function testCreateManyPersistsEntitiesWhenFixtureFactoryIsPersisting(): void
{
$entityManager = self::entityManager();
$faker = self::faker();
Expand All @@ -203,11 +172,11 @@ public function testCreateManyPersistsEntitiesWhenPersistAfterCreateHasBeenEnabl
'name' => FieldDefinition::sequence('name-%d'),
]);

$fixtureFactory->persistAfterCreate();
$persistingFixtureFactory = $fixtureFactory->persisting();

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

$fixtureFactory->createMany(
$persistingFixtureFactory->createMany(
Entity\Organization::class,
Count::exact($value)
);
Expand All @@ -219,36 +188,4 @@ public function testCreateManyPersistsEntitiesWhenPersistAfterCreateHasBeenEnabl

self::assertCount($value, $organizations);
}

public function testCreateManyDoesNotPersistEntitiesWhenPersistAfterCreateHasBeenDisabled(): void
{
$entityManager = self::entityManager();
$faker = self::faker();

$fixtureFactory = new FixtureFactory(
$entityManager,
$faker
);

$fixtureFactory->define(Entity\Organization::class, [
'name' => FieldDefinition::sequence('name-%d'),
]);

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

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

$fixtureFactory->createMany(
Entity\Organization::class,
Count::exact($value)
);

$entityManager->flush();
$entityManager->clear();

$organizations = $entityManager->getRepository(Entity\Organization::class)->findAll();

self::assertEmpty($organizations);
}
}
13 changes: 13 additions & 0 deletions test/Unit/FixtureFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -881,4 +881,17 @@ public function testWithOptionalReturnsMutatedFixtureFactory(): void
self::assertInstanceOf(FixtureFactory::class, $withOptionalFixtureFactory);
self::assertNotSame($fixtureFactory, $withOptionalFixtureFactory);
}

public function testPersistingReturnsMutatedFixtureFactory(): void
{
$fixtureFactory = new FixtureFactory(
self::entityManager(),
self::faker()
);

$oersistingFixtureFactory = $fixtureFactory->persisting();

self::assertInstanceOf(FixtureFactory::class, $oersistingFixtureFactory);
self::assertNotSame($fixtureFactory, $oersistingFixtureFactory);
}
}

0 comments on commit abb051d

Please sign in to comment.