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

[3.x] fix: clean entity manager before loading fixtures #317

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
3 changes: 3 additions & 0 deletions src/Services/DatabaseTools/AbstractDatabaseTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ public function loadAliceFixture(array $paths = [], bool $append = false): array

if (false === $append) {
$this->cleanDatabase();

// Clear the entity manager to avoid the exception `EntityIdentityCollisionException`
$this->om->clear();
}

$files = $this->locateResources($paths);
Expand Down
3 changes: 3 additions & 0 deletions src/Services/DatabaseTools/MongoDBDatabaseTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public function loadFixtures(array $classNames = [], bool $append = false): Abst
$executor->setReferenceRepository($referenceRepository);
if (false === $append) {
$executor->purge();

// Clear the entity manager to avoid the exception `EntityIdentityCollisionException`
$this->om->clear();
}

$loader = $this->fixturesLoaderFactory->getFixtureLoader($classNames);
Expand Down
3 changes: 3 additions & 0 deletions src/Services/DatabaseTools/ORMDatabaseTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ public function loadFixtures(array $classNames = [], bool $append = false): Abst
$this->disableForeignKeyChecksIfApplicable();
$executor->purge();
$this->enableForeignKeyChecksIfApplicable();

// Clear the entity manager to avoid the exception `EntityIdentityCollisionException`
$this->om->clear();
}

$loader = $this->fixturesLoaderFactory->getFixtureLoader($classNames);
Expand Down
3 changes: 3 additions & 0 deletions src/Services/DatabaseTools/ORMSqliteDatabaseTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ public function loadFixtures(array $classNames = [], bool $append = false): Abst
$executor->setReferenceRepository($referenceRepository);
if (false === $append) {
$executor->purge();

// Clear the entity manager to avoid the exception `EntityIdentityCollisionException`
$this->om->clear();
}

$loader = $this->fixturesLoaderFactory->getFixtureLoader($classNames);
Expand Down
1 change: 0 additions & 1 deletion tests/App/DataFixtures/ORM/LoadUserData.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public function load(ObjectManager $manager): void
$user->setEmail('[email protected]');

$manager->persist($user);
$manager->flush();

$this->addReference('user', $user);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ public function load(ObjectManager $manager): void
$user->setEmail('[email protected]');

$manager->persist($user);
$manager->flush();

$this->addReference('user', $user);

$user = clone $this->getReference('user');
$user2 = new User();
$user2->setName('alice bar');
$user2->setEmail('[email protected]');

$manager->persist($user);
$manager->persist($user2);
$manager->flush();
}
}
24 changes: 20 additions & 4 deletions tests/Test/ConfigMysqlCacheDbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@
#[PreserveGlobalState(false)]
class ConfigMysqlCacheDbTest extends ConfigMysqlTest
{
public function testLoadFixturesAndCheckBackup(): void
protected function setUp(): void
{
parent::setUp();

$this->assertTrue($this->databaseTool->isDatabaseCacheEnabled());
}

public function testLoadFixturesAndCheckBackup(): void
{
$this->databaseTool->loadFixtures([
'Liip\Acme\Tests\App\DataFixtures\ORM\LoadUserData',
]);
Expand Down Expand Up @@ -105,21 +110,32 @@ public function testLoadFixturesAndCheckBackup(): void
);
}

public function testLoadFixturesCheckReferences(): void
public function testLoadFixturesCheckReferencesByClass(): void
{
$this->markTestSkipped('This test is broken right now.');

$referenceRepository = $this->databaseTool->loadFixtures([
'Liip\Acme\Tests\App\DataFixtures\ORM\LoadUserData',
])->getReferenceRepository();

$this->assertCount(1, $referenceRepository->getReferences());
$references = $referenceRepository->getReferencesByClass();

$className = 'Liip\Acme\Tests\App\Entity\User';

$this->assertArrayHasKey($className, $references);

$this->assertCount(1, $references[$className]);

$referenceRepository = $this->databaseTool->loadFixtures([
'Liip\Acme\Tests\App\DataFixtures\ORM\LoadUserData',
'Liip\Acme\Tests\App\DataFixtures\ORM\LoadSecondUserData',
])->getReferenceRepository();

$this->assertCount(2, $referenceRepository->getReferences());
$references = $referenceRepository->getReferencesByClass();

$this->assertArrayHasKey($className, $references);

$this->assertCount(2, $references[$className]);
}

