Update unit testing patvisit

This commit is contained in:
mikael-zakaria 2025-10-20 14:09:09 +07:00
parent 88f2633bb6
commit a9ae893c16
6 changed files with 313 additions and 4 deletions

View File

@ -32,7 +32,7 @@ $routes->get('/api/patient/check', 'Patient\Patient::patientCheck');
$routes->get('/api/patvisit', 'PatVisit::index');
$routes->post('/api/patvisit', 'PatVisit::create');
$routes->get('/api/patvisit/(:num)', 'PatVisit::show/$1');
$routes->get('/api/patvisit/(:any)', 'PatVisit::show/$1');
$routes->get('/api/patvisit/patient/(:num)', 'PatVisit::showByPatient/$1');
$routes->delete('/api/patvisit', 'PatVisit::delete');
$routes->patch('/api/patvisit', 'PatVisit::update');

View File

@ -17,7 +17,12 @@ class PatVisit extends BaseController {
public function show($PVID = null) {
try {
$row = $this->model->show($PVID);
return $this->respond([ 'status' => 'success', 'message'=> "data found", 'data' => $row ], 200);
if($row == []) {
$message = "data not found";
} else {
$message = "data found";
}
return $this->respond([ 'status' => 'success', 'message'=> $message, 'data' => $row ], 200);
} catch (\Exception $e) {
return $this->failServerError('Something went wrong '.$e->getMessage());
}
@ -26,7 +31,12 @@ class PatVisit extends BaseController {
public function showByPatient($InternalPID = null) {
try {
$row = $this->model->showByPatient($InternalPID);
return $this->respond(['status' => 'success', 'message'=> "data found", 'data' => $row ], 200);
if($row == []) {
$message = "data not found";
} else {
$message = "data found";
}
return $this->respond(['status' => 'success', 'message'=> $message, 'data' => $row ], 200);
} catch (\Exception $e) {
return $this->failServerError('Something went wrong '.$e->getMessage());
}
@ -34,7 +44,8 @@ class PatVisit extends BaseController {
public function update() {
$input = $this->request->getJSON(true);
try {
try {
if(empty($input)){throw new \Exception('Input data is empty or invalid');}
if (!$input["InternalPVID"] || !is_numeric($input["InternalPVID"])) { return $this->respond(['status' => 'error', 'message' => 'Invalid or missing ID'], 400); }
$data = $this->model->updatePatVisit($input);
return $this->respond(['status' => 'success', 'message' => 'Data updated successfully', 'data' => $data], 201);
@ -46,6 +57,7 @@ class PatVisit extends BaseController {
public function create() {
$input = $this->request->getJSON(true);
try {
if(empty($input)){throw new \Exception('Input data is empty or invalid');}
$data = $this->model->createPatVisit($input);
return $this->respond(['status' => 'success', 'message' => 'Data created successfully', 'data' => $data], 201);
} catch (\Exception $e) {

View File

@ -0,0 +1,55 @@
<?php
namespace Tests\Feature\PatVisit;
use CodeIgniter\Test\FeatureTestTrait;
use CodeIgniter\Test\CIUnitTestCase;
class PatVisitByPatientTest extends CIUnitTestCase
{
use FeatureTestTrait;
protected $endpoint = 'api/patvisit/patient';
/**
* Test: Show all visits by valid InternalPID
*/
public function testShowByPatientSuccess()
{
$InternalPID = 1;
$response = $this->get($this->endpoint . '/' . $InternalPID);
$response->assertStatus(200);
// Pastikan JSON berisi struktur dasar
$response->assertJSONFragment([
'status' => 'success',
// 'message' => 'data not found',
]);
$json = json_decode($response->getJSON(), true);
// Pastikan 'data' ada
$this->assertArrayHasKey('data', $json);
$this->assertIsArray($json['data']);
}
/**
* Test: Show by patient with invalid / nonexistent InternalPID
*/
public function testShowByPatientNotFound()
{
$invalidPID = 9999999; // diasumsikan tidak ada
$response = $this->get($this->endpoint . '/' . $invalidPID);
$response->assertStatus(200);
$json = json_decode($response->getJSON(), true);
$this->assertArrayHasKey('data', $json);
$this->assertIsArray($json['data']);
$this->assertCount(0, $json['data']); // Data kosong
}
}

View File

@ -0,0 +1,87 @@
<?php
namespace Tests\Feature\PatVisit;
use CodeIgniter\Test\FeatureTestTrait;
use CodeIgniter\Test\CIUnitTestCase;
class PatVisitCreateTest extends CIUnitTestCase
{
use FeatureTestTrait;
protected $endpoint = 'api/patvisit';
/**
* Test: Create patient visit with valid data
*/
public function testCreatePatientVisitSuccess()
{
$payload = [
"InternalPID"=> "1",
"EpisodeID"=> null,
"PatDiag"=> [
"DiagCode"=> null,
"Diagnosis"=> null
],
"PatVisitADT"=> [
"ADTCode"=> "A01",
"LocationID"=> "1",
"AttDoc"=> "1",
"RefDoc"=> "1",
"AdmDoc"=> "1",
"CnsDoc"=> "1"
],
"PatAtt"=> []
];
$response = $this->withBodyFormat('json')->call('post', $this->endpoint, $payload);
// Pastikan status 201 (created)
$response->assertStatus(201);
// Pastikan JSON mengandung status success dan message yang benar
$response->assertJSONFragment([
'status' => 'success',
'message' => 'Data created successfully'
]);
$json = json_decode($response->getJSON(), true);
// Pastikan struktur data yang dikembalikan benar
$this->assertArrayHasKey('data', $json);
$this->assertArrayHasKey('PVID', $json['data']);
$this->assertArrayHasKey('InternalPVID', $json['data']);
}
/**
* Test: Create patient visit with invalid (empty) data
*/
public function testCreatePatientVisitInvalidInput()
{
$payload = [];
// Kirim data kosong
$response = $this->withBodyFormat('json')->call('post', $this->endpoint, $payload);
$response->assertStatus(500);
$response->assertJSONFragment([
'status' => 500,
]);
}
// /**
// * Test: Simulate internal server error (trigger exception manually)
// */
public function testCreatePatientVisitThrowsException()
{
// Gunakan input yang memicu exception, misalnya EpisodeID panjang tak wajar
$payload = [
'InternalPID' => 1,
'EpisodeID' => str_repeat('X', 300) // melebihi batas kolom (jika <255)
];
$response = $this->post($this->endpoint , $payload);
$response->assertStatus(500);
}
}

View File

@ -0,0 +1,51 @@
<?php
namespace Tests\Feature\PatVisit;
use CodeIgniter\Test\FeatureTestTrait;
use CodeIgniter\Test\CIUnitTestCase;
class PatVisitShowTest extends CIUnitTestCase
{
use FeatureTestTrait;
protected $endpoint = 'api/patvisit';
public function testShowPatientVisitSuccess()
{
$PVID = '1';
$response = $this->get($this->endpoint .'/'. $PVID);
$response->assertStatus(200);
$response->assertJSONFragment([
'status' => 'success',
'message' => 'data not found',
]);
$json = json_decode($response->getJSON(), true);
$this->assertArrayHasKey('data', $json);
$this->assertIsArray($json['data']);
}
/**
* Test: Show patient visit with invalid/nonexistent PVID
*/
public function testShowPatientVisitNotFound()
{
$invalidPVID = '99999';
$response = $this->get($this->endpoint .'/'. $invalidPVID);
$response->assertStatus(200);
$json = json_decode($response->getJSON(), true);
$this->assertArrayHasKey('data', $json);
$this->assertIsArray($json['data']);
$this->assertCount(0, $json['data']); // harus kosong
}
}

View File

@ -0,0 +1,104 @@
<?php
namespace Tests\Feature\PatVisit;
use CodeIgniter\Test\FeatureTestTrait;
use CodeIgniter\Test\CIUnitTestCase;
class PatVisitUpdateTest extends CIUnitTestCase
{
use FeatureTestTrait;
protected $endpoint = 'api/patvisit';
/**
* Test: Update patient visit successfully
*/
public function testUpdatePatientVisitSuccess()
{
// Sesuaikan nilai ID dengan data di database test kamu
$payload = [
'InternalPVID' => 1,
'PVID' => 'DV0001',
'EpisodeID' => 'EPI001',
'PatDiag' => [
'DiagCode' => 'A02',
'DiagName' => 'Dysentery'
],
'PatVisitADT' => [
'LocationID' => 12,
'AdmitDate' => date('Y-m-d H:i:s')
]
];
$response = $this->withBodyFormat('json')->call('patch', $this->endpoint, $payload);
// Pastikan response sukses
$response->assertStatus(201);
// Periksa fragment JSON
$response->assertJSONFragment([
'status' => 'success',
'message' => 'Data updated successfully',
]);
// Pastikan response mengandung data yang sesuai
$json = json_decode($response->getJSON(), true);
$this->assertArrayHasKey('data', $json);
$this->assertEquals('DV0001', $json['data']);
}
/**
* Test: Update patient visit with invalid or missing ID
*/
public function testUpdatePatientVisitInvalidId()
{
// InternalPVID tidak ada
$payload = [
'EpisodeID' => 'EPI002',
'PatDiag' => ['DiagCode' => 'B01', 'DiagName' => 'Flu']
];
$response = $this->withBodyFormat('json')->call('patch', $this->endpoint, $payload);
// Karena ID tidak ada → 500 Bad Request
$response->assertStatus(500);
$response->assertJSONFragment([
'status' => '500'
]);
}
/**
* Test: Update patient visit throws exception
*/
public function testUpdatePatientVisitThrowsException()
{
$payload = [
'InternalPVID' => 'zehahahaha',
'PVID' => 'DV0001',
'EpisodeID' => 'EPI001',
'PatDiag' => [
'DiagCode' => 'A02',
'DiagName' => 'Dysentery'
],
'PatVisitADT' => [
'LocationID' => 12,
'AdmitDate' => date('Y-m-d H:i:s')
]
];
$response = $this->withBodyFormat('json')->call('patch', $this->endpoint, $payload);
$response->assertStatus(400);
}
/**
* Test: Update patient visit with []
*/
public function testUpdatePatientVisitInvalidInput()
{
$payload = [];
$response = $this->withBodyFormat('json')->call('patch', $this->endpoint, $payload);
$response->assertStatus(500);
}
}