client = $this->getMockBuilder(HttpClientInterface::class) ->disableOriginalConstructor() ->getMock(); } /** * @see DolibarrApiService::getSociety() */ public function testGetSociety(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['getSociety']) ->getMock(); $organizationId = 123; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with('thirdparties', ['limit' => '1', 'sqlfilters' => '(ef.2iopen_organization_id:=:'.$organizationId.')']) ->willReturn([['id' => 1]]); // dummy non-empty data $society = $dolibarrApiService->getSociety($organizationId); $this->assertEquals(['id' => 1], $society); } /** * @see DolibarrApiService::getSociety() */ public function testGetSocietyNotExisting(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['getSociety']) ->getMock(); $organizationId = 123; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with('thirdparties', ['limit' => '1', 'sqlfilters' => '(ef.2iopen_organization_id:=:'.$organizationId.')']) ->willThrowException(new HttpException(404)); $society = $dolibarrApiService->getSociety($organizationId); $this->assertEquals(null, $society); } /** * @see DolibarrApiService::getSociety() */ public function testGetSocietyWithError(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['getSociety']) ->getMock(); $organizationId = 123; $e = new HttpException(500); $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with('thirdparties', ['limit' => '1', 'sqlfilters' => '(ef.2iopen_organization_id:=:'.$organizationId.')']) ->willThrowException($e); $this->expectException($e::class); $dolibarrApiService->getSociety($organizationId); } /** * @see DolibarrApiService::getActiveContract() */ public function testGetActiveContract(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['getActiveContract']) ->getMock(); $socId = 1; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with('contracts', ['limit' => '1', 'sqlfilters' => 'statut:=:1', 'thirdparty_ids' => $socId]) ->willReturn([['id' => 1]]); // dummy non-empty data $this->assertEquals(['id' => 1], $dolibarrApiService->getActiveContract($socId)); } /** * @see DolibarrApiService::getActiveContract() */ public function testGetActiveContractMissing(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['getActiveContract']) ->getMock(); $socId = 1; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with('contracts', ['limit' => '1', 'sqlfilters' => 'statut:=:1', 'thirdparty_ids' => $socId]) ->willThrowException(new HttpException(404)); $this->assertEquals(null, $dolibarrApiService->getActiveContract($socId)); } /** * @see DolibarrApiService::getActiveContract() */ public function testGetActiveContractError(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['getActiveContract']) ->getMock(); $socId = 1; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with('contracts', ['limit' => '1', 'sqlfilters' => 'statut:=:1', 'thirdparty_ids' => $socId]) ->willThrowException(new HttpException(500)); $this->expectException(HttpException::class); $dolibarrApiService->getActiveContract($socId); } /** * @see DolibarrApiService::getBills() */ public function testGetBills(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['getBills']) ->getMock(); $socId = 1; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with('invoices', ['sortfield' => 'datef', 'sortorder' => 'DESC', 'limit' => 5, 'sqlfilters' => "(fk_soc:=:$socId) and (fk_statut:!=:0)"]) ->willReturn([['id' => 10], ['id' => 20]]); $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getBills($socId)); } /** * @see DolibarrApiService::getBills() */ public function testGetBillsMissing(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['getBills']) ->getMock(); $socId = 1; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with('invoices', ['sortfield' => 'datef', 'sortorder' => 'DESC', 'limit' => 5, 'sqlfilters' => "(fk_soc:=:$socId) and (fk_statut:!=:0)"]) ->willThrowException(new HttpException(404)); $this->assertEquals([], $dolibarrApiService->getBills($socId)); } /** * @see DolibarrApiService::getBills() */ public function testGetBillsError(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['getBills']) ->getMock(); $socId = 1; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with('invoices', ['sortfield' => 'datef', 'sortorder' => 'DESC', 'limit' => 5, 'sqlfilters' => "(fk_soc:=:$socId) and (fk_statut:!=:0)"]) ->willThrowException(new HttpException(500)); $this->expectException(HttpException::class); $dolibarrApiService->getBills($socId); } /** * @see DolibarrApiService::getAllClients() */ public function testGetAllClients(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['getAllClients']) ->getMock(); $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with('thirdparties', ['limit' => '1000000', 'sqlfilters' => 'client:=:1']) ->willReturn([['id' => 10], ['id' => 20]]); $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getAllClients()); } /** * @see DolibarrApiService::getContacts() */ public function testGetContacts(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['getContacts']) ->getMock(); $socId = 1; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with('contacts', ['limit' => 1000, 'thirdparty_ids' => $socId]) ->willReturn([['id' => 10], ['id' => 20]]); $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getContacts($socId)); } /** * @see DolibarrApiService::getContacts() */ public function testGetContactsMissing(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['getContacts']) ->getMock(); $socId = 1; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with('contacts', ['limit' => 1000, 'thirdparty_ids' => $socId]) ->willThrowException(new HttpException(404)); $this->assertEquals([], $dolibarrApiService->getContacts($socId)); } /** * @see DolibarrApiService::getContacts() */ public function testGetContactsError(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['getContacts']) ->getMock(); $socId = 1; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with('contacts', ['limit' => 1000, 'thirdparty_ids' => $socId]) ->willThrowException(new HttpException(500)); $this->expectException(HttpException::class); $dolibarrApiService->getContacts($socId); } /** * @see DolibarrApiService::getActiveOpentalentContacts() */ public function testGetActiveOpentalentContacts(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['getActiveOpentalentContacts']) ->getMock(); $socId = 1; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with('contacts?limit=1000&t.statut=1&thirdparty_ids='.$socId.'&sqlfilters:=:(te.2iopen_person_id%3A%3E%3A0)') ->willReturn([['id' => 10], ['id' => 20]]); $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getActiveOpentalentContacts($socId)); } /** * @see DolibarrApiService::getActiveOpentalentContacts() */ public function testGetActiveOpentalentContactsMissing(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['getActiveOpentalentContacts']) ->getMock(); $socId = 1; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with('contacts?limit=1000&t.statut=1&thirdparty_ids='.$socId.'&sqlfilters:=:(te.2iopen_person_id%3A%3E%3A0)') ->willThrowException(new HttpException(404)); $this->assertEquals([], $dolibarrApiService->getActiveOpentalentContacts($socId)); } /** * @see DolibarrApiService::getActiveOpentalentContacts() */ public function testGetActiveOpentalentContactsError(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['getActiveOpentalentContacts']) ->getMock(); $socId = 1; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with('contacts?limit=1000&t.statut=1&thirdparty_ids='.$socId.'&sqlfilters:=:(te.2iopen_person_id%3A%3E%3A0)') ->willThrowException(new HttpException(500)); $this->expectException(HttpException::class); $dolibarrApiService->getActiveOpentalentContacts($socId); } /** * @see DolibarrApiService::getSocietyTagsIds() */ public function testGetSocietyTagsIds(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['getSocietyTagsIds']) ->getMock(); $socId = 1; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with('/thirdparties/1/categories') ->willReturn([['id' => '10'], ['id' => '20']]); $this->assertEquals( [10, 20], $dolibarrApiService->getSocietyTagsIds($socId) ); } /** * @see DolibarrApiService::getSocietyTagsIds() */ public function testGetSocietyTagsIdsMissing(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['getSocietyTagsIds']) ->getMock(); $socId = 1; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with('/thirdparties/1/categories') ->willThrowException(new HttpException(404)); $this->assertEquals( [], $dolibarrApiService->getSocietyTagsIds($socId) ); } /** * @see DolibarrApiService::getSocietyTagsIds() */ public function testGetSocietyTagsIdsError(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['getSocietyTagsIds']) ->getMock(); $socId = 1; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with('/thirdparties/1/categories') ->willThrowException(new HttpException(500)); $this->expectException(HttpException::class); $dolibarrApiService->getSocietyTagsIds($socId); } /** * @see DolibarrApiService::createSociety */ public function testCreateSociety(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['createSociety']) ->getMock(); $organization = $this->getMockBuilder(Organization::class)->getMock(); $organization->method('getId')->willReturn(123); $organization->method('getName')->willReturn('Foo'); $expectedPostBody = [ 'name' => 'Foo', 'client' => 2, 'code_client' => -1, 'import_key' => 'crm', 'array_options' => ['options_2iopen_organization_id' => 123], ]; $response = $this->getMockBuilder(ResponseInterface::class)->getMock(); $response->method('getContent')->willReturn(json_encode(456)); $dolibarrApiService ->expects(self::once()) ->method('post') ->with('/thirdparties', $expectedPostBody) ->willReturn($response); $result = $dolibarrApiService->createSociety($organization); $this->assertEquals(456, $result); } /** * @see DolibarrApiService::getSocietyId() */ public function testGetSocietyId(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['getSocietyId']) ->getMock(); $organizationId = 123; $dolibarrApiService ->expects(self::once()) ->method('getSociety') ->with($organizationId) ->willReturn(['id' => '456']); $societyId = $dolibarrApiService->getSocietyId($organizationId); $this->assertEquals(456, $societyId); } /** * @see DolibarrApiService::getSocietyId() */ public function testGetSocietyIdNotExisting(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['getSocietyId']) ->getMock(); $organizationId = 123; $dolibarrApiService ->expects(self::once()) ->method('getSociety') ->with($organizationId) ->willReturn(null); $societyId = $dolibarrApiService->getSocietyId($organizationId); $this->assertNull($societyId); } /** * @see DolibarrApiService::createSociety */ public function testCreateSocietyIsClient(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['createSociety']) ->getMock(); $organization = $this->getMockBuilder(Organization::class)->getMock(); $organization->method('getId')->willReturn(123); $organization->method('getName')->willReturn('Foo'); $expectedPostBody = [ 'name' => 'Foo', 'client' => 1, 'code_client' => -1, 'import_key' => 'crm', 'array_options' => ['options_2iopen_organization_id' => 123], ]; $response = $this->getMockBuilder(ResponseInterface::class)->getMock(); $response->method('getContent')->willReturn('456'); $dolibarrApiService ->expects(self::once()) ->method('post') ->with('/thirdparties', $expectedPostBody) ->willReturn($response); $result = $dolibarrApiService->createSociety($organization, true); $this->assertEquals(456, $result); } /** * @see DolibarrApiService::getLastOrder() */ public function testGetLastOrder(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['getLastOrder']) ->getMock(); $socId = 1; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with( 'orders', [ 'sortfield' => 't.date_valid', 'sortorder' => 'DESC', 'limit' => 1, 'sqlfilters' => 'fk_soc:=:'.$socId, ] ) ->willReturn([['id' => 10, 'ref' => 'ORDER123']]); $result = $dolibarrApiService->getLastOrder($socId); $this->assertEquals(['id' => 10, 'ref' => 'ORDER123'], $result); } /** * @see DolibarrApiService::getLastOrder() */ public function testGetLastOrderEmpty(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['getLastOrder']) ->getMock(); $socId = 1; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with( 'orders', [ 'sortfield' => 't.date_valid', 'sortorder' => 'DESC', 'limit' => 1, 'sqlfilters' => 'fk_soc:=:'.$socId, ] ) ->willReturn([]); $result = $dolibarrApiService->getLastOrder($socId); $this->assertNull($result); } /** * @see DolibarrApiService::getLastOrder() */ public function testGetLastOrderNotFound(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['getLastOrder']) ->getMock(); $socId = 1; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with( 'orders', [ 'sortfield' => 't.date_valid', 'sortorder' => 'DESC', 'limit' => 1, 'sqlfilters' => 'fk_soc:=:'.$socId, ] ) ->willThrowException(new HttpException(404)); $result = $dolibarrApiService->getLastOrder($socId); $this->assertNull($result); } /** * @see DolibarrApiService::getLastOrder() */ public function testGetLastOrderError(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['getLastOrder']) ->getMock(); $socId = 1; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with( 'orders', [ 'sortfield' => 't.date_valid', 'sortorder' => 'DESC', 'limit' => 1, 'sqlfilters' => 'fk_soc:=:'.$socId, ] ) ->willThrowException(new HttpException(500)); $this->expectException(HttpException::class); $dolibarrApiService->getLastOrder($socId); } public function testSwitchSocietyToProspect(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['switchSocietyToProspect']) ->getMock(); $response = $this->getMockBuilder(ResponseInterface::class)->getMock(); $response->method('getStatusCode')->willReturn(200); $dolibarrApiService ->method('getSociety') ->with(123) ->willReturn(['id' => 456]); $dolibarrApiService ->expects(self::once()) ->method('put') ->with('thirdparties/456', ['client' => 2]) ->willReturn($response); $dolibarrApiService->switchSocietyToProspect(123); } /** * @see DolibarrApiService::downloadBillingDocPdf() */ public function testDownloadBillingDocPdf(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['downloadBillingDocPdf']) ->getMock(); $type = 'invoice'; $docRef = 'FA2023-0001'; $expectedResult = [ 'filename' => 'FA2023-0001.pdf', 'content-type' => 'application/pdf', 'filesize' => 10660, 'content' => 'base64encodedcontent', 'encoding' => 'base64', ]; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with('documents/download?modulepart=invoice&original_file='.urlencode("$docRef/$docRef.pdf")) ->willReturn($expectedResult); $result = $dolibarrApiService->downloadBillingDocPdf($type, $docRef); $this->assertEquals($expectedResult, $result); } /** * @see DolibarrApiService::downloadBillingDocPdf() */ public function testDownloadBillingDocPdfInvalidType(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['downloadBillingDocPdf']) ->getMock(); $type = 'invalid_type'; $docRef = 'FA2023-0001'; $this->expectException(\InvalidArgumentException::class); $dolibarrApiService->downloadBillingDocPdf($type, $docRef); } /** * @see DolibarrApiService::downloadBillingDocPdf() */ public function testDownloadBillingDocPdfError(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['downloadBillingDocPdf']) ->getMock(); $type = 'invoice'; $docRef = 'FA2023-0001'; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with('documents/download?modulepart=invoice&original_file='.urlencode("$docRef/$docRef.pdf")) ->willThrowException(new HttpException(500)); $this->expectException(HttpException::class); $dolibarrApiService->downloadBillingDocPdf($type, $docRef); } public function testSwitchSocietyToProspectWithError(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['switchSocietyToProspect']) ->getMock(); $response = $this->getMockBuilder(ResponseInterface::class)->getMock(); $response->method('getStatusCode')->willReturn(500); $dolibarrApiService ->method('getSociety') ->with(123) ->willReturn(['id' => 456]); $dolibarrApiService ->expects(self::once()) ->method('put') ->with('thirdparties/456', ['client' => 2]) ->willReturn($response); $this->expectException(HttpException::class); $this->expectExceptionMessage('Error while updating the society in Dolibarr'); $dolibarrApiService->switchSocietyToProspect(123); } /** * @see DolibarrApiService::createContract() */ public function testCreateContract(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['createContract']) ->getMock(); // Mock DatesUtils to return a fixed date DatesUtils::setFakeDatetime('2023-01-01 12:00:00'); $date = DatesUtils::new(); $socId = 1; $productId = 2; $isNewClient = false; $duration = 12; // Mock product data $productData = [ 'label' => 'Test Product', 'description' => 'Test Description', 'price' => '100.00', 'price_base_type' => 'HT', 'tva_tx' => '20.00', ]; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with("products/$productId") ->willReturn($productData); // Expected contract data $expectedBody = [ 'socid' => $socId, 'date_contrat' => $date->format('Y-m-d'), 'commercial_signature_id' => 8, 'commercial_suivi_id' => 8, 'statut' => 1, 'lines' => [ [ 'fk_product' => $productId, 'label' => $productData['label'], 'desc' => $productData['description'], 'qty' => 1, 'subprice' => number_format((float) $productData['price'], 2), 'price_base_type' => $productData['price_base_type'], 'tva_tx' => $productData['tva_tx'], ], ], 'array_options' => [ 'options_ec_amount' => number_format((float) $productData['price'], 2), 'options_ec_duration_months' => $duration, 'options_ec_signature_date' => $date->format('Y-m-d'), 'options_ec_effective_date' => $date->format('Y-m-d'), 'options_ec_tacit_renewal' => 1, 'options_ec_termination_period_months' => 2, 'options_ec_billing_due' => 1, 'options_ec_billing_frequency' => 4, 'options_ec_billing_begin_period' => 1, 'options_ec_payment_condition' => 7, 'options_ec_payment_mode' => 6, 'options_ec_account' => 1, 'options_logicielfact' => 1, 'options_versionfact' => 2, 'options_2iopen_origvente' => 3, // Evolution (3) for existing client ], ]; $response = $this->getMockBuilder(ResponseInterface::class)->getMock(); $response->method('getContent')->willReturn('123'); $dolibarrApiService ->expects(self::once()) ->method('post') ->with('contracts', $expectedBody) ->willReturn($response); $result = $dolibarrApiService->createContract($socId, $productId, $isNewClient, $duration); $this->assertEquals(123, $result); } /** * @see DolibarrApiService::createContract() */ public function testCreateContractForNewClient(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['createContract']) ->getMock(); // Mock DatesUtils to return a fixed date DatesUtils::setFakeDatetime('2023-01-01 12:00:00'); $date = DatesUtils::new(); $socId = 1; $productId = 2; $isNewClient = true; // Testing for new client $duration = 12; // Mock product data $productData = [ 'label' => 'Test Product', 'description' => 'Test Description', 'price' => '100.00', 'price_base_type' => 'HT', 'tva_tx' => '20.00', ]; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with("products/$productId") ->willReturn($productData); // Expected contract data with originVente = 1 for new client $expectedBody = [ 'socid' => $socId, 'date_contrat' => $date->format('Y-m-d'), 'commercial_signature_id' => 8, 'commercial_suivi_id' => 8, 'statut' => 1, 'lines' => [ [ 'fk_product' => $productId, 'label' => $productData['label'], 'desc' => $productData['description'], 'qty' => 1, 'subprice' => number_format((float) $productData['price'], 2), 'price_base_type' => $productData['price_base_type'], 'tva_tx' => $productData['tva_tx'], ], ], 'array_options' => [ 'options_ec_amount' => number_format((float) $productData['price'], 2), 'options_ec_duration_months' => $duration, 'options_ec_signature_date' => $date->format('Y-m-d'), 'options_ec_effective_date' => $date->format('Y-m-d'), 'options_ec_tacit_renewal' => 1, 'options_ec_termination_period_months' => 2, 'options_ec_billing_due' => 1, 'options_ec_billing_frequency' => 4, 'options_ec_billing_begin_period' => 1, 'options_ec_payment_condition' => 7, 'options_ec_payment_mode' => 6, 'options_ec_account' => 1, 'options_logicielfact' => 1, 'options_versionfact' => 2, 'options_2iopen_origvente' => 1, // New client (1) ], ]; $response = $this->getMockBuilder(ResponseInterface::class)->getMock(); $response->method('getContent')->willReturn('123'); $dolibarrApiService ->expects(self::once()) ->method('post') ->with('contracts', $expectedBody) ->willReturn($response); $result = $dolibarrApiService->createContract($socId, $productId, $isNewClient, $duration); $this->assertEquals(123, $result); } /** * @see DolibarrApiService::createContractLine() */ public function testCreateContractLine(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['createContractLine']) ->getMock(); // Mock DatesUtils to return a fixed date DatesUtils::setFakeDatetime('2023-01-01 12:00:00'); $date = DatesUtils::new(); $endDate = DatesUtils::new()->modify('+12 months')->modify('-1 day'); $contractId = 1; $productId = 2; $duration = 12; // Mock product data $productData = [ 'label' => 'Test Product', 'description' => 'Test Description', 'price' => '100.00', 'price_base_type' => 'HT', 'tva_tx' => '20.00', ]; $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with("products/$productId") ->willReturn($productData); // Expected contract line data $expectedBody = [ 'fk_product' => $productId, 'label' => $productData['label'], 'desc' => $productData['description'], 'qty' => 1, 'subprice' => number_format((float) $productData['price'], 2), 'price_base_type' => $productData['price_base_type'], 'tva_tx' => $productData['tva_tx'], 'date_start' => $date->format('Y-m-d'), 'date_end' => $endDate->format('Y-m-d'), ]; $response = $this->getMockBuilder(ResponseInterface::class)->getMock(); $response->method('getContent')->willReturn('456'); $dolibarrApiService ->expects(self::once()) ->method('post') ->with("contracts/$contractId/lines", $expectedBody) ->willReturn($response); $result = $dolibarrApiService->createContractLine($contractId, $productId, $duration); $this->assertEquals(456, $result); } /** * @see DolibarrApiService::updateSocietyProduct() */ public function testUpdateSocietyProduct(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['updateSocietyProduct']) ->getMock(); $socId = 1; $productName = 'ARTIST_PREMIUM'; $expectedBody = [ 'array_options' => [ 'options_2iopen_software_opentalent' => $productName, ], ]; $response = $this->getMockBuilder(ResponseInterface::class)->getMock(); $dolibarrApiService ->expects(self::once()) ->method('put') ->with("thirdparties/$socId", $expectedBody) ->willReturn($response); $dolibarrApiService->updateSocietyProduct($socId, $productName); } /** * @see DolibarrApiService::createContract() */ public function testCreateContractError(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['createContract']) ->getMock(); $socId = 1; $productId = 2; $isNewClient = false; $duration = 12; // Mock product data retrieval error $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with("products/$productId") ->willThrowException(new HttpException(500, 'Product not found')); $this->expectException(HttpException::class); $this->expectExceptionMessage('Product not found'); $dolibarrApiService->createContract($socId, $productId, $isNewClient, $duration); } /** * @see DolibarrApiService::createContractLine() */ public function testCreateContractLineError(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['createContractLine']) ->getMock(); $contractId = 1; $productId = 2; $duration = 12; // Mock product data retrieval error $dolibarrApiService ->expects(self::once()) ->method('getJsonContent') ->with("products/$productId") ->willThrowException(new HttpException(500, 'Product not found')); $this->expectException(HttpException::class); $this->expectExceptionMessage('Product not found'); $dolibarrApiService->createContractLine($contractId, $productId, $duration); } /** * @see DolibarrApiService::updateSocietyProduct() */ public function testUpdateSocietyProductError(): void { $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class) ->setConstructorArgs([$this->client]) ->setMethodsExcept(['updateSocietyProduct']) ->getMock(); $socId = 1; $productName = 'ARTIST_PREMIUM'; $expectedBody = [ 'array_options' => [ 'options_2iopen_software_opentalent' => $productName, ], ]; // Mock put method to throw an exception $dolibarrApiService ->expects(self::once()) ->method('put') ->with("thirdparties/$socId", $expectedBody) ->willThrowException(new HttpException(500, 'Error updating society product')); $this->expectException(HttpException::class); $this->expectExceptionMessage('Error updating society product'); $dolibarrApiService->updateSocietyProduct($socId, $productName); } }