Update Unit Test Patient

This commit is contained in:
mikael-zakaria 2025-09-19 16:42:27 +07:00
parent 91e1404bcb
commit 6454bb12f0
7 changed files with 294 additions and 20 deletions

View File

@ -5,9 +5,7 @@ use CodeIgniter\Router\RouteCollection;
/**
* @var RouteCollection $routes
*/
$routes->options('(:any)', function() {
return '';
});
$routes->options('(:any)', function() { return ''; });
$routes->get('/', 'Home::index');
//PUNYA GUS INI TEMP
@ -17,8 +15,6 @@ $routes->post('/api/v1/emr/lab/update-validasi', 'NUHATEMP::update');
$routes->post('/api/v1/emr/lab/detail', 'NUHATEMP::detail');
$routes->group('api', ['filter' => 'auth'], function($routes) {
// $routes->get('coba-auth', 'Auth::coba');
$routes->get('dashboard', 'Dashboard::index');
$routes->get('result', 'Result::index');
$routes->get('sample', 'Sample::index');

View File

@ -2,10 +2,32 @@
namespace App\Controllers;
class Home extends BaseController
{
public function index(): string
{
return view('welcome_message');
use CodeIgniter\API\ResponseTrait;
use CodeIgniter\Controller;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Firebase\JWT\ExpiredException;
use Firebase\JWT\SignatureInvalidException;
use Firebase\JWT\BeforeValidException;
use CodeIgniter\Cookie\Cookie;
class Home extends Controller {
use ResponseTrait;
public function index() {
// $token = $this->request->getCookie('token');
// $key = getenv('JWT_SECRET');
// // Decode Token dengan Key yg ada di .env
// $decodedPayload = JWT::decode($token, new Key($key, 'HS256'));
// return $this->respond([
// 'status' => 'success',
// 'code' => 200,
// 'message' => 'Authenticated',
// 'data' => $decodedPayload
// ], 200);
}
}

View File

@ -176,7 +176,6 @@ class Patient extends Controller {
public function create() {
try {
$input = $this->request->getJSON(true);
$now = date('Y-m-d H:i:s');
// Prepare data
$dataPatient = $this->preparePatientData($input);
@ -252,7 +251,7 @@ class Patient extends Controller {
}
}
private function preparePatientData(array $input, string $now, string $mode = 'create'): array {
private function preparePatientData(array $input, string $mode = 'create'): array {
$LinkTo = null;
if (!empty($input['LinkTo'])) {
$ids = array_column($input['LinkTo'], 'InternalPID');
@ -330,7 +329,6 @@ class Patient extends Controller {
return $data;
}
private function validationError(string $context, array $errors) {
return $this->respond([
'status' => 'error',

View File

@ -15,9 +15,9 @@
"firebase/php-jwt": "^6.11"
},
"require-dev": {
"fakerphp/faker": "^1.9",
"fakerphp/faker": "^1.24",
"mikey179/vfsstream": "^1.6",
"phpunit/phpunit": "^10.5.16"
"phpunit/phpunit": "^10.5"
},
"autoload": {
"psr-4": {

2
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "4e8c3259b8aa9583cd10ce11de4dfcbe",
"content-hash": "378f858a54e9db754b16aab150c31714",
"packages": [
{
"name": "codeigniter4/framework",

View File

@ -42,9 +42,12 @@
</exclude>
</source>
<php>
<!-- Enable / Disable -->
<!-- <env name="CI_ENVIRONMENT" value="testing"/> -->
<!-- <server name="app.baseURL" value="http://example.com/"/> -->
<server name="app.baseURL" value="http://localhost/clqms01/"/>
<server name="CODEIGNITER_SCREAM_DEPRECATIONS" value="0"/>
<server name="CODEIGNITER_SCREAM_DEPRECA TIONS" value="0"/>
<!-- Directory containing phpunit.xml -->
<const name="HOMEPATH" value="./"/>
<!-- Directory containing the Paths config file -->
@ -53,9 +56,9 @@
<const name="PUBLICPATH" value="./public/"/>
<!-- Database configuration -->
<env name="database.tests.hostname" value="localhost"/>
<env name="database.tests.database" value="tests"/>
<env name="database.tests.username" value="test_user"/>
<env name="database.tests.password" value="test_pass"/>
<env name="database.tests.database" value="clqm01"/>
<env name="database.tests.username" value="root"/>
<env name="database.tests.password" value=""/>
<env name="database.tests.DBDriver" value="MySQLi"/>
<!-- <env name="database.tests.DBPrefix" value="tests_"/> -->
</php>

View File

@ -0,0 +1,255 @@
<?php
namespace Tests\App\Controllers;
use CodeIgniter\Test\FeatureTestTrait;
use CodeIgniter\Test\CIUnitTestCase;
class PatientTest extends CIUnitTestCase
{
use FeatureTestTrait;
// Hasil Diharapkan adalah 200
public function testIndexWithoutParams() {
$result = $this->call('get', 'api/patient');
$result->assertStatus(200);
$result->assertJSONFragment([
'status' => 'success',
]);
}
public function testIndexWithWrongParam()
{
// Return Diharapkan adalah 200 dan data adalah []
$result = $this->call('get', 'api/patient', [
'Name' => 'Ngawur'
]);
$result->assertStatus(200);
$result->assertJSONFragment([
'status' => 'success',
'data' => []
]);
}
public function testIndexWithCorrectParam()
{
// Return Diharapkan adalah 200 dan data tidak null
$result = $this->call('get', 'api/patient', [
'Name' => 'Dummy'
]);
$result->assertStatus(200);
$json = $result->getJSON();
$data = json_decode($json, true);
$this->assertEquals('success', $data['status']);
$this->assertNotNull($data['data']); // data tidak null
$this->assertIsArray($data['data']); // data berupa array
}
public function testShowNotFound()
{
$result = $this->call('get', 'api/patient/999999'); // ID yang tidak ada
$result->assertStatus(200);
$json = $result->getJSON(); // bentuk string JSON
$json = json_decode($json, true);
// $this->assertSame('Patient with ID 999999 not found.', $json['message']);
$result->assertJSONFragment([
'status' => 'success',
'data' => []
]);
}
public function testShowFound() {
$result = $this->call('get', 'api/patient/1'); // ID yang tidak ada
$result->assertStatus(200);
$json = $result->getJSON();
$data = json_decode($json, true);
$this->assertEquals('success', $data['status']);
$this->assertNotNull($data['data']); // data tidak null
$this->assertIsArray($data['data']); // data berupa array
}
public function testDeleteFail() {
$payload = [
'InternalPID' => 9999999
];
$result = $this->withBodyFormat('json')
->withBody(json_encode($payload))
->delete('api/patient');
$result->assertStatus(404);
$json = $result->getJSON(); // bentuk string JSON
$json = json_decode($json, true);
$result->assertJSONFragment([
'status' => 404,
'error' => 404
]);
}
public function testDeleteSuccess() {
$payload = [
'InternalPID' => 2
];
$result = $this->withBodyFormat('json')
->withBody(json_encode($payload))
->delete('api/patient');
$result->assertStatus(200);
$json = $result->getJSON(); // bentuk string JSON
$json = json_decode($json, true);
// $this->assertSame('Patient with ID 999999 not found.', $json['message']);
$result->assertJSONFragment([
'status' => 'success'
]);
}
public function testCreatePatientValidationFail()
{
// error 400 yg diharapkan
$payload = ['Name' => 'Ngawur'];
$result = $this->withBodyFormat('json')
->call('post', 'api/patient', $payload);
$result->assertStatus(400);
$result->assertJSONFragment([
'status' => 'error'
]);
// Kondisi Jika PatiD Sama
$payload = [
"PatientID"=> "SMAJ1",
"AlternatePID"=> "ALT001234",
"Prefix"=> "Mr.",
"NameFirst"=> "Budi",
"NameMiddle"=> "Santoso",
"NameMaiden"=> "Kiki",
"NameLast"=> "Wijaya",
"Suffix"=> "S.kom",
"NameAlias"=> "Bud",
"Gender"=> "1",
];
$result = $this->withBodyFormat('json')
->call('post', 'api/patient', $payload);
$result->assertStatus(400);
$result->assertJSONFragment([
'status' => 'error',
"message" => "Validation failed (patient)",
]);
}
// Wajib Diganti ya payloadnya kalau mau dijalankan
public function testCreateSuccess()
{
$payload = [
"PatientID"=> "SMAJ6", //Wajib Ganti
"AlternatePID"=> "P0234",
"Prefix"=> "Mr.",
"NameFirst"=> "Budi",
"NameMiddle"=> "Santoso",
"NameMaiden"=> "Kiki",
"NameLast"=> "Wijaya",
"Suffix"=> "S.kom",
"NameAlias"=> "Bud",
"Gender"=> "1",
'EmailAddress1' => 'kaka@gmail.a1com', //Wajib Ganti
'Identity' => [
"IdentifierType" => "KTP",
"Identifier" => "317409050590100" //Wajib Ganti
]
];
// $result = $this->call('post', 'api/patient', $payload);
$result = $this->withBodyFormat('json')
->call('post', 'api/patient', $payload);
$result->assertStatus(201);
$result->assertJSONFragment([
'status' => 'success',
]);
}
public function testUpdateFail()
{
$payload = [];
$result = $this->withBodyFormat('json')
->call('patch', 'api/patient', $payload);
$result->assertStatus(400);
$payload = [
"InternalPID"=> 100,
"PatientID"=> "SMAJ50", //Wajib Ganti
"AlternatePID"=> "P0234",
"Prefix"=> "Mr.",
"NameFirst"=> "Budi",
"NameMiddle"=> "Santoso",
"NameMaiden"=> "Kiki",
"NameLast"=> "Wijaya",
"Suffix"=> "S.kom",
"NameAlias"=> "Bud",
"Gender"=> "1",
'EmailAddress1' => 'kaka@gmail.a1com', //Wajib Ganti
'Identity' => [
"IdentifierType" => "KTP",
"Identifier" => "317409050590100" //Wajib Ganti
]
];
$result = $this->withBodyFormat('json')
->call('patch', 'api/patient', $payload);
$result->assertStatus(404);
$payload = [
"PatientID"=> "SMAJ50", //Wajib Ganti
"AlternatePID"=> "P0234",
"Prefix"=> "Mr.",
"NameFirst"=> "Budi",
"NameMiddle"=> "Santoso",
"NameMaiden"=> "Kiki",
"NameLast"=> "Wijaya",
"Suffix"=> "S.kom",
"NameAlias"=> "Bud",
"Gender"=> "1",
'EmailAddress1' => 'kaka@gmail.a1com', //Wajib Ganti
'Identity' => [
"IdentifierType" => "KTP",
"Identifier" => "317409050590100" //Wajib Ganti
]
];
$result = $this->withBodyFormat('json')
->call('patch', 'api/patient', $payload);
$result->assertStatus(500);
}
public function testUpdateSuccess()
{
$payload = [
"InternalPID"=> 1, //Wajib Ganti
"PatientID"=> "SMAJ50",
"AlternatePID"=> "asasasa",
"Prefix"=> "Mr.",
"NameFirst"=> "Budi",
"NameMiddle"=> "Santoso",
"NameMaiden"=> "Kiki",
"NameLast"=> "Wijaya",
"Suffix"=> "S.kom",
"NameAlias"=> "Bud",
"Gender"=> "1",
'EmailAddress1' => 'kaka@gmail.a1com',
'Identity' => [
"IdentifierType" => "KTP",
"Identifier" => "317409050590100"
]
];
$result = $this->withBodyFormat('json')
->call('patch', 'api/patient', $payload);
$result->assertStatus(200);
}
}