mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-02-27 02:30:02 +00:00
Corrige le namespace de PersonUpsertHandler et ajoute des tests pour la gestion de l'upsert des utilisateurs
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user