Просмотр исходного кода

add unit tests for CotisationCreator and Security/Module

Olivier Massot 3 лет назад
Родитель
Сommit
8c04041ff0

+ 3 - 3
src/Service/Security/Module.php

@@ -20,7 +20,7 @@ class Module
     private array $moduleConfig;
     private array $moduleByConditionsConfig;
 
-    public function __construct(private Reflection $reflection, private Parser $parser,  private string $opentalentConfig)
+    public function __construct(private Reflection $reflection, private Parser $parser, private string $opentalentConfig)
     {
         $this->moduleConfig = $this->getModuleConfig();
         $this->moduleByConditionsConfig = $this->getModuleByConditionsConfig();
@@ -28,7 +28,7 @@ class Module
 
     /**
      * @todo activer le cache après que la fin de la migration.
-     * Récupère tous les modules de l'oganisation
+     * Récupère tous les modules de l'organisation
      * @param Organization $organization
      * @return array
      */
@@ -153,4 +153,4 @@ class Module
         }
         return null;
     }
-}
+}

+ 37 - 0
tests/Service/Cotisation/CotisationCreatorTest.php

@@ -0,0 +1,37 @@
+<?php
+
+namespace App\Tests\Service\Cotisation;
+
+use App\Entity\Organization\Organization;
+use App\Repository\Organization\OrganizationRepository;
+use App\Service\Cotisation\CotisationCreator;
+use App\Service\Cotisation\Utils;
+use PHPUnit\Framework\TestCase;
+
+class CotisationCreatorTest extends TestCase
+{
+    private OrganizationRepository $organizationRepository;
+    private Utils $cotisationUtils;
+    private CotisationCreator $cotisationCreator;
+
+    public function setUp():void
+    {
+        $this->organizationRepository = $this->getMockBuilder(OrganizationRepository::class)->disableOriginalConstructor()->getMock();
+        $this->cotisationUtils = $this->getMockBuilder(Utils::class)->disableOriginalConstructor()->getMock();
+
+        $this->cotisationCreator = new CotisationCreator(
+            $this->organizationRepository,
+            $this->cotisationUtils
+        );
+    }
+
+    public function testGetCotisation() {
+        $this->cotisationUtils->expects(self::once())->method('getCurrentCotisationYear')->willReturn(2000);
+        $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
+        $this->organizationRepository->expects(self::once())->method('find')->willReturn($organization);
+        $this->cotisationUtils->expects(self::once())->method('getAlertState')->with($organization, 2000);
+        $cotisation = $this->cotisationCreator->getCotisation(1);
+
+        $this->assertEquals(1, $cotisation->getOrganizationId());
+    }
+}

+ 26 - 2
tests/Service/Security/ModuleTest.php

@@ -5,6 +5,7 @@ use App\Entity\Organization\Organization;
 use App\Entity\Organization\Settings;
 use App\Service\Utils\Parser;
 use App\Service\Utils\Reflection;
+use Doctrine\Common\Collections\ArrayCollection;
 use PHPUnit\Framework\TestCase;
 use App\Service\Security\Module;
 
@@ -18,7 +19,30 @@ class ModuleTest extends TestCase
     public function setUp():void
     {
         $this->reflectionMock = $this->getMockBuilder(Reflection::class)->disableOriginalConstructor()->getMock();
-        $this->parser= new Parser();
+        $this->parser = new Parser();
+    }
+
+    public function testGetOrganizationModules() {
+        $module = $this->getMockBuilder(Module::class)
+            ->onlyMethods(['getModuleBySettings', 'getModulesByConditions', 'getModulesByProductConfiguration'])
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $module->expects(self::once())->method('getModuleBySettings')->willReturn(['Sms']);
+        $module->expects(self::once())->method('getModulesByConditions')->willReturn(['CotisationCall']);
+
+        $settings = $this->getMockBuilder(Settings::class)->disableOriginalConstructor()->getMock();
+        $settings->expects(self::once())->method('getProduct')->willReturn('school');
+
+        $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
+        $organization->expects(self::once())->method('getSettings')->willReturn($settings);
+
+        $module->expects(self::once())->method('getModulesByProductConfiguration')->willReturn(['Notification']);
+
+        $this->assertEqualsCanonicalizing(
+            ['Sms', 'CotisationCall', 'Notification'],
+            $module->getOrganizationModules($organization)
+        );
     }
 
     /**
@@ -76,4 +100,4 @@ class ModuleTest extends TestCase
         // assert function to test whether 'value' is a value of array
         $this->assertContains($value, $module->getModulesByProductConfiguration('artist-premium')) ;
     }
-}
+}