organizationRepository = $this->getMockBuilder(OrganizationRepository::class) ->disableOriginalConstructor() ->getMock(); $this->accessRepository = $this->getMockBuilder(AccessRepository::class) ->disableOriginalConstructor() ->getMock(); $this->functionTypeRepository = $this->getMockBuilder(FunctionTypeRepository::class) ->disableOriginalConstructor() ->getMock(); $this->dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->disableOriginalConstructor() ->getMock(); $this->addressPostalUtils = $this->getMockBuilder(AddressPostalUtils::class) ->disableOriginalConstructor() ->getMock(); $this->arrayUtils = $this->getMockBuilder(ArrayUtils::class) ->disableOriginalConstructor() ->getMock(); $this->translator = $this->getMockBuilder(TranslatorInterface::class) ->disableOriginalConstructor() ->getMock(); $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(); } public function testScan(): void { $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class) ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository, $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger]) ->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' => $socId1, 'name' => 'Organization 10', 'address' => '1 Rue Qwerty', 'zip' => '01024', 'town' => 'Ram', 'email' => 'some@email.com', 'phone' => null, 'status' => 0, 'parent' => '0', 'array_options' => [ 'options_2iopeninfoopentalent' => '', 'options_2iopen_software_opentalent' => 'Opentalent Artist' ] ]; // 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([ $orgId1 => $dolibarrSociety1, $orgId2 => $dolibarrSociety2, $orgId3 => $dolibarrSociety3, ]); $dolibarrSyncService ->expects(self::once()) ->method('getActiveMembersIndex') ->willReturn([ $orgId1 => $activeMembers1, $orgId2 => $activeMembers2, $orgId3 => [] ]); // 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]); // Get CMF and FFEC ids $this->dolibarrApiService->method('getSociety')->willReturnMap( [ [$cmfId, ['id' => $cmfDolibarrId]], [$ffecId, ['id' => $ffecDolibarrId]] ] ); // -- Loop over organizations -- $dolibarrSyncService ->expects(self::exactly(5)) // 3 organizations and 2 contacts ->method('sanitizeDolibarrData') ->willReturnCallback(function ($args) { return $args; }); $addressPostal = $this->getMockBuilder(AddressPostal::class)->getMock(); $dolibarrSyncService ->expects(self::exactly(2)) ->method('getOrganizationPostalAddress') ->willReturnMap([ [$organization1, $addressPostal], [$organization2, null], ]); $this->addressPostalUtils ->method('getFullStreetAddress') ->with($addressPostal) ->willReturn($organizationData1['fullAddress']); $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, $organizationData1['email']], [$organization2, $organizationData2['email']], ]); $dolibarrSyncService ->expects(self::exactly(2)) ->method('getOrganizationPhone') ->willReturnMap([ [$organization1, $organizationData1['phone']], [$organization2, $organizationData2['phone']], ]); $dolibarrSyncService ->expects(self::exactly(2)) ->method('getOrganizationNetworkId') ->willReturnMap([ [$organization1, $organizationData1['networkId']], [$organization2, $organizationData2['networkId']], ]); $settings1 = $this->getMockBuilder(Settings::class)->getMock(); $settings1->method('getProduct')->willReturn($organizationData1['product']); $organization1->method('getSettings')->willReturn($settings1); $settings2 = $this->getMockBuilder(Settings::class)->getMock(); $settings1->method('getProduct')->willReturn($organizationData2['product']); $organization2->method('getSettings')->willReturn($settings2); $dolibarrSyncService->method('countWithMission')->willReturnMap([ [[FunctionEnum::STUDENT()->getValue()], $activeMembers1, 1], [[FunctionEnum::ADHERENT()->getValue()], $activeMembers1, 2], [[FunctionEnum::DIRECTOR()->getValue(), FunctionEnum::PRESIDENT()->getValue()], $activeMembers1, 1], [[FunctionEnum::DIRECTOR()->getValue(), FunctionEnum::PRESIDENT()->getValue()], $activeMembers2, 2] ]); $this->arrayUtils ->expects(self::exactly(3)) ->method('getChanges') ->willReturnCallback( function (array $initialArray, array $newArray, bool $recursive = false, ?callable $callback = null) { if (in_array('name', $newArray, true)) { // Organization 1 name is already defined and has not been changed unset($newArray['name']); } if (in_array('statut', $newArray, true)) { // Contact 1 statut is already defined and has not been changed unset($newArray['statut']); } return $newArray; } ); $this->dolibarrApiService->method('getContacts')->willReturnMap([ [$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($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($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 $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(); $access1->method('getPerson')->willReturn($person1); $access2 = $this->getMockBuilder(Access::class)->getMock(); $access2->method('getPerson')->willReturn($person2); $access5 = $this->getMockBuilder(Access::class)->getMock(); $access5->method('getPerson')->willReturn($person5); $this->accessRepository->method('find')->willReturnMap([ [$accessId1, null, null, $access1], [$accessId2, null, null, $access2], [$accessId5, null, null, $access5], ]); $dolibarrSyncService->method('findDolibarrContactFor')->willReturnMap([ [$dolibarrSocietyContacts1, $person1, $contactData1], [$dolibarrSocietyContacts2, $person2, null] ]); $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock(); $contactPoint1->method('getEmail')->willReturn($personData1['email']); $phone = $this->getMockBuilder(PhoneNumber::class)->getMock(); $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([ [$person1, $contactPoint1], [$person2, null], ]); $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) { throw new \AssertionError( 'Progression callback error, expected parameters are (' . $expectedI . ',' . $expectedTotal . ')' . ', got (' . $i . ', ' . $total . ')' ); } }; $operations = $dolibarrSyncService->scan($progressionCallback); $this->assertCount(5, $operations); $this->assertEqualsCanonicalizing( [ '[PUT thirdparties/1]', "address : `1 Rue Qwerty` => `Mr Keyboard\n1 Rue Azerty`", 'zip : `01024` => `01110`', 'town : `Ram` => `ByteCity`', 'email : `some@email.com` => `foo@bar.net`', 'phone : `` => `0102030405`', '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`' ], $operations[0]->getChangeLog() ); $this->assertEqualsCanonicalizing( [ '[PUT contacts/1]', 'civility_code : `` => `MR`', 'lastname : `Dupond` => `Dupont`', 'firstname : `Bob` => `Hercules`', 'email : `abcd@mail.com` => `an@email.net`', 'phone_pro : `+33478570000` => `0102030405`', 'phone_mobile : `+33682980000` => `0607080910`', 'poste : `Secrétaire` => `Président`', 'array_options.options_2iopen_person_id : `` => `100`' ], $operations[1]->getChangeLog() ); $this->assertEqualsCanonicalizing( ['[PUT contacts/4]', 'statut : `1` => `0`'], $operations[2]->getChangeLog() ); $this->assertEqualsCanonicalizing( [ '[PUT thirdparties/2]', 'address : (new) => ``', 'email : (new) => ``', 'name : (new) => `Organization 20`', 'parent : (new) => ``', 'phone : (new) => ``', 'status : (new) => `1`', 'town : (new) => ``', 'zip : (new) => ``', 'array_options.options_2iopeninfoopentalent : (new) => `Nombre d\'accès admin : 2`', ], $operations[3]->getChangeLog() ); $this->assertEqualsCanonicalizing( [ '[POST contacts]', 'civility_code : (new) => ``', 'lastname : (new) => `Simpson`', 'firstname : (new) => `Lisa`', 'email : (new) => ``', 'phone_pro : (new) => ``', 'phone_mobile : (new) => ``', 'poste : (new) => `Président(e)`', 'statut : (new) => `1`', 'array_options.options_2iopen_person_id : (new) => `200`', 'socid : (new) => `2`' ], $operations[4]->getChangeLog() ); $this->assertCount(0, $progressionCallbackExpectedCalls); } 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); $responseError = $this->getMockBuilder(ResponseInterface::class)->getMock(); $responseError->method('getStatusCode')->willReturn(500); $this->dolibarrApiService->method('request')->willReturn($responseError); // POST operation will returned a server error $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); $responseInvalid->method('toArray')->willReturn(['data' => 0]); $this->dolibarrApiService->method('request')->willReturn($responseInvalid); // 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'); $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); $operation = $dolibarrSyncService->execute([$operation])[0]; $this->assertEquals($operation::STATUS_DONE, $operation->getStatus()); } public function testGetDolibarrSocietiesIndex() { $this->dolibarrApiService ->expects($this->once()) ->method('getAllClients') ->willReturn( $this->getJsonContentFromFixture('thirdparties.json') ); $syncService = $this->newDolibarrSyncService(); $index = $syncService->getDolibarrSocietiesIndex(); $this->assertEquals("13930", $index[13930]['array_options']['options_2iopen_organization_id']); } public function testFindDolibarrContactFor() { $contacts = $this->getJsonContentFromFixture('contacts.json'); // Find by id $person1 = $this->getMockBuilder(Person::class)->getMock(); $person1->method('getId')->willReturn(108939); $contact1 = TestableDolibarrSyncService::findDolibarrContactFor($contacts, $person1); $this->assertEquals("5868", $contact1['id']); // Find by full name (contact already has another person id, it should not be returned) $person2 = $this->getMockBuilder(Person::class)->getMock(); $person2->method('getId')->willReturn(-1); $person2->method('getName')->willReturn('dupont'); $person2->method('getGivenName')->willReturn('Valerie'); $contact2 = TestableDolibarrSyncService::findDolibarrContactFor($contacts, $person2); $this->assertEquals(null, $contact2); // Find by full name (contact has no person id, it should be returned) $person3 = $this->getMockBuilder(Person::class)->getMock(); $person3->method('getId')->willReturn(-1); $person3->method('getName')->willReturn('ZORRO'); $person3->method('getGivenName')->willReturn('Fabrice'); $contact3 = TestableDolibarrSyncService::findDolibarrContactFor($contacts, $person3); $this->assertEquals("5872", $contact3['id']); // Do not find $person4 = $this->getMockBuilder(Person::class)->getMock(); $person4->method('getId')->willReturn(-1); $contact4 = TestableDolibarrSyncService::findDolibarrContactFor($contacts, $person4); $this->assertEquals(null, $contact4); } public function testActiveMembersIndex() { $this->accessRepository ->expects($this->once()) ->method('getAllActiveMembersAndMissions') ->willReturn( [ ['id' => 123, 'organization_id' => 1, 'mission' => FunctionEnum::PRESIDENT()->getValue()], ['id' => 123, 'organization_id' => 1, 'mission' => FunctionEnum::TEACHER()->getValue()], ['id' => 124, 'organization_id' => 1, 'mission' => FunctionEnum::ADMINISTRATIVE_STAFF()->getValue()], ['id' => 125, 'organization_id' => 2, 'mission' => FunctionEnum::ADHERENT()->getValue()], ] ); $syncService = $this->newDolibarrSyncService(); $this->assertEquals( [ 1 => [ 123 => [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::TEACHER()->getValue()], 124 => [FunctionEnum::ADMINISTRATIVE_STAFF()->getValue()] ], 2 => [ 125 => [FunctionEnum::ADHERENT()->getValue()] ] ], $syncService->getActiveMembersIndex() ); } public function testSanitizeDolibarrData() { $this->assertEquals(null, TestableDolibarrSyncService::sanitizeDolibarrData(null)); $this->assertEquals( ['a' => 'A', 'b' => null, 'c' => ['d' => 'D', 'e' => null]], TestableDolibarrSyncService::sanitizeDolibarrData( ['a' => 'A', 'b' => '', 'c' => ['d' => 'D', 'e' => '']] ) ); } public function testGetOrganizationPostalAddress() { $organization = $this->getMockBuilder(Organization::class)->getMock(); $organizationAddressPostal1 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock(); $organizationAddressPostal2 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock(); $organizationAddressPostal3 = $this->getMockBuilder(OrganizationAddressPostal::class)->getMock(); $addressPostal = $this->getMockBuilder(AddressPostal::class)->getMock(); $organizationAddressPostal1->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_PRACTICE()->getValue()); $organizationAddressPostal2->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_BILL()->getValue()); $organizationAddressPostal3->method('getType')->willReturn(AddressPostalOrganizationTypeEnum::ADDRESS_OTHER()->getValue()); $organizationAddressPostal2->method('getAddressPostal')->willReturn($addressPostal); $organization->expects($this->once()) ->method('getOrganizationAddressPostals') ->willReturn( new ArrayCollection([$organizationAddressPostal1, $organizationAddressPostal2, $organizationAddressPostal3]) ); $syncService = $this->newDolibarrSyncService($organization); $this->assertEquals( $addressPostal, $syncService->getOrganizationPostalAddress($organization) ); $organization2 = $this->getMockBuilder(Organization::class)->getMock(); $organization2->expects($this->once()) ->method('getOrganizationAddressPostals') ->willReturn(new ArrayCollection([])); $this->assertEquals( null, $syncService->getOrganizationPostalAddress($organization2) ); } public function testGetOrganizationPhoneWithExistingPhone() { $organization = $this->getMockBuilder(Organization::class)->getMock(); $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock(); $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock(); $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock(); $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER()->getValue()); $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL()->getValue()); $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL()->getValue()); $phoneUtil = PhoneNumberUtil::getInstance(); $phoneNumber = $phoneUtil->parse('0161626365', "FR"); $contactPoint2->method('getTelphone')->willReturn($phoneNumber); $organization ->expects($this->once()) ->method('getContactPoints') ->willReturn( new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3]) ); $syncService = $this->newDolibarrSyncService(); $this->assertEquals( '+33161626365', $syncService->getOrganizationPhone($organization) ); } public function testGetOrganizationPhoneWithMobilePhone() { $organization = $this->getMockBuilder(Organization::class)->getMock(); $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock(); $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock(); $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock(); $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER()->getValue()); $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL()->getValue()); $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL()->getValue()); $contactPoint2->expects($this->once())->method('getTelphone')->willReturn(null); $phoneUtil = PhoneNumberUtil::getInstance(); $phoneNumber = $phoneUtil->parse('0661626365', "FR"); $contactPoint2->method('getMobilPhone')->willReturn($phoneNumber); $organization ->expects($this->once()) ->method('getContactPoints') ->willReturn( new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3]) ); $syncService = $this->newDolibarrSyncService(); $this->assertEquals( '+33661626365', $syncService->getOrganizationPhone($organization) ); } public function testGetOrganizationPhoneWithNoPhone() { $organization = $this->getMockBuilder(Organization::class)->getMock(); $organization ->expects($this->once()) ->method('getContactPoints') ->willReturn(new ArrayCollection([])); $syncService = $this->newDolibarrSyncService(); $this->assertEquals( null, $syncService->getOrganizationPhone($organization) ); } public function testGetOrganizationEmailWithExistingEmail() { $organization = $this->getMockBuilder(Organization::class)->getMock(); $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock(); $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock(); $contactPoint3 = $this->getMockBuilder(ContactPoint::class)->getMock(); $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER()->getValue()); $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::BILL()->getValue()); $contactPoint3->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL()->getValue()); $contactPoint2->method('getEmail')->willReturn('email@email.com'); $organization ->expects($this->once()) ->method('getContactPoints') ->willReturn( new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3]) ); $syncService = $this->newDolibarrSyncService(); $this->assertEquals( 'email@email.com', $syncService->getOrganizationEmail($organization) ); } public function testGetOrganizationEmailWithNoEmail() { $organization = $this->getMockBuilder(Organization::class)->getMock(); $organization ->expects($this->once()) ->method('getContactPoints') ->willReturn(new ArrayCollection([])); $syncService = $this->newDolibarrSyncService(); $this->assertEquals( null, $syncService->getOrganizationEmail($organization) ); } public function testGetOrganizationNetworkId() { $organization = $this->getMockBuilder(Organization::class)->getMock(); $network = $this->getMockBuilder(Network::class)->getMock(); $network->method('getId')->willReturn(3); $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock(); $networkOrganization->method('getNetwork')->willReturn($network); $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([$networkOrganization])); $syncService = $this->newDolibarrSyncService(); $this->assertEquals( 3, $syncService->getOrganizationNetworkId($organization) ); } public function testGetOrganizationNetworkIdWithMultipleResult() { $organization = $this->getMockBuilder(Organization::class)->getMock(); $network1 = $this->getMockBuilder(Network::class)->getMock(); $network1->method('getId')->willReturn(3); $networkOrganization1 = $this->getMockBuilder(NetworkOrganization::class)->getMock(); $networkOrganization1->method('getNetwork')->willReturn($network1); $networkOrganization1->method('getEndDate')->willReturn(new \DateTime('2000-01-01')); $network2 = $this->getMockBuilder(Network::class)->getMock(); $network2->method('getId')->willReturn(4); $networkOrganization2 = $this->getMockBuilder(NetworkOrganization::class)->getMock(); $networkOrganization2->method('getNetwork')->willReturn($network2); $networkOrganization2->method('getEndDate')->willReturn(null); $organization->method('getNetworkOrganizations')->willReturn( new ArrayCollection([$networkOrganization1, $networkOrganization2]) ); $syncService = $this->newDolibarrSyncService(); $this->assertEquals( 4, $syncService->getOrganizationNetworkId($organization) ); } public function testGetOrganizationNetworkIdWithNoResult() { $organization = $this->getMockBuilder(Organization::class)->getMock(); $network = $this->getMockBuilder(Network::class)->getMock(); $network->method('getId')->willReturn(3); $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock(); $networkOrganization->method('getNetwork')->willReturn($network); $networkOrganization->method('getEndDate')->willReturn(new \DateTime('2000-01-01')); $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([$networkOrganization])); $syncService = $this->newDolibarrSyncService(); $this->assertEquals( null, $syncService->getOrganizationNetworkId($organization) ); } public function testCountWithMission() { $members = [ 123 => [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::TEACHER()->getValue()], 124 => [FunctionEnum::TEACHER()->getValue()], 125 => [FunctionEnum::STUDENT()->getValue()], 126 => [FunctionEnum::TREASURER()->getValue()], ]; $this->assertEquals( 2, TestableDolibarrSyncService::countWithMission([FunctionEnum::TEACHER()->getValue()], $members) ); $this->assertEquals( 3, TestableDolibarrSyncService::countWithMission( [FunctionEnum::TEACHER()->getValue(), FunctionEnum::TREASURER()->getValue()], $members ) ); $this->assertEquals( 1, TestableDolibarrSyncService::countWithMission([FunctionEnum::STUDENT()->getValue()], $members) ); $this->assertEquals( 0, TestableDolibarrSyncService::countWithMission([FunctionEnum::ARCHIVIST()->getValue()], $members) ); } public function testGetPersonContact() { $person = $this->getMockBuilder(Person::class)->getMock(); $contactPoint1 = $this->getMockBuilder(ContactPoint::class)->getMock(); $contactPoint2 = $this->getMockBuilder(ContactPoint::class)->getMock(); $contactPoint1->method('getContactType')->willReturn(ContactPointTypeEnum::OTHER()->getValue()); $contactPoint2->method('getContactType')->willReturn(ContactPointTypeEnum::PRINCIPAL()->getValue()); $person->expects($this->once())->method('getContactPoints')->willReturn(new ArrayCollection([$contactPoint1, $contactPoint2])); $syncService = $this->newDolibarrSyncService(); $this->assertEquals( $contactPoint2, $syncService->getPersonContact($person) ); $person2 = $this->getMockBuilder(Person::class)->getMock(); $person2->expects($this->once())->method('getContactPoints')->willReturn(new ArrayCollection([])); $this->assertEquals(null, $syncService->getPersonContact($person2)); } public function testFormatContactPosition() { $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(); $this->assertEquals( 'Président(e)', $syncService->formatContactPosition([FunctionEnum::PRESIDENT()->getValue()]) ); $this->assertEquals( 'Président', $syncService->formatContactPosition([FunctionEnum::PRESIDENT()->getValue()], 'MISTER') ); $this->assertEquals( 'Présidente', $syncService->formatContactPosition([FunctionEnum::PRESIDENT()->getValue()], 'MISS') ); $this->assertEquals( 'Présidente, Directrice', $syncService->formatContactPosition( [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::DIRECTOR()->getValue()], 'MISS' ) ); $this->assertEquals( 'Président, Directeur', $syncService->formatContactPosition( [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::DIRECTOR()->getValue(), FunctionEnum::ADHERENT()->getValue()], 'MISTER' ) ); } public function testFormatPhoneNumber() { $phoneUtil = PhoneNumberUtil::getInstance(); $phoneNumber = $phoneUtil->parse('01 02 03 04 05', "FR"); $this->assertEquals( '+33102030405', TestableDolibarrSyncService::formatPhoneNumber($phoneNumber) ); } }