Browse Source

dolibarr sync scan tests ok for society

Olivier Massot 3 years ago
parent
commit
24e18a1e6b

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

@@ -94,7 +94,6 @@ class DolibarrSyncService
             // Populate the expectedContacts array
             $organizationMembers = $membersIndex[$organization->getId()] ?? [];
 
-
             // ===== Update Society =====
             $newSocietyData = [];
 
@@ -147,7 +146,7 @@ class DolibarrSyncService
             // /!\ On est forcé de passer la sub-array entière pour mettre à jour le champ modifié, sinon
             //     tous les autres champs seront passés à null...
             $newSocietyData['array_options'] = $dolibarrSociety["array_options"];
-            $newSocietyData['array_options']['options_2iopeninfoopentalent'] = implode('\n', $infos);
+            $newSocietyData['array_options']['options_2iopeninfoopentalent'] = implode("\n", $infos);
 
             // Only update the fields that are different
             $newSocietyData = $this->filterDiff($dolibarrSociety, $newSocietyData);
@@ -438,11 +437,11 @@ class DolibarrSyncService
      */
     protected function getOrganizationPostalAddress(Organization $organization): ?AddressPostal {
         $addressPriorities = [
-            AddressPostalOrganizationTypeEnum::ADDRESS_BILL(),
-            AddressPostalOrganizationTypeEnum::ADDRESS_CONTACT(),
-            AddressPostalOrganizationTypeEnum::ADDRESS_HEAD_OFFICE(),
-            AddressPostalOrganizationTypeEnum::ADDRESS_PRACTICE(),
-            AddressPostalOrganizationTypeEnum::ADDRESS_OTHER()
+            AddressPostalOrganizationTypeEnum::ADDRESS_BILL()->getValue(),
+            AddressPostalOrganizationTypeEnum::ADDRESS_CONTACT()->getValue(),
+            AddressPostalOrganizationTypeEnum::ADDRESS_HEAD_OFFICE()->getValue(),
+            AddressPostalOrganizationTypeEnum::ADDRESS_PRACTICE()->getValue(),
+            AddressPostalOrganizationTypeEnum::ADDRESS_OTHER()->getValue()
         ];
 
         $organizationAddressPostal = $organization->getOrganizationAddressPostals();
@@ -466,10 +465,10 @@ class DolibarrSyncService
     protected function getOrganizationPhone(Organization $organization): ?string
     {
         $contactPriorities = [
-            ContactPointTypeEnum::BILL(),
-            ContactPointTypeEnum::CONTACT(),
-            ContactPointTypeEnum::PRINCIPAL(),
-            ContactPointTypeEnum::OTHER()
+            ContactPointTypeEnum::BILL()->getValue(),
+            ContactPointTypeEnum::CONTACT()->getValue(),
+            ContactPointTypeEnum::PRINCIPAL()->getValue(),
+            ContactPointTypeEnum::OTHER()->getValue()
         ];
 
         $contactPoints = $organization->getContactPoints();
@@ -478,6 +477,7 @@ class DolibarrSyncService
             foreach ($contactPoints as $contactPoint) {
                 if ($contactPoint->getContactType() === $contactType) {
                     if ($contactPoint->getTelphone() !== null) {
+
                         return $this->formatPhoneNumber($contactPoint->getTelphone());
                     }
                     if ($contactPoint->getMobilPhone() !== null) {
@@ -497,10 +497,10 @@ class DolibarrSyncService
      */
     protected function getOrganizationEmail(Organization $organization): ?string {
         $contactPriorities = [
-            ContactPointTypeEnum::BILL(),
-            ContactPointTypeEnum::CONTACT(),
-            ContactPointTypeEnum::PRINCIPAL(),
-            ContactPointTypeEnum::OTHER()
+            ContactPointTypeEnum::BILL()->getValue(),
+            ContactPointTypeEnum::CONTACT()->getValue(),
+            ContactPointTypeEnum::PRINCIPAL()->getValue(),
+            ContactPointTypeEnum::OTHER()->getValue()
         ];
 
         foreach ($contactPriorities as $contactType) {
@@ -535,15 +535,15 @@ class DolibarrSyncService
      */
     protected function getPersonContact(Person $person): ?ContactPoint {
         $contactPriorities = [
-            ContactPointTypeEnum::PRINCIPAL(),
-            ContactPointTypeEnum::OTHER()
+            ContactPointTypeEnum::PRINCIPAL()->getValue(),
+            ContactPointTypeEnum::OTHER()->getValue()
         ];
 
         $contactPoints = $person->getContactPoints();
 
-        foreach ($contactPriorities as $contactTypeEnum) {
+        foreach ($contactPriorities as $contactType) {
             foreach ($contactPoints as $contactPoint) {
-                if ($contactPoint->getContactType() === $contactTypeEnum->getValue()) {
+                if ($contactPoint->getContactType() === $contactType) {
                     return $contactPoint;
                 }
             }
@@ -559,7 +559,12 @@ class DolibarrSyncService
      * @return string
      */
     protected function formatContactPosition(array $missions, ?string $gender = 'X'): string {
-        $to_exclude = [FunctionEnum::ADHERENT(), FunctionEnum::STUDENT(), FunctionEnum::OTHER()];
+        $to_exclude = [
+            FunctionEnum::ADHERENT()->getValue(),
+            FunctionEnum::STUDENT()->getValue(),
+            FunctionEnum::OTHER()->getValue()
+        ];
+
         $poste = implode(
             ', ',
             array_map(

+ 28 - 28
tests/Service/Dolibarr/DolibarrSyncServiceTest.php

@@ -25,6 +25,7 @@ use App\Service\Dolibarr\DolibarrSyncService;
 use Doctrine\Common\Collections\ArrayCollection;
 use JetBrains\PhpStorm\Pure;
 use libphonenumber\PhoneNumber;
+use libphonenumber\PhoneNumberUtil;
 use PHPUnit\Framework\TestCase;
 use Psr\Log\LoggerInterface;
 use Symfony\Contracts\Translation\TranslatorInterface;
@@ -140,6 +141,7 @@ class DolibarrSyncServiceTest extends TestCase
 
         // Organization's name
         $organization = $this->getMockBuilder(Organization::class)->getMock();
+        $organization->method('getId')->willReturn(37306);
         $organization->method('getName')->willReturn("Etablissement d'Enseignement Artistique");
 
         // Postal address
@@ -158,12 +160,12 @@ class DolibarrSyncServiceTest extends TestCase
         );
 
         // Email and phone
+        $phoneUtil = PhoneNumberUtil::getInstance();
+
         $contactPoint = $this->getMockBuilder(ContactPoint::class)->getMock();
         $contactPoint->method('getContactType')->willReturn(ContactPointTypeEnum::CONTACT()->getValue());
         $contactPoint->method('getEmail')->willReturn('email@email.com');
-        $phoneNumber = new PhoneNumber();
-        $phoneNumber->setCountryCode(33);
-        $phoneNumber->setNationalNumber('1 02 03 04 05');
+        $phoneNumber = $phoneUtil->parse('01 02 03 04 05', 'FR');
         $contactPoint->method('getTelphone')->willReturn($phoneNumber);
         $organization->method('getContactPoints')->willReturn(
             new ArrayCollection([$contactPoint])
@@ -209,9 +211,7 @@ class DolibarrSyncServiceTest extends TestCase
         $personContactPoint = $this->getMockBuilder(ContactPoint::class)->getMock();
         $personContactPoint->method('getContactType')->willReturn(ContactPointTypeEnum::CONTACT()->getValue());
         $personContactPoint->method('getEmail')->willReturn('sherlock@holmes.com');
-        $phoneNumber = new PhoneNumber();
-        $phoneNumber->setCountryCode(33);
-        $phoneNumber->setNationalNumber('2 98 76 54 32');
+        $phoneNumber = $phoneUtil->parse('02 98 76 54 32', 'FR');
         $personContactPoint->method('getTelphone')->willReturn($phoneNumber);
         $personContactPoint->method('getMobilPhone')->willReturn(null);
         $person->method('getContactPoints')->willReturn(
@@ -223,6 +223,14 @@ class DolibarrSyncServiceTest extends TestCase
         $access->method('getPerson')->willReturn($person);
         $this->accessRepository->method('find')->willReturn($access);
 
+        $this->translator->method('trans')->willReturnMap(
+            [
+                ['STUDENTS_COUNT', [], null, null, "Nombre d'élèves"],
+                ['ADHERENTS_COUNT', [], null, null, "Nombre d'adhérents"],
+                ['ADMIN_ACCESS_COUNT', [], null, null, "Nombre d'accès admin"],
+            ]
+        );
+
         $syncService = $this->newDolibarrSyncService();
 
         $operations = $syncService->scan();
@@ -232,15 +240,13 @@ class DolibarrSyncServiceTest extends TestCase
         $this->assertEquals(
             [
                 '[PUT thirdparties/1726]',
-                'address : `\n
-217, rue Raoul Follereau\n
-` => `21b baker street`',
+                "address : `\n217, rue Raoul Follereau\n` => `21b baker street`",
                 'zip : `74300` => `250 329`',
                 'town : `CLUSES` => `Londres`',
                 'email : `` => `email@email.com`',
-                'phone : `+33678403010` => `+331 02 03 04 05`',
+                'phone : `+33678403010` => `+33 1 02 03 04 05`',
                 'parent : `` => `5086`',
-                'array_options.options_2iopeninfoopentalent : `` => ` : 3\n : 1\n : 1`'
+                "array_options.options_2iopeninfoopentalent : `` => `Nombre d'élèves : 1\nNombre d'adhérents : 3\nNombre d'accès admin : 1`"
             ],
             $operations[0]->getChangeLog()
         );
@@ -540,19 +546,15 @@ class DolibarrSyncServiceTest extends TestCase
     }
 
     public function testFormatContactPosition() {
-        $this->translator->method('trans')->will(
-            $this->returnCallback(function($mission, $params) {
-                if ($mission == FunctionEnum::PRESIDENT()) {
-                    if ($params === ['gender' => 'X']) { return 'Président(e)'; }
-                    elseif ($params === ['gender' => 'M']) { return 'Président'; }
-                    elseif ($params === ['gender' => 'F']) { return 'Présidente'; }
-                } elseif ($mission == FunctionEnum::DIRECTOR()) {
-                    if ($params === ['gender' => 'X']) { return 'Directeur(ice)'; }
-                    elseif ($params === ['gender' => 'M']) { return 'Directeur'; }
-                    elseif ($params === ['gender' => 'F']) { return 'Directrice'; }
-                }
-                throw new \AssertionError('translator->trans stub has no matching call for arguments ' . json_encode([$mission, $params]));
-              })
+        $this->translator->method('trans')->willReturnMap(
+            [
+                [FunctionEnum::PRESIDENT()->getValue(), ['gender' => 'X'], null, null, 'Président(e)'],
+                [FunctionEnum::PRESIDENT()->getValue(), ['gender' => 'M'], null, null, 'Président'],
+                [FunctionEnum::PRESIDENT()->getValue(), ['gender' => 'F'], null, null, 'Présidente'],
+                [FunctionEnum::DIRECTOR()->getValue(), ['gender' => 'X'], null, null, 'Directeur(ice)'],
+                [FunctionEnum::DIRECTOR()->getValue(), ['gender' => 'M'], null, null, 'Directeur'],
+                [FunctionEnum::DIRECTOR()->getValue(), ['gender' => 'F'], null, null, 'Directrice'],
+            ]
         );
 
         $syncService = $this->newDolibarrSyncService();
@@ -590,10 +592,8 @@ class DolibarrSyncServiceTest extends TestCase
     }
 
     public function testFormatPhoneNumber() {
-        $phoneNumber = new PhoneNumber();
-        $phoneNumber->setCountryCode(33);
-        $phoneNumber->setNationalNumber('1 02 03 04 05');
-
+        $phoneUtil = PhoneNumberUtil::getInstance();
+        $phoneNumber = $phoneUtil->parse('01 02 03 04 05', "FR");
         $this->assertEquals(
             '+33 1 02 03 04 05',
             TestableDolibarrSyncService::formatPhoneNumber($phoneNumber)