Vincent преди 3 месеца
родител
ревизия
f0a7d4585a

+ 15 - 5
src/State/Processor/Freemium/FreemiumEventProcessor.php

@@ -33,11 +33,7 @@ class FreemiumEventProcessor extends EntityProcessor
      */
     public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): FreemiumEvent
     {
-        if ($operation instanceof Post) {
-            $event = new Event();
-        } else {
-            $event = $this->eventRepository->find($uriVariables['id']);
-        }
+        $event = $this->getEvent($operation, $uriVariables);
 
         if ($operation instanceof Delete) {
             $this->entityManager->remove($event);
@@ -56,4 +52,18 @@ class FreemiumEventProcessor extends EntityProcessor
 
         return $freemiumEvent;
     }
+
+    /**
+     * Retourne soit un nouvel Event en POST soit l'Event de base en PUT ou DELETE
+     * @param Operation $operation
+     * @param array $uriVariables
+     * @return Event
+     */
+    public function getEvent(Operation $operation, array $uriVariables = []): Event{
+        if ($operation instanceof Post) {
+            return new Event();
+        } else {
+            return $this->eventRepository->find($uriVariables['id']);
+        }
+    }
 }

+ 3 - 4
src/State/Provider/Freemium/FreemiumEventProvider.php

@@ -20,7 +20,7 @@ use Symfony\Component\ObjectMapper\ObjectMapperInterface;
 /**
  * Class AccessProfileProvider : custom provider pour assurer l'alimentation de la réponse du GET my_profile.
  */
