From 6ba5c91ee685ca7def0e375c48ad8d90be359117 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Mon, 3 Mar 2025 21:28:45 +0100 Subject: [PATCH] redo test --- .../Service/ThirdpartyMergeServiceTest.php | 102 ++++++------------ 1 file changed, 33 insertions(+), 69 deletions(-) diff --git a/src/Bundle/ChillThirdPartyBundle/Tests/Service/ThirdpartyMergeServiceTest.php b/src/Bundle/ChillThirdPartyBundle/Tests/Service/ThirdpartyMergeServiceTest.php index 42e7ec0d1..3e95870a6 100644 --- a/src/Bundle/ChillThirdPartyBundle/Tests/Service/ThirdpartyMergeServiceTest.php +++ b/src/Bundle/ChillThirdPartyBundle/Tests/Service/ThirdpartyMergeServiceTest.php @@ -11,12 +11,11 @@ declare(strict_types=1); 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\ThirdPartyCategory; use Chill\ThirdPartyBundle\Service\ThirdpartyMergeService; use Doctrine\ORM\EntityManagerInterface; -use libphonenumber\PhoneNumber; +use Doctrine\ORM\Tools\SchemaTool; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; /** @@ -26,89 +25,54 @@ use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; */ class ThirdpartyMergeServiceTest extends KernelTestCase { - private $mergeManager; - private $em; - private $connection; + private EntityManagerInterface $em; + private ThirdpartyMergeService $service; protected function setUp(): void { self::bootKernel(); + $this->em = self::getContainer()->get(EntityManagerInterface::class); - $this->mergeManager = $this->getContainer()->get(ThirdpartyMergeService::class); - $this->em = $this->getContainer()->get(EntityManagerInterface::class); - $this->connection = $this->em->getConnection(); + $schemaTool = new SchemaTool($this->em); + $schemaTool->updateSchema($this->em->getMetadataFactory()->getAllMetadata()); - // Start a transaction before each test - $this->connection->beginTransaction(); + $this->service = new ThirdpartyMergeService($this->em); } - protected function tearDown(): void + public function testMergeUpdatesReferencesAndDeletesThirdparty(): void { - try { - // 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 + // Create ThirdParty entities $toKeep = new ThirdParty(); - $toKeep->setName('Thirdparty ToKeep'); - $toKeep->setEmail('keep@example.com'); + $this->em->persist($toKeep); $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($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(); - // Merge - $this->mergeManager->merge($toKeep, $toDelete); - $this->em->clear(); + // Run merge + $this->service->merge($toKeep, $toDelete); - // Verify data was merged correctly - $mergedThirdparty = $this->em->getRepository(ThirdParty::class)->find($toKeep->getId()); - $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 references were updated + $this->assertEquals($toKeep->getParent()->getId(), $relatedToOneEntity->getId(), 'The parent thirdparty was succesfully merged'); - // Check that relationships are updated - $updatedActivity = $this->em->getRepository(Activity::class)->find($activity->getId()); - $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' - ); + $updatedRelatedManyEntity = $this->em->find(ThirdPartyCategory::class, $relatedManyEntity->getId()); + $this->assertContains($updatedRelatedManyEntity, $toKeep->getCategories(), 'The thirdparty category was found in the toKeep entity'); - $updatedPersonResource = $this->em->getRepository(PersonResource::class)->find($personResource->getId()); - $this->assertEquals( - $mergedThirdparty->getId(), - $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'); + // Check that toDelete was removed + $deletedThirdParty = $this->em->find(ThirdParty::class, $toDelete->getId()); + $this->assertNull($deletedThirdParty); } }