DolibarrServiceTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Tests\Service\Dolibarr;
  3. use App\Service\Dolibarr\DolibarrService;
  4. use PHPUnit\Framework\TestCase;
  5. use Symfony\Contracts\HttpClient\HttpClientInterface;
  6. use Symfony\Contracts\HttpClient\ResponseInterface;
  7. class DolibarrServiceTest extends TestCase
  8. {
  9. private HttpClientInterface $client;
  10. private DolibarrService $dolibarrService;
  11. public function setUp(): void {
  12. $this->client = $this->getMockBuilder(HttpClientInterface::class)
  13. ->disableOriginalConstructor()
  14. ->getMock();
  15. $this->dolibarrService = new DolibarrService($this->client);
  16. }
  17. private function getContentFromFixture(string $filename): string {
  18. $filepath = dirname(__FILE__) . '/fixtures/' . $filename;
  19. return file_get_contents($filepath);
  20. }
  21. /**
  22. * @see DolibarrService::getSociety()
  23. */
  24. public function testGetSociety(): void {
  25. $responseContent = $this->getContentFromFixture('thirdparty.json');
  26. $response = $this->getMockBuilder(ResponseInterface::class)
  27. ->disableOriginalConstructor()
  28. ->getMock();
  29. $response->method('getContent')->willReturn($responseContent);
  30. $this->client
  31. ->expects($this->once())
  32. ->method('request')
  33. ->with("GET", "thirdparties?sqlfilters=ref_int%3D1")
  34. ->willReturn($response);
  35. $data = $this->dolibarrService->getSociety(1);
  36. $this->assertEquals(
  37. $data['id'],
  38. 1726
  39. );
  40. }
  41. /**
  42. * @see DolibarrService::getActiveContract()
  43. */
  44. public function testGetActiveContract(): void {
  45. $responseContent = $this->getContentFromFixture('contract.json');
  46. $response = $this->getMockBuilder(ResponseInterface::class)
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $response->method('getContent')->willReturn($responseContent);
  50. $this->client
  51. ->expects($this->once())
  52. ->method('request')
  53. ->with("GET", "contracts?limit=1&sqlfilters=statut%3D1&thirdparty_ids=1")
  54. ->willReturn($response);
  55. $this->assertEquals(
  56. $this->dolibarrService->getActiveContract(1)['socid'],
  57. 43
  58. );
  59. }
  60. /**
  61. * @see DolibarrService::getBills()
  62. */
  63. public function testGetBills(): void {
  64. $responseContent = $this->getContentFromFixture('bills.json');
  65. $response = $this->getMockBuilder(ResponseInterface::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $response->method('getContent')->willReturn($responseContent);
  69. $this->client
  70. ->expects($this->once())
  71. ->method('request')
  72. ->with("GET", "invoices?sortfield=datef&sortorder=DESC&limit=5&sqlfilters=fk_soc%3D1")
  73. ->willReturn($response);
  74. $this->assertEquals(
  75. $this->dolibarrService->getBills(1)[2]['socid'],
  76. 284
  77. );
  78. }
  79. }