| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158 |
- <?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);
- }
- }
|