Skip to content

Commit

Permalink
- Finish api for CostCenter module with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ianriizky committed May 6, 2022
1 parent e050a3e commit f4e6bbf
Show file tree
Hide file tree
Showing 11 changed files with 1,056 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/Services/Api/CostCenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Ianriizky\TalentaApi\Services\Api;

use Illuminate\Http\Client\Response;

/**
* @property \Ianriizky\TalentaApi\Http\Client\PendingRequest $request
*
* @see https://documenter.getpostman.com/view/12246328/TWDZHvj1#ab263267-7251-41a7-aa23-53fc61d58370
*/
trait CostCenter
{
/**
* Create "/cost-center" GET request to the Talenta api.
*
* Get Cost Center report.
*
* @param array|string|null $query
* @return \Illuminate\Http\Client\Response
*
* @see https://documenter.getpostman.com/view/12246328/TWDZHvj1#f88530c7-082d-4c96-a3b4-2192525cc390
*/
protected function getCostCenterReport($query = null): Response
{
return $this->request->get('/cost-center', $query);
}

/**
* Create "/company/:cost_center_id/cost-center" GET request to the Talenta api.
*
* Get company cost center.
*
* @param string $cost_center_id
* @param array|string|null $query
* @return \Illuminate\Http\Client\Response
*
* @see https://documenter.getpostman.com/view/12246328/TWDZHvj1#600b64a7-d39e-42af-9457-65dbd90fc93d
*/
protected function getCompanyCostCenter(string $cost_center_id, $query = null): Response
{
return $this->request->get('/company/'.$cost_center_id.'/cost-center', $query);
}
}
3 changes: 3 additions & 0 deletions src/Services/TalentaApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use RuntimeException;

