Vincent GUFFON 4 年 前
コミット
ce1bf52bb4

+ 2 - 0
src/Filter/DoctrineFilter/DateTimeFilter.php

@@ -6,6 +6,7 @@ namespace App\Filter\DoctrineFilter;
 use App\Annotation\DateTimeConstraintAware;
 use App\Service\Utils\DateTimeConstraint;
 use App\Service\Utils\StringsUtils;
+use App\Tests\Filter\DoctrineFilter\DateTimeFilterTest;
 use Doctrine\ORM\Mapping\ClassMetadata;
 use Doctrine\ORM\Query\Filter\SQLFilter;
 
@@ -79,6 +80,7 @@ final class DateTimeFilter extends SQLFilter
      * Fonction retournant la valeur arithmétique correspondant à la condition de la contrainte
      * @param $condition
      * @return string|null
+     * @see DateTimeFilterTest::testGetArithmeticValue()
      */
     private function getArithmeticValue($condition): string|null{
         switch ($condition){

+ 12 - 2
src/Service/Utils/DateTimeConstraint.php

@@ -5,6 +5,7 @@ namespace App\Service\Utils;
 
 use App\Entity\Access\Access;
 use App\Service\Organization\Utils as organizationUtils;
+use App\Tests\Service\Utils\DateTimeConstraintTest;
 use Doctrine\ORM\EntityManagerInterface;
 
 /**
@@ -61,6 +62,7 @@ class DateTimeConstraint
      * Retourne true si l'utilisateur veux une période précise
      * @param $historical
      * @return bool
+     * @see DateTimeConstraintTest::testHasCustomPeriods()
      */
     private function hasCustomPeriods($historical): bool{
         return array_key_exists('dateStart', $historical) && $historical['dateStart'] && array_key_exists('dateEnd', $historical) && $historical['dateEnd'];
@@ -84,6 +86,7 @@ class DateTimeConstraint
      * @param Access $access
      * @return array
      * @throws \Exception
+     * @see DateTimeConstraintTest::testGetPeriodsToday()
      */
     private function getPeriods(Access $access): array{
         $organization = $access->getOrganization();
@@ -105,6 +108,7 @@ class DateTimeConstraint
      * @param array $contraints
      * @param array $newContraint
      * @return array
+     * @see DateTimeConstraintTest::testAddConstraint()
      */
     private function addConstraint(array $contraints, array $newContraint): array{
         $contraints = $this->mergeConstraint($contraints,$newContraint,self::START_KEY);
@@ -140,6 +144,7 @@ class DateTimeConstraint
      * Nettoyage des contraintes (toutes celles supérieur à la condition cancel et les valeurs null isolées)
      * @param array $constraints
      * @return array
+     * @see DateTimeConstraintTest::testCleanConstraints()
      */
     private function cleanConstraints(array $constraints): array{
         $constraints[self::START_KEY] = $this->filterConstraint($constraints, self::START_KEY);
@@ -156,6 +161,7 @@ class DateTimeConstraint
      * @param array $constraints
      * @param $key
      * @return array
+     * @see DateTimeConstraintTest::testFilterConstraint()
      */
     private function filterConstraint(array $constraints, string $key): array{
         return array_filter($constraints[$key], function($constraint){
@@ -169,6 +175,7 @@ class DateTimeConstraint
      * @param array $constraints
      * @param $key
      * @return array
+     * @see DateTimeConstraintTest::testClearNull()
      */
     private function clearNull(array $constraints, string $key): array{
         if(count($constraints[$key]) === 1 && array_key_exists(self::NULL_VALUE, $constraints[$key]))
@@ -183,6 +190,7 @@ class DateTimeConstraint
      *  - la date de fin est plus grande ou égale (>=) à la date de fin de période à afficher OU que la date de fin n'est pas remplie (NULL)
      * @param $periods
      * @return array
+     * @see DateTimeConstraintTest::testPresentConstrain()
      */
     private function presentConstraint(array $periods): array{
         return [
@@ -200,7 +208,8 @@ class DateTimeConstraint
      * Une période est dans le passée si :
      * - la date de fin est plus petite (<) que la date de début de période à afficher
      * @param $periods
-     * @return \int[][]
+     * @return array
+     * @see DateTimeConstraintTest::testPastConstrain()
      */
     private function pastConstraint($periods): array{
         return [
@@ -214,7 +223,8 @@ class DateTimeConstraint
      * Une période est dans le future si :
      * - la date de début est plus grande (>) que la date de fin de période à afficher
      * @param $periods
-     * @return \int[][]
+     * @return array
+     * @see DateTimeConstraintTest::testFuturConstrain()
      */
     private function futurConstraint($periods): array{
         return [

+ 3 - 0
src/Service/Utils/StringsUtils.php

@@ -3,6 +3,8 @@ declare(strict_types=1);
 
 namespace App\Service\Utils;
 
+use App\Tests\Service\Utils\StringsUtilsTest;
+
 /**
  * Class StringsUtils : méthodes d'aide pour la gestion de string.
  * @package App\Service\Utils
@@ -13,6 +15,7 @@ class StringsUtils
      * Supprime les quotes d'une chaine de caractères
      * @param string $str
      * @return string
+     * @see StringsUtilsTest::testUnquote()
      */
     public static function unquote(string $str): string {
         return str_replace("'", "", $str);

+ 76 - 0
tests/Filter/DoctrineFilter/DateTimeFilterTest.php

@@ -0,0 +1,76 @@
+<?php
+namespace App\Tests\Filter\DoctrineFilter;
+
+use App\Filter\DoctrineFilter\DateTimeFilter;
+use App\Tests\TestToolsTrait;
+use Doctrine\ORM\EntityManagerInterface;
+use PHPUnit\Framework\TestCase;
+
+
+class DateTimeFilterTest extends TestCase
+{
+    use TestToolsTrait;
+
+    private DateTimeFilter $dateTimeFilter;
+
+    public function setUp(): void
+    {
+        $em = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
+        $this->dateTimeFilter = new DateTimeFilter($em);
+    }
+
+    /**
+     * @see DateTimeFilter::constructQuery()
+     */
+    public function testConstructQuery():void
+    {
+        $queryExpected = "(o.startDate <= '2021-12-20') AND (o.endDate > '2021-12-20' OR o.endDate IS NULL)";
+        $this->assertEquals($queryExpected, $this->invokeMethod($this->dateTimeFilter, 'constructQuery', [
+            [
+                'start' => [
+                    '2021-12-20' => [4]
+                ],
+                'end' => [
+                    '2021-12-20' => [5],
+                    'NULL' => [0]
+                ]
+            ],
+            'o',
+            [
+                'start' => 'startDate',
+                'end' => 'endDate'
+            ]
+        ]));
+
+        $queryExpected = "(o.startDate < '2021-12-20' OR o.startDate > '2021-12-30') AND (o.endDate < '2022-01-20')";
+        $this->assertEquals($queryExpected, $this->invokeMethod($this->dateTimeFilter, 'constructQuery', [
+            [
+                'start' => [
+                    '2021-12-20' => [1],
+                    '2021-12-30' => [5]
+                ],
+                'end' => [
+                    '2022-01-20' => [1]
+                ]
+            ],
+            'o',
+            [
+                'start' => 'startDate',
+                'end' => 'endDate'
+            ]
+        ]));
+    }
+
+    /**
+     * @see DateTimeFilter::getArithmeticValue()
+     */
+    public function testGetArithmeticValue():void
+    {
+        $this->assertEquals('<', $this->invokeMethod($this->dateTimeFilter, 'getArithmeticValue', [1]));
+        $this->assertEquals('<=', $this->invokeMethod($this->dateTimeFilter, 'getArithmeticValue', [4]));
+        $this->assertEquals('=', $this->invokeMethod($this->dateTimeFilter, 'getArithmeticValue', [3]));
+        $this->assertEquals('>=', $this->invokeMethod($this->dateTimeFilter, 'getArithmeticValue', [8]));
+        $this->assertEquals('>', $this->invokeMethod($this->dateTimeFilter, 'getArithmeticValue', [5]));
+        $this->assertEquals(null, $this->invokeMethod($this->dateTimeFilter, 'getArithmeticValue', [0]));
+    }
+}

+ 212 - 0
tests/Service/Utils/DateTimeConstraintTest.php

@@ -0,0 +1,212 @@
+<?php
+namespace App\Tests\Service\Utils;
+
+use App\Entity\Access\Access;
+use App\Entity\Organization\Organization;
+use App\Service\Utils\DateTimeConstraint;
+use App\Tests\TestToolsTrait;
+use Doctrine\ORM\EntityManagerInterface;
+use PHPUnit\Framework\TestCase;
+
+class DateTimeConstraintTest extends TestCase
+{
+   use TestToolsTrait;
+
+   private DateTimeConstraint $dateTimeConstraint;
+   private array $periods;
+
+   public function setUp(): void
+   {
+       $em = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
+       $this->dateTimeConstraint = new DateTimeConstraint($em);
+
+       $this->periods = [
+           'dateStart' => '2021-12-20',
+           'dateEnd' => '2022-08-31'
+       ];
+   }
+
+   /**
+    * @see DateTimeConstraint::presentConstraint()
+    */
+   public function testPresentConstrain(){
+       $constraintExpected = [
+           'start' => [
+               '2022-08-31' => 4
+           ],
+           'end' => [
+               '2021-12-20' => 8,
+               'NULL' => 0
+           ]
+       ];
+       $this->assertEquals($constraintExpected, $this->invokeMethod($this->dateTimeConstraint, 'presentConstraint', [$this->periods]));
+   }
+
+    /**
+     * @see DateTimeConstraint::pastConstraint()
+     */
+    public function testPastConstrain(){
+        $constraintExpected = [
+            'end' => [
+                '2021-12-20' => 1
+            ]
+        ];
+        $this->assertEquals($constraintExpected, $this->invokeMethod($this->dateTimeConstraint, 'pastConstraint', [$this->periods]));
+    }
+
+    /**
+     * @see DateTimeConstraint::futurConstraint()
+     */
+    public function testFuturConstrain(){
+        $constraintExpected = [
+            'start' => [
+                '2022-08-31' => 5
+            ]
+        ];
+        $this->assertEquals($constraintExpected, $this->invokeMethod($this->dateTimeConstraint, 'futurConstraint', [$this->periods]));
+    }
+
+    /**
+     * @see DateTimeConstraint::clearNull()
+     */
+    public function testClearNull(){
+        $originalEndConstraint= [
+            'end' => [
+                'NULL' => 0
+            ]
+        ];
+        $endConstraintExpected = [];
+        $this->assertEquals($endConstraintExpected, $this->invokeMethod($this->dateTimeConstraint, 'clearNull', [$originalEndConstraint, 'end']));
+    }
+
+    /**
+     * @see DateTimeConstraint::filterConstraint()
+     */
+    public function testFilterConstraint(){
+        $originalStartConstraint= [
+            'start' => [
+                '2021-12-20' => [8, 1],
+                '2022-01-01' => [5]
+            ]
+        ];
+        $startConstraintExpected = [
+            '2022-01-01' => [5]
+        ];
+        $this->assertEquals($startConstraintExpected, $this->invokeMethod($this->dateTimeConstraint, 'filterConstraint', [$originalStartConstraint, 'start']));
+    }
+
+    /**
+     * @see DateTimeConstraint::cleanConstraints()
+     */
+    public function testCleanConstraints(){
+        $originalConstraint= [
+            'start' => [
+                '2022-08-31' => [5]
+            ],
+            'end' => [
+                '2021-12-20' => [8, 1],
+                'NULL' => [0]
+            ]
+        ];
+        $constraintExpected= [
+            'start' => [
+                '2022-08-31' => [5]
+            ],
+            'end' => []
+        ];
+        $this->assertEquals($constraintExpected, $this->invokeMethod($this->dateTimeConstraint, 'cleanConstraints', [$originalConstraint]));
+    }
+
+    /**
+     * @see DateTimeConstraint::addConstraint()
+     */
+    public function testAddConstraint(){
+        $originalConstraint = [
+            'start' => [],
+            'end' => []
+        ];
+        $presentConstraint = [
+            'start' => [
+                '2022-08-31' => 4
+            ],
+            'end' => [
+                '2021-12-20' => 8,
+                'NULL' => 0
+            ]
+        ];
+        $pastConstraint = [
+            'end' => [
+                '2021-12-20' => 1
+            ]
+        ];
+        $constraintAfterStartExpected = [
+            'start' => [
+                '2022-08-31' => [4]
+            ],
+            'end' => [
+                '2021-12-20' => [8],
+                'NULL' => [0]
+            ]
+        ];
+        $constraintAfterEndExpected = [
+            'start' => [
+                '2022-08-31' => [4]
+            ],
+            'end' => [
+                '2021-12-20' => [8, 1],
+                'NULL' => [0]
+            ]
+        ];
+        $this->assertEquals($constraintAfterStartExpected, $this->invokeMethod($this->dateTimeConstraint, 'addConstraint', [$originalConstraint, $presentConstraint]));
+        $this->assertEquals($constraintAfterEndExpected, $this->invokeMethod($this->dateTimeConstraint, 'addConstraint', [$constraintAfterStartExpected, $pastConstraint]));
+    }
+
+    /**
+     * @throws \ReflectionException
+     * @see DateTimeConstraint::getPeriods()
+     */
+    public function testGetPeriodsToday(){
+        $today = new \DateTime('now');
+
+        $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
+
+        $access = $this->getMockBuilder(Access::class)->disableOriginalConstructor()->getMock();
+        $access->method('getOrganization')->willReturn($organization);
+        $access->method('getActivityYear')->willReturn(intval($today->format('Y')));
+
+        $periodExpected = ['dateStart' => $today->format('Y-m-d'), 'dateEnd' => '2022-08-31'];
+        $this->assertEquals($periodExpected, $this->invokeMethod($this->dateTimeConstraint, 'getPeriods', [$access]));
+    }
+
+    /**
+     * @throws \ReflectionException
+     * @see DateTimeConstraint::getPeriods()
+     */
+    public function testGetPeriodsNotToday(){
+        $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
+
+        $access = $this->getMockBuilder(Access::class)->disableOriginalConstructor()->getMock();
+        $access->method('getOrganization')->willReturn($organization);
+        $access->method('getActivityYear')->willReturn(2020);
+        $periodExpected = ['dateStart' => '2020-09-01', 'dateEnd' => '2021-08-31'];
+        $this->assertEquals($periodExpected, $this->invokeMethod($this->dateTimeConstraint, 'getPeriods', [$access]));
+    }
+
+    /**
+     * @throws \ReflectionException
+     * @see DateTimeConstraint::hasCustomPeriods()
+     */
+    public function testHasCustomPeriods(){
+        $historical = ['dateStart' => '2020-09-01', 'dateEnd' => '2021-08-31'];
+        $this->assertTrue($this->invokeMethod($this->dateTimeConstraint, 'hasCustomPeriods', [$historical]));
+    }
+
+    /**
+     * @throws \ReflectionException
+     * @see DateTimeConstraint::hasCustomPeriods()
+     */
+    public function testHasNotCustomPeriods(){
+        $historical = ['dateStart' => null, 'dateEnd' => null];
+        $this->assertFalse($this->invokeMethod($this->dateTimeConstraint, 'hasCustomPeriods', [$historical]));
+    }
+}

+ 16 - 0
tests/Service/Utils/StringsUtilsTest.php

@@ -0,0 +1,16 @@
+<?php
+namespace App\Tests\Service\Utils;
+
+use App\Service\Utils\StringsUtils;
+use PHPUnit\Framework\TestCase;
+
+class StringsUtilsTest extends TestCase
+{
+    /**
+     * @see StringsUtils::unquote()
+     */
+    public function testUnquote():void
+    {
+        $this->assertEquals("foo", StringsUtils::unquote("'foo"));
+    }
+}

+ 27 - 0
tests/TestToolsTrait.php

@@ -0,0 +1,27 @@
+<?php
+
+namespace App\Tests;
+
+/**
+ * Classe de base pour les tests unitaires nécessitant
+ * un accès au kernel et à la base de données de test
+ *
+ */
+trait TestToolsTrait
+{
+   /**
+     * Exécute une méthode quelque soit son niveau de visibilité
+     * @param $object
+     * @param $methodName
+     * @param array $parameters
+     * @return mixed
+     * @throws \ReflectionException
+     */
+    protected function invokeMethod(&$object, $methodName, array $parameters = array())
+    {
+        $reflection = new \ReflectionClass(get_class($object));
+        $method = $reflection->getMethod($methodName);
+        $method->setAccessible(true);
+        return $method->invokeArgs($object, $parameters);
+    }
+}