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], ) ); } }