Update Unit Testing Patient

This commit is contained in:
mikael-zakaria 2025-10-17 15:31:48 +07:00
parent 4daf1789db
commit 6287785b73
3 changed files with 28 additions and 61 deletions

View File

@ -13,15 +13,15 @@ class PatientCreateTest extends CIUnitTestCase
// 400 - Passed // 400 - Passed
public function testCreatePatientValidationFail() { public function testCreatePatientValidationFail() {
// Test dengan payload yg tidak lengkap
// error 400 yg diharapkan // error 400 yg diharapkan
$payload = ['Name' => 'Ngawur']; $payload = ['Name' => 'Ngawur'];
$result = $this->withBodyFormat('json') $result = $this->withBodyFormat('json')
->call('post', 'api/patient', $payload); ->call('post', 'api/patient', $payload);
$result->assertStatus(400); $result->assertStatus(400);
$result->assertJSONFragment([
'status' => 'error'
]);
// Test dengan PatiD yg sudah ada
// Kondisi Jika PatiD Sama // Kondisi Jika PatiD Sama
$payload = [ $payload = [
"PatientID"=> "SMAJ1", "PatientID"=> "SMAJ1",
@ -38,20 +38,16 @@ class PatientCreateTest extends CIUnitTestCase
$result = $this->withBodyFormat('json') $result = $this->withBodyFormat('json')
->call('post', 'api/patient', $payload); ->call('post', 'api/patient', $payload);
$result->assertStatus(400); $result->assertStatus(400);
$result->assertJSONFragment([
'status' => 'error',
"message" => "Validation failed (patient)",
]);
} }
// 201 - Passed // 201 - Passed
// Test dengan user dummy
public function testCreatePatientSuccess() { public function testCreatePatientSuccess() {
$faker = Factory::create('id_ID'); $faker = Factory::create('id_ID');
for ($i = 0; $i < 2; $i++) { for ($i = 0; $i < 2; $i++) {
$payload = [ $payload = [
"PatientID" => "DUM" . $faker->numberBetween(1, 1000). $faker->numberBetween(1, 1000).$faker->numberBetween(1, 1000), "PatientID" => "DUM" . $faker->numberBetween(1, 1000). $faker->numberBetween(1, 1000).$faker->numberBetween(1, 1000),
"AlternatePID" => "DMY" . $faker->numberBetween(1, 1000). $faker->numberBetween(1, 1000).$faker->numberBetween(1, 1000), "AlternatePID" => "DMY" . $faker->numberBetween(1, 1000). $faker->numberBetween(1, 1000).$faker->numberBetween(1, 1000),
@ -106,17 +102,7 @@ class PatientCreateTest extends CIUnitTestCase
} }
// Error - Passed // Error - Passed
public function testCreatePatientValidationError() { // Test dengan user dummy dan harus error
$payload = [
"NameFirst" => "Jane" // PatientID kosong
];
$result = $this->withBodyFormat('json')->post($this->endpoint, $payload);
// dd($result);
$result->assertArrayHasKey("errors", $result);
}
// Error - Passed
public function testCreatePatidtValidationError() { public function testCreatePatidtValidationError() {
$faker = Factory::create('id_ID'); $faker = Factory::create('id_ID');
@ -171,11 +157,14 @@ class PatientCreateTest extends CIUnitTestCase
} }
$result = $this->withBodyFormat('json')->post($this->endpoint, $payload); $result = $this->withBodyFormat('json')->post($this->endpoint, $payload);
$json = $result->getJSON();
$result->assertArrayHasKey("errors", $result); $data = json_decode($json, true);
$result->assertArrayHasKey("error", $data);
} }
// 201 - Passed // 201 - Passed
// Test dengan user dummy dan harus berhasil - Attachment kosong
public function testCreateWithoutAttachments() { public function testCreateWithoutAttachments() {
$faker = Factory::create('id_ID'); $faker = Factory::create('id_ID');
@ -231,6 +220,7 @@ class PatientCreateTest extends CIUnitTestCase
} }
// 201 - Passed // 201 - Passed
// Test dengan user dummy dan harus berhasil - PatCom kosong
public function testCreateWithoutPatComments() { public function testCreateWithoutPatComments() {
$faker = Factory::create('id_ID'); $faker = Factory::create('id_ID');
@ -271,9 +261,9 @@ class PatientCreateTest extends CIUnitTestCase
"Identifier" => $faker->nik() ?? $faker->numerify('################') "Identifier" => $faker->nik() ?? $faker->numerify('################')
], ],
"PatAtt" => [ "PatAtt" => [
[ "Address" => "/api/upload/" . $faker->word . ".jpg" ], [ "Address" => "/api/upload/awghghghgef" . $faker->word.$faker->word . ".jpg" ],
[ "Address" => "/api/upload/" . $faker->word . ".jpg" ], [ "Address" => "/api/upload/df6ghghg4" . $faker->word .$faker->word.".jpg" ],
[ "Address" => "/api/upload/" . $faker->word . ".jpg" ] [ "Address" => "/api/upload/ldjghggsf" . $faker->word . $faker->word. ".jpg" ]
], ],
"PatCom" => null, "PatCom" => null,
]; ];
@ -290,10 +280,10 @@ class PatientCreateTest extends CIUnitTestCase
} }
// 500 - Passed // 500 - Passed
// Test dengan user dummy dan harus gagal - identifier tidak valid
public function testCreateDatabaseError() { public function testCreateDatabaseError() {
$faker = Factory::create('id_ID'); $faker = Factory::create('id_ID');
// Insert patient pertama
$payload = [ $payload = [
"PatientID" => "DaUALU" . $faker->numberBetween(1, 1000).$faker->numberBetween(1, 1000), "PatientID" => "DaUALU" . $faker->numberBetween(1, 1000).$faker->numberBetween(1, 1000),
"AlternatePID" => "DaMALU" . $faker->numberBetween(5, 1000).$faker->numberBetween(1, 1000), "AlternatePID" => "DaMALU" . $faker->numberBetween(5, 1000).$faker->numberBetween(1, 1000),
@ -327,15 +317,15 @@ class PatientCreateTest extends CIUnitTestCase
"LinkTo" => (string) $faker->numberBetween(2, 3), "LinkTo" => (string) $faker->numberBetween(2, 3),
"Custodian" => 1, "Custodian" => 1,
"PatIdt" => [ "PatIdt" => [
"IdentifierType" => "KTP", "IdentifierType" => [], // Ini Salah
"Identifier" => $faker->nik() ?? $faker->numerify('################') "Identifier" => $faker->nik() ?? $faker->numerify('################')
], ],
"PatAtt" => [ "PatAtt" => [
[ "Address" => "/api/upload/" . $faker->word . ".jpg" ], [ "Address" => "/api/upload/asasd" . $faker->word . ".jpg" ],
[ "Address" => "/api/upload/" . $faker->word . ".jpg" ], [ "Address" => "/api/upload/adsds" . $faker->word . ".jpg" ],
[ "Address" => "/api/upload/" . $faker->word . ".jpg" ] [ "Address" => "/api/upload/sdjs" . $faker->word . ".jpg" ]
], ],
"PatCom" => [], // Ini Salah "PatCom" => [],
]; ];
if($payload['DeathIndicator'] == '16') { if($payload['DeathIndicator'] == '16') {
@ -345,31 +335,10 @@ class PatientCreateTest extends CIUnitTestCase
} }
$result = $this->withBodyFormat('json')->post($this->endpoint, $payload); $result = $this->withBodyFormat('json')->post($this->endpoint, $payload);
$json = $result->getJSON();
$result->assertStatus(500); $data = json_decode($json, true);
$result->assertArrayHasKey("error", $data);
} }
// public function testCreateDuplicateIdentifier() {
// $payload = [
// "PatientID" => "PX006",
// "NameFirst" => "Alpha",
// "Identity" => [
// "IdentifierType" => "KTP",
// "Identifier" => "DUP123"
// ]
// ];
// $this->withBodyFormat('json')->post($this->endpoint, $payload);
// $payload2 = [
// "PatientID" => "PX007",
// "NameFirst" => "Beta",
// "Identity" => [
// "IdentifierType" => "KTP",
// "Identifier" => "DUP123" // Sama
// ]
// ];
// $result = $this->withBodyFormat('json')->post($this->endpoint, $payload2);
// $result->assertStatus(422);
// }
} }

