From 408cfae5d52834042029cb9c64a71041e029c540 Mon Sep 17 00:00:00 2001 From: mikael-zakaria Date: Tue, 23 Sep 2025 10:18:48 +0700 Subject: [PATCH] Update UnitTesting Patients --- tests/feature/Patients/PatientCreateTest.php | 78 ++++++ tests/feature/Patients/PatientDeleteTest.php | 48 ++++ tests/feature/Patients/PatientIndexTest.php | 143 +++++++++++ tests/feature/Patients/PatientShowTest.php | 39 +++ tests/feature/Patients/PatientUpdateTest.php | 88 +++++++ tests/unit/app/Controllers/PatientTest.php | 255 ------------------- 6 files changed, 396 insertions(+), 255 deletions(-) create mode 100644 tests/feature/Patients/PatientCreateTest.php create mode 100644 tests/feature/Patients/PatientDeleteTest.php create mode 100644 tests/feature/Patients/PatientIndexTest.php create mode 100644 tests/feature/Patients/PatientShowTest.php create mode 100644 tests/feature/Patients/PatientUpdateTest.php delete mode 100644 tests/unit/app/Controllers/PatientTest.php diff --git a/tests/feature/Patients/PatientCreateTest.php b/tests/feature/Patients/PatientCreateTest.php new file mode 100644 index 0000000..a6e105e --- /dev/null +++ b/tests/feature/Patients/PatientCreateTest.php @@ -0,0 +1,78 @@ + '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', +// ]); +// } + +} diff --git a/tests/feature/Patients/PatientDeleteTest.php b/tests/feature/Patients/PatientDeleteTest.php new file mode 100644 index 0000000..0bd73f2 --- /dev/null +++ b/tests/feature/Patients/PatientDeleteTest.php @@ -0,0 +1,48 @@ + 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' +// ]); +// } + +} diff --git a/tests/feature/Patients/PatientIndexTest.php b/tests/feature/Patients/PatientIndexTest.php new file mode 100644 index 0000000..c0804f5 --- /dev/null +++ b/tests/feature/Patients/PatientIndexTest.php @@ -0,0 +1,143 @@ +call('get', $this->endpoint); + + $result->assertStatus(200); + $result->assertJSONFragment([ + 'status' => 'success', + ]); + } + + /** + * Case 2: parameter Name yang tidak ada → return kosong [] + */ + public function testIndexWithWrongNameParam() + { + $result = $this->call('get', $this->endpoint, [ + 'Name' => 'TidakAdaNama' + ]); + + $result->assertStatus(200); + $result->assertJSONFragment([ + 'status' => 'success', + 'data' => [] + ]); + } + + /** + * Case 3: parameter Name benar → return array tidak kosong + */ + public function testIndexWithCorrectNameParam() + { + // Sesuaikan dengan data di database test Anda + $result = $this->call('get', $this->endpoint, [ + 'Name' => 'Dummy' + ]); + + $result->assertStatus(200); + + $json = $result->getJSON(); + $data = json_decode($json, true); + + $this->assertEquals('success', $data['status']); + $this->assertNotEmpty($data['data']); + $this->assertIsArray($data['data']); + } + + /** + * Case 4: parameter InternalPID → return data sesuai ID + */ + public function testIndexWithInternalPID() + { + $result = $this->call('get', $this->endpoint, [ + 'InternalPID' => 1 + ]); + + $result->assertStatus(200); + + $json = $result->getJSON(); + $data = json_decode($json, true); + + $this->assertEquals('success', $data['status']); + if (!empty($data['data'])) { + $this->assertEquals(1, $data['data'][0]['InternalPID']); + } + } + + /** + * Case 5: parameter PatientID → return data sesuai PatientID + */ + public function testIndexWithPatientID() + { + $result = $this->call('get', $this->endpoint, [ + 'PatientID' => '123' + ]); + + $result->assertStatus(200); + + $json = $result->getJSON(); + $data = json_decode($json, true); + + $this->assertEquals('success', $data['status']); + if (!empty($data['data'])) { + $this->assertStringContainsString('123', $data['data'][0]['PatientID']); + } + } + + /** + * Case 6: parameter Birthdate → return data sesuai tanggal + */ + public function testIndexWithBirthdate() + { + $result = $this->call('get', $this->endpoint, [ + 'Birthdate' => '2000-01-01' + ]); + + $result->assertStatus(200); + + $json = $result->getJSON(); + $data = json_decode($json, true); + + $this->assertEquals('success', $data['status']); + if (!empty($data['data'])) { + $this->assertEquals('2000-01-01', $data['data'][0]['Birthdate']); + } + } + + /** + * Case 7: Simulasi server error (optional, jika bisa trigger) + */ + public function testIndexServerErrorSimulation() + { + // Misalnya panggil dengan param aneh untuk trigger exception + $result = $this->call('get', $this->endpoint, [ + 'InternalPID' => "'asasa-" + ]); + + // dd([ + // 'status' => $result->getStatusCode(), + // 'body' => $result->getBody(), + // 'json' => $result->getJSON(), + // ]); + + // Boleh assert 200 (jika sanitasi bagus) atau 500 (kalau exception) + $this->assertContains($result->getStatusCode(), [200, 500, null]); + } + +} diff --git a/tests/feature/Patients/PatientShowTest.php b/tests/feature/Patients/PatientShowTest.php new file mode 100644 index 0000000..1463636 --- /dev/null +++ b/tests/feature/Patients/PatientShowTest.php @@ -0,0 +1,39 @@ +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 + } + +} diff --git a/tests/feature/Patients/PatientUpdateTest.php b/tests/feature/Patients/PatientUpdateTest.php new file mode 100644 index 0000000..7711807 --- /dev/null +++ b/tests/feature/Patients/PatientUpdateTest.php @@ -0,0 +1,88 @@ +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); +// } +} diff --git a/tests/unit/app/Controllers/PatientTest.php b/tests/unit/app/Controllers/PatientTest.php deleted file mode 100644 index 5207c07..0000000 --- a/tests/unit/app/Controllers/PatientTest.php +++ /dev/null @@ -1,255 +0,0 @@ -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); - } - -}