use ProphecyTrait

This commit is contained in:
Boris Waaub
2026-02-26 11:43:01 +01:00
parent 3fcf8c3c17
commit 837bc0250c

View File

@@ -17,6 +17,9 @@ use Chill\PersonBundle\Entity\Identifier\PersonIdentifier;
use Chill\PersonBundle\Actions\Upsert\UpsertMessage;
use PHPUnit\Framework\TestCase;
use Doctrine\ORM\EntityManagerInterface;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
/**
* @internal
@@ -25,27 +28,26 @@ use Doctrine\ORM\EntityManagerInterface;
*/
class PersonUpsertHandlerTest extends TestCase
{
use ProphecyTrait;
public function testInvokeCreatesNewPersonAndIdentifier(): void
{
$personIdentifierDefinitionRepository = $this->createMock(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository::class);
$definitionRepository = $this->createMock(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository::class);
$identifierRepository = $this->createMock(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierRepository::class);
$entityManager = $this->createMock(EntityManagerInterface::class);
$personIdentifierDefinitionRepository = $this->prophesize(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository::class);
$identifierRepository = $this->prophesize(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierRepository::class);
$entityManager = $this->prophesize(EntityManagerInterface::class);
$definition = $this->createMock(\Chill\PersonBundle\Entity\Identifier\PersonIdentifierDefinition::class);
$definitionRepository->method('find')->willReturn($definition);
$identifierRepository->method('findByDefinitionAndCanonical')->willReturn([]);
$definition = $this->prophesize(\Chill\PersonBundle\Entity\Identifier\PersonIdentifierDefinition::class);
$personIdentifierDefinitionRepository->find(1)->willReturn($definition->reveal());
$identifierRepository->findByDefinitionAndCanonical($definition->reveal(), '123')->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');
$entityManager->persist(Argument::type(Person::class))->shouldBeCalled();
$entityManager->persist(Argument::type(PersonIdentifier::class))->shouldBeCalled();
$entityManager->flush()->shouldBeCalled();
$handler = new PersonUpsertHandler(
$personIdentifierDefinitionRepository,
$identifierRepository,
$entityManager
$personIdentifierDefinitionRepository->reveal(),
$identifierRepository->reveal(),
$entityManager->reveal()
);
$message = new UpsertMessage();
@@ -55,34 +57,34 @@ class PersonUpsertHandlerTest extends TestCase
$message->lastName = 'Doe';
$handler->__invoke($message);
$this->assertTrue(true);
}
public function testInvokeUpdatesExistingPerson(): void
{
$personIdentifierDefinitionRepository = $this->createMock(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository::class);
$definitionRepository = $this->createMock(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository::class);
$identifierRepository = $this->createMock(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierRepository::class);
$entityManager = $this->createMock(EntityManagerInterface::class);
$personIdentifierDefinitionRepository = $this->prophesize(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierDefinitionRepository::class);
$identifierRepository = $this->prophesize(\Chill\PersonBundle\Repository\Identifier\PersonIdentifierRepository::class);
$entityManager = $this->prophesize(EntityManagerInterface::class);
$definition = $this->createMock(\Chill\PersonBundle\Entity\Identifier\PersonIdentifierDefinition::class);
$definitionRepository->method('find')->willReturn($definition);
$definition = $this->prophesize(\Chill\PersonBundle\Entity\Identifier\PersonIdentifierDefinition::class);
$personIdentifierDefinitionRepository->find(2)->willReturn($definition->reveal());
$person = $this->createMock(Person::class);
$person->expects($this->once())->method('setFirstName')->with('Jane');
$person->expects($this->once())->method('setLastName')->with('Smith');
$person = $this->prophesize(Person::class);
$person->setFirstName('Jane')->shouldBeCalled();
$person->setLastName('Smith')->shouldBeCalled();
$person->getFirstName()->willReturn('Jane');
$person->getLastName()->willReturn('Smith');
$identifier = $this->createMock(PersonIdentifier::class);
$identifier->method('getPerson')->willReturn($person);
$identifier = $this->prophesize(PersonIdentifier::class);
$identifier->getPerson()->willReturn($person->reveal());
$identifierRepository->method('findByDefinitionAndCanonical')->willReturn([$identifier]);
$identifierRepository->findByDefinitionAndCanonical($definition->reveal(), '456')->willReturn([$identifier->reveal()]);
$entityManager->expects($this->once())->method('flush');
$entityManager->flush()->shouldBeCalled();
$handler = new PersonUpsertHandler(
$personIdentifierDefinitionRepository,
$identifierRepository,
$entityManager
$personIdentifierDefinitionRepository->reveal(),
$identifierRepository->reveal(),
$entityManager->reveal()
);
$message = new UpsertMessage();
@@ -93,4 +95,5 @@ class PersonUpsertHandlerTest extends TestCase
$handler->__invoke($message);
}
}