Update unit testing patvisit
This commit is contained in:
parent
88f2633bb6
commit
a9ae893c16
@ -32,7 +32,7 @@ $routes->get('/api/patient/check', 'Patient\Patient::patientCheck');
|
|||||||
|
|
||||||
$routes->get('/api/patvisit', 'PatVisit::index');
|
$routes->get('/api/patvisit', 'PatVisit::index');
|
||||||
$routes->post('/api/patvisit', 'PatVisit::create');
|
$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->get('/api/patvisit/patient/(:num)', 'PatVisit::showByPatient/$1');
|
||||||
$routes->delete('/api/patvisit', 'PatVisit::delete');
|
$routes->delete('/api/patvisit', 'PatVisit::delete');
|
||||||
$routes->patch('/api/patvisit', 'PatVisit::update');
|
$routes->patch('/api/patvisit', 'PatVisit::update');
|
||||||
|
|||||||
@ -17,7 +17,12 @@ class PatVisit extends BaseController {
|
|||||||
public function show($PVID = null) {
|
public function show($PVID = null) {
|
||||||
try {
|
try {
|
||||||
$row = $this->model->show($PVID);
|
$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) {
|
} catch (\Exception $e) {
|
||||||
return $this->failServerError('Something went wrong '.$e->getMessage());
|
return $this->failServerError('Something went wrong '.$e->getMessage());
|
||||||
}
|
}
|
||||||
@ -26,7 +31,12 @@ class PatVisit extends BaseController {
|
|||||||
public function showByPatient($InternalPID = null) {
|
public function showByPatient($InternalPID = null) {
|
||||||
try {
|
try {
|
||||||
$row = $this->model->showByPatient($InternalPID);
|
$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) {
|
} catch (\Exception $e) {
|
||||||
return $this->failServerError('Something went wrong '.$e->getMessage());
|
return $this->failServerError('Something went wrong '.$e->getMessage());
|
||||||
}
|
}
|
||||||
@ -34,7 +44,8 @@ class PatVisit extends BaseController {
|
|||||||
|
|
||||||
public function update() {
|
public function update() {
|
||||||
$input = $this->request->getJSON(true);
|
$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); }
|
if (!$input["InternalPVID"] || !is_numeric($input["InternalPVID"])) { return $this->respond(['status' => 'error', 'message' => 'Invalid or missing ID'], 400); }
|
||||||
$data = $this->model->updatePatVisit($input);
|
$data = $this->model->updatePatVisit($input);
|
||||||
return $this->respond(['status' => 'success', 'message' => 'Data updated successfully', 'data' => $data], 201);
|
return $this->respond(['status' => 'success', 'message' => 'Data updated successfully', 'data' => $data], 201);
|
||||||
@ -46,6 +57,7 @@ class PatVisit extends BaseController {
|
|||||||
public function create() {
|
public function create() {
|
||||||
$input = $this->request->getJSON(true);
|
$input = $this->request->getJSON(true);
|
||||||
try {
|
try {
|
||||||
|
if(empty($input)){throw new \Exception('Input data is empty or invalid');}
|
||||||
$data = $this->model->createPatVisit($input);
|
$data = $this->model->createPatVisit($input);
|
||||||
return $this->respond(['status' => 'success', 'message' => 'Data created successfully', 'data' => $data], 201);
|
return $this->respond(['status' => 'success', 'message' => 'Data created successfully', 'data' => $data], 201);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
|
|||||||
55
tests/feature/PatVisit/PatVisitByPatientTest.php
Normal file
55
tests/feature/PatVisit/PatVisitByPatientTest.php
Normal 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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
87
tests/feature/PatVisit/PatVisitCreateTest.php
Normal file
87
tests/feature/PatVisit/PatVisitCreateTest.php
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
51
tests/feature/PatVisit/PatVisitShowTest.php
Normal file
51
tests/feature/PatVisit/PatVisitShowTest.php
Normal 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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
104
tests/feature/PatVisit/PatVisitUpdateTest.php
Normal file
104
tests/feature/PatVisit/PatVisitUpdateTest.php
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user