diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 95edd0b..91a46dc 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -5,14 +5,10 @@ use CodeIgniter\Router\RouteCollection; /** * @var RouteCollection $routes */ -$routes->options('(:any)', function() { - return ''; -}); +$routes->options('(:any)', function() { return ''; }); $routes->get('/', 'Home::index'); $routes->group('api', ['filter' => 'auth'], function($routes) { - // $routes->get('coba-auth', 'Auth::coba'); - $routes->get('dashboard', 'Dashboard::index'); $routes->get('result', 'Result::index'); $routes->get('sample', 'Sample::index'); diff --git a/app/Controllers/Home.php b/app/Controllers/Home.php index 5934333..89f165a 100644 --- a/app/Controllers/Home.php +++ b/app/Controllers/Home.php @@ -2,10 +2,32 @@ namespace App\Controllers; -class Home extends BaseController -{ - public function index(): string - { - return view('welcome_message'); +use CodeIgniter\API\ResponseTrait; +use CodeIgniter\Controller; + +use Firebase\JWT\JWT; +use Firebase\JWT\Key; +use Firebase\JWT\ExpiredException; +use Firebase\JWT\SignatureInvalidException; +use Firebase\JWT\BeforeValidException; +use CodeIgniter\Cookie\Cookie; + +class Home extends Controller { + use ResponseTrait; + + public function index() { + + // $token = $this->request->getCookie('token'); + // $key = getenv('JWT_SECRET'); + + // // Decode Token dengan Key yg ada di .env + // $decodedPayload = JWT::decode($token, new Key($key, 'HS256')); + + // return $this->respond([ + // 'status' => 'success', + // 'code' => 200, + // 'message' => 'Authenticated', + // 'data' => $decodedPayload + // ], 200); } } diff --git a/app/Controllers/Patient.php b/app/Controllers/Patient.php index 3522027..d317538 100644 --- a/app/Controllers/Patient.php +++ b/app/Controllers/Patient.php @@ -176,7 +176,6 @@ class Patient extends Controller { public function create() { try { $input = $this->request->getJSON(true); - $now = date('Y-m-d H:i:s'); // Prepare data $dataPatient = $this->preparePatientData($input); @@ -252,7 +251,7 @@ class Patient extends Controller { } } - private function preparePatientData(array $input, string $now, string $mode = 'create'): array { + private function preparePatientData(array $input, string $mode = 'create'): array { $LinkTo = null; if (!empty($input['LinkTo'])) { $ids = array_column($input['LinkTo'], 'InternalPID'); @@ -330,7 +329,6 @@ class Patient extends Controller { return $data; } - private function validationError(string $context, array $errors) { return $this->respond([ 'status' => 'error', diff --git a/composer.json b/composer.json index af86398..902adb9 100644 --- a/composer.json +++ b/composer.json @@ -15,9 +15,9 @@ "firebase/php-jwt": "^6.11" }, "require-dev": { - "fakerphp/faker": "^1.9", + "fakerphp/faker": "^1.24", "mikey179/vfsstream": "^1.6", - "phpunit/phpunit": "^10.5.16" + "phpunit/phpunit": "^10.5" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 6088556..2effc41 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "4e8c3259b8aa9583cd10ce11de4dfcbe", + "content-hash": "378f858a54e9db754b16aab150c31714", "packages": [ { "name": "codeigniter4/framework", diff --git a/phpunit.xml.dist b/phpunit.xml.dist index ea422f6..d7a6003 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -42,9 +42,12 @@ + + + - + @@ -53,9 +56,9 @@ - - - + + + diff --git a/tests/unit/app/Controllers/PatientTest.php b/tests/unit/app/Controllers/PatientTest.php new file mode 100644 index 0000000..5207c07 --- /dev/null +++ b/tests/unit/app/Controllers/PatientTest.php @@ -0,0 +1,255 @@ +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); + } + +}