DolibarrApiServiceTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 testGetSocietyNotExisting(): 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 testGetSocietyWithError(): void {
  54. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  55. ->setConstructorArgs([$this->client])
  56. ->setMethodsExcept(['getSociety'])
  57. ->getMock();
  58. $organizationId = 123;
  59. $e = new HttpException(500);
  60. $dolibarrApiService
  61. ->expects(self::once())
  62. ->method('getJsonContent')
  63. ->with("thirdparties", [ "limit" => "1", "sqlfilters" => "ref_int=" . $organizationId])
  64. ->willThrowException($e);
  65. $this->expectException($e::class);
  66. $dolibarrApiService->getSociety($organizationId);
  67. }
  68. /**
  69. * @see DolibarrApiService::getActiveContract()
  70. */
  71. public function testGetActiveContract(): void {
  72. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  73. ->setConstructorArgs([$this->client])
  74. ->setMethodsExcept(['getActiveContract'])
  75. ->getMock();
  76. $socId = 1;
  77. $dolibarrApiService
  78. ->expects(self::once())
  79. ->method('getJsonContent')
  80. ->with("contracts", ["limit" => "1", "sqlfilters" => "statut=1", "thirdparty_ids" => $socId])
  81. ->willReturn([['id' => 1]]); // dummy non-empty data
  82. $this->assertEquals(['id' => 1], $dolibarrApiService->getActiveContract($socId));
  83. }
  84. /**
  85. * @see DolibarrApiService::getActiveContract()
  86. */
  87. public function testGetActiveContractMissing(): void {
  88. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  89. ->setConstructorArgs([$this->client])
  90. ->setMethodsExcept(['getActiveContract'])
  91. ->getMock();
  92. $socId = 1;
  93. $dolibarrApiService
  94. ->expects(self::once())
  95. ->method('getJsonContent')
  96. ->with("contracts", ["limit" => "1", "sqlfilters" => "statut=1", "thirdparty_ids" => $socId])
  97. ->willThrowException(new HttpException(404));
  98. $this->assertEquals(null, $dolibarrApiService->getActiveContract($socId));
  99. }
  100. /**
  101. * @see DolibarrApiService::getActiveContract()
  102. */
  103. public function testGetActiveContractError(): void {
  104. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  105. ->setConstructorArgs([$this->client])
  106. ->setMethodsExcept(['getActiveContract'])
  107. ->getMock();
  108. $socId = 1;
  109. $dolibarrApiService
  110. ->expects(self::once())
  111. ->method('getJsonContent')
  112. ->with("contracts", ["limit" => "1", "sqlfilters" => "statut=1", "thirdparty_ids" => $socId])
  113. ->willThrowException(new HttpException(500));
  114. $this->expectException(HttpException::class);
  115. $dolibarrApiService->getActiveContract($socId);
  116. }
  117. /**
  118. * @see DolibarrApiService::getBills()
  119. */
  120. public function testGetBills(): void {
  121. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  122. ->setConstructorArgs([$this->client])
  123. ->setMethodsExcept(['getBills'])
  124. ->getMock();
  125. $socId = 1;
  126. $dolibarrApiService
  127. ->expects(self::once())
  128. ->method('getJsonContent')
  129. ->with("invoices", ["sortfield" => "datef", "sortorder" => "DESC", "limit" => 5, "sqlfilters" => "fk_soc=" . $socId])
  130. ->willReturn([['id' => 10], ['id' => 20]]);
  131. $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getBills($socId));
  132. }
  133. /**
  134. * @see DolibarrApiService::getBills()
  135. */
  136. public function testGetBillsMissing(): void {
  137. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  138. ->setConstructorArgs([$this->client])
  139. ->setMethodsExcept(['getBills'])
  140. ->getMock();
  141. $socId = 1;
  142. $dolibarrApiService
  143. ->expects(self::once())
  144. ->method('getJsonContent')
  145. ->with("invoices", ["sortfield" => "datef", "sortorder" => "DESC", "limit" => 5, "sqlfilters" => "fk_soc=" . $socId])
  146. ->willThrowException(new HttpException(404));
  147. $this->assertEquals([], $dolibarrApiService->getBills($socId));
  148. }
  149. /**
  150. * @see DolibarrApiService::getBills()
  151. */
  152. public function testGetBillsError(): void {
  153. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  154. ->setConstructorArgs([$this->client])
  155. ->setMethodsExcept(['getBills'])
  156. ->getMock();
  157. $socId = 1;
  158. $dolibarrApiService
  159. ->expects(self::once())
  160. ->method('getJsonContent')
  161. ->with("invoices", ["sortfield" => "datef", "sortorder" => "DESC", "limit" => 5, "sqlfilters" => "fk_soc=" . $socId])
  162. ->willThrowException(new HttpException(500));
  163. $this->expectException(HttpException::class);
  164. $dolibarrApiService->getBills($socId);
  165. }
  166. /**
  167. * @see DolibarrApiService::getAllClients()
  168. */
  169. public function testGetAllClients(): void
  170. {
  171. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  172. ->setConstructorArgs([$this->client])
  173. ->setMethodsExcept(['getAllClients'])
  174. ->getMock();
  175. $dolibarrApiService
  176. ->expects(self::once())
  177. ->method('getJsonContent')
  178. ->with("thirdparties", ["limit" => "1000000", "sqlfilters" => "client=1"])
  179. ->willReturn([['id' => 10], ['id' => 20]]);
  180. $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getAllClients());
  181. }
  182. /**
  183. * @see DolibarrApiService::getContacts()
  184. */
  185. public function testGetContacts(): void
  186. {
  187. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  188. ->setConstructorArgs([$this->client])
  189. ->setMethodsExcept(['getContacts'])
  190. ->getMock();
  191. $socId = 1;
  192. $dolibarrApiService
  193. ->expects(self::once())
  194. ->method('getJsonContent')
  195. ->with("contacts", ['limit' => 1000, 'thirdparty_ids' => $socId])
  196. ->willReturn([['id' => 10], ['id' => 20]]);
  197. $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getContacts($socId));
  198. }
  199. /**
  200. * @see DolibarrApiService::getContacts()
  201. */
  202. public function testGetContactsMissing(): void {
  203. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  204. ->setConstructorArgs([$this->client])
  205. ->setMethodsExcept(['getContacts'])
  206. ->getMock();
  207. $socId = 1;
  208. $dolibarrApiService
  209. ->expects(self::once())
  210. ->method('getJsonContent')
  211. ->with("contacts", ['limit' => 1000, 'thirdparty_ids' => $socId])
  212. ->willThrowException(new HttpException(404));
  213. $this->assertEquals([], $dolibarrApiService->getContacts($socId));
  214. }
  215. /**
  216. * @see DolibarrApiService::getContacts()
  217. */
  218. public function testGetContactsError(): void {
  219. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  220. ->setConstructorArgs([$this->client])
  221. ->setMethodsExcept(['getContacts'])
  222. ->getMock();
  223. $socId = 1;
  224. $dolibarrApiService
  225. ->expects(self::once())
  226. ->method('getJsonContent')
  227. ->with("contacts", ['limit' => 1000, 'thirdparty_ids' => $socId])
  228. ->willThrowException(new HttpException(500));
  229. $this->expectException(HttpException::class);
  230. $dolibarrApiService->getContacts($socId);
  231. }
  232. /**
  233. * @see DolibarrApiService::getActiveOpentalentContacts()
  234. */
  235. public function testGetActiveOpentalentContacts(): void
  236. {
  237. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  238. ->setConstructorArgs([$this->client])
  239. ->setMethodsExcept(['getActiveOpentalentContacts'])
  240. ->getMock();
  241. $socId = 1;
  242. $dolibarrApiService
  243. ->expects(self::once())
  244. ->method('getJsonContent')
  245. ->with("contacts?limit=1000&t.statut=1&thirdparty_ids=" . $socId . "&sqlfilters=(te.2iopen_person_id%3A%3E%3A0)")
  246. ->willReturn([['id' => 10], ['id' => 20]]);
  247. $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getActiveOpentalentContacts($socId));
  248. }
  249. /**
  250. * @see DolibarrApiService::getActiveOpentalentContacts()
  251. */
  252. public function testGetActiveOpentalentContactsMissing(): void {
  253. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  254. ->setConstructorArgs([$this->client])
  255. ->setMethodsExcept(['getActiveOpentalentContacts'])
  256. ->getMock();
  257. $socId = 1;
  258. $dolibarrApiService
  259. ->expects(self::once())
  260. ->method('getJsonContent')
  261. ->with("contacts?limit=1000&t.statut=1&thirdparty_ids=" . $socId . "&sqlfilters=(te.2iopen_person_id%3A%3E%3A0)")
  262. ->willThrowException(new HttpException(404));
  263. $this->assertEquals([], $dolibarrApiService->getActiveOpentalentContacts($socId));
  264. }
  265. /**
  266. * @see DolibarrApiService::getActiveOpentalentContacts()
  267. */
  268. public function testGetActiveOpentalentContactsError(): void {
  269. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  270. ->setConstructorArgs([$this->client])
  271. ->setMethodsExcept(['getActiveOpentalentContacts'])
  272. ->getMock();
  273. $socId = 1;
  274. $dolibarrApiService
  275. ->expects(self::once())
  276. ->method('getJsonContent')
  277. ->with("contacts?limit=1000&t.statut=1&thirdparty_ids=" . $socId . "&sqlfilters=(te.2iopen_person_id%3A%3E%3A0)")
  278. ->willThrowException(new HttpException(500));
  279. $this->expectException(HttpException::class);
  280. $dolibarrApiService->getActiveOpentalentContacts($socId);
  281. }
  282. }