From 837bc0250cc6dbc17bef45278be8f310ae21731d Mon Sep 17 00:00:00 2001 From: Boris Waaub Date: Thu, 26 Feb 2026 11:43:01 +0100 Subject: [PATCH] use ProphecyTrait --- .../Handler/PersonUpsertHandlerTest.php | 67 ++++++++++--------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Tests/Action/Upsert/Handler/PersonUpsertHandlerTest.php b/src/Bundle/ChillPersonBundle/Tests/Action/Upsert/Handler/PersonUpsertHandlerTest.php index 2ec25e819..965b87f82 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Action/Upsert/Handler/PersonUpsertHandlerTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Action/Upsert/Handler/PersonUpsertHandlerTest.php @@ -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); } + }