DolibarrApiServiceTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. <?php
  2. namespace App\Tests\Unit\Service\Dolibarr;
  3. use App\Entity\Organization\Organization;
  4. use App\Service\Dolibarr\DolibarrApiService;
  5. use PHPUnit\Framework\MockObject\MockObject;
  6. use PHPUnit\Framework\TestCase;
  7. use Symfony\Component\HttpKernel\Exception\HttpException;
  8. use Symfony\Contracts\HttpClient\HttpClientInterface;
  9. use Symfony\Contracts\HttpClient\ResponseInterface;
  10. class DolibarrApiServiceTest extends TestCase
  11. {
  12. private MockObject|HttpClientInterface $client;
  13. public function setUp(): void
  14. {
  15. $this->client = $this->getMockBuilder(HttpClientInterface::class)
  16. ->disableOriginalConstructor()
  17. ->getMock();
  18. }
  19. /**
  20. * @see DolibarrApiService::getSociety()
  21. */
  22. public function testGetSociety(): void
  23. {
  24. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  25. ->setConstructorArgs([$this->client])
  26. ->setMethodsExcept(['getSociety'])
  27. ->getMock();
  28. $organizationId = 123;
  29. $dolibarrApiService
  30. ->expects(self::once())
  31. ->method('getJsonContent')
  32. ->with('thirdparties', ['limit' => '1', 'sqlfilters' => '(ef.2iopen_organization_id:=:'.$organizationId.')'])
  33. ->willReturn([['id' => 1]]); // dummy non-empty data
  34. $society = $dolibarrApiService->getSociety($organizationId);
  35. $this->assertEquals(['id' => 1], $society);
  36. }
  37. /**
  38. * @see DolibarrApiService::getSociety()
  39. */
  40. public function testGetSocietyNotExisting(): void
  41. {
  42. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  43. ->setConstructorArgs([$this->client])
  44. ->setMethodsExcept(['getSociety'])
  45. ->getMock();
  46. $organizationId = 123;
  47. $dolibarrApiService
  48. ->expects(self::once())
  49. ->method('getJsonContent')
  50. ->with('thirdparties', ['limit' => '1', 'sqlfilters' => '(ef.2iopen_organization_id:=:'.$organizationId.')'])
  51. ->willThrowException(new HttpException(404));
  52. $society = $dolibarrApiService->getSociety($organizationId);
  53. $this->assertEquals(null, $society);
  54. }
  55. /**
  56. * @see DolibarrApiService::getSociety()
  57. */
  58. public function testGetSocietyWithError(): void
  59. {
  60. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  61. ->setConstructorArgs([$this->client])
  62. ->setMethodsExcept(['getSociety'])
  63. ->getMock();
  64. $organizationId = 123;
  65. $e = new HttpException(500);
  66. $dolibarrApiService
  67. ->expects(self::once())
  68. ->method('getJsonContent')
  69. ->with('thirdparties', ['limit' => '1', 'sqlfilters' => '(ef.2iopen_organization_id:=:'.$organizationId.')'])
  70. ->willThrowException($e);
  71. $this->expectException($e::class);
  72. $dolibarrApiService->getSociety($organizationId);
  73. }
  74. /**
  75. * @see DolibarrApiService::getActiveContract()
  76. */
  77. public function testGetActiveContract(): void
  78. {
  79. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  80. ->setConstructorArgs([$this->client])
  81. ->setMethodsExcept(['getActiveContract'])
  82. ->getMock();
  83. $socId = 1;
  84. $dolibarrApiService
  85. ->expects(self::once())
  86. ->method('getJsonContent')
  87. ->with('contracts', ['limit' => '1', 'sqlfilters' => 'statut:=:1', 'thirdparty_ids' => $socId])
  88. ->willReturn([['id' => 1]]); // dummy non-empty data
  89. $this->assertEquals(['id' => 1], $dolibarrApiService->getActiveContract($socId));
  90. }
  91. /**
  92. * @see DolibarrApiService::getActiveContract()
  93. */
  94. public function testGetActiveContractMissing(): void
  95. {
  96. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  97. ->setConstructorArgs([$this->client])
  98. ->setMethodsExcept(['getActiveContract'])
  99. ->getMock();
  100. $socId = 1;
  101. $dolibarrApiService
  102. ->expects(self::once())
  103. ->method('getJsonContent')
  104. ->with('contracts', ['limit' => '1', 'sqlfilters' => 'statut:=:1', 'thirdparty_ids' => $socId])
  105. ->willThrowException(new HttpException(404));
  106. $this->assertEquals(null, $dolibarrApiService->getActiveContract($socId));
  107. }
  108. /**
  109. * @see DolibarrApiService::getActiveContract()
  110. */
  111. public function testGetActiveContractError(): void
  112. {
  113. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  114. ->setConstructorArgs([$this->client])
  115. ->setMethodsExcept(['getActiveContract'])
  116. ->getMock();
  117. $socId = 1;
  118. $dolibarrApiService
  119. ->expects(self::once())
  120. ->method('getJsonContent')
  121. ->with('contracts', ['limit' => '1', 'sqlfilters' => 'statut:=:1', 'thirdparty_ids' => $socId])
  122. ->willThrowException(new HttpException(500));
  123. $this->expectException(HttpException::class);
  124. $dolibarrApiService->getActiveContract($socId);
  125. }
  126. /**
  127. * @see DolibarrApiService::getBills()
  128. */
  129. public function testGetBills(): void
  130. {
  131. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  132. ->setConstructorArgs([$this->client])
  133. ->setMethodsExcept(['getBills'])
  134. ->getMock();
  135. $socId = 1;
  136. $dolibarrApiService
  137. ->expects(self::once())
  138. ->method('getJsonContent')
  139. ->with('invoices', ['sortfield' => 'datef', 'sortorder' => 'DESC', 'limit' => 5, 'sqlfilters' => 'fk_soc:=:'.$socId])
  140. ->willReturn([['id' => 10], ['id' => 20]]);
  141. $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getBills($socId));
  142. }
  143. /**
  144. * @see DolibarrApiService::getBills()
  145. */
  146. public function testGetBillsMissing(): void
  147. {
  148. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  149. ->setConstructorArgs([$this->client])
  150. ->setMethodsExcept(['getBills'])
  151. ->getMock();
  152. $socId = 1;
  153. $dolibarrApiService
  154. ->expects(self::once())
  155. ->method('getJsonContent')
  156. ->with('invoices', ['sortfield' => 'datef', 'sortorder' => 'DESC', 'limit' => 5, 'sqlfilters' => 'fk_soc:=:'.$socId])
  157. ->willThrowException(new HttpException(404));
  158. $this->assertEquals([], $dolibarrApiService->getBills($socId));
  159. }
  160. /**
  161. * @see DolibarrApiService::getBills()
  162. */
  163. public function testGetBillsError(): void
  164. {
  165. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  166. ->setConstructorArgs([$this->client])
  167. ->setMethodsExcept(['getBills'])
  168. ->getMock();
  169. $socId = 1;
  170. $dolibarrApiService
  171. ->expects(self::once())
  172. ->method('getJsonContent')
  173. ->with('invoices', ['sortfield' => 'datef', 'sortorder' => 'DESC', 'limit' => 5, 'sqlfilters' => 'fk_soc:=:'.$socId])
  174. ->willThrowException(new HttpException(500));
  175. $this->expectException(HttpException::class);
  176. $dolibarrApiService->getBills($socId);
  177. }
  178. /**
  179. * @see DolibarrApiService::getAllClients()
  180. */
  181. public function testGetAllClients(): void
  182. {
  183. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  184. ->setConstructorArgs([$this->client])
  185. ->setMethodsExcept(['getAllClients'])
  186. ->getMock();
  187. $dolibarrApiService
  188. ->expects(self::once())
  189. ->method('getJsonContent')
  190. ->with('thirdparties', ['limit' => '1000000', 'sqlfilters' => 'client:=:1'])
  191. ->willReturn([['id' => 10], ['id' => 20]]);
  192. $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getAllClients());
  193. }
  194. /**
  195. * @see DolibarrApiService::getContacts()
  196. */
  197. public function testGetContacts(): void
  198. {
  199. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  200. ->setConstructorArgs([$this->client])
  201. ->setMethodsExcept(['getContacts'])
  202. ->getMock();
  203. $socId = 1;
  204. $dolibarrApiService
  205. ->expects(self::once())
  206. ->method('getJsonContent')
  207. ->with('contacts', ['limit' => 1000, 'thirdparty_ids' => $socId])
  208. ->willReturn([['id' => 10], ['id' => 20]]);
  209. $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getContacts($socId));
  210. }
  211. /**
  212. * @see DolibarrApiService::getContacts()
  213. */
  214. public function testGetContactsMissing(): void
  215. {
  216. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  217. ->setConstructorArgs([$this->client])
  218. ->setMethodsExcept(['getContacts'])
  219. ->getMock();
  220. $socId = 1;
  221. $dolibarrApiService
  222. ->expects(self::once())
  223. ->method('getJsonContent')
  224. ->with('contacts', ['limit' => 1000, 'thirdparty_ids' => $socId])
  225. ->willThrowException(new HttpException(404));
  226. $this->assertEquals([], $dolibarrApiService->getContacts($socId));
  227. }
  228. /**
  229. * @see DolibarrApiService::getContacts()
  230. */
  231. public function testGetContactsError(): void
  232. {
  233. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  234. ->setConstructorArgs([$this->client])
  235. ->setMethodsExcept(['getContacts'])
  236. ->getMock();
  237. $socId = 1;
  238. $dolibarrApiService
  239. ->expects(self::once())
  240. ->method('getJsonContent')
  241. ->with('contacts', ['limit' => 1000, 'thirdparty_ids' => $socId])
  242. ->willThrowException(new HttpException(500));
  243. $this->expectException(HttpException::class);
  244. $dolibarrApiService->getContacts($socId);
  245. }
  246. /**
  247. * @see DolibarrApiService::getActiveOpentalentContacts()
  248. */
  249. public function testGetActiveOpentalentContacts(): void
  250. {
  251. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  252. ->setConstructorArgs([$this->client])
  253. ->setMethodsExcept(['getActiveOpentalentContacts'])
  254. ->getMock();
  255. $socId = 1;
  256. $dolibarrApiService
  257. ->expects(self::once())
  258. ->method('getJsonContent')
  259. ->with('contacts?limit=1000&t.statut=1&thirdparty_ids='.$socId.'&sqlfilters:=:(te.2iopen_person_id%3A%3E%3A0)')
  260. ->willReturn([['id' => 10], ['id' => 20]]);
  261. $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getActiveOpentalentContacts($socId));
  262. }
  263. /**
  264. * @see DolibarrApiService::getActiveOpentalentContacts()
  265. */
  266. public function testGetActiveOpentalentContactsMissing(): void
  267. {
  268. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  269. ->setConstructorArgs([$this->client])
  270. ->setMethodsExcept(['getActiveOpentalentContacts'])
  271. ->getMock();
  272. $socId = 1;
  273. $dolibarrApiService
  274. ->expects(self::once())
  275. ->method('getJsonContent')
  276. ->with('contacts?limit=1000&t.statut=1&thirdparty_ids='.$socId.'&sqlfilters:=:(te.2iopen_person_id%3A%3E%3A0)')
  277. ->willThrowException(new HttpException(404));
  278. $this->assertEquals([], $dolibarrApiService->getActiveOpentalentContacts($socId));
  279. }
  280. /**
  281. * @see DolibarrApiService::getActiveOpentalentContacts()
  282. */
  283. public function testGetActiveOpentalentContactsError(): void
  284. {
  285. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  286. ->setConstructorArgs([$this->client])
  287. ->setMethodsExcept(['getActiveOpentalentContacts'])
  288. ->getMock();
  289. $socId = 1;
  290. $dolibarrApiService
  291. ->expects(self::once())
  292. ->method('getJsonContent')
  293. ->with('contacts?limit=1000&t.statut=1&thirdparty_ids='.$socId.'&sqlfilters:=:(te.2iopen_person_id%3A%3E%3A0)')
  294. ->willThrowException(new HttpException(500));
  295. $this->expectException(HttpException::class);
  296. $dolibarrApiService->getActiveOpentalentContacts($socId);
  297. }
  298. /**
  299. * @see DolibarrApiService::getSocietyTagsIds()
  300. */
  301. public function testGetSocietyTagsIds(): void
  302. {
  303. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  304. ->setConstructorArgs([$this->client])
  305. ->setMethodsExcept(['getSocietyTagsIds'])
  306. ->getMock();
  307. $socId = 1;
  308. $dolibarrApiService
  309. ->expects(self::once())
  310. ->method('getJsonContent')
  311. ->with('/thirdparties/1/categories')
  312. ->willReturn([['id' => '10'], ['id' => '20']]);
  313. $this->assertEquals(
  314. [10, 20],
  315. $dolibarrApiService->getSocietyTagsIds($socId)
  316. );
  317. }
  318. /**
  319. * @see DolibarrApiService::getSocietyTagsIds()
  320. */
  321. public function testGetSocietyTagsIdsMissing(): void
  322. {
  323. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  324. ->setConstructorArgs([$this->client])
  325. ->setMethodsExcept(['getSocietyTagsIds'])
  326. ->getMock();
  327. $socId = 1;
  328. $dolibarrApiService
  329. ->expects(self::once())
  330. ->method('getJsonContent')
  331. ->with('/thirdparties/1/categories')
  332. ->willThrowException(new HttpException(404));
  333. $this->assertEquals(
  334. [],
  335. $dolibarrApiService->getSocietyTagsIds($socId)
  336. );
  337. }
  338. /**
  339. * @see DolibarrApiService::getSocietyTagsIds()
  340. */
  341. public function testGetSocietyTagsIdsError(): void
  342. {
  343. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  344. ->setConstructorArgs([$this->client])
  345. ->setMethodsExcept(['getSocietyTagsIds'])
  346. ->getMock();
  347. $socId = 1;
  348. $dolibarrApiService
  349. ->expects(self::once())
  350. ->method('getJsonContent')
  351. ->with('/thirdparties/1/categories')
  352. ->willThrowException(new HttpException(500));
  353. $this->expectException(HttpException::class);
  354. $dolibarrApiService->getSocietyTagsIds($socId);
  355. }
  356. /**
  357. * @see DolibarrApiService::createSociety
  358. */
  359. public function testCreateSociety(): void
  360. {
  361. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  362. ->setConstructorArgs([$this->client])
  363. ->setMethodsExcept(['createSociety'])
  364. ->getMock();
  365. $organization = $this->getMockBuilder(Organization::class)->getMock();
  366. $organization->method('getId')->willReturn(123);
  367. $organization->method('getName')->willReturn('Foo');
  368. $expectedPostBody = [
  369. 'name' => 'Foo',
  370. 'client' => 2,
  371. 'code_client' => -1,
  372. 'import_key' => 'crm',
  373. 'array_options' => ['options_2iopen_organization_id' => 123]
  374. ];
  375. $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
  376. $response->method('getContent')->willReturn('456');
  377. $dolibarrApiService
  378. ->expects(self::once())
  379. ->method('post')
  380. ->with("/thirdparties", $expectedPostBody)
  381. ->willReturn($response);
  382. $result = $dolibarrApiService->createSociety($organization);
  383. $this->assertEquals(456, $result);
  384. }
  385. /**
  386. * @see DolibarrApiService::createSociety
  387. */
  388. public function testCreateSocietyIsClient(): void
  389. {
  390. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  391. ->setConstructorArgs([$this->client])
  392. ->setMethodsExcept(['createSociety'])
  393. ->getMock();
  394. $organization = $this->getMockBuilder(Organization::class)->getMock();
  395. $organization->method('getId')->willReturn(123);
  396. $organization->method('getName')->willReturn('Foo');
  397. $expectedPostBody = [
  398. 'name' => 'Foo',
  399. 'client' => 1,
  400. 'code_client' => -1,
  401. 'import_key' => 'crm',
  402. 'array_options' => ['options_2iopen_organization_id' => 123]
  403. ];
  404. $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
  405. $response->method('getContent')->willReturn('456');
  406. $dolibarrApiService
  407. ->expects(self::once())
  408. ->method('post')
  409. ->with("/thirdparties", $expectedPostBody)
  410. ->willReturn($response);
  411. $result = $dolibarrApiService->createSociety($organization, true);
  412. $this->assertEquals(456, $result);
  413. }
  414. }