Skip to content

Commit

Permalink
Merge pull request #16 from Leo24/develop-#2-testing
Browse files Browse the repository at this point in the history
Develop #2 testing
  • Loading branch information
kchapple authored Sep 5, 2016
2 parents d4043b5 + ba408d0 commit b2274bb
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 54 deletions.
16 changes: 16 additions & 0 deletions src/Adapters/FHIRAppointmentAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public function storeInterface( AppointmentInterface $appointmentInterface )
public function collectionToOutput(Request $request = null)
{
$data = $request->all();
$data = $this->parseUrl($request->server->get('QUERY_STRING'));
$collection = $this->repository->getAppointmentsByParam($data);

$bundle = new FHIRBundle;
Expand Down Expand Up @@ -274,4 +275,19 @@ public function interfaceToModel( AppointmentInterface $appointment )

return $fhirAppointment;
}

private function parseUrl($url)
{
$array = explode('&', $url);
foreach ($array as $ln) {
if (strpos($ln, 'patient') !== false) {
$data['patient'] = substr($ln, strpos($ln, "=") + 1);
}
else{
$data[] = substr($ln, strpos($ln, "=") + 1);
}
}
return $data;

}
}
147 changes: 108 additions & 39 deletions src/Adapters/FHIRPatientAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,73 @@ public function collectionToOutput()
return $output;
}

/**
* @param $id
* @return array
*/
public function showPatient($id)
{
$patient = $this->repository->get($id);
if (!empty($patient)) {
return $this->interfaceToModel( $patient );
} else {
abort(404, 'No Patient with id = ' . $id. ' found');
}
}

public function update($request)
{
$data = $request->json()->all();

if(!isset($data['id'])) {
return json_encode(array('error' => 'no arguments'));
}
// TODO add validation
$storedInterface = $this->requestToInterface( $data['id'], $data );

return $this->interfaceToModel( $storedInterface );

}

/**
* @param string $data
* @return AppointmentInterface
*
* Takes a FHIR post string and returns a AppointmentInterface
*/
public function requestToInterface( $id, $data )
{

$patientInterface = $this->repository->update($id, $data);

return $patientInterface;
}

/**
* @param PatientInterface $patientInterface
* @return PatientInterface
*/
public function updateInterface( PatientInterface $patientInterface )
{
$patientInterface = $this->repository->update( $patientInterface );
return $patientInterface;
}






/**
* @param $id
* @return array
*/
public function removePatient($id)
{
return $this->repository->delete($id);
}


/**
* @param string $data
* @return PatientInterface
Expand All @@ -95,14 +162,14 @@ public function jsonToInterface( $data )
{
$parser = new \PHPFHIRGenerated\PHPFHIRResponseParser();
$fhirPatient = $parser->parse( $data );
if ( is_a( $fhirPatient, 'FHIRPatient' ) ) {
if ( $fhirPatient instanceof FHIRPatient ) {
return $this->modelToInterface( $fhirPatient );
} else {
// Error, the Resource does not match, expecting a Patient,
// // but got something else.
abort(403, 'Error, the Resource does not match, expecting a Patient, but got "' . typeOf($fhirPatient). '"');
}


}

public function modelToInterface( FHIRPatient $fhirPatient )
Expand All @@ -123,49 +190,51 @@ public function modelToInterface( FHIRPatient $fhirPatient )

$phoneNumbers = $fhirPatient->getTelecom();
$primaryPhone = $phoneNumbers[0]->getValue();
$patientInterface->setPrimaryPhone( $primaryPhone );
$patientInterface->setPrimaryPhone($primaryPhone);

$extensions = $fhirPatient->getExtension();
foreach ( $extensions as $extension ) {
$url = $extension->getUrl();
switch ( $url ) {
case "https://fhirdev.ttdnow.com/extension/contracts":
$x2s = $extension->getExtension();
foreach ( $x2s as $x2 ) {
$url2 = $x2->getUrl();
switch ( $url2 ) {
case "#terms-of-service":
break;
case "#allow-sms" :
$allowSms = $x2->getValueBoolean();
$allowSms = ( $allowSms->getValue() == 1 ) ? 'YES' : 'NO';
$patientInterface->setAllowSms( $allowSms );
break;
}
}
break;
}
foreach ($extensions as $extension) {
$url = $extension->getUrl();
switch ($url) {
case "https://fhirdev.ttdnow.com/extension/contracts":
$x2s = $extension->getExtension();
foreach ($x2s as $x2) {
$url2 = $x2->getUrl();
switch ($url2) {
case "#terms-of-service":
break;
case "#allow-sms" :
$allowSms = $x2->getValueBoolean();
$allowSms = ($allowSms->getValue() == 1) ? 'YES' : 'NO';
$patientInterface->setAllowSms($allowSms);
break;
}
}
break;
}
}

$photos = $fhirPatient->getPhoto();
$photo = $photos[0];
$formatCode = $photo->getContentType();
$mimetype = $formatCode->getValue();
$ext = "";
switch ( $mimetype ) {
case "image/jpeg":
$ext = "jpg";
break;
default:
$ext = "jepg";
break;
if(!empty($photos)) {
$photo = $photos[0];
$formatCode = $photo->getContentType();
$mimetype = $formatCode->getValue();
$ext = "";
switch ($mimetype) {
case "image/jpeg":
$ext = "jpg";
break;
default:
$ext = "jpeg";
break;
}
$base64Binary = $photo->getData();
$photo = App::make('LibreEHR\Core\Contracts\DocumentInterface');
$photo->setMimetype($mimetype);
$photo->base64Data = $base64Binary->getValue();
$photo->filename = rand() . "." . $ext;
$patientInterface->setPhoto($photo);
}
$base64Binary = $photo->getData();
$photo = App::make( 'LibreEHR\Core\Contracts\DocumentInterface' );
$photo->setMimetype( $mimetype );
$photo->base64Data = $base64Binary->getValue();
$photo->filename = rand().".".$ext;
$patientInterface->setPhoto( $photo );
}

return $patientInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function edit($id)
* @param int $id
* @return Response
*/
public function update($id)
public function update( Request $request )
{
//
}
Expand Down
16 changes: 16 additions & 0 deletions src/Http/Controllers/PatientController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,25 @@ public function index(Request $request = null)
return $this->patientAdapter->collectionToOutput($request);
}

