DolibarrApiServiceTest.php 14 KB

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