clqms-be/tests/feature/Patients/PatientIndexTest.php
2025-09-25 14:01:33 +07:00

137 lines
3.7 KiB
PHP

<?php
namespace Tests\Feature\Patients;
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' => "' OR 1=2 --"
]);
// 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]);
}
}