Jelajahi Sumber

complete unit tests

Olivier Massot 1 tahun lalu
induk
melakukan
c1315c5a58

+ 28 - 20
src/Service/Dolibarr/DolibarrSyncService.php

@@ -319,26 +319,7 @@ class DolibarrSyncService
 
             // ===== Update Tags =====
             $currentTags = $this->dolibarrApiService->getSocietyTagsIds($dolibarrSocietyId);
-            $expectedTags = [];
-
-            // Association, school or school premium
-            if ($organization->getLegalStatus() === LegalEnum::ASSOCIATION_LAW_1901()->getValue()) {
-                if ($product === SettingsProductEnum::SCHOOL()->getValue()) {
-                    $expectedTags[] = self::ASSOCIATION_SCHOOL_TAG_ID;
-                }
-                if ($product === SettingsProductEnum::SCHOOL_PREMIUM()->getValue()) {
-                    $expectedTags[] = self::ASSOCIATION_PREMIUM_TAG_ID;
-                }
-            }
-            // Local authorities, school or school premium
-            if ($organization->getLegalStatus() === LegalEnum::LOCAL_AUTHORITY()->getValue()) {
-                if ($product === SettingsProductEnum::SCHOOL()->getValue()) {
-                    $expectedTags[] = self::LOCAL_AUTH_SCHOOL_TAG_ID;
-                }
-                if ($product === SettingsProductEnum::SCHOOL_PREMIUM()->getValue()) {
-                    $expectedTags[] = self::LOCAL_AUTH_PREMIUM_TAG_ID;
-                }
-            }
+            $expectedTags = $this->getExpectedTagsFor($organization);
 
             // Remove unexpected tags
             foreach ($currentTags as $tagId) {
@@ -793,6 +774,33 @@ class DolibarrSyncService
         );
     }
 
+    protected function getExpectedTagsFor(Organization $organization)
+    {
+        $expectedTags = [];
+        $product = $organization->getSettings()->getProduct();
+
+        // Association, school or school premium
+        if ($organization->getLegalStatus() === LegalEnum::ASSOCIATION_LAW_1901()->getValue()) {
+            if ($product === SettingsProductEnum::SCHOOL()->getValue()) {
+                $expectedTags[] = self::ASSOCIATION_SCHOOL_TAG_ID;
+            }
+            if ($product === SettingsProductEnum::SCHOOL_PREMIUM()->getValue()) {
+                $expectedTags[] = self::ASSOCIATION_PREMIUM_TAG_ID;
+            }
+        }
+        // Local authorities, school or school premium
+        if ($organization->getLegalStatus() === LegalEnum::LOCAL_AUTHORITY()->getValue()) {
+            if ($product === SettingsProductEnum::SCHOOL()->getValue()) {
+                $expectedTags[] = self::LOCAL_AUTH_SCHOOL_TAG_ID;
+            }
+            if ($product === SettingsProductEnum::SCHOOL_PREMIUM()->getValue()) {
+                $expectedTags[] = self::LOCAL_AUTH_PREMIUM_TAG_ID;
+            }
+        }
+
+        return $expectedTags;
+    }
+
 
     /**
      * Post-validation of the execution of the operation.

+ 115 - 0
tests/Unit/Service/Dolibarr/DolibarrApiServiceTest.php

@@ -40,6 +40,50 @@ class DolibarrApiServiceTest extends TestCase
         $this->assertEquals(['id' => 1], $society);
     }
 
+    /**
+     * @see DolibarrApiService::getSociety()
+     */
+    public function testGetSocietyMissing(): void {
+        $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
+            ->setConstructorArgs([$this->client])
+            ->setMethodsExcept(['getSociety'])
+            ->getMock();
+
+        $organizationId = 123;
+
+        $dolibarrApiService
+            ->expects(self::once())
+            ->method('getJsonContent')
+            ->with("thirdparties", [ "limit" => "1", "sqlfilters" => "ref_int=" . $organizationId])
+            ->willThrowException(new HttpException(404));
+
+        $society = $dolibarrApiService->getSociety($organizationId);
+
+        $this->assertEquals(null, $society);
+    }
+
+    /**
+     * @see DolibarrApiService::getSociety()
+     */
+    public function testGetSocietyError(): void {
+        $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
+            ->setConstructorArgs([$this->client])
+            ->setMethodsExcept(['getSociety'])
+            ->getMock();
+
+        $organizationId = 123;
+
+        $dolibarrApiService
+            ->expects(self::once())
+            ->method('getJsonContent')
+            ->with("thirdparties", [ "limit" => "1", "sqlfilters" => "ref_int=" . $organizationId])
+            ->willThrowException(new HttpException(500));
+
+        $this->expectException(HttpException::class);
+
+        $society = $dolibarrApiService->getSociety($organizationId);
+    }
+
     /**
      * @see DolibarrApiService::getActiveContract()
      */
@@ -309,4 +353,75 @@ class DolibarrApiServiceTest extends TestCase
 
         $dolibarrApiService->getActiveOpentalentContacts($socId);
     }
