redo test

This commit is contained in:
Julie Lenaerts 2025-03-03 21:28:45 +01:00
parent 535409e529
commit 6ba5c91ee6

View File

@ -11,12 +11,11 @@ declare(strict_types=1);
namespace Chill\ThirdPartyBundle\Tests\Service; namespace Chill\ThirdPartyBundle\Tests\Service;
use Chill\ActivityBundle\Entity\Activity;
use Chill\PersonBundle\Entity\Person\PersonResource;
use Chill\ThirdPartyBundle\Entity\ThirdParty; use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Chill\ThirdPartyBundle\Entity\ThirdPartyCategory;
use Chill\ThirdPartyBundle\Service\ThirdpartyMergeService; use Chill\ThirdPartyBundle\Service\ThirdpartyMergeService;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use libphonenumber\PhoneNumber; use Doctrine\ORM\Tools\SchemaTool;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/** /**
@ -26,89 +25,54 @@ use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
*/ */
class ThirdpartyMergeServiceTest extends KernelTestCase class ThirdpartyMergeServiceTest extends KernelTestCase
{ {
private $mergeManager; private EntityManagerInterface $em;
private $em; private ThirdpartyMergeService $service;
private $connection;
protected function setUp(): void protected function setUp(): void
{ {
self::bootKernel(); self::bootKernel();
$this->em = self::getContainer()->get(EntityManagerInterface::class);
$this->mergeManager = $this->getContainer()->get(ThirdpartyMergeService::class); $schemaTool = new SchemaTool($this->em);
$this->em = $this->getContainer()->get(EntityManagerInterface::class); $schemaTool->updateSchema($this->em->getMetadataFactory()->getAllMetadata());
$this->connection = $this->em->getConnection();
// Start a transaction before each test $this->service = new ThirdpartyMergeService($this->em);
$this->connection->beginTransaction();
} }
protected function tearDown(): void public function testMergeUpdatesReferencesAndDeletesThirdparty(): void
{ {
try { // Create ThirdParty entities
// Rollback the transaction after each test to ensure no data is persisted
$this->connection->rollBack();
} catch (\Exception) {
$this->connection->close();
}
parent::tearDown();
}
public function testThirdpartyMerge(): void
{
// Arrange: Create Thirdparty entities
$toKeep = new ThirdParty(); $toKeep = new ThirdParty();
$toKeep->setName('Thirdparty ToKeep'); $this->em->persist($toKeep);
$toKeep->setEmail('keep@example.com');
$toDelete = new ThirdParty(); $toDelete = new ThirdParty();
$toDelete->setName('Thirdparty ToDelete'); // This should be ignored
$toDelete->setTelephone(new PhoneNumber('123456789'));
// Related entities
$activity = new Activity();
$activity->addThirdParty($toDelete); // This is a Many-to-Many relation
$personResource = new PersonResource();
$personResource->setThirdParty($toDelete); // This is a Many-to-One relation
$this->em->persist($toKeep);
$this->em->persist($toDelete); $this->em->persist($toDelete);
$this->em->persist($activity);
$this->em->persist($personResource); // 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(); $this->em->flush();
// Merge // Run merge
$this->mergeManager->merge($toKeep, $toDelete); $this->service->merge($toKeep, $toDelete);
$this->em->clear();
// Verify data was merged correctly // Check that references were updated
$mergedThirdparty = $this->em->getRepository(ThirdParty::class)->find($toKeep->getId()); $this->assertEquals($toKeep->getParent()->getId(), $relatedToOneEntity->getId(), 'The parent thirdparty was succesfully merged');
$this->assertNotNull($mergedThirdparty);
$this->assertEquals('Primary Name', $mergedThirdparty->getName(), 'Name should remain unchanged');
$this->assertEquals('keep@example.com', $mergedThirdparty->getEmail(), 'Email should remain unchanged');
$this->assertEquals('123456789', $mergedThirdparty->getPhone(), 'Phone should be transferred from Thirdparty ToDelete');
// Check that relationships are updated $updatedRelatedManyEntity = $this->em->find(ThirdPartyCategory::class, $relatedManyEntity->getId());
$updatedActivity = $this->em->getRepository(Activity::class)->find($activity->getId()); $this->assertContains($updatedRelatedManyEntity, $toKeep->getCategories(), 'The thirdparty category was found in the toKeep entity');
$this->assertTrue(
$updatedActivity->getThirdParties()->contains($mergedThirdparty),
'Activity should be linked to the merged Thirdparty'
);
$this->assertFalse(
$updatedActivity->getThirdParties()->exists(fn ($key, $tp) => $tp->getId() === $toDelete->getId()),
'Activity should no longer reference the deleted Thirdparty'
);
$updatedPersonResource = $this->em->getRepository(PersonResource::class)->find($personResource->getId()); // Check that toDelete was removed
$this->assertEquals( $deletedThirdParty = $this->em->find(ThirdParty::class, $toDelete->getId());
$mergedThirdparty->getId(), $this->assertNull($deletedThirdParty);
$updatedPersonResource->getThirdParty()->getId(),
'PersonResource should reference the merged Thirdparty'
);
// Ensure the 'toDelete' entity is removed
$deletedThirdparty = $this->em->getRepository(ThirdParty::class)->find($toDelete->getId());
$this->assertNull($deletedThirdparty, 'The deleted Thirdparty should no longer exist in the database');
} }
} }