entityManager = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock(); $this->eventMappingBuilder = $this->getMockBuilder(EventMappingBuilder::class)->disableOriginalConstructor()->getMock(); $this->eventRepository = $this->getMockBuilder(EventRepository::class)->disableOriginalConstructor()->getMock(); } private function getFreemiumEventProcessorMockFor(string $methodName): FreemiumEventProcessor|MockObject { $freemiumEventProcessor = $this ->getMockBuilder(FreemiumEventProcessor::class) ->setConstructorArgs( [ $this->entityManager, $this->eventMappingBuilder, $this->eventRepository, ] ) ->setMethodsExcept([$methodName]) ->getMock(); return $freemiumEventProcessor; } /** * Test process method for POST operation. */ public function testProcessWithPostMethod(): void { $freemiumEventProcessor = $this->getFreemiumEventProcessorMockFor('process'); $data = $this->getMockBuilder(FreemiumEvent::class)->getMock(); $event = $this->getMockBuilder(Event::class)->getMock(); $event->method('getId')->willReturn(1); $operation = new Post(); $freemiumEventProcessor->expects(self::once()) ->method('getEvent') ->with($operation) ->willReturn($event); $this->eventMappingBuilder->expects(self::once()) ->method('mapInformations') ->with($event, $data); $this->entityManager->expects(self::once()) ->method('persist') ->with($event); $this->entityManager->expects(self::once()) ->method('flush') ; $freemiumEvent = $freemiumEventProcessor->process($data, $operation); $this->assertEquals(1, $freemiumEvent->id); } /** * Test process method for PUT operation. */ public function testProcessWithPutMethod(): void { $freemiumEventProcessor = $this->getFreemiumEventProcessorMockFor('process'); $data = $this->getMockBuilder(FreemiumEvent::class)->getMock(); $event = $this->getMockBuilder(Event::class)->getMock(); $data->id = 1; $operation = new Put(); $freemiumEventProcessor->expects(self::once()) ->method('getEvent') ->with($operation) ->willReturn($event); $this->eventMappingBuilder->expects(self::once()) ->method('mapInformations') ->with($event, $data); $this->entityManager->expects(self::once()) ->method('persist') ->with($event); $this->entityManager->expects(self::once()) ->method('flush') ; $freemiumEvent = $freemiumEventProcessor->process($data, $operation); $this->assertEquals(1, $freemiumEvent->id); } /** * Test process method for DELETE operation. */ public function testProcessWithDeleteMethod(): void { $freemiumEventProcessor = $this->getFreemiumEventProcessorMockFor('process'); $freemiumEvent = $this->getMockBuilder(FreemiumEvent::class)->getMock(); $event = $this->getMockBuilder(Event::class)->getMock(); $operation = new Delete(); $freemiumEventProcessor->expects(self::once()) ->method('getEvent') ->with($operation) ->willReturn($event); $this->entityManager->expects(self::once()) ->method('remove') ->with($event); $this->entityManager->expects(self::once()) ->method('flush') ; $freemiumEventProcessor->process($freemiumEvent, $operation); } }