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: Add tests for persistOnGet() #310

Merged
merged 1 commit into from
Jul 13, 2020
Merged
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
161 changes: 143 additions & 18 deletions test/Integration/FixtureFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,7 +31,7 @@
*/
final class FixtureFactoryTest extends AbstractTestCase
{
public function testCreateOnePersistsEntityWhenPersistOnGetHasBeenTurnedOn(): void
public function testCreateOneDoesNotPersistEntityByDefault(): void
{
$entityManager = self::entityManager();
$faker = self::faker();
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
}