chill-bundles/src/Bundle/ChillThirdPartyBundle/Tests/Service/ThirdpartyMergeServiceTest.php

75 lines
2.4 KiB
PHP

<?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\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();
$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);
}
}