Skip to content

Commit

Permalink
fix: clean entity manager before loading fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
alexislefebvre committed Jul 3, 2024
1 parent d8b429d commit 437e674
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 22 deletions.
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
8 changes: 7 additions & 1 deletion tests/Test/ConfigMysqlCacheDbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
#[PreserveGlobalState(false)]
class ConfigMysqlCacheDbTest extends ConfigMysqlTest
{
public function setUp(): void
{
parent::setUp();

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

public function testLoadFixturesAndCheckBackup(): void
{
$this->assertTrue($this->databaseTool->isDatabaseCacheEnabled());
Expand Down Expand Up @@ -107,7 +114,6 @@ public function testLoadFixturesAndCheckBackup(): void

public function testLoadFixturesCheckReferences(): void
{
$this->markTestSkipped('This test is broken right now.');
$referenceRepository = $this->databaseTool->loadFixtures([
'Liip\Acme\Tests\App\DataFixtures\ORM\LoadUserData',
])->getReferenceRepository();
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

0 comments on commit 437e674

Please sign in to comment.