DolibarrApiServiceTest.php 14 KB

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