DolibarrApiServiceTest.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. <?php
  2. namespace App\Tests\Unit\Service\Dolibarr;
  3. use App\Entity\Organization\Organization;
  4. use App\Service\Dolibarr\DolibarrApiService;
  5. use App\Service\Utils\DatesUtils;
  6. use PHPUnit\Framework\MockObject\MockObject;
  7. use PHPUnit\Framework\TestCase;
  8. use Symfony\Component\HttpKernel\Exception\HttpException;
  9. use Symfony\Contracts\HttpClient\HttpClientInterface;
  10. use Symfony\Contracts\HttpClient\ResponseInterface;
  11. class DolibarrApiServiceTest extends TestCase
  12. {
  13. private MockObject|HttpClientInterface $client;
  14. public function setUp(): void
  15. {
  16. $this->client = $this->getMockBuilder(HttpClientInterface::class)
  17. ->disableOriginalConstructor()
  18. ->getMock();
  19. }
  20. /**
  21. * @see DolibarrApiService::getSociety()
  22. */
  23. public function testGetSociety(): void
  24. {
  25. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  26. ->setConstructorArgs([$this->client])
  27. ->setMethodsExcept(['getSociety'])
  28. ->getMock();
  29. $organizationId = 123;
  30. $dolibarrApiService
  31. ->expects(self::once())
  32. ->method('getJsonContent')
  33. ->with('thirdparties', ['limit' => '1', 'sqlfilters' => '(ef.2iopen_organization_id:=:'.$organizationId.')'])
  34. ->willReturn([['id' => 1]]); // dummy non-empty data
  35. $society = $dolibarrApiService->getSociety($organizationId);
  36. $this->assertEquals(['id' => 1], $society);
  37. }
  38. /**
  39. * @see DolibarrApiService::getSociety()
  40. */
  41. public function testGetSocietyNotExisting(): void
  42. {
  43. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  44. ->setConstructorArgs([$this->client])
  45. ->setMethodsExcept(['getSociety'])
  46. ->getMock();
  47. $organizationId = 123;
  48. $dolibarrApiService
  49. ->expects(self::once())
  50. ->method('getJsonContent')
  51. ->with('thirdparties', ['limit' => '1', 'sqlfilters' => '(ef.2iopen_organization_id:=:'.$organizationId.')'])
  52. ->willThrowException(new HttpException(404));
  53. $society = $dolibarrApiService->getSociety($organizationId);
  54. $this->assertEquals(null, $society);
  55. }
  56. /**
  57. * @see DolibarrApiService::getSociety()
  58. */
  59. public function testGetSocietyWithError(): void
  60. {
  61. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  62. ->setConstructorArgs([$this->client])
  63. ->setMethodsExcept(['getSociety'])
  64. ->getMock();
  65. $organizationId = 123;
  66. $e = new HttpException(500);
  67. $dolibarrApiService
  68. ->expects(self::once())
  69. ->method('getJsonContent')
  70. ->with('thirdparties', ['limit' => '1', 'sqlfilters' => '(ef.2iopen_organization_id:=:'.$organizationId.')'])
  71. ->willThrowException($e);
  72. $this->expectException($e::class);
  73. $dolibarrApiService->getSociety($organizationId);
  74. }
  75. /**
  76. * @see DolibarrApiService::getActiveContract()
  77. */
  78. public function testGetActiveContract(): void
  79. {
  80. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  81. ->setConstructorArgs([$this->client])
  82. ->setMethodsExcept(['getActiveContract'])
  83. ->getMock();
  84. $socId = 1;
  85. $dolibarrApiService
  86. ->expects(self::once())
  87. ->method('getJsonContent')
  88. ->with('contracts', ['limit' => '1', 'sqlfilters' => 'statut:=:1', 'thirdparty_ids' => $socId])
  89. ->willReturn([['id' => 1]]); // dummy non-empty data
  90. $this->assertEquals(['id' => 1], $dolibarrApiService->getActiveContract($socId));
  91. }
  92. /**
  93. * @see DolibarrApiService::getActiveContract()
  94. */
  95. public function testGetActiveContractMissing(): void
  96. {
  97. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  98. ->setConstructorArgs([$this->client])
  99. ->setMethodsExcept(['getActiveContract'])
  100. ->getMock();
  101. $socId = 1;
  102. $dolibarrApiService
  103. ->expects(self::once())
  104. ->method('getJsonContent')
  105. ->with('contracts', ['limit' => '1', 'sqlfilters' => 'statut:=:1', 'thirdparty_ids' => $socId])
  106. ->willThrowException(new HttpException(404));
  107. $this->assertEquals(null, $dolibarrApiService->getActiveContract($socId));
  108. }
  109. /**
  110. * @see DolibarrApiService::getActiveContract()
  111. */
  112. public function testGetActiveContractError(): void
  113. {
  114. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  115. ->setConstructorArgs([$this->client])
  116. ->setMethodsExcept(['getActiveContract'])
  117. ->getMock();
  118. $socId = 1;
  119. $dolibarrApiService
  120. ->expects(self::once())
  121. ->method('getJsonContent')
  122. ->with('contracts', ['limit' => '1', 'sqlfilters' => 'statut:=:1', 'thirdparty_ids' => $socId])
  123. ->willThrowException(new HttpException(500));
  124. $this->expectException(HttpException::class);
  125. $dolibarrApiService->getActiveContract($socId);
  126. }
  127. /**
  128. * @see DolibarrApiService::getBills()
  129. */
  130. public function testGetBills(): void
  131. {
  132. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  133. ->setConstructorArgs([$this->client])
  134. ->setMethodsExcept(['getBills'])
  135. ->getMock();
  136. $socId = 1;
  137. $dolibarrApiService
  138. ->expects(self::once())
  139. ->method('getJsonContent')
  140. ->with('invoices', ['sortfield' => 'datef', 'sortorder' => 'DESC', 'limit' => 5, 'sqlfilters' => "(fk_soc:=:$socId) and (fk_statut:!=:0)"])
  141. ->willReturn([['id' => 10], ['id' => 20]]);
  142. $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getBills($socId));
  143. }
  144. /**
  145. * @see DolibarrApiService::getBills()
  146. */
  147. public function testGetBillsMissing(): void
  148. {
  149. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  150. ->setConstructorArgs([$this->client])
  151. ->setMethodsExcept(['getBills'])
  152. ->getMock();
  153. $socId = 1;
  154. $dolibarrApiService
  155. ->expects(self::once())
  156. ->method('getJsonContent')
  157. ->with('invoices', ['sortfield' => 'datef', 'sortorder' => 'DESC', 'limit' => 5, 'sqlfilters' => "(fk_soc:=:$socId) and (fk_statut:!=:0)"])
  158. ->willThrowException(new HttpException(404));
  159. $this->assertEquals([], $dolibarrApiService->getBills($socId));
  160. }
  161. /**
  162. * @see DolibarrApiService::getBills()
  163. */
  164. public function testGetBillsError(): void
  165. {
  166. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  167. ->setConstructorArgs([$this->client])
  168. ->setMethodsExcept(['getBills'])
  169. ->getMock();
  170. $socId = 1;
  171. $dolibarrApiService
  172. ->expects(self::once())
  173. ->method('getJsonContent')
  174. ->with('invoices', ['sortfield' => 'datef', 'sortorder' => 'DESC', 'limit' => 5, 'sqlfilters' => "(fk_soc:=:$socId) and (fk_statut:!=:0)"])
  175. ->willThrowException(new HttpException(500));
  176. $this->expectException(HttpException::class);
  177. $dolibarrApiService->getBills($socId);
  178. }
  179. /**
  180. * @see DolibarrApiService::getAllClients()
  181. */
  182. public function testGetAllClients(): void
  183. {
  184. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  185. ->setConstructorArgs([$this->client])
  186. ->setMethodsExcept(['getAllClients'])
  187. ->getMock();
  188. $dolibarrApiService
  189. ->expects(self::once())
  190. ->method('getJsonContent')
  191. ->with('thirdparties', ['limit' => '1000000', 'sqlfilters' => 'client:=:1'])
  192. ->willReturn([['id' => 10], ['id' => 20]]);
  193. $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getAllClients());
  194. }
  195. /**
  196. * @see DolibarrApiService::getContacts()
  197. */
  198. public function testGetContacts(): void
  199. {
  200. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  201. ->setConstructorArgs([$this->client])
  202. ->setMethodsExcept(['getContacts'])
  203. ->getMock();
  204. $socId = 1;
  205. $dolibarrApiService
  206. ->expects(self::once())
  207. ->method('getJsonContent')
  208. ->with('contacts', ['limit' => 1000, 'thirdparty_ids' => $socId])
  209. ->willReturn([['id' => 10], ['id' => 20]]);
  210. $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getContacts($socId));
  211. }
  212. /**
  213. * @see DolibarrApiService::getContacts()
  214. */
  215. public function testGetContactsMissing(): void
  216. {
  217. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  218. ->setConstructorArgs([$this->client])
  219. ->setMethodsExcept(['getContacts'])
  220. ->getMock();
  221. $socId = 1;
  222. $dolibarrApiService
  223. ->expects(self::once())
  224. ->method('getJsonContent')
  225. ->with('contacts', ['limit' => 1000, 'thirdparty_ids' => $socId])
  226. ->willThrowException(new HttpException(404));
  227. $this->assertEquals([], $dolibarrApiService->getContacts($socId));
  228. }
  229. /**
  230. * @see DolibarrApiService::getContacts()
  231. */
  232. public function testGetContactsError(): void
  233. {
  234. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  235. ->setConstructorArgs([$this->client])
  236. ->setMethodsExcept(['getContacts'])
  237. ->getMock();
  238. $socId = 1;
  239. $dolibarrApiService
  240. ->expects(self::once())
  241. ->method('getJsonContent')
  242. ->with('contacts', ['limit' => 1000, 'thirdparty_ids' => $socId])
  243. ->willThrowException(new HttpException(500));
  244. $this->expectException(HttpException::class);
  245. $dolibarrApiService->getContacts($socId);
  246. }
  247. /**
  248. * @see DolibarrApiService::getActiveOpentalentContacts()
  249. */
  250. public function testGetActiveOpentalentContacts(): void
  251. {
  252. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  253. ->setConstructorArgs([$this->client])
  254. ->setMethodsExcept(['getActiveOpentalentContacts'])
  255. ->getMock();
  256. $socId = 1;
  257. $dolibarrApiService
  258. ->expects(self::once())
  259. ->method('getJsonContent')
  260. ->with('contacts?limit=1000&t.statut=1&thirdparty_ids='.$socId.'&sqlfilters:=:(te.2iopen_person_id%3A%3E%3A0)')
  261. ->willReturn([['id' => 10], ['id' => 20]]);
  262. $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getActiveOpentalentContacts($socId));
  263. }
  264. /**
  265. * @see DolibarrApiService::getActiveOpentalentContacts()
  266. */
  267. public function testGetActiveOpentalentContactsMissing(): void
  268. {
  269. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  270. ->setConstructorArgs([$this->client])
  271. ->setMethodsExcept(['getActiveOpentalentContacts'])
  272. ->getMock();
  273. $socId = 1;
  274. $dolibarrApiService
  275. ->expects(self::once())
  276. ->method('getJsonContent')
  277. ->with('contacts?limit=1000&t.statut=1&thirdparty_ids='.$socId.'&sqlfilters:=:(te.2iopen_person_id%3A%3E%3A0)')
  278. ->willThrowException(new HttpException(404));
  279. $this->assertEquals([], $dolibarrApiService->getActiveOpentalentContacts($socId));
  280. }
  281. /**
  282. * @see DolibarrApiService::getActiveOpentalentContacts()
  283. */
  284. public function testGetActiveOpentalentContactsError(): void
  285. {
  286. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  287. ->setConstructorArgs([$this->client])
  288. ->setMethodsExcept(['getActiveOpentalentContacts'])
  289. ->getMock();
  290. $socId = 1;
  291. $dolibarrApiService
  292. ->expects(self::once())
  293. ->method('getJsonContent')
  294. ->with('contacts?limit=1000&t.statut=1&thirdparty_ids='.$socId.'&sqlfilters:=:(te.2iopen_person_id%3A%3E%3A0)')
  295. ->willThrowException(new HttpException(500));
  296. $this->expectException(HttpException::class);
  297. $dolibarrApiService->getActiveOpentalentContacts($socId);
  298. }
  299. /**
  300. * @see DolibarrApiService::getSocietyTagsIds()
  301. */
  302. public function testGetSocietyTagsIds(): void
  303. {
  304. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  305. ->setConstructorArgs([$this->client])
  306. ->setMethodsExcept(['getSocietyTagsIds'])
  307. ->getMock();
  308. $socId = 1;
  309. $dolibarrApiService
  310. ->expects(self::once())
  311. ->method('getJsonContent')
  312. ->with('/thirdparties/1/categories')
  313. ->willReturn([['id' => '10'], ['id' => '20']]);
  314. $this->assertEquals(
  315. [10, 20],
  316. $dolibarrApiService->getSocietyTagsIds($socId)
  317. );
  318. }
  319. /**
  320. * @see DolibarrApiService::getSocietyTagsIds()
  321. */
  322. public function testGetSocietyTagsIdsMissing(): void
  323. {
  324. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  325. ->setConstructorArgs([$this->client])
  326. ->setMethodsExcept(['getSocietyTagsIds'])
  327. ->getMock();
  328. $socId = 1;
  329. $dolibarrApiService
  330. ->expects(self::once())
  331. ->method('getJsonContent')
  332. ->with('/thirdparties/1/categories')
  333. ->willThrowException(new HttpException(404));
  334. $this->assertEquals(
  335. [],
  336. $dolibarrApiService->getSocietyTagsIds($socId)
  337. );
  338. }
  339. /**
  340. * @see DolibarrApiService::getSocietyTagsIds()
  341. */
  342. public function testGetSocietyTagsIdsError(): void
  343. {
  344. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  345. ->setConstructorArgs([$this->client])
  346. ->setMethodsExcept(['getSocietyTagsIds'])
  347. ->getMock();
  348. $socId = 1;
  349. $dolibarrApiService
  350. ->expects(self::once())
  351. ->method('getJsonContent')
  352. ->with('/thirdparties/1/categories')
  353. ->willThrowException(new HttpException(500));
  354. $this->expectException(HttpException::class);
  355. $dolibarrApiService->getSocietyTagsIds($socId);
  356. }
  357. /**
  358. * @see DolibarrApiService::createSociety
  359. */
  360. public function testCreateSociety(): void
  361. {
  362. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  363. ->setConstructorArgs([$this->client])
  364. ->setMethodsExcept(['createSociety'])
  365. ->getMock();
  366. $organization = $this->getMockBuilder(Organization::class)->getMock();
  367. $organization->method('getId')->willReturn(123);
  368. $organization->method('getName')->willReturn('Foo');
  369. $expectedPostBody = [
  370. 'name' => 'Foo',
  371. 'client' => 2,
  372. 'code_client' => -1,
  373. 'import_key' => 'crm',
  374. 'array_options' => ['options_2iopen_organization_id' => 123],
  375. ];
  376. $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
  377. $response->method('getContent')->willReturn(json_encode(456));
  378. $dolibarrApiService
  379. ->expects(self::once())
  380. ->method('post')
  381. ->with('/thirdparties', $expectedPostBody)
  382. ->willReturn($response);
  383. $result = $dolibarrApiService->createSociety($organization);
  384. $this->assertEquals(456, $result);
  385. }
  386. /**
  387. * @see DolibarrApiService::getSocietyId()
  388. */
  389. public function testGetSocietyId(): void
  390. {
  391. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  392. ->setConstructorArgs([$this->client])
  393. ->setMethodsExcept(['getSocietyId'])
  394. ->getMock();
  395. $organizationId = 123;
  396. $dolibarrApiService
  397. ->expects(self::once())
  398. ->method('getSociety')
  399. ->with($organizationId)
  400. ->willReturn(['id' => '456']);
  401. $societyId = $dolibarrApiService->getSocietyId($organizationId);
  402. $this->assertEquals(456, $societyId);
  403. }
  404. /**
  405. * @see DolibarrApiService::getSocietyId()
  406. */
  407. public function testGetSocietyIdNotExisting(): void
  408. {
  409. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  410. ->setConstructorArgs([$this->client])
  411. ->setMethodsExcept(['getSocietyId'])
  412. ->getMock();
  413. $organizationId = 123;
  414. $dolibarrApiService
  415. ->expects(self::once())
  416. ->method('getSociety')
  417. ->with($organizationId)
  418. ->willReturn(null);
  419. $societyId = $dolibarrApiService->getSocietyId($organizationId);
  420. $this->assertNull($societyId);
  421. }
  422. /**
  423. * @see DolibarrApiService::createSociety
  424. */
  425. public function testCreateSocietyIsClient(): void
  426. {
  427. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  428. ->setConstructorArgs([$this->client])
  429. ->setMethodsExcept(['createSociety'])
  430. ->getMock();
  431. $organization = $this->getMockBuilder(Organization::class)->getMock();
  432. $organization->method('getId')->willReturn(123);
  433. $organization->method('getName')->willReturn('Foo');
  434. $expectedPostBody = [
  435. 'name' => 'Foo',
  436. 'client' => 1,
  437. 'code_client' => -1,
  438. 'import_key' => 'crm',
  439. 'array_options' => ['options_2iopen_organization_id' => 123],
  440. ];
  441. $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
  442. $response->method('getContent')->willReturn('456');
  443. $dolibarrApiService
  444. ->expects(self::once())
  445. ->method('post')
  446. ->with('/thirdparties', $expectedPostBody)
  447. ->willReturn($response);
  448. $result = $dolibarrApiService->createSociety($organization, true);
  449. $this->assertEquals(456, $result);
  450. }
  451. /**
  452. * @see DolibarrApiService::getLastOrder()
  453. */
  454. public function testGetLastOrder(): void
  455. {
  456. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  457. ->setConstructorArgs([$this->client])
  458. ->setMethodsExcept(['getLastOrder'])
  459. ->getMock();
  460. $socId = 1;
  461. $dolibarrApiService
  462. ->expects(self::once())
  463. ->method('getJsonContent')
  464. ->with(
  465. 'orders',
  466. [
  467. 'sortfield' => 't.date_valid',
  468. 'sortorder' => 'DESC',
  469. 'limit' => 1,
  470. 'sqlfilters' => 'fk_soc:=:'.$socId,
  471. ]
  472. )
  473. ->willReturn([['id' => 10, 'ref' => 'ORDER123']]);
  474. $result = $dolibarrApiService->getLastOrder($socId);
  475. $this->assertEquals(['id' => 10, 'ref' => 'ORDER123'], $result);
  476. }
  477. /**
  478. * @see DolibarrApiService::getLastOrder()
  479. */
  480. public function testGetLastOrderEmpty(): void
  481. {
  482. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  483. ->setConstructorArgs([$this->client])
  484. ->setMethodsExcept(['getLastOrder'])
  485. ->getMock();
  486. $socId = 1;
  487. $dolibarrApiService
  488. ->expects(self::once())
  489. ->method('getJsonContent')
  490. ->with(
  491. 'orders',
  492. [
  493. 'sortfield' => 't.date_valid',
  494. 'sortorder' => 'DESC',
  495. 'limit' => 1,
  496. 'sqlfilters' => 'fk_soc:=:'.$socId,
  497. ]
  498. )
  499. ->willReturn([]);
  500. $result = $dolibarrApiService->getLastOrder($socId);
  501. $this->assertNull($result);
  502. }
  503. /**
  504. * @see DolibarrApiService::getLastOrder()
  505. */
  506. public function testGetLastOrderNotFound(): void
  507. {
  508. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  509. ->setConstructorArgs([$this->client])
  510. ->setMethodsExcept(['getLastOrder'])
  511. ->getMock();
  512. $socId = 1;
  513. $dolibarrApiService
  514. ->expects(self::once())
  515. ->method('getJsonContent')
  516. ->with(
  517. 'orders',
  518. [
  519. 'sortfield' => 't.date_valid',
  520. 'sortorder' => 'DESC',
  521. 'limit' => 1,
  522. 'sqlfilters' => 'fk_soc:=:'.$socId,
  523. ]
  524. )
  525. ->willThrowException(new HttpException(404));
  526. $result = $dolibarrApiService->getLastOrder($socId);
  527. $this->assertNull($result);
  528. }
  529. /**
  530. * @see DolibarrApiService::getLastOrder()
  531. */
  532. public function testGetLastOrderError(): void
  533. {
  534. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  535. ->setConstructorArgs([$this->client])
  536. ->setMethodsExcept(['getLastOrder'])
  537. ->getMock();
  538. $socId = 1;
  539. $dolibarrApiService
  540. ->expects(self::once())
  541. ->method('getJsonContent')
  542. ->with(
  543. 'orders',
  544. [
  545. 'sortfield' => 't.date_valid',
  546. 'sortorder' => 'DESC',
  547. 'limit' => 1,
  548. 'sqlfilters' => 'fk_soc:=:'.$socId,
  549. ]
  550. )
  551. ->willThrowException(new HttpException(500));
  552. $this->expectException(HttpException::class);
  553. $dolibarrApiService->getLastOrder($socId);
  554. }
  555. public function testSwitchSocietyToProspect(): void
  556. {
  557. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  558. ->setConstructorArgs([$this->client])
  559. ->setMethodsExcept(['switchSocietyToProspect'])
  560. ->getMock();
  561. $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
  562. $response->method('getStatusCode')->willReturn(200);
  563. $dolibarrApiService
  564. ->method('getSociety')
  565. ->with(123)
  566. ->willReturn(['id' => 456]);
  567. $dolibarrApiService
  568. ->expects(self::once())
  569. ->method('put')
  570. ->with('thirdparties/456', ['client' => 2])
  571. ->willReturn($response);
  572. $dolibarrApiService->switchSocietyToProspect(123);
  573. }
  574. /**
  575. * @see DolibarrApiService::downloadBillingDocPdf()
  576. */
  577. public function testDownloadBillingDocPdf(): void
  578. {
  579. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  580. ->setConstructorArgs([$this->client])
  581. ->setMethodsExcept(['downloadBillingDocPdf'])
  582. ->getMock();
  583. $type = 'invoice';
  584. $docRef = 'FA2023-0001';
  585. $expectedResult = [
  586. 'filename' => 'FA2023-0001.pdf',
  587. 'content-type' => 'application/pdf',
  588. 'filesize' => 10660,
  589. 'content' => 'base64encodedcontent',
  590. 'encoding' => 'base64'
  591. ];
  592. $dolibarrApiService
  593. ->expects(self::once())
  594. ->method('getJsonContent')
  595. ->with('documents/download?modulepart=invoice&original_file='.urlencode("$docRef/$docRef.pdf"))
  596. ->willReturn($expectedResult);
  597. $result = $dolibarrApiService->downloadBillingDocPdf($type, $docRef);
  598. $this->assertEquals($expectedResult, $result);
  599. }
  600. /**
  601. * @see DolibarrApiService::downloadBillingDocPdf()
  602. */
  603. public function testDownloadBillingDocPdfInvalidType(): void
  604. {
  605. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  606. ->setConstructorArgs([$this->client])
  607. ->setMethodsExcept(['downloadBillingDocPdf'])
  608. ->getMock();
  609. $type = 'invalid_type';
  610. $docRef = 'FA2023-0001';
  611. $this->expectException(\InvalidArgumentException::class);
  612. $dolibarrApiService->downloadBillingDocPdf($type, $docRef);
  613. }
  614. /**
  615. * @see DolibarrApiService::downloadBillingDocPdf()
  616. */
  617. public function testDownloadBillingDocPdfError(): void
  618. {
  619. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  620. ->setConstructorArgs([$this->client])
  621. ->setMethodsExcept(['downloadBillingDocPdf'])
  622. ->getMock();
  623. $type = 'invoice';
  624. $docRef = 'FA2023-0001';
  625. $dolibarrApiService
  626. ->expects(self::once())
  627. ->method('getJsonContent')
  628. ->with('documents/download?modulepart=invoice&original_file='.urlencode("$docRef/$docRef.pdf"))
  629. ->willThrowException(new HttpException(500));
  630. $this->expectException(HttpException::class);
  631. $dolibarrApiService->downloadBillingDocPdf($type, $docRef);
  632. }
  633. public function testSwitchSocietyToProspectWithError(): void
  634. {
  635. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  636. ->setConstructorArgs([$this->client])
  637. ->setMethodsExcept(['switchSocietyToProspect'])
  638. ->getMock();
  639. $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
  640. $response->method('getStatusCode')->willReturn(500);
  641. $dolibarrApiService
  642. ->method('getSociety')
  643. ->with(123)
  644. ->willReturn(['id' => 456]);
  645. $dolibarrApiService
  646. ->expects(self::once())
  647. ->method('put')
  648. ->with('thirdparties/456', ['client' => 2])
  649. ->willReturn($response);
  650. $this->expectException(HttpException::class);
  651. $this->expectExceptionMessage('Error while updating the society in Dolibarr');
  652. $dolibarrApiService->switchSocietyToProspect(123);
  653. }
  654. /**
  655. * @see DolibarrApiService::createContract()
  656. */
  657. public function testCreateContract(): void
  658. {
  659. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  660. ->setConstructorArgs([$this->client])
  661. ->setMethodsExcept(['createContract'])
  662. ->getMock();
  663. // Mock DatesUtils to return a fixed date
  664. DatesUtils::setFakeDatetime('2023-01-01 12:00:00');
  665. $date = DatesUtils::new();
  666. $socId = 1;
  667. $productId = 2;
  668. $isNewClient = false;
  669. $duration = 12;
  670. // Mock product data
  671. $productData = [
  672. 'label' => 'Test Product',
  673. 'description' => 'Test Description',
  674. 'price' => '100.00',
  675. 'price_base_type' => 'HT',
  676. 'tva_tx' => '20.00',
  677. ];
  678. $dolibarrApiService
  679. ->expects(self::once())
  680. ->method('getJsonContent')
  681. ->with("products/$productId")
  682. ->willReturn($productData);
  683. // Expected contract data
  684. $expectedBody = [
  685. "socid" => $socId,
  686. "date_contrat" => $date->format('Y-m-d'),
  687. "commercial_signature_id" => 8,
  688. "commercial_suivi_id" => 8,
  689. 'statut' => 1,
  690. 'lines' => [
  691. [
  692. 'fk_product' => $productId,
  693. 'label' => $productData['label'],
  694. 'desc' => $productData['description'],
  695. 'qty' => 1,
  696. 'subprice' => number_format((float)$productData['price'], 2),
  697. 'price_base_type' => $productData['price_base_type'],
  698. 'tva_tx' => $productData['tva_tx'],
  699. ]
  700. ],
  701. 'array_options' => [
  702. 'options_ec_amount' => number_format((float)$productData['price'], 2),
  703. 'options_ec_duration_months' => $duration,
  704. 'options_ec_signature_date' => $date->format('Y-m-d'),
  705. 'options_ec_effective_date' => $date->format('Y-m-d'),
  706. 'options_ec_tacit_renewal' => 1,
  707. 'options_ec_termination_period_months' => 2,
  708. 'options_ec_billing_due' => 1,
  709. 'options_ec_billing_frequency' => 4,
  710. 'options_ec_billing_begin_period' => 1,
  711. 'options_ec_payment_condition' => 7,
  712. 'options_ec_payment_mode' => 6,
  713. 'options_ec_account' => 1,
  714. 'options_logicielfact' => 1,
  715. 'options_versionfact' => 2,
  716. 'options_2iopen_origvente' => 3, // Evolution (3) for existing client
  717. ]
  718. ];
  719. $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
  720. $response->method('getContent')->willReturn('123');
  721. $dolibarrApiService
  722. ->expects(self::once())
  723. ->method('post')
  724. ->with('contracts', $expectedBody)
  725. ->willReturn($response);
  726. $result = $dolibarrApiService->createContract($socId, $productId, $isNewClient, $duration);
  727. $this->assertEquals(123, $result);
  728. }
  729. /**
  730. * @see DolibarrApiService::createContract()
  731. */
  732. public function testCreateContractForNewClient(): void
  733. {
  734. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  735. ->setConstructorArgs([$this->client])
  736. ->setMethodsExcept(['createContract'])
  737. ->getMock();
  738. // Mock DatesUtils to return a fixed date
  739. DatesUtils::setFakeDatetime('2023-01-01 12:00:00');
  740. $date = DatesUtils::new();
  741. $socId = 1;
  742. $productId = 2;
  743. $isNewClient = true; // Testing for new client
  744. $duration = 12;
  745. // Mock product data
  746. $productData = [
  747. 'label' => 'Test Product',
  748. 'description' => 'Test Description',
  749. 'price' => '100.00',
  750. 'price_base_type' => 'HT',
  751. 'tva_tx' => '20.00',
  752. ];
  753. $dolibarrApiService
  754. ->expects(self::once())
  755. ->method('getJsonContent')
  756. ->with("products/$productId")
  757. ->willReturn($productData);
  758. // Expected contract data with originVente = 1 for new client
  759. $expectedBody = [
  760. "socid" => $socId,
  761. "date_contrat" => $date->format('Y-m-d'),
  762. "commercial_signature_id" => 8,
  763. "commercial_suivi_id" => 8,
  764. 'statut' => 1,
  765. 'lines' => [
  766. [
  767. 'fk_product' => $productId,
  768. 'label' => $productData['label'],
  769. 'desc' => $productData['description'],
  770. 'qty' => 1,
  771. 'subprice' => number_format((float)$productData['price'], 2),
  772. 'price_base_type' => $productData['price_base_type'],
  773. 'tva_tx' => $productData['tva_tx'],
  774. ]
  775. ],
  776. 'array_options' => [
  777. 'options_ec_amount' => number_format((float)$productData['price'], 2),
  778. 'options_ec_duration_months' => $duration,
  779. 'options_ec_signature_date' => $date->format('Y-m-d'),
  780. 'options_ec_effective_date' => $date->format('Y-m-d'),
  781. 'options_ec_tacit_renewal' => 1,
  782. 'options_ec_termination_period_months' => 2,
  783. 'options_ec_billing_due' => 1,
  784. 'options_ec_billing_frequency' => 4,
  785. 'options_ec_billing_begin_period' => 1,
  786. 'options_ec_payment_condition' => 7,
  787. 'options_ec_payment_mode' => 6,
  788. 'options_ec_account' => 1,
  789. 'options_logicielfact' => 1,
  790. 'options_versionfact' => 2,
  791. 'options_2iopen_origvente' => 1, // New client (1)
  792. ]
  793. ];
  794. $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
  795. $response->method('getContent')->willReturn('123');
  796. $dolibarrApiService
  797. ->expects(self::once())
  798. ->method('post')
  799. ->with('contracts', $expectedBody)
  800. ->willReturn($response);
  801. $result = $dolibarrApiService->createContract($socId, $productId, $isNewClient, $duration);
  802. $this->assertEquals(123, $result);
  803. }
  804. /**
  805. * @see DolibarrApiService::createContractLine()
  806. */
  807. public function testCreateContractLine(): void
  808. {
  809. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  810. ->setConstructorArgs([$this->client])
  811. ->setMethodsExcept(['createContractLine'])
  812. ->getMock();
  813. // Mock DatesUtils to return a fixed date
  814. DatesUtils::setFakeDatetime('2023-01-01 12:00:00');
  815. $date = DatesUtils::new();
  816. $endDate = DatesUtils::new()->modify('+12 months')->modify('-1 day');
  817. $contractId = 1;
  818. $productId = 2;
  819. $duration = 12;
  820. // Mock product data
  821. $productData = [
  822. 'label' => 'Test Product',
  823. 'description' => 'Test Description',
  824. 'price' => '100.00',
  825. 'price_base_type' => 'HT',
  826. 'tva_tx' => '20.00',
  827. ];
  828. $dolibarrApiService
  829. ->expects(self::once())
  830. ->method('getJsonContent')
  831. ->with("products/$productId")
  832. ->willReturn($productData);
  833. // Expected contract line data
  834. $expectedBody = [
  835. 'fk_product' => $productId,
  836. 'label' => $productData['label'],
  837. 'desc' => $productData['description'],
  838. 'qty' => 1,
  839. 'subprice' => number_format((float)$productData['price'], 2),
  840. 'price_base_type' => $productData['price_base_type'],
  841. 'tva_tx' => $productData['tva_tx'],
  842. 'date_start' => $date->format('Y-m-d'),
  843. 'date_end' => $endDate->format('Y-m-d'),
  844. ];
  845. $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
  846. $response->method('getContent')->willReturn('456');
  847. $dolibarrApiService
  848. ->expects(self::once())
  849. ->method('post')
  850. ->with("contracts/$contractId/lines", $expectedBody)
  851. ->willReturn($response);
  852. $result = $dolibarrApiService->createContractLine($contractId, $productId, $duration);
  853. $this->assertEquals(456, $result);
  854. }
  855. /**
  856. * @see DolibarrApiService::updateSocietyProduct()
  857. */
  858. public function testUpdateSocietyProduct(): void
  859. {
  860. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  861. ->setConstructorArgs([$this->client])
  862. ->setMethodsExcept(['updateSocietyProduct'])
  863. ->getMock();
  864. $socId = 1;
  865. $productName = 'ARTIST_PREMIUM';
  866. $expectedBody = [
  867. 'array_options' => [
  868. 'options_2iopen_software_opentalent' => $productName
  869. ]
  870. ];
  871. $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
  872. $dolibarrApiService
  873. ->expects(self::once())
  874. ->method('put')
  875. ->with("thirdparties/$socId", $expectedBody)
  876. ->willReturn($response);
  877. $dolibarrApiService->updateSocietyProduct($socId, $productName);
  878. }
  879. /**
  880. * @see DolibarrApiService::createContract()
  881. */
  882. public function testCreateContractError(): void
  883. {
  884. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  885. ->setConstructorArgs([$this->client])
  886. ->setMethodsExcept(['createContract'])
  887. ->getMock();
  888. $socId = 1;
  889. $productId = 2;
  890. $isNewClient = false;
  891. $duration = 12;
  892. // Mock product data retrieval error
  893. $dolibarrApiService
  894. ->expects(self::once())
  895. ->method('getJsonContent')
  896. ->with("products/$productId")
  897. ->willThrowException(new HttpException(500, 'Product not found'));
  898. $this->expectException(HttpException::class);
  899. $this->expectExceptionMessage('Product not found');
  900. $dolibarrApiService->createContract($socId, $productId, $isNewClient, $duration);
  901. }
  902. /**
  903. * @see DolibarrApiService::createContractLine()
  904. */
  905. public function testCreateContractLineError(): void
  906. {
  907. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  908. ->setConstructorArgs([$this->client])
  909. ->setMethodsExcept(['createContractLine'])
  910. ->getMock();
  911. $contractId = 1;
  912. $productId = 2;
  913. $duration = 12;
  914. // Mock product data retrieval error
  915. $dolibarrApiService
  916. ->expects(self::once())
  917. ->method('getJsonContent')
  918. ->with("products/$productId")
  919. ->willThrowException(new HttpException(500, 'Product not found'));
  920. $this->expectException(HttpException::class);
  921. $this->expectExceptionMessage('Product not found');
  922. $dolibarrApiService->createContractLine($contractId, $productId, $duration);
  923. }
  924. /**
  925. * @see DolibarrApiService::updateSocietyProduct()
  926. */
  927. public function testUpdateSocietyProductError(): void
  928. {
  929. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  930. ->setConstructorArgs([$this->client])
  931. ->setMethodsExcept(['updateSocietyProduct'])
  932. ->getMock();
  933. $socId = 1;
  934. $productName = 'ARTIST_PREMIUM';
  935. $expectedBody = [
  936. 'array_options' => [
  937. 'options_2iopen_software_opentalent' => $productName
  938. ]
  939. ];
  940. // Mock put method to throw an exception
  941. $dolibarrApiService
  942. ->expects(self::once())
  943. ->method('put')
  944. ->with("thirdparties/$socId", $expectedBody)
  945. ->willThrowException(new HttpException(500, 'Error updating society product'));
  946. $this->expectException(HttpException::class);
  947. $this->expectExceptionMessage('Error updating society product');
  948. $dolibarrApiService->updateSocietyProduct($socId, $productName);
  949. }
  950. }