DolibarrApiServiceTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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' => 'ref_int='.$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::getActiveContract()
  37. */
  38. public function testGetActiveContract(): void
  39. {
  40. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  41. ->setConstructorArgs([$this->client])
  42. ->setMethodsExcept(['getActiveContract'])
  43. ->getMock();
  44. $socId = 1;
  45. $dolibarrApiService
  46. ->expects(self::once())
  47. ->method('getJsonContent')
  48. ->with('contracts', ['limit' => '1', 'sqlfilters' => 'statut=1', 'thirdparty_ids' => $socId])
  49. ->willReturn([['id' => 1]]); // dummy non-empty data
  50. $this->assertEquals(['id' => 1], $dolibarrApiService->getActiveContract($socId));
  51. }
  52. /**
  53. * @see DolibarrApiService::getActiveContract()
  54. */
  55. public function testGetActiveContractMissing(): void
  56. {
  57. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  58. ->setConstructorArgs([$this->client])
  59. ->setMethodsExcept(['getActiveContract'])
  60. ->getMock();
  61. $socId = 1;
  62. $dolibarrApiService
  63. ->expects(self::once())
  64. ->method('getJsonContent')
  65. ->with('contracts', ['limit' => '1', 'sqlfilters' => 'statut=1', 'thirdparty_ids' => $socId])
  66. ->willThrowException(new HttpException(404));
  67. $this->assertEquals(null, $dolibarrApiService->getActiveContract($socId));
  68. }
  69. /**
  70. * @see DolibarrApiService::getActiveContract()
  71. */
  72. public function testGetActiveContractError(): 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. ->willThrowException(new HttpException(500));
  84. $this->expectException(HttpException::class);
  85. $dolibarrApiService->getActiveContract($socId);
  86. }
  87. /**
  88. * @see DolibarrApiService::getBills()
  89. */
  90. public function testGetBills(): void
  91. {
  92. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  93. ->setConstructorArgs([$this->client])
  94. ->setMethodsExcept(['getBills'])
  95. ->getMock();
  96. $socId = 1;
  97. $dolibarrApiService
  98. ->expects(self::once())
  99. ->method('getJsonContent')
  100. ->with('invoices', ['sortfield' => 'datef', 'sortorder' => 'DESC', 'limit' => 5, 'sqlfilters' => 'fk_soc='.$socId])
  101. ->willReturn([['id' => 10], ['id' => 20]]);
  102. $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getBills($socId));
  103. }
  104. /**
  105. * @see DolibarrApiService::getBills()
  106. */
  107. public function testGetBillsMissing(): void
  108. {
  109. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  110. ->setConstructorArgs([$this->client])
  111. ->setMethodsExcept(['getBills'])
  112. ->getMock();
  113. $socId = 1;
  114. $dolibarrApiService
  115. ->expects(self::once())
  116. ->method('getJsonContent')
  117. ->with('invoices', ['sortfield' => 'datef', 'sortorder' => 'DESC', 'limit' => 5, 'sqlfilters' => 'fk_soc='.$socId])
  118. ->willThrowException(new HttpException(404));
  119. $this->assertEquals([], $dolibarrApiService->getBills($socId));
  120. }
  121. /**
  122. * @see DolibarrApiService::getBills()
  123. */
  124. public function testGetBillsError(): 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. ->willThrowException(new HttpException(500));
  136. $this->expectException(HttpException::class);
  137. $dolibarrApiService->getBills($socId);
  138. }
  139. /**
  140. * @see DolibarrApiService::getAllClients()
  141. */
  142. public function testGetAllClients(): void
  143. {
  144. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  145. ->setConstructorArgs([$this->client])
  146. ->setMethodsExcept(['getAllClients'])
  147. ->getMock();
  148. $dolibarrApiService
  149. ->expects(self::once())
  150. ->method('getJsonContent')
  151. ->with('thirdparties', ['limit' => '1000000', 'sqlfilters' => 'client=1'])
  152. ->willReturn([['id' => 10], ['id' => 20]]);
  153. $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getAllClients());
  154. }
  155. /**
  156. * @see DolibarrApiService::getContacts()
  157. */
  158. public function testGetContacts(): void
  159. {
  160. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  161. ->setConstructorArgs([$this->client])
  162. ->setMethodsExcept(['getContacts'])
  163. ->getMock();
  164. $socId = 1;
  165. $dolibarrApiService
  166. ->expects(self::once())
  167. ->method('getJsonContent')
  168. ->with('contacts', ['limit' => 1000, 'thirdparty_ids' => $socId])
  169. ->willReturn([['id' => 10], ['id' => 20]]);
  170. $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getContacts($socId));
  171. }
  172. /**
  173. * @see DolibarrApiService::getContacts()
  174. */
  175. public function testGetContactsMissing(): void
  176. {
  177. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  178. ->setConstructorArgs([$this->client])
  179. ->setMethodsExcept(['getContacts'])
  180. ->getMock();
  181. $socId = 1;
  182. $dolibarrApiService
  183. ->expects(self::once())
  184. ->method('getJsonContent')
  185. ->with('contacts', ['limit' => 1000, 'thirdparty_ids' => $socId])
  186. ->willThrowException(new HttpException(404));
  187. $this->assertEquals([], $dolibarrApiService->getContacts($socId));
  188. }
  189. /**
  190. * @see DolibarrApiService::getContacts()
  191. */
  192. public function testGetContactsError(): 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. ->willThrowException(new HttpException(500));
  204. $this->expectException(HttpException::class);
  205. $dolibarrApiService->getContacts($socId);
  206. }
  207. /**
  208. * @see DolibarrApiService::getActiveOpentalentContacts()
  209. */
  210. public function testGetActiveOpentalentContacts(): void
  211. {
  212. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  213. ->setConstructorArgs([$this->client])
  214. ->setMethodsExcept(['getActiveOpentalentContacts'])
  215. ->getMock();
  216. $socId = 1;
  217. $dolibarrApiService
  218. ->expects(self::once())
  219. ->method('getJsonContent')
  220. ->with('contacts?limit=1000&t.statut=1&thirdparty_ids='.$socId.'&sqlfilters=(te.2iopen_person_id%3A%3E%3A0)')
  221. ->willReturn([['id' => 10], ['id' => 20]]);
  222. $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getActiveOpentalentContacts($socId));
  223. }
  224. /**
  225. * @see DolibarrApiService::getActiveOpentalentContacts()
  226. */
  227. public function testGetActiveOpentalentContactsMissing(): void
  228. {
  229. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  230. ->setConstructorArgs([$this->client])
  231. ->setMethodsExcept(['getActiveOpentalentContacts'])
  232. ->getMock();
  233. $socId = 1;
  234. $dolibarrApiService
  235. ->expects(self::once())
  236. ->method('getJsonContent')
  237. ->with('contacts?limit=1000&t.statut=1&thirdparty_ids='.$socId.'&sqlfilters=(te.2iopen_person_id%3A%3E%3A0)')
  238. ->willThrowException(new HttpException(404));
  239. $this->assertEquals([], $dolibarrApiService->getActiveOpentalentContacts($socId));
  240. }
  241. /**
  242. * @see DolibarrApiService::getActiveOpentalentContacts()
  243. */
  244. public function testGetActiveOpentalentContactsError(): 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. ->willThrowException(new HttpException(500));
  256. $this->expectException(HttpException::class);
  257. $dolibarrApiService->getActiveOpentalentContacts($socId);
  258. }
  259. }