|
|
|
|
@@ -0,0 +1,949 @@
|
|
|
|
|
<?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\Action\Upsert\Handler;
|
|
|
|
|
|
|
|
|
|
use Chill\MainBundle\Entity\Gender;
|
|
|
|
|
use Chill\MainBundle\Entity\GenderEnum;
|
|
|
|
|
use Chill\MainBundle\Repository\CenterRepositoryInterface;
|
|
|
|
|
use Chill\MainBundle\Repository\GenderRepository;
|
|
|
|
|
use Chill\MainBundle\Repository\PostalCodeRepositoryInterface;
|
|
|
|
|
use Chill\PersonBundle\Actions\Upsert\Handler\PersonUpsertHandler;
|
|
|
|
|
use Chill\PersonBundle\Entity\Identifier\PersonIdentifierDefinition;
|
|
|
|
|
use Chill\PersonBundle\Entity\Household\Household;
|
|
|
|
|
use Chill\PersonBundle\Entity\Household\HouseholdMember;
|
|
|
|
|
use Chill\PersonBundle\Entity\Person;
|
|
|
|
|
use Chill\PersonBundle\Entity\Identifier\PersonIdentifier;
|
|
|
|
|
use Chill\PersonBundle\Actions\Upsert\UpsertMessage;
|
|
|
|
|
use Chill\PersonBundle\Household\MembersEditor;
|
|
|
|
|
use Chill\PersonBundle\Household\MembersEditorFactory;
|
|
|
|
|
use libphonenumber\PhoneNumberUtil;
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
|
use Prophecy\Argument;
|
|
|
|
|
use Prophecy\PhpUnit\ProphecyTrait;
|
|
|
|
|
use Psr\Log\NullLogger;
|
|
|
|
|
use Chill\PersonBundle\PersonIdentifier\PersonIdentifierManagerInterface;
|
|
|
|
|
use Symfony\Component\Clock\MockClock;
|
|
|
|
|
use Symfony\Component\Validator\ConstraintViolationList;
|
|
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
|
|
|
|
use Symfony\Component\Validator\ConstraintViolationListInterface;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @internal
|
|
|
|
|
*
|
|
|
|
|
* @coversNothing
|
|
|
|
|
*/
|
|
|
|
|
class PersonUpsertHandlerTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
use ProphecyTrait;
|
|
|
|
|
|
|
|
|
|
private function createMembersEditorFactoryMock(): MembersEditorFactory
|
|
|
|
|
{
|
|
|
|
|
$membersEditor = $this->prophesize(MembersEditor::class);
|
|
|
|
|
$violations = $this->prophesize(ConstraintViolationListInterface::class);
|
|
|
|
|
$violations->count()->willReturn(0);
|
|
|
|
|
$membersEditor->validate()->willReturn($violations->reveal());
|
|
|
|
|
$membersEditor->addMovement(Argument::type(\DateTimeImmutable::class), Argument::type(Person::class), null, true)->will(function ($args) use ($membersEditor) {
|
|
|
|
|
$date = $args[0];
|
|
|
|
|
$person = $args[1];
|
|
|
|
|
$household = $membersEditor->reveal()->getHousehold();
|
|
|
|
|
|
|
|
|
|
if ($household instanceof Household) {
|
|
|
|
|
$membership = (new HouseholdMember())
|
|
|
|
|
->setStartDate($date)
|
|
|
|
|
->setPerson($person);
|
|
|
|
|
|
|
|
|
|
$household->addMember($membership);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $membersEditor->reveal();
|
|
|
|
|
});
|
|
|
|
|
$membersEditor->getPersistable()->willReturn([]);
|
|
|
|
|
$membersEditor->postMove();
|
|
|
|
|
|
|
|
|
|
$factory = $this->prophesize(MembersEditorFactory::class);
|
|
|
|
|
$factory->createEditor()->will(function ($args) use ($membersEditor) {
|
|
|
|
|
$membersEditor->getHousehold()->willReturn(new Household());
|
|
|
|
|
|
|
|
|
|
return $membersEditor->reveal();
|
|
|
|
|
});
|
|
|
|
|
$factory->createEditor(Argument::type(Household::class))->will(function ($args) use ($membersEditor) {
|
|
|
|
|
$membersEditor->getHousehold()->willReturn($args[0]);
|
|
|
|
|
|
|
|
|
|
return $membersEditor->reveal();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return $factory->reveal();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testInvokeCreatesNewPersonAndIdentifier(): void
|
|
|
|
|
{
|
|
|
|
|
$personIdentifierDefinitionRepository = $this->prophesize(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository::class);
|
|
|
|
|
$identifierRepository = $this->prophesize(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierRepository::class);
|
|
|
|
|
$entityManager = $this->prophesize(EntityManagerInterface::class);
|
|
|
|
|
$membersEditorFactory = $this->createMembersEditorFactoryMock();
|
|
|
|
|
$postalCodeRepository = $this->prophesize(PostalCodeRepositoryInterface::class);
|
|
|
|
|
$centerRepository = $this->prophesize(CenterRepositoryInterface::class);
|
|
|
|
|
$genderRepository = $this->prophesize(GenderRepository::class);
|
|
|
|
|
$clock = new MockClock();
|
|
|
|
|
$phoneNumberUtil = $this->prophesize(PhoneNumberUtil::class);
|
|
|
|
|
$logger = new NullLogger();
|
|
|
|
|
$personIdentifierManager = $this->prophesize(PersonIdentifierManagerInterface::class);
|
|
|
|
|
$validator = $this->prophesize(ValidatorInterface::class);
|
|
|
|
|
|
|
|
|
|
$definition = new PersonIdentifierDefinition(['fr' => 'dummy'], 'dummy');
|
|
|
|
|
$personIdentifierDefinitionRepository->find(1)->willReturn($definition);
|
|
|
|
|
$identifierRepository->findByDefinitionAndCanonical($definition, '123')->willReturn([]);
|
|
|
|
|
|
|
|
|
|
// Mock gender repository
|
|
|
|
|
$gender = new Gender();
|
|
|
|
|
$gender->setGenderTranslation(GenderEnum::NEUTRAL);
|
|
|
|
|
$genderRepository->findByGenderTranslation(Argument::any())->willReturn([$gender]);
|
|
|
|
|
|
|
|
|
|
// Mock center repository - no center found
|
|
|
|
|
$centerRepository->findBy(['name' => null])->willReturn([]);
|
|
|
|
|
|
|
|
|
|
// Mock validator
|
|
|
|
|
$validator->validate(Argument::any())->willReturn(new ConstraintViolationList([]));
|
|
|
|
|
|
|
|
|
|
// Mock person identifier manager - create real worker with mocked engine
|
|
|
|
|
$identifierEngine = $this->prophesize(\Chill\PersonBundle\PersonIdentifier\PersonIdentifierEngineInterface::class);
|
|
|
|
|
$identifierEngine->canonicalizeValue(Argument::any(), Argument::any())->willReturn('123');
|
|
|
|
|
$worker = new \Chill\PersonBundle\PersonIdentifier\PersonIdentifierWorker(
|
|
|
|
|
$identifierEngine->reveal(),
|
|
|
|
|
$definition
|
|
|
|
|
);
|
|
|
|
|
$personIdentifierManager->buildWorkerByPersonIdentifierDefinition(Argument::any())->willReturn($worker);
|
|
|
|
|
|
|
|
|
|
$person = null;
|
|
|
|
|
$entityManager->persist(Argument::that(function ($arg) use (&$person) {
|
|
|
|
|
if ($arg instanceof Person) {
|
|
|
|
|
$person = $arg;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}))->shouldBeCalled();
|
|
|
|
|
$entityManager->persist(Argument::that(fn ($arg) => $arg instanceof PersonIdentifier && '123' === $arg->getCanonical() && $arg->getValue() === ['content' => '123']))->shouldBeCalled();
|
|
|
|
|
$entityManager->flush()->shouldBeCalled();
|
|
|
|
|
$entityManager->clear()->shouldBeCalled();
|
|
|
|
|
|
|
|
|
|
$handler = new PersonUpsertHandler(
|
|
|
|
|
$personIdentifierDefinitionRepository->reveal(),
|
|
|
|
|
$identifierRepository->reveal(),
|
|
|
|
|
$entityManager->reveal(),
|
|
|
|
|
$membersEditorFactory,
|
|
|
|
|
$postalCodeRepository->reveal(),
|
|
|
|
|
$centerRepository->reveal(),
|
|
|
|
|
$genderRepository->reveal(),
|
|
|
|
|
$clock,
|
|
|
|
|
$phoneNumberUtil->reveal(),
|
|
|
|
|
$logger,
|
|
|
|
|
$personIdentifierManager->reveal(),
|
|
|
|
|
$validator->reveal(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$message = new UpsertMessage();
|
|
|
|
|
$message->externalId = '123';
|
|
|
|
|
$message->personIdentifierDefinitionId = 1;
|
|
|
|
|
$message->firstName = 'John';
|
|
|
|
|
$message->lastName = 'Doe';
|
|
|
|
|
|
|
|
|
|
$handler->__invoke($message);
|
|
|
|
|
|
|
|
|
|
self::assertInstanceOf(Person::class, $person);
|
|
|
|
|
self::assertEquals('John', $person->getFirstName());
|
|
|
|
|
self::assertEquals('Doe', $person->getLastName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testInvokeUpdatesExistingPerson(): void
|
|
|
|
|
{
|
|
|
|
|
$personIdentifierDefinitionRepository = $this->prophesize(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository::class);
|
|
|
|
|
$identifierRepository = $this->prophesize(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierRepository::class);
|
|
|
|
|
$entityManager = $this->prophesize(EntityManagerInterface::class);
|
|
|
|
|
$membersEditorFactory = $this->createMembersEditorFactoryMock();
|
|
|
|
|
$postalCodeRepository = $this->prophesize(PostalCodeRepositoryInterface::class);
|
|
|
|
|
$centerRepository = $this->prophesize(CenterRepositoryInterface::class);
|
|
|
|
|
$genderRepository = $this->prophesize(GenderRepository::class);
|
|
|
|
|
$clock = new MockClock();
|
|
|
|
|
$phoneNumberUtil = $this->prophesize(PhoneNumberUtil::class);
|
|
|
|
|
$logger = new NullLogger();
|
|
|
|
|
$personIdentifierManager = $this->prophesize(PersonIdentifierManagerInterface::class);
|
|
|
|
|
$validator = $this->prophesize(ValidatorInterface::class);
|
|
|
|
|
|
|
|
|
|
$definition = new PersonIdentifierDefinition(['fr' => 'dummy'], 'dummy');
|
|
|
|
|
$personIdentifierDefinitionRepository->find(2)->willReturn($definition);
|
|
|
|
|
|
|
|
|
|
$person = new Person();
|
|
|
|
|
|
|
|
|
|
$identifier = new PersonIdentifier($definition);
|
|
|
|
|
$identifier->setPerson($person);
|
|
|
|
|
|
|
|
|
|
$identifierRepository->findByDefinitionAndCanonical($definition, '456')->willReturn([$identifier]);
|
|
|
|
|
|
|
|
|
|
// Mock gender repository - ensure it always returns an array
|
|
|
|
|
$gender = new Gender();
|
|
|
|
|
$gender->setGenderTranslation(GenderEnum::NEUTRAL);
|
|
|
|
|
$genderRepository->findByGenderTranslation(Argument::any())->willReturn([$gender]);
|
|
|
|
|
|
|
|
|
|
// Mock center repository - no center found
|
|
|
|
|
$centerRepository->findBy(['name' => null])->willReturn([]);
|
|
|
|
|
|
|
|
|
|
// Mock validator
|
|
|
|
|
$validator->validate(Argument::any())->willReturn(new ConstraintViolationList([]));
|
|
|
|
|
|
|
|
|
|
$entityManager->flush()->shouldBeCalled();
|
|
|
|
|
$entityManager->clear()->shouldBeCalled();
|
|
|
|
|
|
|
|
|
|
$handler = new PersonUpsertHandler(
|
|
|
|
|
$personIdentifierDefinitionRepository->reveal(),
|
|
|
|
|
$identifierRepository->reveal(),
|
|
|
|
|
$entityManager->reveal(),
|
|
|
|
|
$membersEditorFactory,
|
|
|
|
|
$postalCodeRepository->reveal(),
|
|
|
|
|
$centerRepository->reveal(),
|
|
|
|
|
$genderRepository->reveal(),
|
|
|
|
|
$clock,
|
|
|
|
|
$phoneNumberUtil->reveal(),
|
|
|
|
|
$logger,
|
|
|
|
|
$personIdentifierManager->reveal(),
|
|
|
|
|
$validator->reveal(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$message = new UpsertMessage();
|
|
|
|
|
$message->externalId = '456';
|
|
|
|
|
$message->personIdentifierDefinitionId = 2;
|
|
|
|
|
$message->firstName = 'Jane';
|
|
|
|
|
$message->lastName = 'Smith';
|
|
|
|
|
|
|
|
|
|
$handler->__invoke($message);
|
|
|
|
|
|
|
|
|
|
self::assertEquals('Jane', $person->getFirstName());
|
|
|
|
|
self::assertEquals('Smith', $person->getLastName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testInvokeWithGenderHandling(): void
|
|
|
|
|
{
|
|
|
|
|
$personIdentifierDefinitionRepository = $this->prophesize(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository::class);
|
|
|
|
|
$identifierRepository = $this->prophesize(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierRepository::class);
|
|
|
|
|
$entityManager = $this->prophesize(EntityManagerInterface::class);
|
|
|
|
|
$membersEditorFactory = $this->createMembersEditorFactoryMock();
|
|
|
|
|
$postalCodeRepository = $this->prophesize(PostalCodeRepositoryInterface::class);
|
|
|
|
|
$centerRepository = $this->prophesize(CenterRepositoryInterface::class);
|
|
|
|
|
$genderRepository = $this->prophesize(GenderRepository::class);
|
|
|
|
|
$clock = new MockClock();
|
|
|
|
|
$phoneNumberUtil = $this->prophesize(PhoneNumberUtil::class);
|
|
|
|
|
$logger = new NullLogger();
|
|
|
|
|
$personIdentifierManager = $this->prophesize(PersonIdentifierManagerInterface::class);
|
|
|
|
|
$validator = $this->prophesize(ValidatorInterface::class);
|
|
|
|
|
|
|
|
|
|
$definition = new PersonIdentifierDefinition(['fr' => 'dummy'], 'dummy');
|
|
|
|
|
$personIdentifierDefinitionRepository->find(1)->willReturn($definition);
|
|
|
|
|
$identifierRepository->findByDefinitionAndCanonical($definition, '123')->willReturn([]);
|
|
|
|
|
|
|
|
|
|
// Mock gender repository - configure to return male gender
|
|
|
|
|
$maleGender = new Gender();
|
|
|
|
|
$maleGender->setGenderTranslation(GenderEnum::MALE);
|
|
|
|
|
$genderRepository->findByGenderTranslation(GenderEnum::MALE)->willReturn([$maleGender]);
|
|
|
|
|
$genderRepository->findByGenderTranslation(Argument::any())->willReturn([$maleGender]);
|
|
|
|
|
|
|
|
|
|
// Mock center repository
|
|
|
|
|
$centerRepository->findBy(['name' => null])->willReturn([]);
|
|
|
|
|
|
|
|
|
|
// Mock validator
|
|
|
|
|
$validator->validate(Argument::any())->willReturn(new ConstraintViolationList([]));
|
|
|
|
|
|
|
|
|
|
// Mock person identifier manager - create real worker with mocked engine
|
|
|
|
|
$identifierEngine = $this->prophesize(\Chill\PersonBundle\PersonIdentifier\PersonIdentifierEngineInterface::class);
|
|
|
|
|
$identifierEngine->canonicalizeValue(Argument::any(), Argument::any())->willReturn('123');
|
|
|
|
|
$worker = new \Chill\PersonBundle\PersonIdentifier\PersonIdentifierWorker(
|
|
|
|
|
$identifierEngine->reveal(),
|
|
|
|
|
$definition
|
|
|
|
|
);
|
|
|
|
|
$personIdentifierManager->buildWorkerByPersonIdentifierDefinition(Argument::any())->willReturn($worker);
|
|
|
|
|
|
|
|
|
|
$person = null;
|
|
|
|
|
$entityManager->persist(Argument::that(function ($arg) use (&$person) {
|
|
|
|
|
if ($arg instanceof Person) {
|
|
|
|
|
$person = $arg;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}))->shouldBeCalled();
|
|
|
|
|
$entityManager->persist(Argument::any())->shouldBeCalled();
|
|
|
|
|
$entityManager->flush()->shouldBeCalled();
|
|
|
|
|
$entityManager->clear()->shouldBeCalled();
|
|
|
|
|
|
|
|
|
|
$handler = new PersonUpsertHandler(
|
|
|
|
|
$personIdentifierDefinitionRepository->reveal(),
|
|
|
|
|
$identifierRepository->reveal(),
|
|
|
|
|
$entityManager->reveal(),
|
|
|
|
|
$membersEditorFactory,
|
|
|
|
|
$postalCodeRepository->reveal(),
|
|
|
|
|
$centerRepository->reveal(),
|
|
|
|
|
$genderRepository->reveal(),
|
|
|
|
|
$clock,
|
|
|
|
|
$phoneNumberUtil->reveal(),
|
|
|
|
|
$logger,
|
|
|
|
|
$personIdentifierManager->reveal(),
|
|
|
|
|
$validator->reveal(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$message = new UpsertMessage();
|
|
|
|
|
$message->externalId = '123';
|
|
|
|
|
$message->personIdentifierDefinitionId = 1;
|
|
|
|
|
$message->firstName = 'John';
|
|
|
|
|
$message->lastName = 'Doe';
|
|
|
|
|
$message->gender = 'male';
|
|
|
|
|
|
|
|
|
|
$handler->__invoke($message);
|
|
|
|
|
|
|
|
|
|
self::assertInstanceOf(Person::class, $person);
|
|
|
|
|
self::assertSame($maleGender, $person->getGender());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testInvokeWithCenterHandling(): void
|
|
|
|
|
{
|
|
|
|
|
$personIdentifierDefinitionRepository = $this->prophesize(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository::class);
|
|
|
|
|
$identifierRepository = $this->prophesize(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierRepository::class);
|
|
|
|
|
$entityManager = $this->prophesize(EntityManagerInterface::class);
|
|
|
|
|
$membersEditorFactory = $this->createMembersEditorFactoryMock();
|
|
|
|
|
$postalCodeRepository = $this->prophesize(PostalCodeRepositoryInterface::class);
|
|
|
|
|
$centerRepository = $this->prophesize(CenterRepositoryInterface::class);
|
|
|
|
|
$genderRepository = $this->prophesize(GenderRepository::class);
|
|
|
|
|
$clock = new MockClock();
|
|
|
|
|
$phoneNumberUtil = $this->prophesize(PhoneNumberUtil::class);
|
|
|
|
|
$logger = new NullLogger();
|
|
|
|
|
$personIdentifierManager = $this->prophesize(PersonIdentifierManagerInterface::class);
|
|
|
|
|
$validator = $this->prophesize(ValidatorInterface::class);
|
|
|
|
|
|
|
|
|
|
$definition = new PersonIdentifierDefinition(['fr' => 'dummy'], 'dummy');
|
|
|
|
|
$personIdentifierDefinitionRepository->find(1)->willReturn($definition);
|
|
|
|
|
$identifierRepository->findByDefinitionAndCanonical($definition, '123')->willReturn([]);
|
|
|
|
|
|
|
|
|
|
// Mock gender repository
|
|
|
|
|
$neutralGender = new Gender();
|
|
|
|
|
$neutralGender->setGenderTranslation(GenderEnum::NEUTRAL);
|
|
|
|
|
$genderRepository->findByGenderTranslation(GenderEnum::NEUTRAL)->willReturn([$neutralGender]);
|
|
|
|
|
$genderRepository->findByGenderTranslation(Argument::any())->willReturn([$neutralGender]);
|
|
|
|
|
|
|
|
|
|
// Mock center repository - find center by name
|
|
|
|
|
$center = new \Chill\MainBundle\Entity\Center();
|
|
|
|
|
$center->setName('Main Center');
|
|
|
|
|
$centerRepository->findBy(['name' => 'Main Center'])->willReturn([$center]);
|
|
|
|
|
|
|
|
|
|
// Mock validator
|
|
|
|
|
$validator->validate(Argument::any())->willReturn(new ConstraintViolationList([]));
|
|
|
|
|
|
|
|
|
|
// Mock person identifier manager - create real worker with mocked engine
|
|
|
|
|
$identifierEngine = $this->prophesize(\Chill\PersonBundle\PersonIdentifier\PersonIdentifierEngineInterface::class);
|
|
|
|
|
$identifierEngine->canonicalizeValue(Argument::any(), Argument::any())->willReturn('123');
|
|
|
|
|
$worker = new \Chill\PersonBundle\PersonIdentifier\PersonIdentifierWorker(
|
|
|
|
|
$identifierEngine->reveal(),
|
|
|
|
|
$definition
|
|
|
|
|
);
|
|
|
|
|
$personIdentifierManager->buildWorkerByPersonIdentifierDefinition(Argument::any())->willReturn($worker);
|
|
|
|
|
|
|
|
|
|
$person = null;
|
|
|
|
|
$entityManager->persist(Argument::that(function ($arg) use (&$person) {
|
|
|
|
|
if ($arg instanceof Person) {
|
|
|
|
|
$person = $arg;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}))->shouldBeCalled();
|
|
|
|
|
$entityManager->persist(Argument::any())->shouldBeCalled();
|
|
|
|
|
$entityManager->flush()->shouldBeCalled();
|
|
|
|
|
$entityManager->clear()->shouldBeCalled();
|
|
|
|
|
|
|
|
|
|
$handler = new PersonUpsertHandler(
|
|
|
|
|
$personIdentifierDefinitionRepository->reveal(),
|
|
|
|
|
$identifierRepository->reveal(),
|
|
|
|
|
$entityManager->reveal(),
|
|
|
|
|
$membersEditorFactory,
|
|
|
|
|
$postalCodeRepository->reveal(),
|
|
|
|
|
$centerRepository->reveal(),
|
|
|
|
|
$genderRepository->reveal(),
|
|
|
|
|
$clock,
|
|
|
|
|
$phoneNumberUtil->reveal(),
|
|
|
|
|
$logger,
|
|
|
|
|
$personIdentifierManager->reveal(),
|
|
|
|
|
$validator->reveal(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$message = new UpsertMessage();
|
|
|
|
|
$message->externalId = '123';
|
|
|
|
|
$message->personIdentifierDefinitionId = 1;
|
|
|
|
|
$message->firstName = 'John';
|
|
|
|
|
$message->lastName = 'Doe';
|
|
|
|
|
$message->center = 'Main Center';
|
|
|
|
|
|
|
|
|
|
$handler->__invoke($message);
|
|
|
|
|
|
|
|
|
|
self::assertInstanceOf(Person::class, $person);
|
|
|
|
|
self::assertSame($center, $person->getCenter());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testInvokeWithGenderAndCenterHandling(): void
|
|
|
|
|
{
|
|
|
|
|
$personIdentifierDefinitionRepository = $this->prophesize(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository::class);
|
|
|
|
|
$identifierRepository = $this->prophesize(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierRepository::class);
|
|
|
|
|
$entityManager = $this->prophesize(EntityManagerInterface::class);
|
|
|
|
|
$membersEditorFactory = $this->createMembersEditorFactoryMock();
|
|
|
|
|
$postalCodeRepository = $this->prophesize(PostalCodeRepositoryInterface::class);
|
|
|
|
|
$centerRepository = $this->prophesize(CenterRepositoryInterface::class);
|
|
|
|
|
$genderRepository = $this->prophesize(GenderRepository::class);
|
|
|
|
|
$clock = new MockClock();
|
|
|
|
|
$phoneNumberUtil = $this->prophesize(PhoneNumberUtil::class);
|
|
|
|
|
$logger = new NullLogger();
|
|
|
|
|
$personIdentifierManager = $this->prophesize(PersonIdentifierManagerInterface::class);
|
|
|
|
|
$validator = $this->prophesize(ValidatorInterface::class);
|
|
|
|
|
|
|
|
|
|
$definition = new PersonIdentifierDefinition(['fr' => 'dummy'], 'dummy');
|
|
|
|
|
$personIdentifierDefinitionRepository->find(1)->willReturn($definition);
|
|
|
|
|
$identifierRepository->findByDefinitionAndCanonical($definition, '123')->willReturn([]);
|
|
|
|
|
|
|
|
|
|
// Mock gender repository - configure to return female gender
|
|
|
|
|
$femaleGender = new Gender();
|
|
|
|
|
$femaleGender->setGenderTranslation(GenderEnum::FEMALE);
|
|
|
|
|
$genderRepository->findByGenderTranslation(GenderEnum::FEMALE)->willReturn([$femaleGender]);
|
|
|
|
|
$genderRepository->findByGenderTranslation(Argument::any())->willReturn([$femaleGender]);
|
|
|
|
|
|
|
|
|
|
// Mock center repository - find center by name
|
|
|
|
|
$center = new \Chill\MainBundle\Entity\Center();
|
|
|
|
|
$center->setName('Secondary Center');
|
|
|
|
|
$centerRepository->findBy(['name' => 'Secondary Center'])->willReturn([$center]);
|
|
|
|
|
|
|
|
|
|
// Mock validator
|
|
|
|
|
$validator->validate(Argument::any())->willReturn(new ConstraintViolationList([]));
|
|
|
|
|
|
|
|
|
|
// Mock person identifier manager - create real worker with mocked engine
|
|
|
|
|
$identifierEngine = $this->prophesize(\Chill\PersonBundle\PersonIdentifier\PersonIdentifierEngineInterface::class);
|
|
|
|
|
$identifierEngine->canonicalizeValue(Argument::any(), Argument::any())->willReturn('123');
|
|
|
|
|
$worker = new \Chill\PersonBundle\PersonIdentifier\PersonIdentifierWorker(
|
|
|
|
|
$identifierEngine->reveal(),
|
|
|
|
|
$definition
|
|
|
|
|
);
|
|
|
|
|
$personIdentifierManager->buildWorkerByPersonIdentifierDefinition(Argument::any())->willReturn($worker);
|
|
|
|
|
|
|
|
|
|
$person = null;
|
|
|
|
|
$entityManager->persist(Argument::that(function ($arg) use (&$person) {
|
|
|
|
|
if ($arg instanceof Person) {
|
|
|
|
|
$person = $arg;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}))->shouldBeCalled();
|
|
|
|
|
$entityManager->persist(Argument::any())->shouldBeCalled();
|
|
|
|
|
$entityManager->flush()->shouldBeCalled();
|
|
|
|
|
$entityManager->clear()->shouldBeCalled();
|
|
|
|
|
|
|
|
|
|
$handler = new PersonUpsertHandler(
|
|
|
|
|
$personIdentifierDefinitionRepository->reveal(),
|
|
|
|
|
$identifierRepository->reveal(),
|
|
|
|
|
$entityManager->reveal(),
|
|
|
|
|
$membersEditorFactory,
|
|
|
|
|
$postalCodeRepository->reveal(),
|
|
|
|
|
$centerRepository->reveal(),
|
|
|
|
|
$genderRepository->reveal(),
|
|
|
|
|
$clock,
|
|
|
|
|
$phoneNumberUtil->reveal(),
|
|
|
|
|
$logger,
|
|
|
|
|
$personIdentifierManager->reveal(),
|
|
|
|
|
$validator->reveal(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$message = new UpsertMessage();
|
|
|
|
|
$message->externalId = '123';
|
|
|
|
|
$message->personIdentifierDefinitionId = 1;
|
|
|
|
|
$message->firstName = 'Jane';
|
|
|
|
|
$message->lastName = 'Smith';
|
|
|
|
|
$message->gender = 'female';
|
|
|
|
|
$message->center = 'Secondary Center';
|
|
|
|
|
|
|
|
|
|
$handler->__invoke($message);
|
|
|
|
|
|
|
|
|
|
self::assertInstanceOf(Person::class, $person);
|
|
|
|
|
self::assertSame($femaleGender, $person->getGender());
|
|
|
|
|
self::assertSame($center, $person->getCenter());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testInvokeWithBirthdate(): void
|
|
|
|
|
{
|
|
|
|
|
$personIdentifierDefinitionRepository = $this->prophesize(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository::class);
|
|
|
|
|
$identifierRepository = $this->prophesize(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierRepository::class);
|
|
|
|
|
$entityManager = $this->prophesize(EntityManagerInterface::class);
|
|
|
|
|
$membersEditorFactory = $this->createMembersEditorFactoryMock();
|
|
|
|
|
$postalCodeRepository = $this->prophesize(PostalCodeRepositoryInterface::class);
|
|
|
|
|
$centerRepository = $this->prophesize(CenterRepositoryInterface::class);
|
|
|
|
|
$genderRepository = $this->prophesize(GenderRepository::class);
|
|
|
|
|
$clock = new MockClock();
|
|
|
|
|
$phoneNumberUtil = $this->prophesize(PhoneNumberUtil::class);
|
|
|
|
|
$logger = new NullLogger();
|
|
|
|
|
$personIdentifierManager = $this->prophesize(PersonIdentifierManagerInterface::class);
|
|
|
|
|
$validator = $this->prophesize(ValidatorInterface::class);
|
|
|
|
|
|
|
|
|
|
$definition = new PersonIdentifierDefinition(['fr' => 'dummy'], 'dummy');
|
|
|
|
|
$personIdentifierDefinitionRepository->find(1)->willReturn($definition);
|
|
|
|
|
$identifierRepository->findByDefinitionAndCanonical($definition, '123')->willReturn([]);
|
|
|
|
|
|
|
|
|
|
// Mock gender repository
|
|
|
|
|
$gender = new Gender();
|
|
|
|
|
$gender->setGenderTranslation(GenderEnum::NEUTRAL);
|
|
|
|
|
$genderRepository->findByGenderTranslation(Argument::any())->willReturn([$gender]);
|
|
|
|
|
|
|
|
|
|
// Mock center repository
|
|
|
|
|
$centerRepository->findBy(['name' => null])->willReturn([]);
|
|
|
|
|
|
|
|
|
|
// Mock validator
|
|
|
|
|
$validator->validate(Argument::any())->willReturn(new ConstraintViolationList([]));
|
|
|
|
|
|
|
|
|
|
// Mock person identifier manager - create real worker with mocked engine
|
|
|
|
|
$identifierEngine = $this->prophesize(\Chill\PersonBundle\PersonIdentifier\PersonIdentifierEngineInterface::class);
|
|
|
|
|
$identifierEngine->canonicalizeValue(Argument::any(), Argument::any())->willReturn('123');
|
|
|
|
|
$worker = new \Chill\PersonBundle\PersonIdentifier\PersonIdentifierWorker(
|
|
|
|
|
$identifierEngine->reveal(),
|
|
|
|
|
$definition
|
|
|
|
|
);
|
|
|
|
|
$personIdentifierManager->buildWorkerByPersonIdentifierDefinition(Argument::any())->willReturn($worker);
|
|
|
|
|
|
|
|
|
|
$person = null;
|
|
|
|
|
$entityManager->persist(Argument::that(function ($arg) use (&$person) {
|
|
|
|
|
if ($arg instanceof Person) {
|
|
|
|
|
$person = $arg;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}))->shouldBeCalled();
|
|
|
|
|
$entityManager->persist(Argument::any())->shouldBeCalled();
|
|
|
|
|
$entityManager->flush()->shouldBeCalled();
|
|
|
|
|
$entityManager->clear()->shouldBeCalled();
|
|
|
|
|
|
|
|
|
|
$handler = new PersonUpsertHandler(
|
|
|
|
|
$personIdentifierDefinitionRepository->reveal(),
|
|
|
|
|
$identifierRepository->reveal(),
|
|
|
|
|
$entityManager->reveal(),
|
|
|
|
|
$membersEditorFactory,
|
|
|
|
|
$postalCodeRepository->reveal(),
|
|
|
|
|
$centerRepository->reveal(),
|
|
|
|
|
$genderRepository->reveal(),
|
|
|
|
|
$clock,
|
|
|
|
|
$phoneNumberUtil->reveal(),
|
|
|
|
|
$logger,
|
|
|
|
|
$personIdentifierManager->reveal(),
|
|
|
|
|
$validator->reveal(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$message = new UpsertMessage();
|
|
|
|
|
$message->externalId = '123';
|
|
|
|
|
$message->personIdentifierDefinitionId = 1;
|
|
|
|
|
$message->firstName = 'John';
|
|
|
|
|
$message->lastName = 'Doe';
|
|
|
|
|
$message->birthdate = '1990-01-15';
|
|
|
|
|
|
|
|
|
|
$handler->__invoke($message);
|
|
|
|
|
|
|
|
|
|
self::assertInstanceOf(Person::class, $person);
|
|
|
|
|
self::assertEquals(new \DateTime('1990-01-15'), $person->getBirthdate());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testInvokeWithPhoneNumber(): void
|
|
|
|
|
{
|
|
|
|
|
$personIdentifierDefinitionRepository = $this->prophesize(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository::class);
|
|
|
|
|
$identifierRepository = $this->prophesize(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierRepository::class);
|
|
|
|
|
$entityManager = $this->prophesize(EntityManagerInterface::class);
|
|
|
|
|
$membersEditorFactory = $this->createMembersEditorFactoryMock();
|
|
|
|
|
$postalCodeRepository = $this->prophesize(PostalCodeRepositoryInterface::class);
|
|
|
|
|
$centerRepository = $this->prophesize(CenterRepositoryInterface::class);
|
|
|
|
|
$genderRepository = $this->prophesize(GenderRepository::class);
|
|
|
|
|
$clock = new MockClock();
|
|
|
|
|
$phoneNumberUtil = $this->prophesize(PhoneNumberUtil::class);
|
|
|
|
|
$logger = new NullLogger();
|
|
|
|
|
$personIdentifierManager = $this->prophesize(PersonIdentifierManagerInterface::class);
|
|
|
|
|
$validator = $this->prophesize(ValidatorInterface::class);
|
|
|
|
|
|
|
|
|
|
$definition = new PersonIdentifierDefinition(['fr' => 'dummy'], 'dummy');
|
|
|
|
|
$personIdentifierDefinitionRepository->find(1)->willReturn($definition);
|
|
|
|
|
$identifierRepository->findByDefinitionAndCanonical($definition, '123')->willReturn([]);
|
|
|
|
|
|
|
|
|
|
// Mock gender repository
|
|
|
|
|
$gender = new Gender();
|
|
|
|
|
$gender->setGenderTranslation(GenderEnum::NEUTRAL);
|
|
|
|
|
$genderRepository->findByGenderTranslation(Argument::any())->willReturn([$gender]);
|
|
|
|
|
|
|
|
|
|
// Mock center repository
|
|
|
|
|
$centerRepository->findBy(['name' => null])->willReturn([]);
|
|
|
|
|
|
|
|
|
|
// Mock phone number parsing
|
|
|
|
|
$phoneNumber = $this->prophesize(\libphonenumber\PhoneNumber::class);
|
|
|
|
|
$phoneNumberUtil->parse('+32123456789')->willReturn($phoneNumber->reveal());
|
|
|
|
|
|
|
|
|
|
// Mock validator
|
|
|
|
|
$validator->validate(Argument::any())->willReturn(new ConstraintViolationList([]));
|
|
|
|
|
|
|
|
|
|
// Mock person identifier manager - create real worker with mocked engine
|
|
|
|
|
$identifierEngine = $this->prophesize(\Chill\PersonBundle\PersonIdentifier\PersonIdentifierEngineInterface::class);
|
|
|
|
|
$identifierEngine->canonicalizeValue(Argument::any(), Argument::any())->willReturn('123');
|
|
|
|
|
$worker = new \Chill\PersonBundle\PersonIdentifier\PersonIdentifierWorker(
|
|
|
|
|
$identifierEngine->reveal(),
|
|
|
|
|
$definition
|
|
|
|
|
);
|
|
|
|
|
$personIdentifierManager->buildWorkerByPersonIdentifierDefinition(Argument::any())->willReturn($worker);
|
|
|
|
|
|
|
|
|
|
$person = null;
|
|
|
|
|
$entityManager->persist(Argument::that(function ($arg) use (&$person) {
|
|
|
|
|
if ($arg instanceof Person) {
|
|
|
|
|
$person = $arg;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}))->shouldBeCalled();
|
|
|
|
|
$entityManager->persist(Argument::any())->shouldBeCalled();
|
|
|
|
|
$entityManager->flush()->shouldBeCalled();
|
|
|
|
|
$entityManager->clear()->shouldBeCalled();
|
|
|
|
|
|
|
|
|
|
$handler = new PersonUpsertHandler(
|
|
|
|
|
$personIdentifierDefinitionRepository->reveal(),
|
|
|
|
|
$identifierRepository->reveal(),
|
|
|
|
|
$entityManager->reveal(),
|
|
|
|
|
$membersEditorFactory,
|
|
|
|
|
$postalCodeRepository->reveal(),
|
|
|
|
|
$centerRepository->reveal(),
|
|
|
|
|
$genderRepository->reveal(),
|
|
|
|
|
$clock,
|
|
|
|
|
$phoneNumberUtil->reveal(),
|
|
|
|
|
$logger,
|
|
|
|
|
$personIdentifierManager->reveal(),
|
|
|
|
|
$validator->reveal(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$message = new UpsertMessage();
|
|
|
|
|
$message->externalId = '123';
|
|
|
|
|
$message->personIdentifierDefinitionId = 1;
|
|
|
|
|
$message->firstName = 'John';
|
|
|
|
|
$message->lastName = 'Doe';
|
|
|
|
|
$message->phoneNumber = '+32123456789';
|
|
|
|
|
|
|
|
|
|
$handler->__invoke($message);
|
|
|
|
|
|
|
|
|
|
self::assertInstanceOf(Person::class, $person);
|
|
|
|
|
self::assertSame($phoneNumber->reveal(), $person->getPhonenumber());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testInvokeWithMobileNumber(): void
|
|
|
|
|
{
|
|
|
|
|
$personIdentifierDefinitionRepository = $this->prophesize(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository::class);
|
|
|
|
|
$identifierRepository = $this->prophesize(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierRepository::class);
|
|
|
|
|
$entityManager = $this->prophesize(EntityManagerInterface::class);
|
|
|
|
|
$membersEditorFactory = $this->createMembersEditorFactoryMock();
|
|
|
|
|
$postalCodeRepository = $this->prophesize(PostalCodeRepositoryInterface::class);
|
|
|
|
|
$centerRepository = $this->prophesize(CenterRepositoryInterface::class);
|
|
|
|
|
$genderRepository = $this->prophesize(GenderRepository::class);
|
|
|
|
|
$clock = new MockClock();
|
|
|
|
|
$phoneNumberUtil = PhoneNumberUtil::getInstance();
|
|
|
|
|
$logger = new NullLogger();
|
|
|
|
|
$personIdentifierManager = $this->prophesize(PersonIdentifierManagerInterface::class);
|
|
|
|
|
$validator = $this->prophesize(ValidatorInterface::class);
|
|
|
|
|
|
|
|
|
|
$definition = new PersonIdentifierDefinition(['fr' => 'dummy'], 'dummy');
|
|
|
|
|
$personIdentifierDefinitionRepository->find(1)->willReturn($definition);
|
|
|
|
|
$identifierRepository->findByDefinitionAndCanonical($definition, '123')->willReturn([]);
|
|
|
|
|
|
|
|
|
|
// Mock gender repository
|
|
|
|
|
$gender = new Gender();
|
|
|
|
|
$gender->setGenderTranslation(GenderEnum::NEUTRAL);
|
|
|
|
|
$genderRepository->findByGenderTranslation(Argument::any())->willReturn([$gender]);
|
|
|
|
|
|
|
|
|
|
// Mock center repository
|
|
|
|
|
$centerRepository->findBy(['name' => null])->willReturn([]);
|
|
|
|
|
|
|
|
|
|
// Mock validator
|
|
|
|
|
$validator->validate(Argument::any())->willReturn(new ConstraintViolationList());
|
|
|
|
|
|
|
|
|
|
// Mock person identifier manager - create real worker with mocked engine
|
|
|
|
|
$identifierEngine = $this->prophesize(\Chill\PersonBundle\PersonIdentifier\PersonIdentifierEngineInterface::class);
|
|
|
|
|
$identifierEngine->canonicalizeValue(Argument::any(), Argument::any())->willReturn('123');
|
|
|
|
|
$worker = new \Chill\PersonBundle\PersonIdentifier\PersonIdentifierWorker(
|
|
|
|
|
$identifierEngine->reveal(),
|
|
|
|
|
$definition
|
|
|
|
|
);
|
|
|
|
|
$personIdentifierManager->buildWorkerByPersonIdentifierDefinition(Argument::any())->willReturn($worker);
|
|
|
|
|
|
|
|
|
|
$person = null;
|
|
|
|
|
$entityManager->persist(Argument::that(function ($arg) use (&$person) {
|
|
|
|
|
if ($arg instanceof Person) {
|
|
|
|
|
$person = $arg;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}))->shouldBeCalled();
|
|
|
|
|
$entityManager->persist(Argument::any())->shouldBeCalled();
|
|
|
|
|
$entityManager->flush()->shouldBeCalled();
|
|
|
|
|
$entityManager->clear()->shouldBeCalled();
|
|
|
|
|
|
|
|
|
|
$handler = new PersonUpsertHandler(
|
|
|
|
|
$personIdentifierDefinitionRepository->reveal(),
|
|
|
|
|
$identifierRepository->reveal(),
|
|
|
|
|
$entityManager->reveal(),
|
|
|
|
|
$membersEditorFactory,
|
|
|
|
|
$postalCodeRepository->reveal(),
|
|
|
|
|
$centerRepository->reveal(),
|
|
|
|
|
$genderRepository->reveal(),
|
|
|
|
|
$clock,
|
|
|
|
|
$phoneNumberUtil,
|
|
|
|
|
$logger,
|
|
|
|
|
$personIdentifierManager->reveal(),
|
|
|
|
|
$validator->reveal(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$message = new UpsertMessage();
|
|
|
|
|
$message->externalId = '123';
|
|
|
|
|
$message->personIdentifierDefinitionId = 1;
|
|
|
|
|
$message->firstName = 'John';
|
|
|
|
|
$message->lastName = 'Doe';
|
|
|
|
|
$message->mobileNumber = '+32475123456';
|
|
|
|
|
|
|
|
|
|
$handler->__invoke($message);
|
|
|
|
|
|
|
|
|
|
self::assertInstanceOf(Person::class, $person);
|
|
|
|
|
self::assertEquals('475123456', (string) $person->getMobilenumber()->getNationalNumber());
|
|
|
|
|
self::assertEquals('32', $person->getMobilenumber()->getCountryCode());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testInvokeWithAddressFields(): void
|
|
|
|
|
{
|
|
|
|
|
$personIdentifierDefinitionRepository = $this->prophesize(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository::class);
|
|
|
|
|
$identifierRepository = $this->prophesize(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierRepository::class);
|
|
|
|
|
$entityManager = $this->prophesize(EntityManagerInterface::class);
|
|
|
|
|
$membersEditorFactory = $this->createMembersEditorFactoryMock();
|
|
|
|
|
$postalCodeRepository = $this->prophesize(PostalCodeRepositoryInterface::class);
|
|
|
|
|
$centerRepository = $this->prophesize(CenterRepositoryInterface::class);
|
|
|
|
|
$genderRepository = $this->prophesize(GenderRepository::class);
|
|
|
|
|
$clock = new MockClock('2026-03-09');
|
|
|
|
|
$phoneNumberUtil = $this->prophesize(PhoneNumberUtil::class);
|
|
|
|
|
$logger = new NullLogger();
|
|
|
|
|
$personIdentifierManager = $this->prophesize(PersonIdentifierManagerInterface::class);
|
|
|
|
|
$validator = $this->prophesize(ValidatorInterface::class);
|
|
|
|
|
|
|
|
|
|
$definition = new PersonIdentifierDefinition(['fr' => 'dummy'], 'dummy');
|
|
|
|
|
$personIdentifierDefinitionRepository->find(1)->willReturn($definition);
|
|
|
|
|
$identifierRepository->findByDefinitionAndCanonical($definition, '123')->willReturn([]);
|
|
|
|
|
|
|
|
|
|
// Mock gender repository
|
|
|
|
|
$gender = new Gender();
|
|
|
|
|
$gender->setGenderTranslation(GenderEnum::NEUTRAL);
|
|
|
|
|
$genderRepository->findByGenderTranslation(Argument::any())->willReturn([$gender]);
|
|
|
|
|
|
|
|
|
|
// Mock center repository
|
|
|
|
|
$centerRepository->findBy(['name' => null])->willReturn([]);
|
|
|
|
|
|
|
|
|
|
// Mock postal code repository
|
|
|
|
|
$postalCode = new \Chill\MainBundle\Entity\PostalCode();
|
|
|
|
|
$postalCode->setCode('1000');
|
|
|
|
|
$postalCodeRepository->findOneBy(['code' => '1000'])->willReturn($postalCode);
|
|
|
|
|
|
|
|
|
|
// Mock validator
|
|
|
|
|
$validator->validate(Argument::any())->willReturn(new ConstraintViolationList([]));
|
|
|
|
|
|
|
|
|
|
// Mock person identifier manager - create real worker with mocked engine
|
|
|
|
|
$identifierEngine = $this->prophesize(\Chill\PersonBundle\PersonIdentifier\PersonIdentifierEngineInterface::class);
|
|
|
|
|
$identifierEngine->canonicalizeValue(Argument::any(), Argument::any())->willReturn('123');
|
|
|
|
|
$worker = new \Chill\PersonBundle\PersonIdentifier\PersonIdentifierWorker(
|
|
|
|
|
$identifierEngine->reveal(),
|
|
|
|
|
$definition
|
|
|
|
|
);
|
|
|
|
|
$personIdentifierManager->buildWorkerByPersonIdentifierDefinition(Argument::any())->willReturn($worker);
|
|
|
|
|
|
|
|
|
|
$person = null;
|
|
|
|
|
$entityManager->persist(Argument::that(function ($arg) use (&$person) {
|
|
|
|
|
if ($arg instanceof Person) {
|
|
|
|
|
$person = $arg;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}))->shouldBeCalled();
|
|
|
|
|
$entityManager->persist(Argument::any())->shouldBeCalled();
|
|
|
|
|
$entityManager->flush()->shouldBeCalled();
|
|
|
|
|
$entityManager->clear()->shouldBeCalled();
|
|
|
|
|
|
|
|
|
|
$handler = new PersonUpsertHandler(
|
|
|
|
|
$personIdentifierDefinitionRepository->reveal(),
|
|
|
|
|
$identifierRepository->reveal(),
|
|
|
|
|
$entityManager->reveal(),
|
|
|
|
|
$membersEditorFactory,
|
|
|
|
|
$postalCodeRepository->reveal(),
|
|
|
|
|
$centerRepository->reveal(),
|
|
|
|
|
$genderRepository->reveal(),
|
|
|
|
|
$clock,
|
|
|
|
|
$phoneNumberUtil->reveal(),
|
|
|
|
|
$logger,
|
|
|
|
|
$personIdentifierManager->reveal(),
|
|
|
|
|
$validator->reveal(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$message = new UpsertMessage();
|
|
|
|
|
$message->externalId = '123';
|
|
|
|
|
$message->personIdentifierDefinitionId = 1;
|
|
|
|
|
$message->firstName = 'John';
|
|
|
|
|
$message->lastName = 'Doe';
|
|
|
|
|
$message->addressStreet = 'Main Street';
|
|
|
|
|
$message->addressStreetNumber = '42';
|
|
|
|
|
$message->addressPostcode = '1000';
|
|
|
|
|
$message->addressExtra = 'Apartment 3B';
|
|
|
|
|
$message->addressCity = 'Brussels';
|
|
|
|
|
|
|
|
|
|
$handler->__invoke($message);
|
|
|
|
|
|
|
|
|
|
self::assertInstanceOf(Person::class, $person);
|
|
|
|
|
$household = $person->getCurrentHousehold($clock->now());
|
|
|
|
|
self::assertNotNull($household);
|
|
|
|
|
$address = $household->getCurrentAddress(\DateTime::createFromImmutable($clock->now()));
|
|
|
|
|
self::assertNotNull($address);
|
|
|
|
|
self::assertEquals('Main Street', $address->getStreet());
|
|
|
|
|
self::assertEquals('42', $address->getStreetNumber());
|
|
|
|
|
self::assertSame($postalCode, $address->getPostcode());
|
|
|
|
|
self::assertEquals('Apartment 3B', $address->getExtra());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testInvokeWithAllFields(): void
|
|
|
|
|
{
|
|
|
|
|
$personIdentifierDefinitionRepository = $this->prophesize(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository::class);
|
|
|
|
|
$identifierRepository = $this->prophesize(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierRepository::class);
|
|
|
|
|
$entityManager = $this->prophesize(EntityManagerInterface::class);
|
|
|
|
|
$membersEditorFactory = $this->createMembersEditorFactoryMock();
|
|
|
|
|
$postalCodeRepository = $this->prophesize(PostalCodeRepositoryInterface::class);
|
|
|
|
|
$centerRepository = $this->prophesize(CenterRepositoryInterface::class);
|
|
|
|
|
$genderRepository = $this->prophesize(GenderRepository::class);
|
|
|
|
|
$clock = new MockClock('2026-03-09');
|
|
|
|
|
$phoneNumberUtil = $this->prophesize(PhoneNumberUtil::class);
|
|
|
|
|
$logger = new NullLogger();
|
|
|
|
|
$personIdentifierManager = $this->prophesize(PersonIdentifierManagerInterface::class);
|
|
|
|
|
$validator = $this->prophesize(ValidatorInterface::class);
|
|
|
|
|
|
|
|
|
|
$definition = new PersonIdentifierDefinition(['fr' => 'dummy'], 'dummy');
|
|
|
|
|
$personIdentifierDefinitionRepository->find(1)->willReturn($definition);
|
|
|
|
|
$identifierRepository->findByDefinitionAndCanonical($definition, '123')->willReturn([]);
|
|
|
|
|
|
|
|
|
|
// Mock gender repository
|
|
|
|
|
$femaleGender = new Gender();
|
|
|
|
|
$femaleGender->setGenderTranslation(GenderEnum::FEMALE);
|
|
|
|
|
$genderRepository->findByGenderTranslation(GenderEnum::FEMALE)->willReturn([$femaleGender]);
|
|
|
|
|
$genderRepository->findByGenderTranslation(Argument::any())->willReturn([$femaleGender]);
|
|
|
|
|
|
|
|
|
|
// Mock center repository
|
|
|
|
|
$center = new \Chill\MainBundle\Entity\Center();
|
|
|
|
|
$center->setName('Main Center');
|
|
|
|
|
$centerRepository->findBy(['name' => 'Main Center'])->willReturn([$center]);
|
|
|
|
|
|
|
|
|
|
// Mock postal code repository
|
|
|
|
|
$postalCode = new \Chill\MainBundle\Entity\PostalCode();
|
|
|
|
|
$postalCode->setCode('1000');
|
|
|
|
|
$postalCodeRepository->findOneBy(['code' => '1000'])->willReturn($postalCode);
|
|
|
|
|
|
|
|
|
|
// Mock phone number parsing
|
|
|
|
|
$phoneNumber = $this->prophesize(\libphonenumber\PhoneNumber::class);
|
|
|
|
|
$mobileNumber = $this->prophesize(\libphonenumber\PhoneNumber::class);
|
|
|
|
|
$phoneNumberUtil->parse('+32123456789')->willReturn($phoneNumber->reveal());
|
|
|
|
|
$phoneNumberUtil->parse('+32987654321')->willReturn($mobileNumber->reveal());
|
|
|
|
|
|
|
|
|
|
// Mock validator
|
|
|
|
|
$validator->validate(Argument::any())->willReturn(new ConstraintViolationList([]));
|
|
|
|
|
|
|
|
|
|
// Mock person identifier manager - create real worker with mocked engine
|
|
|
|
|
$identifierEngine = $this->prophesize(\Chill\PersonBundle\PersonIdentifier\PersonIdentifierEngineInterface::class);
|
|
|
|
|
$identifierEngine->canonicalizeValue(Argument::any(), Argument::any())->willReturn('123');
|
|
|
|
|
$worker = new \Chill\PersonBundle\PersonIdentifier\PersonIdentifierWorker(
|
|
|
|
|
$identifierEngine->reveal(),
|
|
|
|
|
$definition
|
|
|
|
|
);
|
|
|
|
|
$personIdentifierManager->buildWorkerByPersonIdentifierDefinition(Argument::any())->willReturn($worker);
|
|
|
|
|
|
|
|
|
|
$person = null;
|
|
|
|
|
$entityManager->persist(Argument::that(function ($arg) use (&$person) {
|
|
|
|
|
if ($arg instanceof Person) {
|
|
|
|
|
$person = $arg;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}))->shouldBeCalled();
|
|
|
|
|
$entityManager->persist(Argument::any())->shouldBeCalled();
|
|
|
|
|
$entityManager->flush()->shouldBeCalled();
|
|
|
|
|
$entityManager->clear()->shouldBeCalled();
|
|
|
|
|
|
|
|
|
|
$handler = new PersonUpsertHandler(
|
|
|
|
|
$personIdentifierDefinitionRepository->reveal(),
|
|
|
|
|
$identifierRepository->reveal(),
|
|
|
|
|
$entityManager->reveal(),
|
|
|
|
|
$membersEditorFactory,
|
|
|
|
|
$postalCodeRepository->reveal(),
|
|
|
|
|
$centerRepository->reveal(),
|
|
|
|
|
$genderRepository->reveal(),
|
|
|
|
|
$clock,
|
|
|
|
|
$phoneNumberUtil->reveal(),
|
|
|
|
|
$logger,
|
|
|
|
|
$personIdentifierManager->reveal(),
|
|
|
|
|
$validator->reveal(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$message = new UpsertMessage();
|
|
|
|
|
$message->externalId = '123';
|
|
|
|
|
$message->personIdentifierDefinitionId = 1;
|
|
|
|
|
$message->firstName = 'Jane';
|
|
|
|
|
$message->lastName = 'Smith';
|
|
|
|
|
$message->gender = 'female';
|
|
|
|
|
$message->birthdate = '1985-05-20';
|
|
|
|
|
$message->phoneNumber = '+32123456789';
|
|
|
|
|
$message->mobileNumber = '+32987654321';
|
|
|
|
|
$message->addressStreet = 'Main Street';
|
|
|
|
|
$message->addressStreetNumber = '42';
|
|
|
|
|
$message->addressPostcode = '1000';
|
|
|
|
|
$message->addressExtra = 'Apartment 3B';
|
|
|
|
|
$message->addressCity = 'Brussels';
|
|
|
|
|
$message->center = 'Main Center';
|
|
|
|
|
|
|
|
|
|
$handler->__invoke($message);
|
|
|
|
|
|
|
|
|
|
self::assertInstanceOf(Person::class, $person);
|
|
|
|
|
self::assertEquals('Jane', $person->getFirstName());
|
|
|
|
|
self::assertEquals('Smith', $person->getLastName());
|
|
|
|
|
self::assertEquals(new \DateTime('1985-05-20'), $person->getBirthdate());
|
|
|
|
|
self::assertSame($femaleGender, $person->getGender());
|
|
|
|
|
self::assertSame($center, $person->getCenter());
|
|
|
|
|
self::assertSame($phoneNumber->reveal(), $person->getPhonenumber());
|
|
|
|
|
self::assertSame($mobileNumber->reveal(), $person->getMobilenumber());
|
|
|
|
|
$household = $person->getCurrentHousehold($clock->now());
|
|
|
|
|
self::assertNotNull($household);
|
|
|
|
|
$address = $household->getCurrentAddress(\DateTime::createFromImmutable($clock->now()));
|
|
|
|
|
self::assertNotNull($address);
|
|
|
|
|
self::assertEquals('Main Street', $address->getStreet());
|
|
|
|
|
self::assertEquals('42', $address->getStreetNumber());
|
|
|
|
|
self::assertSame($postalCode, $address->getPostcode());
|
|
|
|
|
self::assertEquals('Apartment 3B', $address->getExtra());
|
|
|
|
|
}
|
|
|
|
|
}
|