Browse Source

refact testScan

Olivier Massot 3 years ago
parent
commit
60bf886d53
1 changed files with 240 additions and 127 deletions
  1. 240 127
      tests/Service/Dolibarr/DolibarrSyncServiceTest.php

+ 240 - 127
tests/Service/Dolibarr/DolibarrSyncServiceTest.php

@@ -102,8 +102,95 @@ class DolibarrSyncServiceTest extends TestCase
             ->setMethodsExcept(['scan'])
             ->getMock();
 
+
+        // ----- Opentalent Organizations -----
+        $orgId1 = 10;
+        $organization1 = $this->getMockBuilder(Organization::class)->getMock();
+        $organization1->method('getId')->willReturn($orgId1);
+        $organization1->method('getName')->willReturn('Organization 10');
+
+        $organizationData1 = [
+            'fullAddress' => '1 Rue Azerty',
+            'addressOwner' => 'Mr Keyboard',
+            'postalCode' => '01110',
+            'city' => 'ByteCity',
+            'email' => 'foo@bar.net',
+            'phone' => '0102030405',
+            'networkId' => NetworkEnum::CMF()->getValue(),
+            'product' => SettingsProductEnum::SCHOOL()->getValue()
+        ];
+
+        $orgId2 = 20;
+        $organization2 = $this->getMockBuilder(Organization::class)->getMock();
+        $organization2->method('getId')->willReturn($orgId2);
+        $organization2->method('getName')->willReturn('Organization 20');
+
+        $organizationData2 = [
+            'email' => null,
+            'phone' => null,
+            'networkId' => null,
+            'product' => SettingsProductEnum::ARTIST()->getValue()
+        ];
+
+        $orgId3 = 30;
+        $organization3 = null; // This organization does not exist
+
+
+        // Persons
+        $accessId1 = 11; // Person 1
+        $accessId2 = 21; // Person 2
+        $accessId3 = 12; // Shall be ignored, because not an office member
+        // access 4 does not exist
+        $accessId5 = 13; // Invalid Person
+
+        $personId1 = 100;
+        $personData1 = [
+            'name' => 'Dupont',
+            'givenName' => 'Hercules',
+            'gender' => 'Mr',
+            'email' => 'an@email.net',
+            'phone' => '0102030405',
+            'mobilePhone' => '0607080910',
+        ];
+
+        $personId2 = 200;
+        $personData2 = [
+            'name' => 'Simpson',
+            'givenName' => 'Lisa',
+            'gender' => null,
+        ];
+
+        $personId3 = 300; // Obsolete contact, does not exist anymore in the Opentalent DB
+        $personId4 = 400; // Obsolete contact, does not exist anymore in the Opentalent DB
+        $personId5 = 900; // Invalid contact with no firstname and no lastname
+        $personData5 = [
+            'name' => '',
+            'givenName' => '',
+            'gender' => null,
+        ];
+
+        $activeMembers1 = [
+            $accessId1 => [FunctionEnum::PRESIDENT()->getValue()],
+            $accessId3 => [FunctionEnum::STUDENT()->getValue()],
+            $accessId5 => [FunctionEnum::TREASURER()->getValue()]
+        ];
+
+        $activeMembers2 = [
+            $accessId2 => [FunctionEnum::PRESIDENT()->getValue()]
+        ];
+
+
+        // ----- Opentalent : other vars -----
+        $cmfId = 12097;
+        $cmfDolibarrId = 12098;
+        $ffecId = 91295;
+        $ffecDolibarrId = 91296;
+
+        // ----- Dolibarr societies -----
+        // Existing society about to be updated
+        $socId1 = 1;
         $dolibarrSociety1 = [
-            'id' => 1,
+            'id' => $socId1,
             'name' => 'Organization 10',
             'address' => '1 Rue Qwerty',
             'zip' => '01024',
@@ -117,30 +204,94 @@ class DolibarrSyncServiceTest extends TestCase
                 'options_2iopen_software_opentalent' => 'Opentalent Artist'
             ]
         ];
