DolibarrApiServiceTest.php 14 KB

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