mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Fix merge service and test
This commit is contained in:
parent
95972399a1
commit
b3bf405c5b
@ -60,18 +60,19 @@ class ThirdpartyMergeService
|
||||
if (($assoc['type'] & ClassMetadata::TO_ONE) !== 0) {
|
||||
$joinColumn = $meta->getSingleAssociationJoinColumnName($assoc['fieldName']);
|
||||
|
||||
if (ThirdParty::class === $assoc['sourceEntity'] && 'parent_id' !== $joinColumn) {
|
||||
continue;
|
||||
$suffix = (ThirdParty::class === $assoc['sourceEntity']) ? 'chill_3party.' : '';
|
||||
|
||||
if (ThirdParty::class === $assoc['sourceEntity'] && 'parent_id' === $joinColumn) {
|
||||
$queries[] = [
|
||||
'sql' => "UPDATE {$suffix}{$tableName} SET parent_id = (SELECT parent_id FROM chill_3party.third_party WHERE id = :toDelete) WHERE id = :toKeep",
|
||||
'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()],
|
||||
];
|
||||
} else {
|
||||
$queries[] = [
|
||||
'sql' => "UPDATE {$suffix}{$tableName} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete",
|
||||
'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()],
|
||||
];
|
||||
}
|
||||
|
||||
$schemaPrefix = (ThirdParty::class === $assoc['sourceEntity'] && 'parent_id' === $joinColumn)
|
||||
? 'chill_3party.'
|
||||
: '';
|
||||
|
||||
$queries[] = [
|
||||
'sql' => "UPDATE {$schemaPrefix}{$tableName} SET {$joinColumn} = :toKeep WHERE {$joinColumn} = :toDelete",
|
||||
'params' => ['toKeep' => $toKeep->getId(), 'toDelete' => $toDelete->getId()],
|
||||
];
|
||||
} elseif (8 === $assoc['type'] && isset($assoc['joinTable'])) {
|
||||
$joinTable = $assoc['joinTable']['name'];
|
||||
$joinColumn = $assoc['joinTable']['joinColumns'][0]['name'];
|
||||
|
@ -39,35 +39,41 @@ class ThirdpartyMergeServiceTest extends KernelTestCase
|
||||
{
|
||||
// Create ThirdParty entities
|
||||
$toKeep = new ThirdParty();
|
||||
$toKeep->setName('Thirdparty to keep');
|
||||
$this->em->persist($toKeep);
|
||||
|
||||
$toDelete = new ThirdParty();
|
||||
$toDelete->setName('Thirdparty to delete');
|
||||
$this->em->persist($toDelete);
|
||||
|
||||
// Create a related entity with TO_ONE relation (thirdparty parent)
|
||||
$relatedToOneEntity = new ThirdParty();
|
||||
$relatedToOneEntity->setName('RelatedToOne 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);
|
||||
$thirdpartyCategory = new ThirdPartyCategory();
|
||||
$thirdpartyCategory->setName(['fr' => 'Thirdparty category']);
|
||||
$this->em->persist($thirdpartyCategory);
|
||||
$toDelete->addCategory($thirdpartyCategory);
|
||||
$this->em->persist($toDelete);
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
// Run merge
|
||||
$this->service->merge($toKeep, $toDelete);
|
||||
$this->em->refresh($toKeep);
|
||||
|
||||
// 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());
|
||||
$updatedRelatedManyEntity = $this->em->find(ThirdPartyCategory::class, $thirdpartyCategory->getId());
|
||||
$this->assertContains($updatedRelatedManyEntity, $toKeep->getCategories(), 'The thirdparty category was found in the toKeep entity');
|
||||
|
||||
// Check that toDelete was removed
|
||||
$this->em->clear();
|
||||
$deletedThirdParty = $this->em->find(ThirdParty::class, $toDelete->getId());
|
||||
$this->assertNull($deletedThirdParty);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user