-        $dolibarrSociety2 = ['id' => 2, "array_options" => []];
-        $dolibarrSociety3 = ['id' => 3, "array_options" => []];
+
+        // Existing society with no data
+        $socId2 = 2;
+        $dolibarrSociety2 = [
+            'id' => $socId2,
+            "array_options" => []
+        ];
+
+        // This organization does not exist in the opentalent DB
+        $socId3 = 3;
+        $dolibarrSociety3 = null;
+
+        // Dolibarr contacts
+        $contactData1 = [
+            'id' => '1',
+            'civility_code' => '',
+            'lastname' => 'Dupond',
+            'firstname' => 'Bob',
+            'email' => 'abcd@mail.com',
+            'phone_pro' => '+33478570000',
+            'phone_mobile' => '+33682980000',
+            'poste' => 'Secrétaire',
+            'statut' => '1',
+            'array_options' => [
+                'options_2iopen_person_id' => ''
+            ]
+        ];
+
+        // An obsolete contact that should be disabled
+        $obsoleteContactData = [
+            'id' => '4',
+            'lastname' => 'Doe',
+            'firstname' => 'John',
+            'statut' => '1',
+            'array_options' => [
+                'options_2iopen_person_id' => $personId3
+            ]
+        ];
+
+        // An obsolete contact that should is already disabled
+        $obsoleteContactData2 = [
+            'id' => '5',
+            'lastname' => 'Foo',
+            'firstname' => 'John',
+            'statut' => '0',
+            'array_options' => [
+                'options_2iopen_person_id' => $personId4
+            ]
+        ];
+
+        $dolibarrSocietyContacts1 = [$contactData1, $obsoleteContactData, $obsoleteContactData2];
+        $dolibarrSocietyContacts2 = [];
+
+
+        // ----- Setup Mocks -----
+        $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"],
+            ['school', [], null, null, 'Opentalent School'],
+            ['artist', [], null, null, 'Opentalent Artist'],
+            ['Mr', [], null, null, 'MR'],
+        ]);
 
         // Get societies
+        $this->organizationRepository->method('find')
+            ->willReturnMap([
+                [$orgId1, null, null, $organization1],
+                [$orgId2, null, null, $organization2],
+                [$orgId3, null, null, $organization3]
+            ]);
+
         $dolibarrSyncService
             ->expects(self::once())
             ->method('getDolibarrSocietiesIndex')
             ->willReturn([
-                10 => $dolibarrSociety1,
-                20 => $dolibarrSociety2,
-                30 => $dolibarrSociety3
+                $orgId1 => $dolibarrSociety1,
+                $orgId2 => $dolibarrSociety2,
+                $orgId3 => $dolibarrSociety3,
             ]);
 
-        // Get members
-        $activeMembers1 = [11 => [FunctionEnum::PRESIDENT()->getValue()], 12 => [FunctionEnum::STUDENT()->getValue()], 13 => [FunctionEnum::TREASURER()->getValue()]];
-        $activeMembers2 = [21 => [FunctionEnum::PRESIDENT()->getValue()]];
-
         $dolibarrSyncService
             ->expects(self::once())
             ->method('getActiveMembersIndex')
             ->willReturn([
-                10 => $activeMembers1,
-                20 => $activeMembers2,
-                30 => [],
+                $orgId1 => $activeMembers1,
+                $orgId2 => $activeMembers2,
+                $orgId3 => []
             ]);
 
         // Function types
@@ -157,8 +308,8 @@ class DolibarrSyncServiceTest extends TestCase
         // Get CMF and FFEC ids
         $this->dolibarrApiService->method('getSociety')->willReturnMap(
             [
-                [12097, ['id' => 10]],
-                [91295, ['id' => 20]]
+                [$cmfId, ['id' => $cmfDolibarrId]],
+                [$ffecId, ['id' => $ffecDolibarrId]]
             ]
         );
 
@@ -168,21 +319,6 @@ class DolibarrSyncServiceTest extends TestCase
             ->method('sanitizeDolibarrData')
             ->willReturnCallback(function ($args) { return $args; });
 
-        $organization1 = $this->getMockBuilder(Organization::class)->getMock();
-        $organization1->method('getId')->willReturn(10);
-        $organization1->method('getName')->willReturn('Organization 10');
-
-        $organization2 = $this->getMockBuilder(Organization::class)->getMock();
-        $organization2->method('getId')->willReturn(20);
-        $organization2->method('getName')->willReturn('Organization 20');
-
-        $this->organizationRepository->method('find')
-            ->willReturnMap([
-                [10, null, null, $organization1],
-                [20, null, null, $organization2],
-                [30, null, null, null]
-            ]);
-
         $addressPostal = $this->getMockBuilder(AddressPostal::class)->getMock();
 
         $dolibarrSyncService
@@ -196,42 +332,42 @@ class DolibarrSyncServiceTest extends TestCase
         $this->addressPostalUtils
             ->method('getFullStreetAddress')
             ->with($addressPostal)
-            ->willReturn('1 Rue Azerty');
+            ->willReturn($organizationData1['fullAddress']);
 