public function show($id = null)
{
return $this->patientAdapter->showPatient($id);
}

public function post(Request $request)
{
return $this->patientAdapter->store($request);
}


public function update( Request $request)
{
return $this->patientAdapter->update($request);
}

public function destroy($id)
{
return $this->patientAdapter->removePatient($id);
}
}

3 changes: 3 additions & 0 deletions src/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
// See resource routes in Laravel Docs
Route::resource('Patient', '\LibreEHR\FHIR\Http\Controllers\PatientController');
Route::resource('Patients', '\LibreEHR\FHIR\Http\Controllers\PatientController');
Route::get('Patient/{id}', '\LibreEHR\FHIR\Http\Controllers\PatientController@show($id)');
Route::post('Patient', '\LibreEHR\FHIR\Http\Controllers\PatientController@post');
Route::put('Patient', '\LibreEHR\FHIR\Http\Controllers\PatientController@update');
Route::delete('Patient/{id}', '\LibreEHR\FHIR\Http\Controllers\PatientController@destroy');

Route::get('Appointment', '\LibreEHR\FHIR\Http\Controllers\AppointmentController@index');
Route::post('Appointment', '\LibreEHR\FHIR\Http\Controllers\AppointmentController@post');
Expand Down
80 changes: 70 additions & 10 deletions tests/AppointmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public function testGetAllPatientAppointments()
{
$this->get('/fhir/Appointment?patient=1')
->seeJsonStructure([
"resourceType",
"id",
"meta",
"resourceType",
"type",
"total",
"link",
Expand All @@ -47,20 +47,80 @@ public function testGetAllPatientAppointments()
public function testCreateAppointment()
{
$path = __DIR__."/data";
$data = file_get_contents( "$path/appointment_create.json");
$data = json_decode(file_get_contents( "$path/appointment_create.json"), true);
$this->json('POST', '/fhir/Appointment', $data)
->seeJsonStructure([
"id",
"resourceType",
"identifier",
"name",
"telecom",
"gender",
"birthDate",
"extension",
"status",
"description",
"start",
"end",
]
);



}

/**
* Test checks functionality for getting appointments by EQUAL date
*/
public function testGetPatientAppointmentByDateEQ()
{
$patientId = 1;
$date = 'eq2016-08-04';
$response = $this->get('/fhir/Appointment?patient='.$patientId.'&date_eq='.$date);
$start = $response->response->original->entry[0]->resource->Appointment->start->value->value;
$start = $this->trimDate($start);
$this->assertEquals($this->getDate($date, 'eq'), $start);
}

/**
* Test checks functionality for getting appointments by NOT EQUAL date
*/
public function testGetPatientAppointmentByDateNE()
{
$patientId = 1;
$date = 'ne2016-08-04';
$response = $this->get('/fhir/Appointment?patient='.$patientId.'&date='.$date);
$data = $response->response->original->entry;
$assertionCount = 0;
foreach ($data as $ln) {
if ($ln->resource->Appointment->start->value->value == $this->getDate($date, 'ne')) {
$assertionCount++;
}
}
$this->assertEquals(0, $assertionCount);
}

/**
* Test checks functionality for getting appointments by GREATER THAN and LOWER THAN date
*/
public function testGetPatientAppointmentByDateGtLt()
{
$patientId = 1;
$date = 'gt2016-08-04';
$response = $this->get('/fhir/Appointment?patient='.$patientId.'&date'.$date);
$data = $response->response->original->entry;
$assertionCount = 0;
foreach ($data as $ln) {
$startDate = $this->trimDate($ln->resource->Appointment->start->value->value);
if (($startDate < $this->getDate($date, 'gt'))) {
$assertionCount++;
}
}
$this->assertEquals(0, $assertionCount);
}


private function trimDate($date){
return substr($date, 0, strpos($date, " "));
}

private function getDate($ln, $param)
{
return substr($ln, strpos($ln, $param) + 2);
}
}
Loading

0 comments on commit b2274bb

Please sign in to comment.