+
+    /**
+     * @see DolibarrApiService::getSocietyTagsIds()
+     */
+    public function testGetSocietyTagsIds(): void
+    {
+        $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
+            ->setConstructorArgs([$this->client])
+            ->setMethodsExcept(['getSocietyTagsIds'])
+            ->getMock();
+
+        $socId = 1;
+
+        $dolibarrApiService
+            ->expects(self::once())
+            ->method('getJsonContent')
+            ->with("/thirdparties/1/categories")
+            ->willReturn([['id' => '10'], ['id' => '20']]);
+
+        $this->assertEquals(
+            [10, 20],
+            $dolibarrApiService->getSocietyTagsIds($socId)
+        );
+    }
+
+    /**
+     * @see DolibarrApiService::getSocietyTagsIds()
+     */
+    public function testGetSocietyTagsIdsMissing(): void
+    {
+        $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
+            ->setConstructorArgs([$this->client])
+            ->setMethodsExcept(['getSocietyTagsIds'])
+            ->getMock();
+
+        $socId = 1;
+
+        $dolibarrApiService
+            ->expects(self::once())
+            ->method('getJsonContent')
+            ->with("/thirdparties/1/categories")
+            ->willThrowException(new HttpException(404));
+
+        $this->assertEquals(
+            [],
+            $dolibarrApiService->getSocietyTagsIds($socId)
+        );
+    }
+
+    /**
+     * @see DolibarrApiService::getSocietyTagsIds()
+     */
+    public function testGetSocietyTagsIdsError(): void
+    {
+        $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
+            ->setConstructorArgs([$this->client])
+            ->setMethodsExcept(['getSocietyTagsIds'])
+            ->getMock();
+
+        $socId = 1;
+
+        $dolibarrApiService
+            ->expects(self::once())
+            ->method('getJsonContent')
+            ->with("/thirdparties/1/categories")
+            ->willThrowException(new HttpException(500));
+
+        $this->expectException(HttpException::class);
+
+        $dolibarrApiService->getSocietyTagsIds($socId);
+    }
 }

+ 101 - 6
tests/Unit/Service/Dolibarr/DolibarrSyncServiceTest.php

@@ -17,6 +17,7 @@ use App\Enum\Access\RoleEnum;
 use App\Enum\Core\ContactPointTypeEnum;
 use App\Enum\Network\NetworkEnum;
 use App\Enum\Organization\AddressPostalOrganizationTypeEnum;
+use App\Enum\Organization\LegalEnum;
 use App\Enum\Organization\SettingsProductEnum;
 use App\Repository\Access\AccessRepository;
 use App\Repository\Access\FunctionTypeRepository;
@@ -53,6 +54,7 @@ class TestableDolibarrSyncService extends DolibarrSyncService {
     public function getPersonContact(Person $person): ?ContactPoint { return parent::getPersonContact($person); }
     public function formatContactPosition(array $missions, ?string $gender = 'X'): string { return parent::formatContactPosition($missions, $gender); }
     public function formatPhoneNumber(PhoneNumber $phoneNumber): string { return parent::formatPhoneNumber($phoneNumber); }
+    public function getExpectedTagsFor(Organization $organization) { return parent::getExpectedTagsFor($organization); }
     public function validateResponse(ResponseInterface $response, BaseRestOperation $operation): void { parent::validateResponse($response, $operation); }
 }
 
@@ -133,7 +135,8 @@ class DolibarrSyncServiceTest extends TestCase
             'email' => 'foo@bar.net',
             'phone' => '0102030405',
             'networkId' => NetworkEnum::CMF()->getValue(),
-            'product' => SettingsProductEnum::SCHOOL()->getValue()
+            'product' => SettingsProductEnum::SCHOOL()->getValue(),
+            'legalStatus' => LegalEnum::LOCAL_AUTHORITY()
         ];
 
         $orgId2 = 20;
@@ -145,7 +148,8 @@ class DolibarrSyncServiceTest extends TestCase
             'email' => null,
             'phone' => null,
             'networkId' => null,
-            'product' => SettingsProductEnum::ARTIST()->getValue()
+            'product' => SettingsProductEnum::ARTIST()->getValue(),
+            'legalStatus' => LegalEnum::ASSOCIATION_LAW_1901()
         ];
 
         $orgId3 = 30;
@@ -485,6 +489,16 @@ class DolibarrSyncServiceTest extends TestCase
             ["Organization 30 not found in the Opentalent DB"],
         );
 
