Переглянути джерело

add tests for ApiReqestServiceTest and DolibarrAccountCreator

Olivier Massot 4 роки тому
батько
коміт
08a3b1d9b6

+ 47 - 0
tests/Service/ApiRequestServiceTest.php

@@ -0,0 +1,47 @@
+<?php
+
+namespace App\Tests\Service;
+
+use App\Service\ApiRequestService;
+use PHPUnit\Framework\TestCase;
+use Symfony\Contracts\HttpClient\HttpClientInterface;
+use Symfony\Contracts\HttpClient\ResponseInterface;
+
+class ApiRequestServiceTest extends TestCase
+{
+    private HttpClientInterface $client;
+    private ApiRequestService $apiRequestService;
+
+    public function setUp(): void {
+        $this->client = $this->getMockBuilder(HttpClientInterface::class)
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $response = $this->getMockBuilder(ResponseInterface::class)
+            ->disableOriginalConstructor()
+            ->getMock();
+        $response->method('getContent')->willReturn('{"a": 1}');
+
+        $this->client
+            ->expects($this->once())
+            ->method('request')
+            ->with("GET", "my_url.org")
+            ->willReturn($response);
+
+        $this->apiRequestService = new ApiRequestService($this->client);
+    }
+
+    public function testGetJsonContent() {
+        $this->assertEquals(
+            $this->apiRequestService->getJsonContent('my_url.org'),
+            ['a' => 1]
+        );
+    }
+
+    public function testGetContent() {
+        $this->assertEquals(
+            $this->apiRequestService->getContent('my_url.org'),
+            '{"a": 1}'
+        );
+    }
+}

+ 118 - 0
tests/Service/Dolibarr/DolibarrAccountCreatorTest.php

@@ -0,0 +1,118 @@
+<?php
+
+namespace App\Tests\Service\Dolibarr;
+
+use App\Service\Dolibarr\DolibarrAccountCreator;
+use App\Service\Dolibarr\DolibarrService;
+use PHPUnit\Framework\TestCase;
+
+class DolibarrAccountCreatorTest extends TestCase
+{
+    private DolibarrService $dolibarrService;
+    private DolibarrAccountCreator $dolibarrAccountCreator;
+
+    public function setUp(): void {
+        $this->dolibarrService = $this->getMockBuilder(DolibarrService::class)
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $this->dolibarrAccountCreator = new DolibarrAccountCreator($this->dolibarrService);
+    }
+
+    private function getJsonContentFromFixture(string $filename): array {
+        $filepath = dirname(__FILE__) . '/fixtures/' . $filename;
+        return json_decode(file_get_contents($filepath), true);
+    }
+
+    public function testGetDolibarrAccount() {
+        $this->dolibarrService
+            ->expects($this->once())
+            ->method('getSociety')
+            ->with(1)
+            ->willReturn(
+                $this->getJsonContentFromFixture('thirdparty.json')[0]
+            );
+
+        $this->dolibarrService
+            ->expects($this->once())
+            ->method('getActiveContract')
+            ->with(1726)
+            ->willReturn(
+                $this->getJsonContentFromFixture('contract.json')[0]
+            );
+
+        $this->dolibarrService
+            ->expects($this->once())
+            ->method('getBills')
+            ->with(1726)
+            ->willReturn(
+                $this->getJsonContentFromFixture('bills.json')
+            );
+
+        $dolibarrAccount = $this->dolibarrAccountCreator->getDolibarrAccount(1);
+
+        $this->assertEquals(
+            $dolibarrAccount->getClientNumber(),
+            "001855"
+        );
+        $this->assertEquals(
+            $dolibarrAccount->getContract()->getRef(),
+            "0000037-C1_1"
+        );
+        $this->assertEquals(
+            $dolibarrAccount->getContract()->getLines()[0]->getServiceRef(),
+            "ABO_ART_PRE_CMF"
+        );
+        $this->assertEquals(
+            $dolibarrAccount->getBills()[2]->getRef(),
+            "FA2105-03135"
+        );
+    }
+
+    public function testCreateDolibarrAccount() {
+        $accountData = $this->getJsonContentFromFixture('thirdparty.json')[0];
+
+        $dolibarrAccount = $this->dolibarrAccountCreator->createDolibarrAccount(1, $accountData);
+        $this->assertEquals(
+            $dolibarrAccount->getClientNumber(),
+            "001855"
+        );
+    }
+
+    public function testCreateDolibarrContract() {
+        $contractData = $this->getJsonContentFromFixture('contract.json')[0];
+
+        $dolibarrContract = $this->dolibarrAccountCreator->createDolibarrContract($contractData);
+
+        $this->assertEquals(
+            $dolibarrContract->getRef(),
+            "0000037-C1_1"
+        );
+        $this->assertEquals(
+            $dolibarrContract->getLines()[0]->getServiceRef(),
+            "ABO_ART_PRE_CMF"
+        );
+    }
+
+    public function testCreateDolibarrContractLine() {
+        $lineData = $this->getJsonContentFromFixture('contract.json')[0]['lines'][0];
+
+        $dolibarrContractLine = $this->dolibarrAccountCreator->createDolibarrContractLine($lineData);
+
+        $this->assertEquals(
+            $dolibarrContractLine->getServiceRef(),
+            "ABO_ART_PRE_CMF"
+        );
+    }
+
+    public function testCreateDolibarrBill() {
+        $billData = $this->getJsonContentFromFixture('bills.json')[0];
+
+        $dolibarrBill = $this->dolibarrAccountCreator->createDolibarrBill($billData);
+        $this->assertEquals(
+            $dolibarrBill->getRef(),
+            "AV2106-02992"
+        );
+    }
+
+}