-        $addressPostal->method('getAddressOwner')->willReturn('Mr Keyboard');
-        $addressPostal->method('getPostalCode')->willReturn('01110');
-        $addressPostal->method('getAddressCity')->willReturn('ByteCity');
+        $addressPostal->method('getAddressOwner')->willReturn($organizationData1['addressOwner']);
+        $addressPostal->method('getPostalCode')->willReturn($organizationData1['postalCode']);
+        $addressPostal->method('getAddressCity')->willReturn($organizationData1['city']);
 
         $dolibarrSyncService
             ->expects(self::exactly(2))
             ->method('getOrganizationEmail')
             ->willReturnMap([
-                [$organization1, 'foo@bar.net'],
-                [$organization2, null],
+                [$organization1, $organizationData1['email']],
+                [$organization2, $organizationData2['email']],
             ]);
 
         $dolibarrSyncService
             ->expects(self::exactly(2))
             ->method('getOrganizationPhone')
             ->willReturnMap([
-                [$organization1, '0102030405'],
-                [$organization2, null],
+                [$organization1, $organizationData1['phone']],
+                [$organization2, $organizationData2['phone']],
             ]);
 
         $dolibarrSyncService
             ->expects(self::exactly(2))
             ->method('getOrganizationNetworkId')
             ->willReturnMap([
-                [$organization1, NetworkEnum::CMF()->getValue()],
-                [$organization2, null],
+                [$organization1, $organizationData1['networkId']],
+                [$organization2, $organizationData2['networkId']],
             ]);
 
         $settings1 = $this->getMockBuilder(Settings::class)->getMock();
-        $settings1->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL()->getValue());
+        $settings1->method('getProduct')->willReturn($organizationData1['product']);
         $organization1->method('getSettings')->willReturn($settings1);
 
         $settings2 = $this->getMockBuilder(Settings::class)->getMock();
-        $settings1->method('getProduct')->willReturn(SettingsProductEnum::ARTIST()->getValue());
+        $settings1->method('getProduct')->willReturn($organizationData2['product']);
         $organization2->method('getSettings')->willReturn($settings2);
 
         $dolibarrSyncService->method('countWithMission')->willReturnMap([
@@ -241,15 +377,6 @@ class DolibarrSyncServiceTest extends TestCase
             [[FunctionEnum::DIRECTOR()->getValue(), FunctionEnum::PRESIDENT()->getValue()], $activeMembers2, 2]
         ]);
 
-        $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"],
-           ['school', [], null, null, 'Opentalent School'],
-           ['artist', [], null, null, 'Opentalent Artist'],
-           ['Mr', [], null, null, 'MR'],
-        ]);
-
         $this->arrayUtils
             ->expects(self::exactly(3))
             ->method('getChanges')
@@ -267,71 +394,31 @@ class DolibarrSyncServiceTest extends TestCase
                 }
             );
 
-        $contactData1 = [
-            'id' => '1',
-            'civility_code' => '',
-            'lastname' => 'Dupond',
-            'firstname' => 'Bob',
-            'email' => 'abcd@mail.com',
-            'phone_pro' => '+33478570000',
-            'phone_mobile' => '+33682980000',
-            'poste' => 'Secrétaire',
-            'statut' => '1',
-            'array_options' => [
-                'options_2iopen_person_id' => ''
-            ]
-        ];
-
-        // An obsolete contact that should be disabled
-        $obsoleteContactData = [
-            'id' => '4',
-            'lastname' => 'Doe',
-            'firstname' => 'John',
-            'statut' => '1',
-            'array_options' => [
-                'options_2iopen_person_id' => 300
-            ]
-        ];
-
-        // An obsolete contact that should is already disabled
-        $obsoleteContactData2 = [
-            'id' => '5',
-            'lastname' => 'Foo',
-            'firstname' => 'John',
-            'statut' => '0',
-            'array_options' => [
-                'options_2iopen_person_id' => 400
-            ]
-        ];
-
-        $dolibarrSocietyContacts1 = [$contactData1, $obsoleteContactData, $obsoleteContactData2];
-        $dolibarrSocietyContacts2 = [];
-
         $this->dolibarrApiService->method('getContacts')->willReturnMap([
-            [1, $dolibarrSocietyContacts1],
-            [2, $dolibarrSocietyContacts2]
+            [$socId1, $dolibarrSocietyContacts1],
+            [$socId2, $dolibarrSocietyContacts2]
         ]);
 
         // Loop over contacts
         // NB: Student will be skipped since it has no office role
 
         $person1 = $this->getMockBuilder(Person::class)->getMock();
