DolibarrApiServiceTest.php 10 KB

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