/**
* @method \Illuminate\Http\Client\Response getCostCenterReport(array|string|null $query = null) Create "/cost-center" GET request to the Talenta api.
* @method \Illuminate\Http\Client\Response getCompanyCostCenter(string $cost_center_id, array|string|null $query = null) Create "/company/:cost_center_id/cost-center" GET request to the Talenta api.
* @method \Illuminate\Http\Client\Response addEmployee(array $data = []) Create "/employee" POST request to the Talenta api.
* @method \Illuminate\Http\Client\Response getEmployeeByUserID(string $employee_id, array|string|null $query = null) Create "/employee/:employee_id" GET request to the Talenta api.
* @method \Illuminate\Http\Client\Response getAllEmployee(array|string|null array|string|null $query = null) Create "/employee" GET request to the Talenta api.
Expand All @@ -26,6 +28,7 @@ class TalentaApi
use Macroable {
__call as macroCall;
}
use Api\CostCenter;
use Api\Employee;
use Concerns\HandleAuthentication;
use Concerns\HandleHTTPClient;
Expand Down
2 changes: 2 additions & 0 deletions src/Support/Facades/TalentaApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

/**
* @method static string createAuthenticationSignature(\Illuminate\Support\Carbon $date, string|\Psr\Http\Message\RequestInterface $request, string $hmacSecret) Create authentication signature using sha256 hash and base64 encoding.
* @method static \Illuminate\Http\Client\Response getCostCenterReport(array|string|null $query = null) Create "/cost-center" GET request to the Talenta api.
* @method static \Illuminate\Http\Client\Response getCompanyCostCenter(string $cost_center_id, array|string|null $query = null) Create "/company/:cost_center_id/cost-center" GET request to the Talenta api.
* @method static \Illuminate\Http\Client\Response addEmployee(array $data = []) Create "/employee" POST request to the Talenta api.
* @method static \Illuminate\Http\Client\Response getEmployeeByUserID(string $employee_id, array|string|null $query = null) Create "/employee/:employee_id" GET request to the Talenta api.
* @method static \Illuminate\Http\Client\Response getAllEmployee(array|string|null $query = null) Create "/employee" GET request to the Talenta api.
Expand Down
169 changes: 169 additions & 0 deletions tests/Api/CostCenterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php

namespace Ianriizky\TalentaApi\Tests\Api;

use Ianriizky\TalentaApi\Support\Facades\TalentaApi;
use Ianriizky\TalentaApi\Tests\ApiTestCase;
use Illuminate\Http\Client\RequestException;
use Illuminate\Http\Client\Response;
use Illuminate\Http\Response as HttpResponse;

/**
* @see \Ianriizky\TalentaApi\Services\Api\Employee
*/
class CostCenterTest extends ApiTestCase
{
public function test_getCostCenterReport_response_200()
{
$jsonPath = 'cost_center/getCostCenterReport/200.json';

$this->factory->fakeUsingJsonPath($jsonPath);

tap(TalentaApi::getCostCenterReport([
'user_id' => '571919',
'year' => '2021',
'month' => '8',
]), function ($response) use ($jsonPath) {
$this->assertInstanceOf(Response::class, $response);

$response->assertSameWithJsonPath($jsonPath);
});

$this->factory->assertSentCount(1);
}

public function test_getCostCenterReport_response_200_with_filter()
{
$jsonPath = 'cost_center/getCostCenterReport/200_with_filter.json';

$this->factory->fakeUsingJsonPath($jsonPath);

tap(TalentaApi::getCostCenterReport([
'user_id' => '571919',
'year' => '2021',
'month' => '8',
'payment_schedule_id' => '2475',
'cost_center_id' => '71',
// 'week' => '',
]), function ($response) use ($jsonPath) {
$this->assertInstanceOf(Response::class, $response);

$response->assertSameWithJsonPath($jsonPath);
});

$this->factory->assertSentCount(1);
}

public function test_getCostCenterReport_response_400_error_validation_month_required()
{
$jsonPath = 'cost_center/getCostCenterReport/400_error_validation_month_required.json';

$this->factory->fakeUsingJsonPath($jsonPath, HttpResponse::HTTP_BAD_REQUEST);

$this->expectException(RequestException::class);
$this->expectExceptionCode(HttpResponse::HTTP_BAD_REQUEST);

try {
TalentaApi::getCostCenterReport([
'user_id' => '571919',
'year' => '2021',
]);
} catch (RequestException $th) {
$th->response->assertSameWithJsonPath($jsonPath);

throw $th;
}

$this->factory->assertSentCount(1);
}

public function test_getCostCenterReport_response_400_error_company_sap_integration()
{
$jsonPath = 'cost_center/getCostCenterReport/400_error_company_sap_integration.json';

$this->factory->fakeUsingJsonPath($jsonPath, HttpResponse::HTTP_BAD_REQUEST);

$this->expectException(RequestException::class);
$this->expectExceptionCode(HttpResponse::HTTP_BAD_REQUEST);

try {
TalentaApi::getCostCenterReport([
'user_id' => '571919',
'year' => '2021',
]);
} catch (RequestException $th) {
$th->response->assertSameWithJsonPath($jsonPath);

throw $th;
}

$this->factory->assertSentCount(1);
}

public function test_getCompanyCostCenter_response_200()
{
$jsonPath = 'cost_center/getCompanyCostCenter/200.json';

$this->factory->fakeUsingJsonPath($jsonPath);

tap(TalentaApi::getCompanyCostCenter('3053', [
'limit' => '10',
'page' => '1',
'user_id' => '988152',
]), function ($response) use ($jsonPath) {
$this->assertInstanceOf(Response::class, $response);

$response->assertSameWithJsonPath($jsonPath);
});

$this->factory->assertSentCount(1);
}

public function test_getCompanyCostCenter_response_400()
{
$jsonPath = 'cost_center/getCompanyCostCenter/400.json';

$this->factory->fakeUsingJsonPath($jsonPath, HttpResponse::HTTP_BAD_REQUEST);

$this->expectException(RequestException::class);
$this->expectExceptionCode(HttpResponse::HTTP_BAD_REQUEST);

try {
TalentaApi::getCompanyCostCenter('3053', [
'limit' => '1000',
'page' => '1',
'user_id' => '988152x',
]);
} catch (RequestException $th) {
$th->response->assertSameWithJsonPath($jsonPath);

throw $th;
}

$this->factory->assertSentCount(1);
}

public function test_getCompanyCostCenter_response_401()
{
$jsonPath = 'cost_center/getCompanyCostCenter/401.json';

$this->factory->fakeUsingJsonPath($jsonPath, HttpResponse::HTTP_UNAUTHORIZED);

$this->expectException(RequestException::class);
$this->expectExceptionCode(HttpResponse::HTTP_UNAUTHORIZED);

try {
TalentaApi::getCompanyCostCenter('3053', [
'limit' => '10',
'page' => '1',
'user_id' => '988152',
]);
} catch (RequestException $th) {
$th->response->assertSameWithJsonPath($jsonPath);

throw $th;
}

$this->factory->assertSentCount(1);
}
}
24 changes: 24 additions & 0 deletions tests/responses/cost_center/getCompanyCostCenter/200.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"message": "Get company cost center successfully",
"data": {
"costcenter": [
{
"id": 71,
"codename": "COST CENTER DONT DELETE"
}
],
"pagination": {
"current_page": 0,
"first_page_url": "http://api-upstream.talentadev.comhttps://talenta-staging-core-haatori-web.talentadev.comhttps://talenta-staging-core-haatori-web.talentadev.com/public-api/v2/cost-center/list?companyId=3053&limit=10&page=1&userId=988152&per-page=10",
"from": 1,
"last_page": 1,
"last_page_url": "http://api-upstream.talentadev.comhttps://talenta-staging-core-haatori-web.talentadev.comhttps://talenta-staging-core-haatori-web.talentadev.com/public-api/v2/cost-center/list?companyId=3053&limit=10&page=1&userId=988152&per-page=10",
"next_page_url": "",
"path": "http://api-upstream.talentadev.comhttps://talenta-staging-core-haatori-web.talentadev.comhttps://talenta-staging-core-haatori-web.talentadev.com/public-api/v2/cost-center/list",
"per_page": 10,
"prev_page_url": "",
"to": 1,
"total": 1
}
}
}
7 changes: 7 additions & 0 deletions tests/responses/cost_center/getCompanyCostCenter/400.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"message": "input validation error",
"errors": [
"user_id param should be positive integer",
"limit must be an integer between 1 - 100"
]
}
6 changes: 6 additions & 0 deletions tests/responses/cost_center/getCompanyCostCenter/401.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"message": "Unauthorized",
"errors": [
"Unauthorized user"
]
}
54 changes: 54 additions & 0 deletions tests/responses/cost_center/getCostCenterReport/200.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"message": "Get cost center success",
"data": {
"cost_center": [
{
"codename": "567 TDA",
"payroll_component": [
{
"name": "Basic salary",
"amount": 0,
"gl_account": "100-Basic Salary",
"gl_account_type": "Direct",
"type": "Debit"
},
{
"name": "Basic salary",
"amount": 6000000,
"gl_account": "100-Basic Salary",
"gl_account_type": "In-direct",
"type": "Debit"
},
{
"name": "Tunjangan Makan",
"amount": 0,
"gl_account": "200-Tunjangan Makan",
"gl_account_type": "Direct",
"type": "Debit"
},
{
"name": "Tunjangan Makan",
"amount": 0,
"gl_account": "200-Tunjangan Makan",
"gl_account_type": "In-direct",
"type": "Debit"
},
{
"name": "Take home pay",
"amount": 0,
"gl_account": "600-Take home pay",
"gl_account_type": "Direct",
"type": "Debit"
},
{
"name": "Take home pay",
"amount": 800000,
"gl_account": "600-Take home pay",
"gl_account_type": "In-direct",
"type": "Debit"
}
]
}
]
}
}

0 comments on commit f4e6bbf

Please sign in to comment.