protected static function getKernelClass(): string
Expand Down
2 changes: 0 additions & 2 deletions tests/Test/ConfigMysqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,6 @@ public function testLoadFixturesAndPurge(): void
$users
);

$this->getTestContainer()->get('doctrine')->getManager()->clear();

// Reload fixtures
$this->databaseTool->loadFixtures([
'Liip\Acme\Tests\App\DataFixtures\ORM\LoadUserData',
Expand Down
31 changes: 12 additions & 19 deletions tests/Test/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Liip\Acme\Tests\Test;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ObjectRepository;
use Liip\Acme\Tests\App\Entity\User;
use Liip\Acme\Tests\AppConfig\AppConfigKernel;
Expand Down Expand Up @@ -45,7 +44,6 @@ class ConfigTest extends KernelTestCase
private $userRepository;
/** @var SqliteDatabaseBackup */
private $sqliteDatabaseBackup;
private EntityManagerInterface $entityManager;

protected function setUp(): void
{
Expand All @@ -64,10 +62,6 @@ protected function setUp(): void
$this->sqliteDatabaseBackup = $this->getTestContainer()->get(SqliteDatabaseBackup::class);

$this->assertInstanceOf(SqliteDatabaseBackup::class, $this->sqliteDatabaseBackup);

$this->entityManager = $this->getTestContainer()->get(EntityManagerInterface::class);

$this->assertInstanceOf(EntityManagerInterface::class, $this->entityManager);
}

/**
Expand Down Expand Up @@ -101,8 +95,6 @@ public function testLoadFixturesFilesWithCustomProvider(): void
$user1->getName()
);

$this->getTestContainer()->get('doctrine')->getManager()->clear();

// Load Data Fixtures with custom loader defined in configuration.
$fixtures = $this->databaseTool->loadAliceFixture([
'@AcmeBundle/DataFixtures/ORM/user_with_custom_provider.yml',
Expand Down Expand Up @@ -130,9 +122,12 @@ public function testCacheCanBeDisabled(): void

$this->databaseTool->setDatabaseCacheEnabled(false);

// Cache is not up-to-date.
$this->assertFalse($this->databaseTool->isDatabaseCacheEnabled());

$this->databaseTool->loadFixtures($fixtures);

// Load data from database
// Load data from database.
/** @var User $user1 */
$user1 = $this->userRepository->findOneBy(['id' => 1]);

Expand All @@ -141,19 +136,22 @@ public function testCacheCanBeDisabled(): void

sleep(2);

$this->clearEntityManager();
// Cache is up-to-date, but it won't be used since its usage is disabled.
$this->assertTrue($this->sqliteDatabaseBackup->isBackupActual());

// Reload the fixtures.
$this->databaseTool->loadFixtures($fixtures);

/** @var User $user1 */
$user1 = $this->userRepository->findOneBy(['id' => 1]);

// The salt are not the same because cache were not used
// The salt are not the same because the cache was not used.
$this->assertNotSame($user1Salt, $user1->getSalt());

// Enable the cache again
$this->databaseTool->setDatabaseCacheEnabled(true);

$this->assertTrue($this->databaseTool->isDatabaseCacheEnabled());
}

/**
Expand Down Expand Up @@ -191,7 +189,7 @@ public function testBackupIsRefreshed(): void

sleep(2);

$this->clearEntityManager();
$this->assertTrue($this->sqliteDatabaseBackup->isBackupActual());

// Reload the fixtures.
$this->databaseTool->loadFixtures($fixtures);
Expand All @@ -217,11 +215,11 @@ public function testBackupIsRefreshed(): void

sleep(2);

$this->clearEntityManager();

// Update the filemtime of the fixture file used as a dependency.
touch($dependentFixtureFilePath);

$this->assertFalse($this->sqliteDatabaseBackup->isBackupActual());

$this->databaseTool->loadFixtures($fixtures);

// The mtime of the fixture file has been updated.
Expand Down Expand Up @@ -249,11 +247,6 @@ protected static function getKernelClass(): string
return AppConfigKernel::class;
}

protected function clearEntityManager(): void
{
$this->entityManager->clear();
}

protected function tearDown(): void
{
parent::tearDown();
Expand Down