Forráskód Böngészése

add dolibarrsync::scan unitest (ongoing) and minor fixes

Olivier Massot 3 éve
szülő
commit
ffdfe1c05a

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

@@ -117,6 +117,7 @@ class FunctionEnum extends Enum
     {
         return [
             self::PRESIDENT,
+            self::DIRECTOR,
             self::SECRETARY,
             self::TREASURER,
             self::ADMINISTRATIVE_OFFICER,

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

@@ -8,6 +8,7 @@ use MyCLabs\Enum\Enum;
 /**
  * Role
  * @method static ROLE_ADMIN()
+ * @method static DIRECTOR()
  */
 class RoleEnum extends Enum
 {

+ 1 - 0
src/Enum/Organization/AddressPostalOrganizationTypeEnum.php

@@ -12,6 +12,7 @@ use MyCLabs\Enum\Enum;
  * @method static ADDRESS_HEAD_OFFICE()
  * @method static ADDRESS_PRACTICE()
  * @method static ADDRESS_OTHER()
+ * @method static ADDRESS_PRINCIPAL()
  */
 class AddressPostalOrganizationTypeEnum extends Enum
 {

+ 1 - 0
src/Enum/Organization/SettingsProductEnum.php

@@ -7,6 +7,7 @@ use MyCLabs\Enum\Enum;
 
 /**
  * Type de produit disponible pour une organisation
+ * @method static SCHOOL()
  */
 class SettingsProductEnum extends Enum
 {

+ 0 - 2
src/Service/Dolibarr/DolibarrSyncService.php

@@ -16,7 +16,6 @@ use App\Enum\Organization\SettingsProductEnum;
 use App\Enum\Person\GenderEnum;
 use App\Repository\Access\AccessRepository;
 use App\Repository\Access\FunctionTypeRepository;
-use App\Repository\Core\ContactPointRepository;
 use App\Repository\Organization\OrganizationRepository;
 use App\Service\Core\AddressPostalUtils;
 use App\Service\Rest\Operation\BaseRestOperation;
@@ -41,7 +40,6 @@ class DolibarrSyncService
     public function __construct(
         private OrganizationRepository $organizationRepository,
         private AccessRepository $accessRepository,
-        private ContactPointRepository $contactPointRepository,
         private FunctionTypeRepository $functionTypeRepository,
         private DolibarrApiService $dolibarrApiService,
         private TranslatorInterface $translator,

+ 174 - 6
tests/Service/Dolibarr/DolibarrSyncServiceTest.php

@@ -2,20 +2,27 @@
 
 namespace App\Tests\Service\Dolibarr;
 
+use App\Entity\Access\Access;
+use App\Entity\Access\FunctionType;
 use App\Entity\Core\AddressPostal;
 use App\Entity\Core\ContactPoint;
+use App\Entity\Network\Network;
+use App\Entity\Network\NetworkOrganization;
 use App\Entity\Organization\Organization;
 use App\Entity\Organization\OrganizationAddressPostal;
+use App\Entity\Organization\Settings;
 use App\Entity\Person\Person;
 use App\Enum\Access\FunctionEnum;
+use App\Enum\Access\RoleEnum;
 use App\Enum\Core\ContactPointTypeEnum;
 use App\Enum\Organization\AddressPostalOrganizationTypeEnum;
+use App\Enum\Organization\SettingsProductEnum;
 use App\Repository\Access\AccessRepository;
 use App\Repository\Access\FunctionTypeRepository;
-use App\Repository\Core\ContactPointRepository;
 use App\Repository\Organization\OrganizationRepository;
 use App\Service\Dolibarr\DolibarrApiService;
 use App\Service\Dolibarr\DolibarrSyncService;
+use Doctrine\Common\Collections\ArrayCollection;
 use JetBrains\PhpStorm\Pure;
 use libphonenumber\PhoneNumber;
 use PHPUnit\Framework\TestCase;
@@ -41,7 +48,6 @@ class DolibarrSyncServiceTest extends TestCase
 {
     private OrganizationRepository $organizationRepository;
     private AccessRepository $accessRepository;
-    private ContactPointRepository $contactPointRepository;
     private FunctionTypeRepository $functionTypeRepository;
     private DolibarrApiService $dolibarrApiService;
     private TranslatorInterface $translator;
@@ -54,9 +60,6 @@ class DolibarrSyncServiceTest extends TestCase
         $this->accessRepository = $this->getMockBuilder(AccessRepository::class)
             ->disableOriginalConstructor()
             ->getMock();
-        $this->contactPointRepository = $this->getMockBuilder(ContactPointRepository::class)
-            ->disableOriginalConstructor()
-            ->getMock();
         $this->functionTypeRepository = $this->getMockBuilder(FunctionTypeRepository::class)
             ->disableOriginalConstructor()
             ->getMock();
@@ -69,6 +72,11 @@ class DolibarrSyncServiceTest extends TestCase
         $this->logger = $this->getMockBuilder(LoggerInterface::class)
             ->disableOriginalConstructor()
             ->getMock();
+
+        $this->logger->method('info')->willReturnSelf();
+        $this->logger->method('debug')->willReturnSelf();
+        $this->logger->method('warning')->willReturnSelf();
+        $this->logger->method('error')->willReturnSelf();
     }
 
     #[Pure]
@@ -77,7 +85,6 @@ class DolibarrSyncServiceTest extends TestCase
         return new TestableDolibarrSyncService(
              $this->organizationRepository,
              $this->accessRepository,
-             $this->contactPointRepository,
              $this->functionTypeRepository,
              $this->dolibarrApiService,
              $this->translator,
@@ -90,6 +97,167 @@ class DolibarrSyncServiceTest extends TestCase
         return json_decode(file_get_contents($filepath), true);
     }
 
+    public function testScan() {
+
+        // mock services and special methods from repos
+        $this->dolibarrApiService
+            ->expects($this->once())
+            ->method('getAllClients')
+            ->willReturn(
+                $this->getJsonContentFromFixture('thirdparty.json')
+            );
+
+        $this->dolibarrApiService->method('getSociety')->willReturnMap(
+            [
+                [12097, ['id' => 711]],
+                [91295, ['id' => 5086]]
+            ]
+        );
+        $this->accessRepository
+            ->expects($this->once())
+            ->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()],
+                ]
+            );
+
+        // Function types
+        $functionType1 = $this->getMockBuilder(FunctionType::class)->getMock();
+        $functionType1->method('getMission')->willReturn(FunctionEnum::DIRECTOR()->getValue());
+        $functionType2 = $this->getMockBuilder(FunctionType::class)->getMock();
+        $functionType2->method('getMission')->willReturn(FunctionEnum::PRESIDENT()->getValue());
+        $this->functionTypeRepository
+            ->expects($this->once())
+            ->method('findBy')
+            ->with(['roleByDefault' => RoleEnum::ROLE_ADMIN()->getValue()])
+            ->willReturn([$functionType1, $functionType2]);
+
+        // Organization's name
+        $organization = $this->getMockBuilder(Organization::class)->getMock();
+        $organization->method('getName')->willReturn("Etablissement d'Enseignement Artistique");
+
+        // Postal address
+        $organizationAddressPostal = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock();
+        $addressPostal = $this->getMockBuilder(AddressPostal::class)->getMock();
+        $addressPostal->method('getStreetAddress')->willReturn('21b baker street');
+        $addressPostal->method('getStreetAddressSecond')->willReturn('');
+        $addressPostal->method('getStreetAddressThird')->willReturn('');
+        $addressPostal->method('getAddressOwner')->willReturn('');
+        $addressPostal->method('getPostalCode')->willReturn('250 329');
+        $addressPostal->method('getAddressCity')->willReturn('Londres');
+        $organizationAddressPostal->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_CONTACT()->getValue());
+        $organizationAddressPostal->method('getAddressPostal')->willReturn($addressPostal);
+        $organization->method('getOrganizationAddressPostals')->willReturn(
+            new ArrayCollection([$organizationAddressPostal])
+        );
+
+        // Email and phone
+        $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');
+        $contactPoint->method('getTelphone')->willReturn($phoneNumber);
+        $organization->method('getContactPoints')->willReturn(
+            new ArrayCollection([$contactPoint])
+        );
+
+        // Network
+        $network = $this->getMockBuilder(Network::class)->getMock();
+        $network->method('getId')->willReturn(91295);
+        $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
+        $networkOrganization->method('getNetwork')->willReturn($network);
+        $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([$networkOrganization]));
+
+        // Product
+        $settings = $this->getMockBuilder(Settings::class)->getMock();
+        $settings->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL()->getValue());
+        $organization->method('getSettings')->willReturn($settings);
+
+        // Get dolibarr contacts
+        $this->dolibarrApiService
+        ->method('getOpentalentContacts')
+        ->with(1726)
+        ->willReturn(
+            array_filter(
+                $this->getJsonContentFromFixture('contacts.json'),
+                function ($c) {
+                    return in_array(
+                        (int)$c["array_options"]["options_2iopen_person_id"],
+                        [108939, 156252, 302117]
+                    ); }
+            )
+        );
+
+        $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');
+
+        $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');
+        $personContactPoint->method('getTelphone')->willReturn($phoneNumber);
+        $personContactPoint->method('getMobilPhone')->willReturn(null);
+        $person->method('getContactPoints')->willReturn(
+            new ArrayCollection([$personContactPoint])
+        );
+
+
+
+        $access->method('getPerson')->willReturn($person);
+        $this->accessRepository->method('find')->willReturn($access);
+
+        $syncService = $this->newDolibarrSyncService();
+
+        $operations = $syncService->scan();
+
+        $this->assertCount(4, $operations);
+
+        $this->assertEquals(
+            [
+                '[PUT thirdparties/1726]',
+                'address : `\n
+217, 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`',
+                'parent : `` => `5086`',
+                'array_options.options_2iopeninfoopentalent : `` => ` : 3\n : 1\n : 1`'
+            ],
+            $operations[0]->getChangeLog()
+        );
+        $this->assertEquals(
+            ['PUT contact/5868', ''],
+            $operations[1]->getChangeLog()
+        );
+        $this->assertEquals(
+            ['PUT thirdparty/5869', ''],
+            $operations[2]->getChangeLog()
+        );
+        $this->assertEquals(
+            ['PUT thirdparty/5871', ''],
+            $operations[2]->getChangeLog()
+        );
+    }
+
     public function testGetDolibarrSocietiesIndex() {
         $this->dolibarrApiService
             ->expects($this->once())

+ 131 - 0
tests/Service/Dolibarr/fixtures/thirdparties.json

@@ -1,4 +1,135 @@
 [
+  {
+    "entity": "1",
+    "name": "Etablissement d'Enseignement Artistique",
+    "name_alias": null,
+    "address": "\n217, rue Raoul Follereau\n",
+    "zip": "74300",
+    "town": "CLUSES",
+    "status": "1",
+    "state_id": "81",
+    "state_code": "74",
+    "state": "Haute-Savoie",
+    "phone": "+33678403010",
+    "fax": null,
+    "email": null,
+    "skype": null,
+    "twitter": null,
+    "facebook": null,
+    "linkedin": null,
+    "url": null,
+    "barcode": null,
+    "idprof1": null,
+    "idprof2": null,
+    "idprof3": null,
+    "idprof4": null,
+    "idprof5": null,
+    "idprof6": null,
+    "tva_assuj": "1",
+    "tva_intra": null,
+    "localtax1_assuj": "0",
+    "localtax1_value": null,
+    "localtax2_assuj": "0",
+    "localtax2_value": null,
+    "managers": null,
+    "capital": null,
+    "typent_id": "0",
+    "typent_code": "TE_UNKNOWN",
+    "effectif": "",
+    "effectif_id": "0",
+    "forme_juridique_code": "812",
+    "forme_juridique": null,
+    "remise_percent": "",
+    "remise_supplier_percent": "0",
+    "mode_reglement_supplier_id": null,
+    "cond_reglement_supplier_id": null,
+    "transport_mode_supplier_id": null,
+    "fk_prospectlevel": null,
+    "date_modification": 1632728888,
+    "user_modification": null,
+    "date_creation": "",
+    "user_creation": null,
+    "specimen": null,
+    "client": "2",
+    "prospect": 0,
+    "fournisseur": "0",
+    "code_client": "001855",
+    "code_fournisseur": null,
+    "code_compta": "0001855",
+    "code_compta_client": null,
+    "code_compta_fournisseur": null,
+    "note_private": null,
+    "note_public": null,
+    "stcomm_id": "0",
+    "statut_commercial": "Never contacted",
+    "stcomm_picto": null,
+    "price_level": null,
+    "outstanding_limit": null,
+    "order_min_amount": null,
+    "supplier_order_min_amount": null,
+    "parent": null,
+    "default_lang": null,
+    "ref": "1726",
+    "ref_ext": null,
+    "import_key": "crm",
+    "webservices_url": null,
+    "webservices_key": null,
+    "logo": null,
+    "logo_small": null,
+    "logo_mini": null,
+    "logo_squarred": null,
+    "logo_squarred_small": null,
+    "logo_squarred_mini": null,
+    "accountancy_code_sell": null,
+    "accountancy_code_buy": null,
+    "array_options": {
+      "options_rfltr_model_id": null,
+      "options_sirene_status": null,
+      "options_b4d_spe_name": null,
+      "options_sirene_update_date": "",
+      "options_2iopen_domain": "7",
+      "options_2iopen_structure_type": "12",
+      "options_2iopen_nombre_eleves": null,
+      "options_2iopen_software_used": "4",
+      "options_2iopen_num_portable": null,
+      "options_2iopen_structure_type_cmf": null,
+      "options_2iopen_organization_id": "37306"
+    },
+    "fk_incoterms": null,
+    "location_incoterms": null,
+    "libelle_incoterms": null,
+    "fk_multicurrency": null,
+    "multicurrency_code": null,
+    "id": "1726",
+    "linkedObjectsIds": null,
+    "canvas": null,
+    "fk_project": null,
+    "contact": null,
+    "contact_id": null,
+    "user": null,
+    "origin": null,
+    "origin_id": null,
+    "statut": null,
+    "country": "France",
+    "country_id": "1",
+    "country_code": "FR",
+    "barcode_type": null,
+    "barcode_type_code": null,
+    "barcode_type_label": null,
+    "barcode_type_coder": null,
+    "mode_reglement_id": "2",
+    "cond_reglement_id": "1",
+    "transport_mode_id": null,
+    "cond_reglement": null,
+    "shipping_method_id": null,
+    "modelpdf": null,
+    "last_main_doc": null,
+    "fk_account": "0",
+    "lastname": null,
+    "firstname": null,
+    "civility_id": null,
+    "date_validation": null
+  },
   {
     "entity": "1",
     "name": "Société Musicale l'Abeille",

+ 3 - 1
tests/Service/Dolibarr/fixtures/thirdparty.json

@@ -92,7 +92,9 @@
       "options_2iopen_nombre_eleves": null,
       "options_2iopen_software_used": "4",
       "options_2iopen_num_portable": null,
-      "options_2iopen_structure_type_cmf": null
+      "options_2iopen_structure_type_cmf": null,
+      "options_2iopen_organization_id": "37306",
+      "options_2iopeninfoopentalent": ""
     },
     "fk_incoterms": null,
     "location_incoterms": null,

+ 5 - 5
tests/Service/Rest/Operation/BaseRestOperationTest.php

@@ -6,7 +6,7 @@ use PHPUnit\Framework\TestCase;
 use Symfony\Component\HttpClient\Exception\ClientException;
 use Symfony\Contracts\HttpClient\ResponseInterface;
 
-class ForTestsBaseRestOperation extends BaseRestOperation {
+class TestableBaseRestOperation extends BaseRestOperation {
     public function getChangeLog(): array { return []; }
 }
 
@@ -21,7 +21,7 @@ class BaseRestOperationTest extends TestCase
     }
 
     public function testGetters() {
-        $operation = new ForTestsBaseRestOperation(
+        $operation = new TestableBaseRestOperation(
             'a label',
             'GET',
             '/a/path',
@@ -45,7 +45,7 @@ class BaseRestOperationTest extends TestCase
      */
     public function testExecuteValid()
     {
-        $operation = new ForTestsBaseRestOperation(
+        $operation = new TestableBaseRestOperation(
             'Update entity 1', 'PUT', 'entity/1', [], [], ['json' => '{"a":1}']
         );
 
@@ -70,7 +70,7 @@ class BaseRestOperationTest extends TestCase
      */
     public function testExecuteInvalid()
     {
-        $operation = new ForTestsBaseRestOperation(
+        $operation = new TestableBaseRestOperation(
             'Update entity 1', 'PUT', 'entity/2'
         );
 
@@ -95,7 +95,7 @@ class BaseRestOperationTest extends TestCase
      * Test execution if the request throw an HTTP exception
      */
     public function testExecutionError() {
-        $operation = new ForTestsBaseRestOperation(
+        $operation = new TestableBaseRestOperation(
             'Update entity 1', 'PUT', 'entity/3'
         );