Browse Source

dolibarr sync unit tests ok

Olivier Massot 3 years ago
parent
commit
fe22b487bb

+ 1 - 0
src/Enum/Access/FunctionEnum.php

@@ -16,6 +16,7 @@ use MyCLabs\Enum\Enum;
  * @method static TREASURER()
  * @method static ARCHIVIST()
  * @method static DIRECTOR()
+ * @method static MUSIC_DIRECTOR_AND_HEAD()
  */
 class FunctionEnum extends Enum
 {

+ 4 - 4
src/Service/Dolibarr/DolibarrSyncService.php

@@ -294,9 +294,7 @@ class DolibarrSyncService
                 continue;
             }
 
-            $operation = $this->dolibarrApiService->execute(
-                $operation
-            );
+            $operation->execute($this->dolibarrApiService);
 
             if ($operation->getStatus() === BaseRestOperation::STATUS_ERROR) {
                 $this->logger->error('Error while executing operation : ' . $operation);
@@ -503,8 +501,10 @@ class DolibarrSyncService
             ContactPointTypeEnum::OTHER()->getValue()
         ];
 
+        $contactPoints = $organization->getContactPoints();
+
         foreach ($contactPriorities as $contactType) {
-            foreach ($organization->getContactPoints() as $contactPoint) {
+            foreach ($contactPoints as $contactPoint) {
                 if ($contactPoint->getContactType() == $contactType && $contactPoint->getEmail() !== null) {
                     return $contactPoint->getEmail();
                 }

+ 0 - 9
src/Service/Rest/ApiRequestInterface.php

@@ -94,13 +94,4 @@ interface ApiRequestInterface
         array  $parameters = [],
         array  $options = []
     ): ResponseInterface;
-
-    /**
-     * Executes the given Rest Operation, then returns it
-     *
-     * @param BaseRestOperation $operation
-     * @return BaseRestOperation
-     */
-    function execute(BaseRestOperation $operation): BaseRestOperation;
-
 }

+ 0 - 13
src/Service/Rest/ApiRequestService.php

@@ -133,17 +133,4 @@ class ApiRequestService implements ApiRequestInterface
             throw new HttpException(500, 'Request error : ', $e);
         }
     }
-
-    /**
-     * Executes the given Rest Operation, then returns it
-     *
-     * @param BaseRestOperation $operation
-     * @return BaseRestOperation
-     */
-    public function execute(BaseRestOperation $operation): BaseRestOperation
-    {
-        $operation->setApiRequestService($this);
-        $operation->execute();
-        return $operation;
-    }
 }

+ 3 - 6
src/Service/Rest/Operation/BaseRestOperation.php

@@ -23,7 +23,6 @@ abstract class BaseRestOperation
     const STATUS_DONE = 2;
     const STATUS_ERROR = 3;
 
-    protected ApiRequestInterface $apiService;
     protected int $status = self::STATUS_READY;
     protected string $label;
     protected string $method;
@@ -33,9 +32,6 @@ abstract class BaseRestOperation
     protected array $currentData;
     protected string $errorMessage = "";
 
