From ab05f5f5678ed23dd38db13b37fa2eb7800c8c71 Mon Sep 17 00:00:00 2001 From: Boris Waaub Date: Mon, 23 Feb 2026 15:07:36 +0100 Subject: [PATCH] Corrige le namespace de PersonUpsertHandler et ajoute des tests pour la gestion de l'upsert des utilisateurs --- .../Upsert/Handler/PersonUpsertHandler.php | 2 +- .../Person/Upsert/PersonUpsertHandlerTest.php | 98 +++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 src/Bundle/ChillPersonBundle/Tests/Entity/Person/Upsert/PersonUpsertHandlerTest.php diff --git a/src/Bundle/ChillPersonBundle/Entity/Person/Upsert/Handler/PersonUpsertHandler.php b/src/Bundle/ChillPersonBundle/Entity/Person/Upsert/Handler/PersonUpsertHandler.php index e5ab106e7..2ad827789 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Person/Upsert/Handler/PersonUpsertHandler.php +++ b/src/Bundle/ChillPersonBundle/Entity/Person/Upsert/Handler/PersonUpsertHandler.php @@ -9,7 +9,7 @@ declare(strict_types=1); * the LICENSE file that was distributed with this source code. */ -namespace Chill\Bundle\ChillPersonBundle\Entity\Person\Upsert\Handler; +namespace Chill\PersonBundle\Entity\Person\Upsert\Handler; use Chill\PersonBundle\Repository\PersonRepository; use Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository; diff --git a/src/Bundle/ChillPersonBundle/Tests/Entity/Person/Upsert/PersonUpsertHandlerTest.php b/src/Bundle/ChillPersonBundle/Tests/Entity/Person/Upsert/PersonUpsertHandlerTest.php new file mode 100644 index 000000000..d0f924a67 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Tests/Entity/Person/Upsert/PersonUpsertHandlerTest.php @@ -0,0 +1,98 @@ +createMock(\Chill\PersonBundle\Repository\PersonRepository::class); + $definitionRepository = $this->createMock(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository::class); + $identifierRepository = $this->createMock(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierRepository::class); + $entityManager = $this->createMock(EntityManagerInterface::class); + + $definition = $this->createMock(\Chill\PersonBundle\Entity\Identifier\PersonIdentifierDefinition::class); + $definitionRepository->method('find')->willReturn($definition); + $identifierRepository->method('findByDefinitionAndCanonical')->willReturn([]); + + $entityManager->expects($this->exactly(2))->method('persist')->with($this->logicalOr( + $this->isInstanceOf(Person::class), + $this->isInstanceOf(PersonIdentifier::class) + )); + $entityManager->expects($this->once())->method('flush'); + + $handler = new PersonUpsertHandler( + $personRepository, + $definitionRepository, + $identifierRepository, + $entityManager + ); + + $message = new UpsertMessage(); + $message->externalId = '123'; + $message->personIdentifierDefinitionId = 1; + $message->firstName = 'John'; + $message->lastName = 'Doe'; + + $handler->__invoke($message); + $this->assertTrue(true); // Pour éviter le test risky + } + + public function testInvokeUpdatesExistingPerson(): void + { + $personRepository = $this->createMock(\Chill\PersonBundle\Repository\PersonRepository::class); + $definitionRepository = $this->createMock(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository::class); + $identifierRepository = $this->createMock(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierRepository::class); + $entityManager = $this->createMock(EntityManagerInterface::class); + + $definition = $this->createMock(\Chill\PersonBundle\Entity\Identifier\PersonIdentifierDefinition::class); + $definitionRepository->method('find')->willReturn($definition); + + $person = $this->createMock(Person::class); + $person->expects($this->once())->method('setFirstName')->with('Jane'); + $person->expects($this->once())->method('setLastName')->with('Smith'); + + $identifier = $this->createMock(PersonIdentifier::class); + $identifier->method('getPerson')->willReturn($person); + + $identifierRepository->method('findByDefinitionAndCanonical')->willReturn([$identifier]); + + $entityManager->expects($this->once())->method('flush'); + + $handler = new PersonUpsertHandler( + $personRepository, + $definitionRepository, + $identifierRepository, + $entityManager + ); + + $message = new UpsertMessage(); + $message->externalId = '456'; + $message->personIdentifierDefinitionId = 2; + $message->firstName = 'Jane'; + $message->lastName = 'Smith'; + + $handler->__invoke($message); + } +}