em = self::getContainer()->get(EntityManagerInterface::class); $this->service = new ThirdpartyMergeService($this->em); } public function testMergeUpdatesReferencesAndDeletesThirdparty(): void { // Create ThirdParty entities $toKeep = new ThirdParty(); $this->em->persist($toKeep); $toDelete = new ThirdParty(); $this->em->persist($toDelete); // Create a related entity with TO_ONE relation (thirdparty parent) $relatedToOneEntity = new ThirdParty(); $this->em->persist($relatedToOneEntity); $toDelete->setParent($relatedToOneEntity); $this->em->persist($toDelete); // Create a related entity with TO_MANY relation (thirdparty category) $relatedManyEntity = new ThirdPartyCategory(); $this->em->persist($relatedManyEntity); $toDelete->addCategory($relatedManyEntity); $this->em->persist($toDelete); $this->em->flush(); // Run merge $this->service->merge($toKeep, $toDelete); // Check that references were updated $this->assertEquals($toKeep->getParent()->getId(), $relatedToOneEntity->getId(), 'The parent thirdparty was succesfully merged'); $updatedRelatedManyEntity = $this->em->find(ThirdPartyCategory::class, $relatedManyEntity->getId()); $this->assertContains($updatedRelatedManyEntity, $toKeep->getCategories(), 'The thirdparty category was found in the toKeep entity'); // Check that toDelete was removed $deletedThirdParty = $this->em->find(ThirdParty::class, $toDelete->getId()); $this->assertNull($deletedThirdParty); } }