-    #[Required]
-    public function setApiRequestService(ApiRequestService $apiService) { $this->apiService = $apiService; }
-
     public function __construct(
         string $label,
         string $method,
@@ -55,10 +51,11 @@ abstract class BaseRestOperation
     /**
      * Execute the operation and update its status according to the result
      */
-    public function execute() {
+    public function execute(ApiRequestInterface $apiService) {
         $this->status = self::STATUS_PENDING;
         try {
-            $response = $this->apiService->request($this->method, $this->path, $this->parameters, $this->options);
+            $response = $apiService->request($this->method, $this->path, $this->parameters, $this->options);
+
             if ($response->getStatusCode() !== 200) {
                 $this->status = self::STATUS_ERROR;
                 $this->errorMessage = 'Error ' . $response->getStatusCode() . ' : ' . $response->getContent();

+ 117 - 44
tests/Service/Dolibarr/DolibarrSyncServiceTest.php

@@ -22,12 +22,15 @@ use App\Repository\Access\FunctionTypeRepository;
 use App\Repository\Organization\OrganizationRepository;
 use App\Service\Dolibarr\DolibarrApiService;
 use App\Service\Dolibarr\DolibarrSyncService;
+use App\Service\Rest\ApiRequestService;
+use App\Service\Rest\Operation\CreateOperation;
 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\HttpClient\ResponseInterface;
 use Symfony\Contracts\Translation\TranslatorInterface;
 
 class TestableDolibarrSyncService extends DolibarrSyncService {
@@ -35,7 +38,7 @@ class TestableDolibarrSyncService extends DolibarrSyncService {
     public function getDolibarrContactsIndex(int $socId): array { return parent::getDolibarrContactsIndex($socId); }
     public function getActiveMembersIndex(): array { return parent::getActiveMembersIndex(); }
     public static function sanitizeDolibarrData(?array $data): ?array { return parent::sanitizeDolibarrData($data); }
-    public function getOrganizationPostalAddress(Organization $organization): AddressPostal { return parent::getOrganizationPostalAddress($organization); }
+    public function getOrganizationPostalAddress(Organization $organization): ?AddressPostal { return parent::getOrganizationPostalAddress($organization); }
     public function getOrganizationPhone(Organization $organization): ?string { return parent::getOrganizationPhone($organization); }
     public function getOrganizationEmail(Organization $organization): ?string { return parent::getOrganizationEmail($organization); }
     public static function countWithMission(array $missions, array $members): int { return parent::countWithMission($missions, $members); }
@@ -119,12 +122,12 @@ class DolibarrSyncServiceTest extends TestCase
             ->method('getAllActiveMembersAndMissions')
             ->willReturn(
                 [
-                    ['id' => 108939, 'organization_id' => 37306, 'mission' => FunctionEnum::PRESIDENT()->getValue()],
-                    ['id' => 108939, 'organization_id' => 37306, 'mission' => FunctionEnum::ADHERENT()->getValue()],
-                    ['id' => 156252, 'organization_id' => 37306, 'mission' => FunctionEnum::TREASURER()->getValue()],
-                    ['id' => 156252, 'organization_id' => 37306, 'mission' => FunctionEnum::ADHERENT()->getValue()],
-                    ['id' => 112775, 'organization_id' => 37306, 'mission' => FunctionEnum::STUDENT()->getValue()],
-                    ['id' => 112775, 'organization_id' => 37306, 'mission' => FunctionEnum::ADHERENT()->getValue()],
+                    ['id' => 1, 'organization_id' => 37306, 'mission' => FunctionEnum::PRESIDENT()->getValue()],
+                    ['id' => 1, 'organization_id' => 37306, 'mission' => FunctionEnum::ADHERENT()->getValue()],
+                    ['id' => 2, 'organization_id' => 37306, 'mission' => FunctionEnum::TREASURER()->getValue()],
+                    ['id' => 2, 'organization_id' => 37306, 'mission' => FunctionEnum::ADHERENT()->getValue()],
+                    ['id' => 3, 'organization_id' => 37306, 'mission' => FunctionEnum::STUDENT()->getValue()],
+                    ['id' => 3, 'organization_id' => 37306, 'mission' => FunctionEnum::ADHERENT()->getValue()],
                 ]
             );
 
@@ -193,35 +196,65 @@ class DolibarrSyncServiceTest extends TestCase
                 function ($c) {
                     return in_array(
                         (int)$c["array_options"]["options_2iopen_person_id"],
-                        [108939, 156252, 302117]
+                        [
+                            108939, // existing person, to be updated
+                            156252  // no matching person, to be deleted
+                                   // a new contact shall be created too
+                        ]
                     ); }
             )
         );
 
         $this->organizationRepository->method('find')->willReturn($organization);
 
-        $access = $this->getMockBuilder(Access::class)->getMock();
-        $person = $this->getMockBuilder(Person::class)->getMock();
-        $person->method('getId')->willReturn(108939);
-        $person->method('getName')->willReturn('Holmes');
-        $person->method('getGender')->willReturn('MISTER');
-        $person->method('getGivenName')->willReturn('Sherlock');
-        $person->method('getGivenName')->willReturn('Sherlock');
+        $access1 = $this->getMockBuilder(Access::class)->getMock();
+        $access1->method('getId')->willReturn(1);
 
-        $personContactPoint = $this->getMockBuilder(ContactPoint::class)->getMock();
-        $personContactPoint->method('getContactType')->willReturn(ContactPointTypeEnum::CONTACT()->getValue());
-        $personContactPoint->method('getEmail')->willReturn('sherlock@holmes.com');
-        $phoneNumber = $phoneUtil->parse('02 98 76 54 32', 'FR');
-        $personContactPoint->method('getTelphone')->willReturn($phoneNumber);
-        $personContactPoint->method('getMobilPhone')->willReturn(null);
-        $person->method('getContactPoints')->willReturn(
-            new ArrayCollection([$personContactPoint])
-        );
+        $person1 = $this->getMockBuilder(Person::class)->getMock();
+        $person1->method('getId')->willReturn(108939);
+        $person1->method('getName')->willReturn('Holmes');
+        $person1->method('getGender')->willReturn('MISTER');
+        $person1->method('getGivenName')->willReturn('Sherlock');
+        $person1->method('getGivenName')->willReturn('Sherlock');
 
+        $personContactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
+        $personContactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::CONTACT()->getValue());
+        $personContactPoint1->method('getEmail')->willReturn('sherlock@holmes.com');
+        $phoneNumber1 = $phoneUtil->parse('02 98 76 54 32', 'FR');
+        $personContactPoint1->method('getTelphone')->willReturn($phoneNumber1);
+        $personContactPoint1->method('getMobilPhone')->willReturn(null);
+        $person1->method('getContactPoints')->willReturn(
+            new ArrayCollection([$personContactPoint1])
+        );
+        $access1->method('getPerson')->willReturn($person1);
 
+        $access2 = $this->getMockBuilder(Access::class)->getMock();
+        $access2->method('getId')->willReturn(1);
 
-        $access->method('getPerson')->willReturn($person);
-        $this->accessRepository->method('find')->willReturn($access);
+        $person2 = $this->getMockBuilder(Person::class)->getMock();
+        $person2->method('getId')->willReturn(1000);
+        $person2->method('getName')->willReturn('Watson');
+        $person2->method('getGender')->willReturn('MISTER');
+        $person2->method('getGivenName')->willReturn('John');
+        $person2->method('getGivenName')->willReturn('John');
+
+        $personContactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock();
+        $personContactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::CONTACT()->getValue());
+        $personContactPoint2->method('getEmail')->willReturn('docteur@watson.com');
+        $phoneNumber2 = $phoneUtil->parse('02 10 11 12 13', 'FR');
+        $personContactPoint2->method('getTelphone')->willReturn($phoneNumber2);
+        $personContactPoint2->method('getMobilPhone')->willReturn(null);
+        $person2->method('getContactPoints')->willReturn(
+            new ArrayCollection([$personContactPoint2])
+        );
+        $access2->method('getPerson')->willReturn($person2);
+
+        $this->accessRepository->method('find')->willReturnMap(
+            [
+                [1, null, null, $access1],
+                [2, null, null, $access2],
+            ]
+        );
 
         $this->translator->method('trans')->willReturnMap(
             [
@@ -251,19 +284,54 @@ class DolibarrSyncServiceTest extends TestCase
             $operations[0]->getChangeLog()
         );
         $this->assertEquals(
-            ['PUT contact/5868', ''],
+            [
+                '[PUT contacts/5868]',
+                'civility_code : `MME` => ``',
+                'lastname : `DUPONT` => `Holmes`',
+                'firstname : `Valerie` => `Sherlock`',
+                'email : `abcd@hotmail.com` => ``',
+                'phone_pro : `+33478570000` => ``',
+                'phone_mobile : `+33682980000` => ``',
+                'poste : `Secrétaire` => ``'
+            ],
             $operations[1]->getChangeLog()
         );
         $this->assertEquals(
-            ['PUT thirdparty/5869', ''],
+            [
+                '[POST contacts]',
+                'civility_code : ``',
+                'lastname : `Watson`',
+                'firstname : `John`',
+                'email : ``',
+                'phone_pro : ``',
+                'phone_mobile : ``',
+                'poste : ``',
+                'socid : `1726`',
+                'array_options.options_2iopen_person_id : `1000`'
+            ],
             $operations[2]->getChangeLog()
         );
         $this->assertEquals(
-            ['PUT thirdparty/5871', ''],
-            $operations[2]->getChangeLog()
+            ['[PUT contacts/5869]', 'statut : `1` => `0`'],
+            $operations[3]->getChangeLog()
         );
     }
 
+    public function testExecuteOk() {
+
+        $operation = new CreateOperation('operation 1', 'thirdparty', ['data' => 1]);
+        $this->assertEquals($operation->getStatus(), $operation::STATUS_READY);
+
+        $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
+        $response->method('getStatusCode')->willReturn(200);
+        $this->dolibarrApiService->method('request')->willReturn($response);
+
+        $syncService = $this->newDolibarrSyncService();
+        $operations = $syncService->execute([$operation]);
+
+        $this->assertEquals($operation::STATUS_DONE, $operations[0]->getStatus());
+    }
+
     public function testGetDolibarrSocietiesIndex() {
         $this->dolibarrApiService
             ->expects($this->once())
@@ -353,7 +421,7 @@ class DolibarrSyncServiceTest extends TestCase
         $organization->expects($this->once())
             ->method('getOrganizationAddressPostals')
             ->willReturn(
-                [$organizationAddressPostal1, $organizationAddressPostal2, $organizationAddressPostal3]
+                new ArrayCollection([$organizationAddressPostal1, $organizationAddressPostal2, $organizationAddressPostal3])
             );
 
         $syncService = $this->newDolibarrSyncService($organization);
@@ -364,9 +432,9 @@ class DolibarrSyncServiceTest extends TestCase
         );
 
         $organization2 = $this->getMockBuilder(Organization::class)->getMock();
-        $organization->expects($this->once())
+        $organization2->expects($this->once())
             ->method('getOrganizationAddressPostals')
-            ->willReturn([]);
+            ->willReturn(new ArrayCollection([]));
 
         $this->assertEquals(
             null,
@@ -385,19 +453,21 @@ class DolibarrSyncServiceTest extends TestCase
         $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL()->getValue());
         $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL()->getValue());
 
-        $contactPoint2->expects($this->once())->method('getTelphone')->willReturn('0161626365');
+        $phoneUtil = PhoneNumberUtil::getInstance();
+        $phoneNumber = $phoneUtil->parse('0161626365', "FR");
+        $contactPoint2->method('getTelphone')->willReturn($phoneNumber);
 
         $organization
             ->expects($this->once())
             ->method('getContactPoints')
             ->willReturn(
-                [$contactPoint1, $contactPoint2, $contactPoint3]
+                new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
             );
 
         $syncService = $this->newDolibarrSyncService();
 
         $this->assertEquals(
-            '+331 61 62 63 65',
+            '+33 1 61 62 63 65',
             $syncService->getOrganizationPhone($organization)
         );
     }
@@ -414,19 +484,22 @@ class DolibarrSyncServiceTest extends TestCase
         $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL()->getValue());
 
         $contactPoint2->expects($this->once())->method('getTelphone')->willReturn(null);
