Corrige le namespace de PersonUpsertHandler et ajoute des tests pour la gestion de l'upsert des utilisateurs

This commit is contained in:
Boris Waaub
2026-02-23 15:07:36 +01:00
parent 312dbf6cda
commit ab05f5f567
2 changed files with 99 additions and 1 deletions

View File

@@ -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;

View File

@@ -0,0 +1,98 @@
<?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\PersonBundle\Tests\Entity\Person\Upsert;
use Chill\PersonBundle\Entity\Person\Upsert\Handler\PersonUpsertHandler;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\Identifier\PersonIdentifier;
use Chill\PersonBundle\Entity\Person\Upsert\UpsertMessage;
use PHPUnit\Framework\TestCase;
use Doctrine\ORM\EntityManagerInterface;
/**
* @internal
*
* @coversNothing
*/
class PersonUpsertHandlerTest extends TestCase
{
public function testInvokeCreatesNewPersonAndIdentifier(): 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);
$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);
}
}