| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401 |
- <?php /** @noinspection ALL */
- 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\Network\NetworkEnum;
- use App\Enum\Organization\AddressPostalOrganizationTypeEnum;
- use App\Enum\Organization\SettingsProductEnum;
- use App\Repository\Access\AccessRepository;
- use App\Repository\Access\FunctionTypeRepository;
- use App\Repository\Organization\OrganizationRepository;
- use App\Service\Dolibarr\DolibarrApiService;
- use App\Service\Dolibarr\DolibarrSyncService;
- use App\Service\Core\AddressPostalUtils;
- use App\Service\Rest\Operation\BaseRestOperation;
- use App\Service\Rest\Operation\CreateOperation;
- use App\Service\Rest\Operation\DeleteOperation;
- use App\Service\Rest\Operation\UpdateOperation;
- use App\Service\Utils\ArrayUtils;
- use Doctrine\Common\Collections\ArrayCollection;
- use libphonenumber\PhoneNumber;
- use libphonenumber\PhoneNumberUtil;
- use PHPUnit\Framework\MockObject\MockObject;
- use PHPUnit\Framework\TestCase;
- use Psr\Log\LoggerInterface;
- use RuntimeException;
- use Symfony\Component\HttpClient\Exception\ServerException;
- use Symfony\Contracts\HttpClient\ResponseInterface;
- use Symfony\Contracts\Translation\TranslatorInterface;
- class TestableDolibarrSyncService extends DolibarrSyncService {
- public function getDolibarrSocietiesIndex(): array { return parent::getDolibarrSocietiesIndex(); }
- public function findDolibarrContactFor(array $dolibarrContacts, Person $person): ?array { return parent::findDolibarrContactFor($dolibarrContacts, $person); }
- public function getActiveMembersIndex(): array { return parent::getActiveMembersIndex(); }
- public function sanitizeDolibarrData(?array $data): ?array { return parent::sanitizeDolibarrData($data); }
- 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 function getOrganizationNetworkId(Organization $organization): ?int { return parent::getOrganizationNetworkId($organization); }
- public function countWithMission(array $missions, array $members): int { return parent::countWithMission($missions, $members); }
- public function getPersonContact(Person $person): ?ContactPoint { return parent::getPersonContact($person); }
- public function formatContactPosition(array $missions, ?string $gender = 'X'): string { return parent::formatContactPosition($missions, $gender); }
- public function formatPhoneNumber(PhoneNumber $phoneNumber): string { return parent::formatPhoneNumber($phoneNumber); }
- public function validateResponse(ResponseInterface $response, BaseRestOperation $operation): void { parent::validateResponse($response, $operation); }
- }
- class DolibarrSyncServiceTest extends TestCase
- {
- private MockObject | OrganizationRepository $organizationRepository;
- private MockObject | AccessRepository $accessRepository;
- private MockObject | FunctionTypeRepository $functionTypeRepository;
- private MockObject | DolibarrApiService $dolibarrApiService;
- private MockObject | AddressPostalUtils $addressPostalUtils;
- private MockObject | ArrayUtils $arrayUtils;
- private MockObject | TranslatorInterface $translator;
- private MockObject | LoggerInterface $logger;
- public function setUp(): void {
- $this->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();
- }
- /**
- * Full test of the scan method
- *
- * @see DolibarrSyncService::scan()
- */
- 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);
- }
- /**
- * All valid operations shall be executed
- * If an operation is not in status READY, a warning shall be logged, and the operation shall be skipped
- *
- * @see DolibarrSyncService::execute()
- */
- public function testExecute(): void
- {
- $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, 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 . ')'
- );
- }
- };
- $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
- $operation1 = $this->getMockBuilder(UpdateOperation::class)->disableOriginalConstructor()->getMock();
- $operation1->method('getStatus')->willReturnOnConsecutiveCalls(BaseRestOperation::STATUS_READY, BaseRestOperation::STATUS_DONE);
- $operation1->method('getChangeLog')->willReturn(['foo']);
- $operation1->expects(self::once())->method('execute')->willReturn($response);
- $dolibarrSyncService->method('validateResponse')->with($response, $operation1)->willThrowException(new RuntimeException());
- $operation2 = $this->getMockBuilder(CreateOperation::class)->disableOriginalConstructor()->getMock();
- $operation2->method('getStatus')->willReturn(
- BaseRestOperation::STATUS_READY, BaseRestOperation::STATUS_ERROR, BaseRestOperation::STATUS_ERROR // An error happened
- );
- $operation2->expects(self::once())->method('execute');
- $operation3 = $this->getMockBuilder(DeleteOperation::class)->disableOriginalConstructor()->getMock();
- $operation3->method('getStatus')->willReturn(BaseRestOperation::STATUS_DONE); // Invalid status, should log a warning and not execute
- $operation3->expects(self::never())->method('execute');
- $this->logger->expects(self::exactly(3))->method('warning'); // 1 warning from validateResponse on the Update Op, and 2 because of the bad status of the Create Op
- $this->logger->expects(self::exactly(3))->method('error'); // The exception thrown during the execution of the Delete op will log 3 errors
- $dolibarrSyncService->execute([$operation1, $operation2, $operation3], $progressionCallback);
- }
- /**
- * @see DolibarrSyncService::run()
- */
- public function testRun() {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['run'])
- ->getMock();
- $operations = ['operation1', 'operation2'];
- $dolibarrSyncService->expects(self::once())->method('scan')->willReturn($operations);
- $dolibarrSyncService->expects(self::once())->method('execute')->with($operations);
- $result = $dolibarrSyncService->run();
- $this->assertEquals($operations, $result);
- }
- /**
- * @see DolibarrSyncService::getDolibarrSocietiesIndex()
- */
- public function testGetDolibarrSocietiesIndex(): void
- {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['getDolibarrSocietiesIndex'])
- ->getMock();
- $this->dolibarrApiService
- ->expects($this->once())
- ->method('getAllClients')
- ->willReturn(
- [
- ['id' => 1, 'array_options' => ['options_2iopen_organization_id' => 101]],
- ['id' => 2, 'array_options' => ['options_2iopen_organization_id' => 102]],
- ['id' => 3, 'array_options' => ['options_2iopen_organization_id' => null]], // No org id but also no contract, so it's ok
- ['id' => 4, 'name' => 'foo', 'array_options' => ['options_2iopen_organization_id' => null]], // No org id but has a contract, a warning should be logged
- ]
- );
- $this->dolibarrApiService->expects(self::exactly(2))->method('getActiveContract')->willReturnMap([
- [3, null],
- [4, ['dummy non-empty data']]
- ]);
- $this->logger->expects(self::once())->method('warning')->with('Dolibarr client has no organization id: foo (4)');
- $index = $dolibarrSyncService->getDolibarrSocietiesIndex();
- $this->assertEqualsCanonicalizing(
- [
- 1 => ['id' => 1, 'array_options' => ['options_2iopen_organization_id' => 101]],
- 2 => ['id' => 2, 'array_options' => ['options_2iopen_organization_id' => 102]]
- ],
- $index
- );
- }
- /**
- * @see DolibarrSyncService::getActiveMembersIndex()
- */
- public function testGetActiveMembersIndex(): void
- {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['getActiveMembersIndex'])
- ->getMock();
- $this->accessRepository
- ->expects($this->once())
- ->method('getAllActiveMembersAndMissions')
- ->willReturn(
- [
- ['id' => 1, 'organization_id' => 1, 'mission' => FunctionEnum::PRESIDENT()->getValue()],
- ['id' => 2, 'organization_id' => 1, 'mission' => FunctionEnum::STUDENT()->getValue()],
- ['id' => 3, 'organization_id' => 2, 'mission' => FunctionEnum::PRESIDENT()->getValue()],
- ['id' => 3, 'organization_id' => 2, 'mission' => FunctionEnum::TEACHER()->getValue()]
- ]
- );
- $index = $dolibarrSyncService->getActiveMembersIndex();
- $this->assertEqualsCanonicalizing([
- 1 => [1 => [FunctionEnum::PRESIDENT()->getValue()], 2 => [FunctionEnum::STUDENT()->getValue()]],
- 2 => [3 => [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::TEACHER()->getValue()]]
- ], $index);
- }
- /**
- * @see DolibarrSyncService::findDolibarrContactFor()
- */
- public function testFindDolibarrContactForById(): void
- {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['findDolibarrContactFor'])
- ->getMock();
- $contacts = [
- ['id' => 1, 'array_options' => ['options_2iopen_person_id' => 101]],
- ['id' => 2, 'array_options' => ['options_2iopen_person_id' => 102]],
- ];
- // Find by id
- $person = $this->getMockBuilder(Person::class)->getMock();
- $person->method('getId')->willReturn(102);
- $contact = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
- $this->assertEquals(2, $contact['id']);
- }
- /**
- * @see DolibarrSyncService::findDolibarrContactFor()
- */
- public function testFindDolibarrContactForByName(): void
- {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['findDolibarrContactFor'])
- ->getMock();
- $contacts = [
- ['id' => 1, 'firstname' => 'mister', 'lastname' => 'X', 'array_options' => ['options_2iopen_person_id' => null]],
- ['id' => 2, 'firstname' => 'miss', 'lastname' => 'Y', 'array_options' => ['options_2iopen_person_id' => null]],
- ];
- // Find by full name (contact has no person id, it should be returned)
- $person = $this->getMockBuilder(Person::class)->getMock();
- $person->method('getId')->willReturn(101);
- $person->method('getName')->willReturn('X');
- $person->method('getGivenName')->willReturn('mister');
- $result = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
- $this->assertEquals(1, $result['id']);
- }
- /**
- * @see DolibarrSyncService::findDolibarrContactFor()
- */
- public function testFindDolibarrContactForByNameWithConflict(): void
- {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['findDolibarrContactFor'])
- ->getMock();
- $contacts = [
- ['id' => 1, 'firstname' => 'mister', 'lastname' => 'X', 'array_options' => ['options_2iopen_person_id' => 1]],
- ['id' => 2, 'firstname' => 'miss', 'lastname' => 'Y', 'array_options' => ['options_2iopen_person_id' => 2]],
- ];
- // Find by full name (contact already has another person id, it should not be returned)
- $person = $this->getMockBuilder(Person::class)->getMock();
- $person->method('getId')->willReturn(101);
- $person->method('getName')->willReturn('X');
- $person->method('getGivenName')->willReturn('mister');
- $result = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
- $this->assertEquals(null, $result);
- }
- /**
- * @see DolibarrSyncService::findDolibarrContactFor()
- */
- public function testFindDolibarrContactNotFound(): void
- {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['findDolibarrContactFor'])
- ->getMock();
- $contacts = [
- ['id' => 1, 'firstname' => 'mister', 'lastname' => 'X', 'array_options' => ['options_2iopen_person_id' => 1]],
- ['id' => 2, 'firstname' => 'miss', 'lastname' => 'Y', 'array_options' => ['options_2iopen_person_id' => 2]],
- ];
- // Do not find
- $person = $this->getMockBuilder(Person::class)->getMock();
- $person->method('getId')->willReturn(-1);
- $person->method('getName')->willReturn('Presley');
- $person->method('getGivenName')->willReturn('Elvis');
- $result = $dolibarrSyncService->findDolibarrContactFor($contacts, $person);
- $this->assertEquals(null, $result);
- }
- /**
- * @see DolibarrSyncService::sanitizeDolibarrData()
- */
- public function testSanitizeDolibarrData(): void
- {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['sanitizeDolibarrData'])
- ->getMock();
- $result = $dolibarrSyncService->sanitizeDolibarrData(['a' => 'A', 'b' => '', 'c' => ['d' => 'D', 'e' => '']]);
- $this->assertEquals(
- ['a' => 'A', 'b' => null, 'c' => ['d' => 'D', 'e' => null]],
- $result
- );
- }
- /**
- * @see DolibarrSyncService::sanitizeDolibarrData()
- */
- public function testSanitizeDolibarrDataWithNull(): void
- {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['sanitizeDolibarrData'])
- ->getMock();
- $result = $dolibarrSyncService->sanitizeDolibarrData(null);
- $this->assertEquals(null, $result);
- }
- /**
- * @see DolibarrSyncService::getOrganizationPostalAddress()
- */
- public function testGetOrganizationPostalAddress(): void
- {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['getOrganizationPostalAddress'])
- ->getMock();
- $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])
- );
- $this->assertEquals(
- $addressPostal,
- $dolibarrSyncService->getOrganizationPostalAddress($organization)
- );
- }
- /**
- * @see DolibarrSyncService::getOrganizationPostalAddress()
- */
- public function testGetOrganizationPostalAddressNoResult(): void
- {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['getOrganizationPostalAddress'])
- ->getMock();
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization->expects($this->once())
- ->method('getOrganizationAddressPostals')
- ->willReturn(new ArrayCollection([]));
- $this->assertEquals(
- null,
- $dolibarrSyncService->getOrganizationPostalAddress($organization)
- );
- }
- /**
- * @see DolibarrSyncService::getOrganizationPhone()
- */
- public function testGetOrganizationPhoneWithExistingPhone(): void
- {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['getOrganizationPhone'])
- ->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());
- $phone = $this->getMockBuilder(PhoneNumber::class)->disableOriginalConstructor()->getMock();
- $contactPoint2->method('getTelphone')->willReturn($phone);
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization
- ->expects($this->once())
- ->method('getContactPoints')
- ->willReturn(
- new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
- );
- $dolibarrSyncService->expects(self::once())->method('formatPhoneNumber')->with($phone)->willReturn('+33161626365');
- $this->assertEquals(
- '+33161626365',
- $dolibarrSyncService->getOrganizationPhone($organization)
- );
- }
- /**
- * @see DolibarrSyncService::getOrganizationPhone()
- */
- public function testGetOrganizationPhoneWithMobilePhone() {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['getOrganizationPhone'])
- ->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);
- $mobilePhone = $this->getMockBuilder(PhoneNumber::class)->disableOriginalConstructor()->getMock();
- $contactPoint2->method('getMobilPhone')->willReturn($mobilePhone);
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization
- ->expects($this->once())
- ->method('getContactPoints')
- ->willReturn(
- new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
- );
- $dolibarrSyncService->expects(self::once())->method('formatPhoneNumber')->with($mobilePhone)->willReturn('+33661626365');
- $this->assertEquals(
- '+33661626365',
- $dolibarrSyncService->getOrganizationPhone($organization)
- );
- }
- /**
- * @see DolibarrSyncService::getOrganizationPhone()
- */
- public function testGetOrganizationPhoneWithNoPhone() {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['getOrganizationPhone'])
- ->getMock();
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization
- ->expects($this->once())
- ->method('getContactPoints')
- ->willReturn(new ArrayCollection([]));
- $dolibarrSyncService->expects(self::never())->method('formatPhoneNumber');
- $this->assertEquals(
- null,
- $dolibarrSyncService->getOrganizationPhone($organization)
- );
- }
- /**
- * @see DolibarrSyncService::getOrganizationEmail()
- */
- public function testGetOrganizationEmailWithExistingEmail() {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['getOrganizationEmail'])
- ->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 = $this->getMockBuilder(Organization::class)->getMock();
- $organization
- ->expects($this->once())
- ->method('getContactPoints')
- ->willReturn(
- new ArrayCollection([$contactPoint1, $contactPoint2, $contactPoint3])
- );
- $this->assertEquals(
- 'email@email.com',
- $dolibarrSyncService->getOrganizationEmail($organization)
- );
- }
- /**
- * @see DolibarrSyncService::getOrganizationEmail()
- */
- public function testGetOrganizationEmailWithNoEmail() {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['getOrganizationEmail'])
- ->getMock();
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization
- ->expects($this->once())
- ->method('getContactPoints')
- ->willReturn(new ArrayCollection([]));
- $this->assertEquals(
- null,
- $dolibarrSyncService->getOrganizationEmail($organization)
- );
- }
- /**
- * @see DolibarrSyncService::getOrganizationNetworkId()
- */
- public function testGetOrganizationNetworkId() {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['getOrganizationNetworkId'])
- ->getMock();
- $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]));
- $this->assertEquals(
- 3,
- $dolibarrSyncService->getOrganizationNetworkId($organization)
- );
- }
- /**
- * @see DolibarrSyncService::getOrganizationNetworkId()
- */
- public function testGetOrganizationNetworkIdWithMultipleResult() {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['getOrganizationNetworkId'])
- ->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 = $this->getMockBuilder(Organization::class)->getMock();
- $organization->method('getNetworkOrganizations')->willReturn(
- new ArrayCollection([$networkOrganization1, $networkOrganization2])
- );
- $this->assertEquals(
- 4,
- $dolibarrSyncService->getOrganizationNetworkId($organization)
- );
- }
- /**
- * @see DolibarrSyncService::getOrganizationNetworkId()
- */
- public function testGetOrganizationNetworkIdWithNoResult() {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['getOrganizationNetworkId'])
- ->getMock();
- $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]));
- $this->assertEquals(
- null,
- $dolibarrSyncService->getOrganizationNetworkId($organization)
- );
- }
- /**
- * @see DolibarrSyncService::countWithMission()
- */
- public function testCountWithMission() {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['countWithMission'])
- ->getMock();
- $members = [
- 123 => [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::TEACHER()->getValue()],
- 124 => [FunctionEnum::TEACHER()->getValue()],
- 125 => [FunctionEnum::STUDENT()->getValue()],
- 126 => [FunctionEnum::TREASURER()->getValue()],
- ];
- $this->assertEquals(
- 2,
- $dolibarrSyncService->countWithMission([FunctionEnum::TEACHER()->getValue()], $members)
- );
- $this->assertEquals(
- 3,
- $dolibarrSyncService->countWithMission(
- [FunctionEnum::TEACHER()->getValue(), FunctionEnum::TREASURER()->getValue()],
- $members
- )
- );
- $this->assertEquals(
- 1,
- $dolibarrSyncService->countWithMission([FunctionEnum::STUDENT()->getValue()], $members)
- );
- $this->assertEquals(
- 0,
- $dolibarrSyncService->countWithMission([FunctionEnum::ARCHIVIST()->getValue()], $members)
- );
- }
- /**
- * @see DolibarrSyncService::getPersonContact()
- */
- public function testGetPersonContact() {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['getPersonContact'])
- ->getMock();
- $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]));
- $this->assertEquals(
- $contactPoint2,
- $dolibarrSyncService->getPersonContact($person)
- );
- $person2 = $this->getMockBuilder(Person::class)->getMock();
- $person2->expects($this->once())->method('getContactPoints')->willReturn(new ArrayCollection([]));
- $this->assertEquals(
- null,
- $dolibarrSyncService->getPersonContact($person2)
- );
- }
- /**
- * @see DolibarrSyncService::formatContactPosition()
- */
- public function testFormatContactPosition() {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['formatContactPosition'])
- ->getMock();
- $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'],
- [FunctionEnum::TEACHER()->getValue(), ['gender' => 'X'], null, null, 'Professeur(e)'],
- [FunctionEnum::ARCHIVIST()->getValue(), ['gender' => 'X'], null, null, 'Archiviste'],
- [FunctionEnum::TREASURER()->getValue(), ['gender' => 'X'], null, null, 'Trésorier(ère)'],
- [FunctionEnum::ADMINISTRATIVE_STAFF()->getValue(), ['gender' => 'X'], null, null, 'Personnel administratif'],
- ]
- );
- $this->assertEquals(
- 'Président(e)',
- $dolibarrSyncService->formatContactPosition([FunctionEnum::PRESIDENT()->getValue()])
- );
- $this->assertEquals(
- 'Président',
- $dolibarrSyncService->formatContactPosition([FunctionEnum::PRESIDENT()->getValue()], 'MISTER')
- );
- $this->assertEquals(
- 'Présidente',
- $dolibarrSyncService->formatContactPosition([FunctionEnum::PRESIDENT()->getValue()], 'MISS')
- );
- $this->assertEquals(
- 'Présidente, Directrice',
- $dolibarrSyncService->formatContactPosition(
- [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::DIRECTOR()->getValue()],
- 'MISS'
- )
- );
- $this->assertEquals(
- 'Président, Directeur',
- $dolibarrSyncService->formatContactPosition(
- [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::DIRECTOR()->getValue(), FunctionEnum::ADHERENT()->getValue()],
- 'MISTER'
- )
- );
- $this->assertEquals(
- 'Président, Directeur',
- $dolibarrSyncService->formatContactPosition(
- [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::DIRECTOR()->getValue(), FunctionEnum::ADHERENT()->getValue()],
- 'MISTER'
- )
- );
- $this->assertEquals(
- 'Président(e), Directeur(ice), Professeur(e), Archiviste, Trésorier(ère), Pers...',
- $dolibarrSyncService->formatContactPosition(
- [
- FunctionEnum::PRESIDENT()->getValue(),
- FunctionEnum::DIRECTOR()->getValue(),
- FunctionEnum::TEACHER()->getValue(),
- FunctionEnum::ARCHIVIST()->getValue(),
- FunctionEnum::TREASURER()->getValue(),
- FunctionEnum::ADMINISTRATIVE_STAFF()->getValue(),
- ],
- 'X'
- )
- );
- }
- /**
- * @see DolibarrSyncService::formatPhoneNumber()
- */
- public function testFormatPhoneNumber() {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['formatPhoneNumber'])
- ->getMock();
- $phoneUtil = PhoneNumberUtil::getInstance();
- $phoneNumber = $phoneUtil->parse('01 02 03 04 05', "FR");
- $this->assertEquals(
- '+33102030405',
- $dolibarrSyncService->formatPhoneNumber($phoneNumber)
- );
- }
- /**
- * @see DolibarrSyncService::validateResponse()
- */
- public function testValidateResponse(): void {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['validateResponse'])
- ->getMock();
- $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
- $response->method('toArray')->willReturn(['a' => 1]);
- $operation = $this->getMockBuilder(CreateOperation::class)->disableOriginalConstructor()->getMock();
- $operation->method('getData')->willReturn(['a' => 1]);
- $dolibarrSyncService->expects(self::exactly(2))->method('sanitizeDolibarrData')->with(['a' => 1])->willReturn(['a' => 1]);
- $this->arrayUtils->expects(self::once())->method('getChanges')->with(['a' => 1], ['a' => 1], true)->willReturn([]);
- $dolibarrSyncService->validateResponse($response, $operation);
- }
- /**
- * @see DolibarrSyncService::validateResponse()
- */
- public function testValidateResponseInvalid(): void {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['validateResponse'])
- ->getMock();
- $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
- $response->method('toArray')->willReturn(['a' => 1]);
- $operation = $this->getMockBuilder(CreateOperation::class)->disableOriginalConstructor()->getMock();
- $operation->method('getData')->willReturn(['a' => 0]);
- $dolibarrSyncService->expects(self::exactly(2))
- ->method('sanitizeDolibarrData')
- ->willReturnMap([
- [['a' => 1], ['a' => 1]],
- [['a' => 0], ['a' => 0]]
- ]);
- $this->arrayUtils->expects(self::once())->method('getChanges')->with(['a' => 1], ['a' => 0], true)->willReturn(['a' => 0]);
- $this->expectException(RuntimeException::class);
- $dolibarrSyncService->validateResponse($response, $operation);
- }
- /**
- * @see DolibarrSyncService::validateResponse()
- */
- public function testValidateResponseRequestError(): void {
- $dolibarrSyncService = $this->getMockBuilder(TestableDolibarrSyncService::class)
- ->setConstructorArgs([$this->organizationRepository, $this->accessRepository, $this->functionTypeRepository,
- $this->dolibarrApiService, $this->addressPostalUtils, $this->arrayUtils, $this->translator, $this->logger])
- ->setMethodsExcept(['validateResponse'])
- ->getMock();
- $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
- $response->method('getInfo')->willReturnMap([
- ['http_code', '200'], ['url', 'http://url.com'], ['response_headers', []]
- ]);
- $response->method('getContent')->willReturn("");
- $response->method('toArray')->willThrowException(new ServerException($response));
- $operation = $this->getMockBuilder(CreateOperation::class)->disableOriginalConstructor()->getMock();
- $operation->method('getData')->willReturn(['a' => 0]);
- $dolibarrSyncService->expects(self::never())->method('sanitizeDolibarrData');
- $this->arrayUtils->expects(self::never())->method('getChanges');
- $this->expectException(RuntimeException::class);
- $dolibarrSyncService->validateResponse($response, $operation);
- }
- }
|