clqms-be/tests/unit/app/Controllers/PatientTest.php
2025-09-19 16:42:27 +07:00

256 lines
7.7 KiB
PHP

<?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);
}
}