|
|
@@ -41,7 +41,6 @@ use libphonenumber\PhoneNumberUtil;
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
-use Symfony\Component\HttpClient\Exception\ServerException;
|
|
|
use Symfony\Contracts\HttpClient\ResponseInterface;
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
|
|
|
|
|
@@ -72,6 +71,11 @@ class TestableDolibarrSyncService extends DolibarrSyncService
|
|
|
return parent::getOrganizationPostalAddress($organization);
|
|
|
}
|
|
|
|
|
|
+ public function getOrganizationBillingPostalAddress(Organization $organization): ?AddressPostal
|
|
|
+ {
|
|
|
+ return parent::getOrganizationBillingPostalAddress($organization);
|
|
|
+ }
|
|
|
+
|
|
|
public function getOrganizationPhone(Organization $organization): ?string
|
|
|
{
|
|
|
return parent::getOrganizationPhone($organization);
|
|
|
@@ -200,6 +204,7 @@ class DolibarrSyncServiceTest extends TestCase
|
|
|
|
|
|
$organizationData1 = [
|
|
|
'fullAddress' => '1 Rue Azerty',
|
|
|
+ 'fullBillingAddress' => '2 Rue Ascii',
|
|
|
'addressOwner' => 'Mr Keyboard',
|
|
|
'postalCode' => '01110',
|
|
|
'city' => 'ByteCity',
|
|
|
@@ -344,7 +349,20 @@ class DolibarrSyncServiceTest extends TestCase
|
|
|
],
|
|
|
];
|
|
|
|
|
|
- $dolibarrSocietyContacts1 = [$contactData1, $obsoleteContactData, $obsoleteContactData2];
|
|
|
+ $billingContactData = [
|
|
|
+ 'id' => '5',
|
|
|
+ 'lastname' => 'Service facturation',
|
|
|
+ 'firstname' => null,
|
|
|
+ 'address' => '',
|
|
|
+ 'zip' => '',
|
|
|
+ 'town' => '',
|
|
|
+ 'statut' => '1',
|
|
|
+ 'array_options' => [
|
|
|
+ 'options_2iopen_person_id' => null,
|
|
|
+ ],
|
|
|
+ ];
|
|
|
+
|
|
|
+ $dolibarrSocietyContacts1 = [$contactData1, $obsoleteContactData, $obsoleteContactData2, $billingContactData];
|
|
|
$dolibarrSocietyContacts2 = [];
|
|
|
|
|
|
// ----- Setup Mocks -----
|
|
|
@@ -408,24 +426,37 @@ class DolibarrSyncServiceTest extends TestCase
|
|
|
->method('sanitizeDolibarrData')
|
|
|
->willReturnCallback(function ($args) { return $args; });
|
|
|
|
|
|
- $addressPostal = $this->getMockBuilder(AddressPostal::class)->getMock();
|
|
|
+ $mainPostalAddress = $this->getMockBuilder(AddressPostal::class)->getMock();
|
|
|
+ $mainPostalAddress->method('getStreetAddress')->willReturn($organizationData1['fullAddress']);
|
|
|
+ $mainPostalAddress->method('getAddressOwner')->willReturn($organizationData1['addressOwner']);
|
|
|
+ $mainPostalAddress->method('getPostalCode')->willReturn($organizationData1['postalCode']);
|
|
|
+ $mainPostalAddress->method('getAddressCity')->willReturn($organizationData1['city']);
|
|
|
+
|
|
|
+ $billingPostalAddress = $this->getMockBuilder(AddressPostal::class)->getMock();
|
|
|
+ $billingPostalAddress->method('getStreetAddress')->willReturn($organizationData1['fullBillingAddress']);
|
|
|
+ $billingPostalAddress->method('getAddressOwner')->willReturn($organizationData1['addressOwner']);
|
|
|
+ $billingPostalAddress->method('getPostalCode')->willReturn($organizationData1['postalCode']);
|
|
|
+ $billingPostalAddress->method('getAddressCity')->willReturn($organizationData1['city']);
|
|
|
|
|
|
$dolibarrSyncService
|
|
|
->expects(self::exactly(2))
|
|
|
->method('getOrganizationPostalAddress')
|
|
|
->willReturnMap([
|
|
|
- [$organization1, $addressPostal],
|
|
|
+ [$organization1, $mainPostalAddress],
|
|
|
+ [$organization2, null],
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $dolibarrSyncService
|
|
|
+ ->expects(self::exactly(1))
|
|
|
+ ->method('getOrganizationBillingPostalAddress')
|
|
|
+ ->willReturnMap([
|
|
|
+ [$organization1, $billingPostalAddress],
|
|
|
[$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']);
|
|
|
+ ->willReturnCallback(function ($a) { return $a->getStreetAddress(); });
|
|
|
|
|
|
$dolibarrSyncService
|
|
|
->expects(self::exactly(2))
|
|
|
@@ -477,7 +508,7 @@ class DolibarrSyncServiceTest extends TestCase
|
|
|
]);
|
|
|
|
|
|
$this->arrayUtils
|
|
|
- ->expects(self::exactly(3))
|
|
|
+ ->expects(self::exactly(4))
|
|
|
->method('getChanges')
|
|
|
->willReturnCallback(
|
|
|
function (array $initialArray, array $newArray, bool $recursive = false, ?callable $callback = null) {
|
|
|
@@ -591,7 +622,7 @@ class DolibarrSyncServiceTest extends TestCase
|
|
|
|
|
|
$operations = $dolibarrSyncService->scan($progressionCallback);
|
|
|
|
|
|
- $this->assertCount(8, $operations);
|
|
|
+ $this->assertCount(9, $operations);
|
|
|
|
|
|
$this->assertEqualsCanonicalizing(
|
|
|
[
|
|
|
@@ -637,18 +668,28 @@ class DolibarrSyncServiceTest extends TestCase
|
|
|
|
|
|
$this->assertEqualsCanonicalizing(
|
|
|
[
|
|
|
- '[DELETE /thirdparties/1/categories/68]',
|
|
|
+ '[PUT contacts/5]',
|
|
|
+ 'address : `` => `2 Rue Ascii`',
|
|
|
+ 'town : `` => `ByteCity`',
|
|
|
+ 'zip : `` => `01110`',
|
|
|
],
|
|
|
$operations[3]->getChangeLog()
|
|
|
);
|
|
|
|
|
|
$this->assertEqualsCanonicalizing(
|
|
|
[
|
|
|
- '[POST /thirdparties/1/categories/67]',
|
|
|
+ '[DELETE /thirdparties/1/categories/68]',
|
|
|
],
|
|
|
$operations[4]->getChangeLog()
|
|
|
);
|
|
|
|
|
|
+ $this->assertEqualsCanonicalizing(
|
|
|
+ [
|
|
|
+ '[POST /thirdparties/1/categories/67]',
|
|
|
+ ],
|
|
|
+ $operations[5]->getChangeLog()
|
|
|
+ );
|
|
|
+
|
|
|
$this->assertEqualsCanonicalizing(
|
|
|
[
|
|
|
'[PUT thirdparties/2]',
|
|
|
@@ -664,7 +705,7 @@ class DolibarrSyncServiceTest extends TestCase
|
|
|
"array_options.options_2iopeninfoopentalent : (new) => `Nombre d'adhérents : 0\nNombre d'accès admin : 2`",
|
|
|
'array_options.options_2iopen_last_sync_date : (new) => `2024-01-01T00:00:00+00:00`',
|
|
|
],
|
|
|
- $operations[5]->getChangeLog()
|
|
|
+ $operations[6]->getChangeLog()
|
|
|
);
|
|
|
|
|
|
$this->assertEqualsCanonicalizing(
|
|
|
@@ -682,14 +723,14 @@ class DolibarrSyncServiceTest extends TestCase
|
|
|
'array_options.options_2iopen_last_sync_date : (new) => `2024-01-01T00:00:00+00:00`',
|
|
|
'socid : (new) => `2`',
|
|
|
],
|
|
|
- $operations[6]->getChangeLog()
|
|
|
+ $operations[7]->getChangeLog()
|
|
|
);
|
|
|
|
|
|
$this->assertEqualsCanonicalizing(
|
|
|
[
|
|
|
'[DELETE /thirdparties/2/categories/67]',
|
|
|
],
|
|
|
- $operations[7]->getChangeLog()
|
|
|
+ $operations[8]->getChangeLog()
|
|
|
);
|
|
|
|
|
|
$this->assertCount(0, $progressionCallbackExpectedCalls);
|
|
|
@@ -721,8 +762,6 @@ class DolibarrSyncServiceTest extends TestCase
|
|
|
$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
|
|
|
@@ -733,7 +772,7 @@ class DolibarrSyncServiceTest extends TestCase
|
|
|
$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(2))->method('warning'); // 2 warnings 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);
|
|
|
@@ -1412,76 +1451,4 @@ class DolibarrSyncServiceTest extends TestCase
|
|
|
[]
|
|
|
);
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * @see DolibarrSyncService::validateResponse()
|
|
|
- */
|
|
|
- public function testValidateResponse(): void
|
|
|
- {
|
|
|
- $dolibarrSyncService = $this->getMockForMethod('validateResponse');
|
|
|
-
|
|
|
- $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->getMockForMethod('validateResponse');
|
|
|
-
|
|
|
- $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->getMockForMethod('validateResponse');
|
|
|
-
|
|
|
- $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);
|
|
|
- }
|
|
|
}
|