From 3474977b9e6bf1b024175656a0590ac2fe8ebb8d Mon Sep 17 00:00:00 2001 From: Alexis Lefebvre Date: Thu, 30 May 2019 21:28:34 +0200 Subject: [PATCH 1/5] PHPUnit: set executionOrder as random and enable resolveDependencies --- phpunit.xml.dist | 2 ++ 1 file changed, 2 insertions(+) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index ea5e9d8c..7d9fc2f2 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -6,6 +6,8 @@ xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" colors="true" beStrictAboutOutputDuringTests="true" + executionOrder="random" + resolveDependencies="true" > From cdee30c9f2ef18082b7b7a504eb2233a1e5e7799 Mon Sep 17 00:00:00 2001 From: Alexis Lefebvre Date: Thu, 30 May 2019 22:50:07 +0200 Subject: [PATCH 2/5] Fix test testLoadFixturesFilesWithPurgeModeTruncate --- tests/Test/ConfigSqliteTest.php | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tests/Test/ConfigSqliteTest.php b/tests/Test/ConfigSqliteTest.php index 8318a2cb..0fe01dc9 100644 --- a/tests/Test/ConfigSqliteTest.php +++ b/tests/Test/ConfigSqliteTest.php @@ -362,8 +362,20 @@ public function testLoadNonexistentFixturesFiles(): void */ public function testLoadFixturesFilesWithPurgeModeTruncate(): void { + // Load initial fixtures + $this->testLoadFixturesFiles(); + + $users = $this->userRepository->findAll(); + + // There are 10 users in the database + $this->assertSame( + 10, + \count($users) + ); + $this->databaseTool->setPurgeMode(ORMPurger::PURGE_MODE_TRUNCATE); + // Load fixtures with append = true $fixtures = $this->databaseTool->loadAliceFixture([ '@AcmeBundle/DataFixtures/ORM/user.yml', ], true); @@ -376,7 +388,16 @@ public function testLoadFixturesFilesWithPurgeModeTruncate(): void $fixtures ); - $id = 1; + $users = $this->userRepository->findAll(); + + // There are only 10 users in the database + $this->assertSame( + 10, + \count($users) + ); + + // Auto-increment hasn't been altered, so ids start from 11 + $id = 11; /** @var User $user */ foreach ($fixtures as $user) { $this->assertSame($id++, $user->getId()); From 843360b04f41ce2378e38d8c30ec8b003bc81a9a Mon Sep 17 00:00:00 2001 From: Alexis Lefebvre Date: Thu, 7 Jul 2022 22:18:46 +0200 Subject: [PATCH 3/5] Rely on ORMExecutor to purge the database --- src/Services/DatabaseTools/MongoDBDatabaseTool.php | 5 +---- src/Services/DatabaseTools/ORMDatabaseTool.php | 13 +++++-------- .../DatabaseTools/ORMSqliteDatabaseTool.php | 5 +---- src/Services/DatabaseTools/PHPCRDatabaseTool.php | 5 +---- 4 files changed, 8 insertions(+), 20 deletions(-) diff --git a/src/Services/DatabaseTools/MongoDBDatabaseTool.php b/src/Services/DatabaseTools/MongoDBDatabaseTool.php index 259b510f..bc7db29b 100644 --- a/src/Services/DatabaseTools/MongoDBDatabaseTool.php +++ b/src/Services/DatabaseTools/MongoDBDatabaseTool.php @@ -82,12 +82,9 @@ public function loadFixtures(array $classNames = [], bool $append = false): Abst $executor = $this->getExecutor($this->getPurger()); $executor->setReferenceRepository($referenceRepository); - if (false === $append) { - $executor->purge(); - } $loader = $this->fixturesLoaderFactory->getFixtureLoader($classNames); - $executor->execute($loader->getFixtures(), true); + $executor->execute($loader->getFixtures(), $append); if ($backupService) { $event = new ReferenceSaveEvent($this->om, $executor, $backupService->getBackupFilePath()); diff --git a/src/Services/DatabaseTools/ORMDatabaseTool.php b/src/Services/DatabaseTools/ORMDatabaseTool.php index e30acd4e..06ff5634 100644 --- a/src/Services/DatabaseTools/ORMDatabaseTool.php +++ b/src/Services/DatabaseTools/ORMDatabaseTool.php @@ -27,6 +27,8 @@ use Liip\TestFixturesBundle\Event\PreFixtureBackupRestoreEvent; use Liip\TestFixturesBundle\Event\ReferenceSaveEvent; use Liip\TestFixturesBundle\LiipTestFixturesEvents; +use function count; +use function in_array; /** * @author Aleksey Tupichenkov @@ -103,7 +105,7 @@ public function loadFixtures(array $classNames = [], bool $append = false): Abst // TODO: handle case when using persistent connections. Fail loudly? if (false === $this->getKeepDatabaseAndSchemaParameter()) { $schemaTool = new SchemaTool($this->om); - if (\count($this->excludedDoctrineTables) > 0 || true === $append) { + if (count($this->excludedDoctrineTables) > 0 || true === $append) { if (!empty($this->getMetadatas())) { $schemaTool->updateSchema($this->getMetadatas()); } @@ -120,14 +122,9 @@ public function loadFixtures(array $classNames = [], bool $append = false): Abst $executor = $this->getExecutor($this->getPurger()); $executor->setReferenceRepository($referenceRepository); - if (false === $append) { - $this->disableForeignKeyChecksIfApplicable(); - $executor->purge(); - $this->enableForeignKeyChecksIfApplicable(); - } $loader = $this->fixturesLoaderFactory->getFixtureLoader($classNames); - $executor->execute($loader->getFixtures(), true); + $executor->execute($loader->getFixtures(), $append); if ($backupService) { $event = new ReferenceSaveEvent($this->om, $executor, $backupService->getBackupFilePath()); @@ -179,7 +176,7 @@ protected function createDatabaseIfNotExists(): void $tmpConnection = DriverManager::getConnection($params); $tmpConnection->connect(); - if (!\in_array($dbName, $tmpConnection->getSchemaManager()->listDatabases(), true)) { + if (!in_array($dbName, $tmpConnection->getSchemaManager()->listDatabases(), true)) { $tmpConnection->getSchemaManager()->createDatabase($dbName); } diff --git a/src/Services/DatabaseTools/ORMSqliteDatabaseTool.php b/src/Services/DatabaseTools/ORMSqliteDatabaseTool.php index 9b3f4f54..5643ddff 100644 --- a/src/Services/DatabaseTools/ORMSqliteDatabaseTool.php +++ b/src/Services/DatabaseTools/ORMSqliteDatabaseTool.php @@ -100,12 +100,9 @@ public function loadFixtures(array $classNames = [], bool $append = false): Abst $executor = $this->getExecutor($this->getPurger()); $executor->setReferenceRepository($referenceRepository); - if (false === $append) { - $executor->purge(); - } $loader = $this->fixturesLoaderFactory->getFixtureLoader($classNames); - $executor->execute($loader->getFixtures(), true); + $executor->execute($loader->getFixtures(), $append); if ($backupService) { $event = new ReferenceSaveEvent($this->om, $executor, $backupService->getBackupFilePath()); diff --git a/src/Services/DatabaseTools/PHPCRDatabaseTool.php b/src/Services/DatabaseTools/PHPCRDatabaseTool.php index a43d3386..a1d3cded 100644 --- a/src/Services/DatabaseTools/PHPCRDatabaseTool.php +++ b/src/Services/DatabaseTools/PHPCRDatabaseTool.php @@ -80,12 +80,9 @@ public function loadFixtures(array $classNames = [], bool $append = false): Abst $executor = $this->getExecutor($this->getPurger(), $this->getInitializerManager()); $executor->setReferenceRepository($referenceRepository); - if (false === $append) { - $executor->purge(); - } $loader = $this->fixturesLoaderFactory->getFixtureLoader($classNames); - $executor->execute($loader->getFixtures(), true); + $executor->execute($loader->getFixtures(), $append); if ($backupService) { $event = new ReferenceSaveEvent($this->om, $executor, $backupService->getBackupFilePath()); From ba46c6fa0831a007fe50ad738f673841f17c6c6a Mon Sep 17 00:00:00 2001 From: Alexis Lefebvre Date: Thu, 7 Jul 2022 22:28:56 +0200 Subject: [PATCH 4/5] user.yml: set id in fixtures --- tests/App/DataFixtures/ORM/user.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/App/DataFixtures/ORM/user.yml b/tests/App/DataFixtures/ORM/user.yml index 83a84573..051dc539 100644 --- a/tests/App/DataFixtures/ORM/user.yml +++ b/tests/App/DataFixtures/ORM/user.yml @@ -1,4 +1,5 @@ Liip\Acme\Tests\App\Entity\User: id{1..10}: + id: '' name: email: From 402ed4294245c5208c2630beb15a8e2a58b40a81 Mon Sep 17 00:00:00 2001 From: Alexis Lefebvre Date: Fri, 8 Jul 2022 15:03:12 +0200 Subject: [PATCH 5/5] =?UTF-8?q?PHPUnit:=20set=20=E2=80=9CprocessIsolation?= =?UTF-8?q?=3D"true"=E2=80=9D=20in=20conf=20instead=20of=20each=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpunit.xml.dist | 1 + tests/Test/ConfigSqliteTest.php | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 7d9fc2f2..53ef0563 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -8,6 +8,7 @@ beStrictAboutOutputDuringTests="true" executionOrder="random" resolveDependencies="true" + processIsolation="true" > diff --git a/tests/Test/ConfigSqliteTest.php b/tests/Test/ConfigSqliteTest.php index 0fe01dc9..5f3ed79f 100644 --- a/tests/Test/ConfigSqliteTest.php +++ b/tests/Test/ConfigSqliteTest.php @@ -29,6 +29,7 @@ class_alias('\Doctrine\Persistence\ObjectManager', '\Doctrine\Common\Persistence use Liip\TestFixturesBundle\Services\DatabaseTools\AbstractDatabaseTool; use Liip\TestFixturesBundle\Services\DatabaseTools\ORMSqliteDatabaseTool; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use function count; /** * @runTestsInSeparateProcesses @@ -370,7 +371,7 @@ public function testLoadFixturesFilesWithPurgeModeTruncate(): void // There are 10 users in the database $this->assertSame( 10, - \count($users) + count($users) ); $this->databaseTool->setPurgeMode(ORMPurger::PURGE_MODE_TRUNCATE); @@ -393,7 +394,7 @@ public function testLoadFixturesFilesWithPurgeModeTruncate(): void // There are only 10 users in the database $this->assertSame( 10, - \count($users) + count($users) ); // Auto-increment hasn't been altered, so ids start from 11