diff --git a/app/Config/Routes.php b/app/Config/Routes.php index eee34f6..bcb7dca 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -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'); diff --git a/app/Controllers/PatVisit.php b/app/Controllers/PatVisit.php index 9e72e2e..603b307 100644 --- a/app/Controllers/PatVisit.php +++ b/app/Controllers/PatVisit.php @@ -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) { diff --git a/tests/feature/PatVisit/PatVisitByPatientTest.php b/tests/feature/PatVisit/PatVisitByPatientTest.php new file mode 100644 index 0000000..ef0562e --- /dev/null +++ b/tests/feature/PatVisit/PatVisitByPatientTest.php @@ -0,0 +1,55 @@ +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 + } + +} diff --git a/tests/feature/PatVisit/PatVisitCreateTest.php b/tests/feature/PatVisit/PatVisitCreateTest.php new file mode 100644 index 0000000..f4366eb --- /dev/null +++ b/tests/feature/PatVisit/PatVisitCreateTest.php @@ -0,0 +1,87 @@ + "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); + } + +} diff --git a/tests/feature/PatVisit/PatVisitShowTest.php b/tests/feature/PatVisit/PatVisitShowTest.php new file mode 100644 index 0000000..819fbbe --- /dev/null +++ b/tests/feature/PatVisit/PatVisitShowTest.php @@ -0,0 +1,51 @@ +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 + } + +} diff --git a/tests/feature/PatVisit/PatVisitUpdateTest.php b/tests/feature/PatVisit/PatVisitUpdateTest.php new file mode 100644 index 0000000..cccefb7 --- /dev/null +++ b/tests/feature/PatVisit/PatVisitUpdateTest.php @@ -0,0 +1,104 @@ + 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); + } + +}