-        $person1->method('getId')->willReturn(100);
-        $person1->method('getName')->willReturn('Dupont');
-        $person1->method('getGivenName')->willReturn('Hercules');
-        $person1->method('getGender')->willReturn('Mr');
+        $person1->method('getId')->willReturn($personId1);
+        $person1->method('getName')->willReturn($personData1['name']);
+        $person1->method('getGivenName')->willReturn($personData1['givenName']);
+        $person1->method('getGender')->willReturn($personData1['gender']);
 
         $person2 = $this->getMockBuilder(Person::class)->getMock();
-        $person2->method('getId')->willReturn(200);
-        $person2->method('getName')->willReturn('Simpson');
-        $person2->method('getGivenName')->willReturn('Lisa');
-        $person2->method('getGender')->willReturn(null);
+        $person2->method('getId')->willReturn($personId2);
+        $person2->method('getName')->willReturn($personData2['name']);
+        $person2->method('getGivenName')->willReturn($personData2['givenName']);
+        $person2->method('getGender')->willReturn($personData2['gender']);
 
         // An invalid person that should be ignored
-        $invalidPerson = $this->getMockBuilder(Person::class)->getMock();
-        $invalidPerson->method('getId')->willReturn(900);
-        $invalidPerson->method('getName')->willReturn('');
-        $invalidPerson->method('getGivenName')->willReturn('');
+        $person5 = $this->getMockBuilder(Person::class)->getMock();
+        $person5->method('getId')->willReturn($personId5);
+        $person5->method('getName')->willReturn($personData5['name']);
+        $person5->method('getGivenName')->willReturn($personData5['givenName']);
 
 
         $access1 = $this->getMockBuilder(Access::class)->getMock();
@@ -340,13 +427,13 @@ class DolibarrSyncServiceTest extends TestCase
         $access2 = $this->getMockBuilder(Access::class)->getMock();
         $access2->method('getPerson')->willReturn($person2);
 
-        $access3 = $this->getMockBuilder(Access::class)->getMock();
-        $access3->method('getPerson')->willReturn($invalidPerson);
+        $access5 = $this->getMockBuilder(Access::class)->getMock();
+        $access5->method('getPerson')->willReturn($person5);
 
         $this->accessRepository->method('find')->willReturnMap([
-            [11, null, null, $access1],
-            [21, null, null, $access2],
-            [13, null, null, $access3],
+            [$accessId1, null, null, $access1],
+            [$accessId2, null, null, $access2],
+            [$accessId5, null, null, $access5],
         ]);
 
         $dolibarrSyncService->method('findDolibarrContactFor')->willReturnMap([
@@ -355,12 +442,15 @@ class DolibarrSyncServiceTest extends TestCase
         ]);
 
         $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock();
-        $contactPoint1->method('getEmail')->willReturn('mail@domain.net');
+        $contactPoint1->method('getEmail')->willReturn($personData1['email']);
 
         $phone = $this->getMockBuilder(PhoneNumber::class)->getMock();
-        $contactPoint1->method('getTelphone')->willReturn($phone);
-
         $mobilePhone = $this->getMockBuilder(PhoneNumber::class)->getMock();
+        $dolibarrSyncService->method('formatPhoneNumber')->willReturnMap([
+            [$phone, $personData1['phone']],
+            [$mobilePhone, $personData1['mobilePhone']],
+        ]);
+        $contactPoint1->method('getTelphone')->willReturn($phone);
         $contactPoint1->method('getMobilPhone')->willReturn($mobilePhone);
 
         $dolibarrSyncService->method('getPersonContact')->willReturnMap([
@@ -368,23 +458,19 @@ class DolibarrSyncServiceTest extends TestCase
            [$person2, null],
         ]);
 
-        $dolibarrSyncService->method('formatPhoneNumber')->willReturnMap([
-            [$phone, '0102030405'],
-            [$mobilePhone, '0607080910'],
-        ]);
-
         $dolibarrSyncService->method('formatContactPosition')->willReturnMap([
             [[FunctionEnum::PRESIDENT()->getValue()], 'Mr', 'Président'],
             [[FunctionEnum::PRESIDENT()->getValue()], null, 'Président(e)'],
         ]);
 
+        // Expected logged error messages
         $this->logger->expects(self::exactly(2))->method('error')->withConsecutive(
             ["Person 900 miss a lastname and/or a firstname, ignored."],
             ["Organization 30 not found in the Opentalent DB"],
         );
 
+        // Expected progression callback triggers
         $progressionCallbackExpectedCalls = [[1, 3], [2, 3], [3, 3]];