-        $contactPoint2->expects($this->once())->method('getMobilPhone')->willReturn('0661626365');
+
+        $phoneUtil = PhoneNumberUtil::getInstance();
+        $phoneNumber = $phoneUtil->parse('0661626365', "FR");
+        $contactPoint2->method('getMobilPhone')->willReturn($phoneNumber);
 
         $organization
             ->expects($this->once())
             ->method('getContactPoints')
             ->willReturn(
-                [$contactPoint1, $contactPoint2, $contactPoint3]
+                new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
             );
 
         $syncService = $this->newDolibarrSyncService();
 
         $this->assertEquals(
-            '+336 61 62 63 65',
+            '+33 6 61 62 63 65',
             $syncService->getOrganizationPhone($organization)
         );
     }
@@ -436,7 +509,7 @@ class DolibarrSyncServiceTest extends TestCase
         $organization
             ->expects($this->once())
             ->method('getContactPoints')
-            ->willReturn([]);
+            ->willReturn(new ArrayCollection([]));
 
         $syncService = $this->newDolibarrSyncService();
 
@@ -463,7 +536,7 @@ class DolibarrSyncServiceTest extends TestCase
             ->expects($this->once())
             ->method('getContactPoints')
             ->willReturn(
-                [$contactPoint1, $contactPoint2, $contactPoint3]
+                new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
             );
 
         $syncService = $this->newDolibarrSyncService();
@@ -480,7 +553,7 @@ class DolibarrSyncServiceTest extends TestCase
         $organization
             ->expects($this->once())
             ->method('getContactPoints')
-            ->willReturn([]);
+            ->willReturn(new ArrayCollection([]));
 
         $syncService = $this->newDolibarrSyncService();
 
@@ -531,7 +604,7 @@ class DolibarrSyncServiceTest extends TestCase
         $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER()->getValue());
         $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL()->getValue());
 
-        $person->expects($this->once())->method('getContactPoints')->willReturn([$contactPoint1, $contactPoint2]);
+        $person->expects($this->once())->method('getContactPoints')->willReturn(new ArrayCollection([$contactPoint1, $contactPoint2]));
 
         $syncService = $this->newDolibarrSyncService();
 
@@ -541,7 +614,7 @@ class DolibarrSyncServiceTest extends TestCase
         );
 
         $person2 = $this->getMockBuilder(Person::class)->getMock();
-        $person2->expects($this->once())->method('getContactPoints')->willReturn([]);
+        $person2->expects($this->once())->method('getContactPoints')->willReturn(new ArrayCollection([]));
         $this->assertEquals(null, $syncService->getPersonContact($person2));
     }