DolibarrApiServiceTest.php 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  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. }