-
         $progressionCallback = static function ($i, $total) use (&$progressionCallbackExpectedCalls) {
             [$expectedI, $expectedTotal] = array_shift($progressionCallbackExpectedCalls);
             if ($i !== $expectedI || $total !== $expectedTotal) {
@@ -407,7 +493,7 @@ class DolibarrSyncServiceTest extends TestCase
                 'town : `Ram` => `ByteCity`',
                 'email : `some@email.com` => `foo@bar.net`',
                 'phone : `` => `0102030405`',
-                'parent : `0` => `10`',
+                'parent : `0` => `12098`',
                 "array_options.options_2iopen_software_opentalent : `Opentalent Artist` => `Opentalent School`",
                 "array_options.options_2iopeninfoopentalent : `` => `Nombre d'élèves : 1\nNombre d'adhérents : 2\nNombre d'accès admin : 1`",
                 'status : `0` => `1`'
@@ -421,7 +507,7 @@ class DolibarrSyncServiceTest extends TestCase
                 'civility_code : `` => `MR`',
                 'lastname : `Dupond` => `Dupont`',
                 'firstname : `Bob` => `Hercules`',
-                'email : `abcd@mail.com` => `mail@domain.net`',
+                'email : `abcd@mail.com` => `an@email.net`',
                 'phone_pro : `+33478570000` => `0102030405`',
                 'phone_mobile : `+33682980000` => `0607080910`',
                 'poste : `Secrétaire` => `Président`',
@@ -473,6 +559,12 @@ class DolibarrSyncServiceTest extends TestCase
 
     public function testExecuteError()
     {
+        $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
+            ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
+                $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
+            ->setMethodsExcept(['execute'])
+            ->getMock();
+
         $operation = new CreateOperation('operation 1', 'thirdparty', ['data' => 1]);
         $this->assertEquals($operation->getStatus(), $operation::STATUS_READY);
 
@@ -481,13 +573,18 @@ class DolibarrSyncServiceTest extends TestCase
         $this->dolibarrApiService->method('request')->willReturn($responseError);
 
         // POST operation will returned a server error
-        $syncService = $this->newDolibarrSyncService();
-        $operation = $syncService->execute([$operation])[0];
+        $operation = $dolibarrSyncService->execute([$operation])[0];
         $this->assertEquals($operation::STATUS_ERROR, $operation->getStatus());
     }
 
     public function testExecuteInvalid()
     {
+        $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
+            ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
+                $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
+            ->setMethodsExcept(['execute'])
+            ->getMock();
+
         $operation = new UpdateOperation('operation 1', 'thirdparty', 1, ['data' => 1]);
         $responseInvalid = $this->getMockBuilder(ResponseInterface::class)->getMock();
         $responseInvalid->method('getStatusCode')->willReturn(200);
@@ -497,20 +594,36 @@ class DolibarrSyncServiceTest extends TestCase
         // POST operation will return a different content that the one which were posted, this should log a warning
         $this->logger->expects($this->once())->method('warning');
 
-        $syncService = $this->newDolibarrSyncService();
-        $operation = $syncService->execute([$operation])[0];
+        $operation = $dolibarrSyncService->execute([$operation])[0];
         $this->assertEquals($operation::STATUS_DONE, $operation->getStatus());
     }
 
     public function testExecuteOk() {
+        $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
+            ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
+                $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
+            ->setMethodsExcept(['execute'])
+            ->getMock();
+
+        $progressionCallbackExpectedCalls = [[1, 1]];
+
+        $progressionCallback = static function ($i, $total) use (&$progressionCallbackExpectedCalls) {
+            [$expectedI, $expectedTotal] = array_shift($progressionCallbackExpectedCalls);
+            if ($i !== $expectedI || $total !== $expectedTotal) {
+                throw new \AssertionError(
+                    'Progression callback error, expected parameters are (' . $expectedI . ',' . $expectedTotal . ')' .
+                    ', got (' . $i . ', ' . $total . ')'
+                );
+            }
+        };
+
         $operation = new CreateOperation('operation 1', 'thirdparty', ['data' => 1]);
         $responseOk = $this->getMockBuilder(ResponseInterface::class)->getMock();
         $responseOk->method('getStatusCode')->willReturn(200);
         $responseOk->method('toArray')->willReturn(['data' => 1]);
         $this->dolibarrApiService->method('request')->willReturn($responseOk);
 
-        $syncService = $this->newDolibarrSyncService();
-        $operation = $syncService->execute([$operation])[0];
+        $operation = $dolibarrSyncService->execute([$operation])[0];
         $this->assertEquals($operation::STATUS_DONE, $operation->getStatus());
     }