| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469 |
- <?php
- namespace App\Tests\Service\Dolibarr;
- use App\Entity\Core\AddressPostal;
- use App\Entity\Core\ContactPoint;
- use App\Entity\Organization\Organization;
- use App\Entity\Organization\OrganizationAddressPostal;
- use App\Entity\Person\Person;
- use App\Enum\Access\FunctionEnum;
- use App\Enum\Core\ContactPointTypeEnum;
- use App\Enum\Organization\AddressPostalOrganizationTypeEnum;
- use App\Repository\Access\AccessRepository;
- use App\Repository\Access\FunctionTypeRepository;
- use App\Repository\Core\ContactPointRepository;
- use App\Repository\Organization\OrganizationRepository;
- use App\Service\Dolibarr\DolibarrApiService;
- use App\Service\Dolibarr\DolibarrSyncService;
- use JetBrains\PhpStorm\Pure;
- use libphonenumber\PhoneNumber;
- use PHPUnit\Framework\TestCase;
- use Psr\Log\LoggerInterface;
- use Symfony\Contracts\Translation\TranslatorInterface;
- class TestableDolibarrSyncService extends DolibarrSyncService {
- public function getDolibarrSocietiesIndex(): array { return parent::getDolibarrSocietiesIndex(); }
- public function getDolibarrContactsIndex(int $socId): array { return parent::getDolibarrContactsIndex($socId); }
- public function getActiveMembersIndex(): array { return parent::getActiveMembersIndex(); }
- public static function sanitizeDolibarrData(?array $data): ?array { return parent::sanitizeDolibarrData($data); }
- public function getOrganizationPostalAddress(Organization $organization): AddressPostal { return parent::getOrganizationPostalAddress($organization); }
- public function getOrganizationPhone(Organization $organization): ?string { return parent::getOrganizationPhone($organization); }
- public function getOrganizationEmail(Organization $organization): ?string { return parent::getOrganizationEmail($organization); }
- public static function countWithMission(array $missions, array $members): int { return parent::countWithMission($missions, $members); }
- 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 static function formatPhoneNumber(PhoneNumber $phoneNumber): string { return parent::formatPhoneNumber($phoneNumber); }
- public static function filterDiff(array $initialData, array $newData): array { return parent::filterDiff($initialData, $newData); }
- }
- class DolibarrSyncServiceTest extends TestCase
- {
- private OrganizationRepository $organizationRepository;
- private AccessRepository $accessRepository;
- private ContactPointRepository $contactPointRepository;
- private FunctionTypeRepository $functionTypeRepository;
- private DolibarrApiService $dolibarrApiService;
- private TranslatorInterface $translator;
- private LoggerInterface $logger;
- public function setUp(): void {
- $this->organizationRepository = $this->getMockBuilder(OrganizationRepository::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->accessRepository = $this->getMockBuilder(AccessRepository::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->contactPointRepository = $this->getMockBuilder(ContactPointRepository::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->functionTypeRepository = $this->getMockBuilder(FunctionTypeRepository::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->translator = $this->getMockBuilder(TranslatorInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->logger = $this->getMockBuilder(LoggerInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- }
- #[Pure]
- private function newDolibarrSyncService(): TestableDolibarrSyncService
- {
- return new TestableDolibarrSyncService(
- $this->organizationRepository,
- $this->accessRepository,
- $this->contactPointRepository,
- $this->functionTypeRepository,
- $this->dolibarrApiService,
- $this->translator,
- $this->logger
- );
- }
- private function getJsonContentFromFixture(string $filename): array {
- $filepath = dirname(__FILE__) . '/fixtures/' . $filename;
- return json_decode(file_get_contents($filepath), true);
- }
- 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 testDolibarrContactsIndex() {
- $this->dolibarrApiService
- ->expects($this->once())
- ->method('getOpentalentContacts')
- ->with(9)
- ->willReturn(
- $this->getJsonContentFromFixture('contacts.json')
- );
- $syncService = $this->newDolibarrSyncService();
- $index = $syncService->getDolibarrContactsIndex(9);
- $this->assertEquals("302117", $index[302117]['array_options']['options_2iopen_person_id']);
- }
- 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(
- [$organizationAddressPostal1, $organizationAddressPostal2, $organizationAddressPostal3]
- );
- $syncService = $this->newDolibarrSyncService($organization);
- $this->assertEquals(
- $addressPostal,
- $syncService->getOrganizationPostalAddress($organization)
- );
- $organization2 = $this->getMockBuilder(Organization::class)->getMock();
- $organization->expects($this->once())
- ->method('getOrganizationAddressPostals')
- ->willReturn([]);
- $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());
- $contactPoint2->expects($this->once())->method('getTelphone')->willReturn('0161626365');
- $organization
- ->expects($this->once())
- ->method('getContactPoints')
- ->willReturn(
- [$contactPoint1, $contactPoint2, $contactPoint3]
- );
- $syncService = $this->newDolibarrSyncService();
- $this->assertEquals(
- '+331 61 62 63 65',
- $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);
- $contactPoint2->expects($this->once())->method('getMobilPhone')->willReturn('0661626365');
- $organization
- ->expects($this->once())
- ->method('getContactPoints')
- ->willReturn(
- [$contactPoint1, $contactPoint2, $contactPoint3]
- );
- $syncService = $this->newDolibarrSyncService();
- $this->assertEquals(
- '+336 61 62 63 65',
- $syncService->getOrganizationPhone($organization)
- );
- }
- public function testGetOrganizationPhoneWithNoPhone() {
- $organization = $this->getMockBuilder(Organization::class)->getMock();
- $organization
- ->expects($this->once())
- ->method('getContactPoints')
- ->willReturn([]);
- $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(
- [$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([]);
- $syncService = $this->newDolibarrSyncService();
- $this->assertEquals(
- null,
- $syncService->getOrganizationEmail($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([$contactPoint1, $contactPoint2]);
- $syncService = $this->newDolibarrSyncService();
- $this->assertEquals(
- $contactPoint2,
- $syncService->getPersonContact($person)
- );
- $person2 = $this->getMockBuilder(Person::class)->getMock();
- $person2->expects($this->once())->method('getContactPoints')->willReturn([]);
- $this->assertEquals(null, $syncService->getPersonContact($person2));
- }
- public function testFormatContactPosition() {
- $this->translator->method('trans')->will(
- $this->returnCallback(function($mission, $params) {
- if ($mission == FunctionEnum::PRESIDENT()) {
- if ($params === ['gender' => 'X']) { return 'Président(e)'; }
- elseif ($params === ['gender' => 'M']) { return 'Président'; }
- elseif ($params === ['gender' => 'F']) { return 'Présidente'; }
- } elseif ($mission == FunctionEnum::DIRECTOR()) {
- if ($params === ['gender' => 'X']) { return 'Directeur(ice)'; }
- elseif ($params === ['gender' => 'M']) { return 'Directeur'; }
- elseif ($params === ['gender' => 'F']) { return 'Directrice'; }
- }
- throw new \AssertionError('translator->trans stub has no matching call for arguments ' . json_encode([$mission, $params]));
- })
- );
- $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() {
- $phoneNumber = new PhoneNumber();
- $phoneNumber->setCountryCode(33);
- $phoneNumber->setNationalNumber('1 02 03 04 05');
- $this->assertEquals(
- '+33 1 02 03 04 05',
- TestableDolibarrSyncService::formatPhoneNumber($phoneNumber)
- );
- }
- public function testFilterDiff() {
- $this->assertEquals(
- ['b' => -2, 'c' => ['e' => ['f' => -5]], 'g' => 7],
- TestableDolibarrSyncService::filterDiff(
- ['a' => 1, 'b' => 2, 'c' => ['d' => 4, 'e' => ['f' => 5]]],
- ['a' => 1, 'b' => -2, 'c' => ['d' => 4, 'e' => ['f' => -5]], 'g' => 7],
- )
- );
- $this->assertEquals(
- [],
- TestableDolibarrSyncService::filterDiff(
- ['a' => 1, 'b' => 2, 'c' => ['d' => 4, 'e' => ['f' => 5]]],
- ['a' => 1, 'b' => 2, 'c' => ['d' => 4, 'e' => ['f' => 5]]],
- )
- );
- $this->assertEquals(
- [],
- TestableDolibarrSyncService::filterDiff(
- [],
- [],
- )
- );
- $this->assertEquals(
- ['a' => 1],
- TestableDolibarrSyncService::filterDiff(
- [],
- ['a' => 1],
- )
- );
- }
- }
|