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