Resolve "Fusion des tiers"

This commit is contained in:
2025-08-18 15:34:48 +00:00
committed by Julien Fastré
parent aa085a1562
commit f5668592ca
14 changed files with 580 additions and 10 deletions

View File

@@ -0,0 +1,86 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\ThirdPartyBundle\Tests\Service;
use Chill\ActivityBundle\Entity\Activity;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Chill\ThirdPartyBundle\Entity\ThirdPartyCategory;
use Chill\ThirdPartyBundle\Service\ThirdpartyMergeService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* @internal
*
* @coversNothing
*/
class ThirdpartyMergeServiceTest extends KernelTestCase
{
private EntityManagerInterface $em;
private ThirdpartyMergeService $service;
protected function setUp(): void
{
self::bootKernel();
$this->em = self::getContainer()->get(EntityManagerInterface::class);
$this->service = new ThirdpartyMergeService($this->em);
}
public function testMergeUpdatesReferencesAndDeletesThirdparty(): void
{
// 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');
$relatedToOneEntity->setParent($toDelete);
$this->em->persist($relatedToOneEntity);
// Create a related entity with TO_MANY relation (thirdparty category)
$thirdpartyCategory = new ThirdPartyCategory();
$thirdpartyCategory->setName(['fr' => 'Thirdparty category']);
$this->em->persist($thirdpartyCategory);
$toDelete->addCategory($thirdpartyCategory);
$this->em->persist($toDelete);
$activity = new Activity();
$activity->setDate(new \DateTime());
$activity->addThirdParty($toDelete);
$this->em->persist($activity);
$this->em->flush();
// Run merge
$this->service->merge($toKeep, $toDelete);
$this->em->refresh($toKeep);
$this->em->refresh($relatedToOneEntity);
// Check that references were updated
$this->assertEquals($toKeep->getId(), $relatedToOneEntity->getParent()->getId(), 'The parent thirdparty was succesfully merged');
$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);
}
}