| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159 |
- <?php
- namespace App\Tests\Unit\Service\Dolibarr;
- use App\Entity\Organization\Organization;
- use App\Service\Dolibarr\DolibarrApiService;
- use App\Service\Utils\DatesUtils;
- use PHPUnit\Framework\MockObject\MockObject;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\HttpKernel\Exception\HttpException;
- use Symfony\Contracts\HttpClient\HttpClientInterface;
- use Symfony\Contracts\HttpClient\ResponseInterface;
- class DolibarrApiServiceTest extends TestCase
- {
- private MockObject|HttpClientInterface $client;
- public function setUp(): void
- {
- $this->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);
- }
- }
|