Update UnitTesting Patients

This commit is contained in:
mikael-zakaria 2025-09-23 10:18:48 +07:00
parent d1f91e30ba
commit 408cfae5d5
6 changed files with 396 additions and 255 deletions

View File

@ -0,0 +1,78 @@
<?php
namespace Tests\Feature\Patient;
use CodeIgniter\Test\FeatureTestTrait;
use CodeIgniter\Test\CIUnitTestCase;
class PatientCreateTest extends CIUnitTestCase
{
use FeatureTestTrait;
// 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',
// ]);
// }
}

View File

@ -0,0 +1,48 @@
<?php
namespace Tests\Feature\Patient;
use CodeIgniter\Test\FeatureTestTrait;
use CodeIgniter\Test\CIUnitTestCase;
class PatientDeleteTest extends CIUnitTestCase
{
use FeatureTestTrait;
// 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'
// ]);
// }
}

View File

@ -0,0 +1,143 @@
<?php
namespace Tests\Feature\Patient;
use CodeIgniter\Test\FeatureTestTrait;
use CodeIgniter\Test\CIUnitTestCase;
class PatientIndexTest extends CIUnitTestCase
{
use FeatureTestTrait;
protected $endpoint = 'api/patient';
/**
* Case 1: tanpa parameter, harus 200 dan status success
*/
public function testIndexWithoutParams()
{
$result = $this->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]);
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace Tests\Feature\Patient;
use CodeIgniter\Test\FeatureTestTrait;
use CodeIgniter\Test\CIUnitTestCase;
class PatientShowTest extends CIUnitTestCase
{
use FeatureTestTrait;
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
}
}

View File

@ -0,0 +1,88 @@
<?php
namespace Tests\Feature\Patient;
use CodeIgniter\Test\FeatureTestTrait;
use CodeIgniter\Test\CIUnitTestCase;
class PatientUpdateTest extends CIUnitTestCase
{
use FeatureTestTrait;
// 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);
// }
}

View File

@ -1,255 +0,0 @@
<?php
namespace Tests\App\Controllers;
use CodeIgniter\Test\FeatureTestTrait;
use CodeIgniter\Test\CIUnitTestCase;
class PatientTest extends CIUnitTestCase
{
use FeatureTestTrait;
// Hasil Diharapkan adalah 200
public function testIndexWithoutParams() {
$result = $this->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);
}
}