View File

@ -32,8 +32,6 @@ class PatientShowTest extends CIUnitTestCase
$result->assertStatus(200); $result->assertStatus(200);
$result->assertArrayHasKey('data', $data); $result->assertArrayHasKey('data', $data);
$result->assertIsArray($data['data']); $result->assertIsArray($data['data']);
// $this->assertEquals('success', $data['status']);
} }
} }

View File

@ -48,7 +48,7 @@ class PatientUpdateTest extends CIUnitTestCase
} }
// /** // /**
// * Test gagal karena InternalPID kosong (validasi) // * Test gagal karena InternalPID kosong (validasi)
// */ // */
public function testUpdatePatientValidationError() public function testUpdatePatientValidationError()
{ {
@ -66,11 +66,11 @@ class PatientUpdateTest extends CIUnitTestCase
$data = json_decode($json, true); $data = json_decode($json, true);
$result->assertStatus(400); $result->assertStatus(400);
$this->assertArrayHasKey('errors', $data); $this->assertArrayHasKey('error', $data);
} }
// /** // /**
// * Test update dengan address baru & hapus address lama // * Test update dengan address baru & hapus address lama
// */ // */
public function testUpdatePatientWithAddressChange() public function testUpdatePatientWithAddressChange()
{ {
@ -116,7 +116,7 @@ class PatientUpdateTest extends CIUnitTestCase
} }
// /** // /**
// * ⚠️ Test update kosong → semua pattat address dihapus (soft delete) // * Test update kosong → semua pattat address dihapus (soft delete)
// */ // */
public function testUpdatePatientEmptyAddress() public function testUpdatePatientEmptyAddress()
{ {