DolibarrApiServiceTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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::getBills()
  67. */
  68. public function testGetBills(): void {
  69. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  70. ->setConstructorArgs([$this->client])
  71. ->setMethodsExcept(['getBills'])
  72. ->getMock();
  73. $socId = 1;
  74. $dolibarrApiService
  75. ->expects(self::once())
  76. ->method('getJsonContent')
  77. ->with("invoices", ["sortfield" => "datef", "sortorder" => "DESC", "limit" => 5, "sqlfilters" => "fk_soc=" . $socId])
  78. ->willReturn([['id' => 10], ['id' => 20]]);
  79. $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getBills($socId));
  80. }
  81. /**
  82. * @see DolibarrApiService::getBills()
  83. */
  84. public function testGetBillsMissing(): void {
  85. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  86. ->setConstructorArgs([$this->client])
  87. ->setMethodsExcept(['getBills'])
  88. ->getMock();
  89. $socId = 1;
  90. $dolibarrApiService
  91. ->expects(self::once())
  92. ->method('getJsonContent')
  93. ->with("invoices", ["sortfield" => "datef", "sortorder" => "DESC", "limit" => 5, "sqlfilters" => "fk_soc=" . $socId])
  94. ->willThrowException(new HttpException(404));
  95. $this->assertEquals([], $dolibarrApiService->getBills($socId));
  96. }
  97. public function testGetAllClients(): void
  98. {
  99. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  100. ->setConstructorArgs([$this->client])
  101. ->setMethodsExcept(['getAllClients'])
  102. ->getMock();
  103. $dolibarrApiService
  104. ->expects(self::once())
  105. ->method('getJsonContent')
  106. ->with("thirdparties", ["limit" => "1000000", "sqlfilters" => "client=1"])
  107. ->willReturn([['id' => 10], ['id' => 20]]);
  108. $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getAllClients());
  109. }
  110. public function testGetContacts() {
  111. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  112. ->setConstructorArgs([$this->client])
  113. ->setMethodsExcept(['getContacts'])
  114. ->getMock();
  115. $socId = 1;
  116. $dolibarrApiService
  117. ->expects(self::once())
  118. ->method('getJsonContent')
  119. ->with("contacts", ['limit' => 1000, 'thirdparty_ids' => $socId])
  120. ->willReturn([['id' => 10], ['id' => 20]]);
  121. $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getContacts($socId));
  122. }
  123. public function testGetContactsMissing(): void {
  124. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  125. ->setConstructorArgs([$this->client])
  126. ->setMethodsExcept(['getContacts'])
  127. ->getMock();
  128. $socId = 1;
  129. $dolibarrApiService
  130. ->expects(self::once())
  131. ->method('getJsonContent')
  132. ->with("contacts", ['limit' => 1000, 'thirdparty_ids' => $socId])
  133. ->willThrowException(new HttpException(404));
  134. $this->assertEquals([], $dolibarrApiService->getContacts($socId));
  135. }
  136. public function testGetActiveOpentalentContacts() {
  137. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  138. ->setConstructorArgs([$this->client])
  139. ->setMethodsExcept(['getActiveOpentalentContacts'])
  140. ->getMock();
  141. $socId = 1;
  142. $dolibarrApiService
  143. ->expects(self::once())
  144. ->method('getJsonContent')
  145. ->with("contacts?limit=1000&t.statut=1&thirdparty_ids=" . $socId . "&sqlfilters=(te.2iopen_person_id%3A%3E%3A0)")
  146. ->willReturn([['id' => 10], ['id' => 20]]);
  147. $this->assertEquals([['id' => 10], ['id' => 20]], $dolibarrApiService->getActiveOpentalentContacts($socId));
  148. }
  149. public function testGetActiveOpentalentContactsMissing(): void {
  150. $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
  151. ->setConstructorArgs([$this->client])
  152. ->setMethodsExcept(['getActiveOpentalentContacts'])
  153. ->getMock();
  154. $socId = 1;
  155. $dolibarrApiService
  156. ->expects(self::once())
  157. ->method('getJsonContent')
  158. ->with("contacts?limit=1000&t.statut=1&thirdparty_ids=" . $socId . "&sqlfilters=(te.2iopen_person_id%3A%3E%3A0)")
  159. ->willThrowException(new HttpException(404));
  160. $this->assertEquals([], $dolibarrApiService->getActiveOpentalentContacts($socId));
  161. }
  162. }