+        // Tags
+        $this->dolibarrApiService->method('getSocietyTagsIds')->willReturnMap([
+            [$socId1, [1, 68]],
+            [$socId2, [3, 67]]
+        ]);
+        $dolibarrSyncService->method('getExpectedTagsFor')->willReturnMap([
+            [$organization1, [67]],
+            [$organization2, []]
+        ]);
+
         // Expected progression callback triggers
         $progressionCallbackExpectedCalls = [[1, 3], [2, 3], [3, 3]];
         $progressionCallback = static function ($i, $total) use (&$progressionCallbackExpectedCalls) {
@@ -499,7 +513,7 @@ class DolibarrSyncServiceTest extends TestCase
 
         $operations = $dolibarrSyncService->scan($progressionCallback);
 
-        $this->assertCount(5, $operations);
+        $this->assertCount(8, $operations);
 
         $this->assertEqualsCanonicalizing(
             [
@@ -533,10 +547,27 @@ class DolibarrSyncServiceTest extends TestCase
         );
 
         $this->assertEqualsCanonicalizing(
-            ['[PUT contacts/4]', 'statut : `1` => `0`'],
+            [
+                '[PUT contacts/4]',
+                'statut : `1` => `0`'
+            ],
             $operations[2]->getChangeLog()
         );
 
+        $this->assertEqualsCanonicalizing(
+            [
+                '[DELETE /thirdparties/1/categories/68]'
+            ],
+            $operations[3]->getChangeLog()
+        );
+
+        $this->assertEqualsCanonicalizing(
+            [
+                '[POST /thirdparties/1/categories/67]'
+            ],
+            $operations[4]->getChangeLog()
+        );
+
         $this->assertEqualsCanonicalizing(
             [
                 '[PUT thirdparties/2]',
@@ -550,7 +581,7 @@ class DolibarrSyncServiceTest extends TestCase
                 'zip : (new) => ``',
                 'array_options.options_2iopeninfoopentalent : (new) => `Nombre d\'accès admin : 2`',
             ],
-            $operations[3]->getChangeLog()
+            $operations[5]->getChangeLog()
         );
 
         $this->assertEqualsCanonicalizing(
@@ -567,7 +598,14 @@ class DolibarrSyncServiceTest extends TestCase
                 'array_options.options_2iopen_person_id : (new) => `200`',
                 'socid : (new) => `2`'
             ],
-            $operations[4]->getChangeLog()
+            $operations[6]->getChangeLog()
+        );
+
+        $this->assertEqualsCanonicalizing(
+            [
+                '[DELETE /thirdparties/2/categories/67]'
+            ],
+            $operations[7]->getChangeLog()
         );
 
         $this->assertCount(0, $progressionCallbackExpectedCalls);
@@ -1229,6 +1267,63 @@ class DolibarrSyncServiceTest extends TestCase
         );
     }
 
+    public function testGetExpectedTagsFor()
+    {
+        $dolibarrSyncService = $this->getMockForMethod('getExpectedTagsFor');
+
+        $organization1 = $this->getMockBuilder(Organization::class)->getMock();
+        $organization1->method('getLegalStatus')->willReturn(LegalEnum::ASSOCIATION_LAW_1901()->getValue());
+        $settings1 = $this->getMockBuilder(Settings::class)->getMock();
+        $settings1->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL()->getValue());
+        $organization1->method('getSettings')->willReturn($settings1);
+
+        $organization2 = $this->getMockBuilder(Organization::class)->getMock();
+        $organization2->method('getLegalStatus')->willReturn(LegalEnum::ASSOCIATION_LAW_1901()->getValue());
+        $settings2 = $this->getMockBuilder(Settings::class)->getMock();
+        $settings2->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL_PREMIUM()->getValue());
+        $organization2->method('getSettings')->willReturn($settings2);
+
+        $organization3 = $this->getMockBuilder(Organization::class)->getMock();
+        $organization3->method('getLegalStatus')->willReturn(LegalEnum::LOCAL_AUTHORITY()->getValue());
+        $settings3 = $this->getMockBuilder(Settings::class)->getMock();
+        $settings3->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL()->getValue());
+        $organization3->method('getSettings')->willReturn($settings3);
+
+        $organization4 = $this->getMockBuilder(Organization::class)->getMock();
+        $organization4->method('getLegalStatus')->willReturn(LegalEnum::LOCAL_AUTHORITY()->getValue());
+        $settings4 = $this->getMockBuilder(Settings::class)->getMock();
+        $settings4->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL_PREMIUM()->getValue());
+        $organization4->method('getSettings')->willReturn($settings4);
+
+        $organization5 = $this->getMockBuilder(Organization::class)->getMock();
+        $organization5->method('getLegalStatus')->willReturn(LegalEnum::LOCAL_AUTHORITY()->getValue());
+        $settings5 = $this->getMockBuilder(Settings::class)->getMock();
+        $settings5->method('getProduct')->willReturn(SettingsProductEnum::ARTIST()->getValue());
+        $organization5->method('getSettings')->willReturn($settings5);
+
+        $this->assertEquals(
+            $dolibarrSyncService->getExpectedTagsFor($organization1),
+            [67]
+        );
+        $this->assertEquals(
+            $dolibarrSyncService->getExpectedTagsFor($organization2),
+            [69]
+        );
+        $this->assertEquals(
+            $dolibarrSyncService->getExpectedTagsFor($organization3),
+            [68]
+        );
+        $this->assertEquals(
+            $dolibarrSyncService->getExpectedTagsFor($organization4),
+            [70]
+        );
+        $this->assertEquals(
+            $dolibarrSyncService->getExpectedTagsFor($organization5),
+            []
+        );
+
+    }
+
     /**
      * @see DolibarrSyncService::validateResponse()
      */