-final class FreemiumEventProvider implements ProviderInterface
+class FreemiumEventProvider implements ProviderInterface
 {
     public function __construct(
         private ProviderUtils $providerUtils,
@@ -49,7 +49,7 @@ final class FreemiumEventProvider implements ProviderInterface
     /**
      * @param array<mixed> $context
      */
-    private function provideCollection(Operation $operation, array $context): TraversablePaginator
+    public function provideCollection(Operation $operation, array $context): TraversablePaginator
     {
         $this->filtersConfigurationService->suspendTimeConstraintFilters();
 
@@ -59,7 +59,6 @@ final class FreemiumEventProvider implements ProviderInterface
         foreach ($originalPaginator as $item) {
             $mappedItems[] = $this->objectMapper->map($item, FreemiumEvent::class);
         }
-
         $this->filtersConfigurationService->restoreTimeConstraintFilters();
 
         return $this->providerUtils->getTraversablePaginator($mappedItems, $originalPaginator);
@@ -72,7 +71,7 @@ final class FreemiumEventProvider implements ProviderInterface
      * @throws \Doctrine\ORM\Exception\ORMException
      * @throws \Doctrine\ORM\OptimisticLockException
      */
-    private function provideItem(array $uriVariables, array $context): FreemiumEvent
+    public function provideItem(array $uriVariables, array $context): FreemiumEvent
     {
         $this->filtersConfigurationService->suspendTimeConstraintFilters();
         /** @var Event $event */

+ 147 - 0
tests/Unit/State/Processor/Freemium/FreemiumEventProcessorTest.php

@@ -0,0 +1,147 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Tests\Unit\State\Processor\Freemium;
+
+use ApiPlatform\Metadata\Delete;
+use ApiPlatform\Metadata\Post;
+use ApiPlatform\Metadata\Put;
+use App\ApiResources\Freemium\FreemiumEvent;
+use App\Entity\Booking\Event;
+use App\Repository\Booking\EventRepository;
+use App\Service\ApiResourceBuilder\Freemium\EventMappingBuilder;
+use App\State\Processor\Freemium\FreemiumEventProcessor;
+use Doctrine\ORM\EntityManagerInterface;
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+
+/**
+ * Unit tests for ShopService.
+ */
+class FreemiumEventProcessorTest extends TestCase
+{
+    private readonly MockObject|EntityManagerInterface $entityManager;
+    private readonly MockObject|EventMappingBuilder $eventMappingBuilder;
+    private readonly MockObject|EventRepository $eventRepository;
+
+    public function setUp(): void
+    {
+        $this->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);
+    }
+}

+ 168 - 0
tests/Unit/State/Provider/FreemiumEventProviderTest.php

@@ -0,0 +1,168 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\tests\Unit\State\Provider;
+
+use ApiPlatform\Metadata\Get;
+use ApiPlatform\Metadata\GetCollection;
+use ApiPlatform\State\Pagination\TraversablePaginator;
+use App\ApiResources\Freemium\FreemiumEvent;
+use App\Entity\Booking\Event;
+use App\Entity\Core\Categories;
+use App\Repository\Booking\EventRepository;
+use App\Service\Doctrine\FiltersConfigurationService;
+use App\Service\State\Provider\ProviderUtils;
+use App\State\Provider\Freemium\FreemiumEventProvider;
+use Doctrine\Common\Collections\ArrayCollection;
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+use Symfony\Component\ObjectMapper\ObjectMapperInterface;
+
+/**
+ * Unit tests for ShopService.
+ */
+class FreemiumEventProviderTest extends TestCase
+{
+    private readonly MockObject|ProviderUtils $providerUtils;
+    private readonly MockObject|ObjectMapperInterface $objectMapper;
+    private readonly MockObject|EventRepository $eventRepository;
+    private readonly MockObject|FiltersConfigurationService $filtersConfigurationService;
+
+    public function setUp(): void
+    {
+        $this->providerUtils = $this->getMockBuilder(ProviderUtils::class)->disableOriginalConstructor()->getMock();
+        $this->objectMapper = $this->getMockBuilder(ObjectMapperInterface::class)->disableOriginalConstructor()->getMock();
+        $this->eventRepository = $this->getMockBuilder(EventRepository::class)->disableOriginalConstructor()->getMock();
+        $this->filtersConfigurationService = $this->getMockBuilder(FiltersConfigurationService::class)->disableOriginalConstructor()->getMock();
+    }
+
+    private function getFreemiumEventProviderMockFor(string $methodName): FreemiumEventProvider|MockObject
+    {
+        $freemiumEventProvider = $this
+            ->getMockBuilder(FreemiumEventProvider::class)
+            ->setConstructorArgs(
+                [
+                    $this->providerUtils,
+                    $this->objectMapper,
+                    $this->eventRepository,
+                    $this->filtersConfigurationService,
+                ]
+            )
+            ->setMethodsExcept([$methodName])
+            ->getMock();
+
+        return $freemiumEventProvider;
+    }
+
+    /**
+     * Test provide method for GetCollection operation
+     */
+    public function testProvideGetCollectionMethod(): void
+    {
+        $freemiumEventProvide = $this->getFreemiumEventProviderMockFor('provide');
+
+        $operation = new GetCollection();
+
+        $freemiumEventProvide->expects(self::once())
+            ->method('provideCollection')
+            ->with($operation, [])
+            ->willReturn(new TraversablePaginator(new \ArrayIterator([]), 0, 10, 0))
+        ;
+
+        $freemiumEventProvide->provide($operation);
+    }
+
+    /**
+     * Test provide method for Get operation
+     */
+    public function testProvideGetMethod(): void
+    {
+        $freemiumEventProvide = $this->getFreemiumEventProviderMockFor('provide');
+        $freemiumEvent= $this->getMockBuilder(FreemiumEvent::class)->getMock();
+
+        $operation = new Get();
+
+        $freemiumEventProvide->expects(self::once())
+            ->method('provideItem')
+            ->with([], [])
+            ->willReturn($freemiumEvent)
+        ;
+
+        $freemiumEventProvide->provide($operation);
+    }
+
+    /**
+     * Test provideCollection method
+     */
+    public function testProvideCollectionMethod(): void
+    {
+        $freemiumEventProvide = $this->getFreemiumEventProviderMockFor('provideCollection');
+        $freemiumEvent= $this->getMockBuilder(FreemiumEvent::class)->getMock();
+
+        $operation = new GetCollection();
+
+        $this->filtersConfigurationService->expects(self::once())
+            ->method('suspendTimeConstraintFilters');
+
+        $traversable = new TraversablePaginator(new \ArrayIterator([$freemiumEvent, $freemiumEvent]), 0, 10, 2);
+        $this->providerUtils->expects(self::once())
+            ->method('applyCollectionExtensionsAndPagination')
+            ->with(Event::class, $operation, [])
+            ->willReturn($traversable);
+
+        $this->objectMapper->expects(self::exactly(2))
+            ->method('map')
+            ->willReturnOnConsecutiveCalls($freemiumEvent, $freemiumEvent);
+
+        $this->filtersConfigurationService->expects(self::once())
+            ->method('restoreTimeConstraintFilters');
+
+        $this->providerUtils->expects(self::once())
+            ->method('getTraversablePaginator')
+            ->with([$freemiumEvent, $freemiumEvent], $traversable)
+            ->willReturn($traversable)
+        ;
+
+        $freemiumEventProvide->provideCollection($operation, []);
+    }
+
+    /**
+     * Test provideItem method
+     */
+    public function testProvideItemMethod(): void
+    {
+        $uriVariables = ['id' => 1];
+        $freemiumEventProvide = $this->getFreemiumEventProviderMockFor('provideItem');
+
+        $event= $this->getMockBuilder(Event::class)->getMock();
+        $category= $this->getMockBuilder(Categories::class)->getMock();
+        $categories = new ArrayCollection([$category, $category]);
+        $event->method('getCategories')->willReturn($categories);
+
+        $this->filtersConfigurationService->expects(self::once())
+            ->method('suspendTimeConstraintFilters');
+
+        $this->eventRepository->expects(self::once())
+            ->method('find')
+            ->with(1)
+            ->willReturn($event)
+        ;
+
+        $this->filtersConfigurationService->expects(self::once())
+            ->method('restoreTimeConstraintFilters');
+
+        $freemiumEvent= new FreemiumEvent();
+        $this->objectMapper->expects(self::once())
+            ->method('map')
+            ->willReturn($freemiumEvent);
+
+        $event->expects(self::once())
+            ->method('getCategories')
+            ->willReturn($categories)
+        ;
+
+        $freemiumEventProvide->provideItem($uriVariables, []);
+        $this->assertEquals(2, count($freemiumEvent->getCategories()));
+    }
+}