DolibarrApiServiceTest.php 14 KB

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