DolibarrApiServiceTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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() {
  151. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  152. ->setConstructorArgs([$this->client])
  153. ->setMethodsExcept(['getContacts'])
  154. ->getMock();
  155. $socId = 1;
  156. $dolibarrApiService
  157. ->expects(self::once())
  158. ->method('getJsonContent')
  159. ->with("contacts", ['limit' => 1000, 'thirdparty_ids' => $socId])
  160. ->willReturn([['id' => 10], ['id' => 20]]);
  161. $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getContacts($socId));
  162. }
  163. /**
  164. * @see DolibarrApiService::getContacts()
  165. */
  166. public function testGetContactsMissing(): void {
  167. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  168. ->setConstructorArgs([$this->client])
  169. ->setMethodsExcept(['getContacts'])
  170. ->getMock();
  171. $socId = 1;
  172. $dolibarrApiService
  173. ->expects(self::once())
  174. ->method('getJsonContent')
  175. ->with("contacts", ['limit' => 1000, 'thirdparty_ids' => $socId])
  176. ->willThrowException(new HttpException(404));
  177. $this->assertEquals([], $dolibarrApiService->getContacts($socId));
  178. }
  179. /**
  180. * @see DolibarrApiService::getContacts()
  181. */
  182. public function testGetContactsError(): void {
  183. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  184. ->setConstructorArgs([$this->client])
  185. ->setMethodsExcept(['getContacts'])
  186. ->getMock();
  187. $socId = 1;
  188. $dolibarrApiService
  189. ->expects(self::once())
  190. ->method('getJsonContent')
  191. ->with("contacts", ['limit' => 1000, 'thirdparty_ids' => $socId])
  192. ->willThrowException(new HttpException(500));
  193. $this->expectException(HttpException::class);
  194. $dolibarrApiService->getContacts($socId);
  195. }
  196. /**
  197. * @see DolibarrApiService::getActiveOpentalentContacts()
  198. */
  199. public function testGetActiveOpentalentContacts() {
  200. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  201. ->setConstructorArgs([$this->client])
  202. ->setMethodsExcept(['getActiveOpentalentContacts'])
  203. ->getMock();
  204. $socId = 1;
  205. $dolibarrApiService
  206. ->expects(self::once())
  207. ->method('getJsonContent')
  208. ->with("contacts?limit=1000&t.statut=1&thirdparty_ids=" . $socId . "&sqlfilters=(te.2iopen_person_id%3A%3E%3A0)")
  209. ->willReturn([['id' => 10], ['id' => 20]]);
  210. $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getActiveOpentalentContacts($socId));
  211. }
  212. /**
  213. * @see DolibarrApiService::getActiveOpentalentContacts()
  214. */
  215. public function testGetActiveOpentalentContactsMissing(): void {
  216. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  217. ->setConstructorArgs([$this->client])
  218. ->setMethodsExcept(['getActiveOpentalentContacts'])
  219. ->getMock();
  220. $socId = 1;
  221. $dolibarrApiService
  222. ->expects(self::once())
  223. ->method('getJsonContent')
  224. ->with("contacts?limit=1000&t.statut=1&thirdparty_ids=" . $socId . "&sqlfilters=(te.2iopen_person_id%3A%3E%3A0)")
  225. ->willThrowException(new HttpException(404));
  226. $this->assertEquals([], $dolibarrApiService->getActiveOpentalentContacts($socId));
  227. }
  228. /**
  229. * @see DolibarrApiService::getActiveOpentalentContacts()
  230. */
  231. public function testGetActiveOpentalentContactsError(): void {
  232. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  233. ->setConstructorArgs([$this->client])
  234. ->setMethodsExcept(['getActiveOpentalentContacts'])
  235. ->getMock();
  236. $socId = 1;
  237. $dolibarrApiService
  238. ->expects(self::once())
  239. ->method('getJsonContent')
  240. ->with("contacts?limit=1000&t.statut=1&thirdparty_ids=" . $socId . "&sqlfilters=(te.2iopen_person_id%3A%3E%3A0)")
  241. ->willThrowException(new HttpException(500));
  242. $this->expectException(HttpException::class);
  243. $dolibarrApiService->getActiveOpentalentContacts($socId);
  244. }
  245. }