Skip to content

Commit

Permalink
Resolved issue with pagination attachment to response.
Browse files Browse the repository at this point in the history
Added test for pagination response attachment.
  • Loading branch information
Dan Richards committed Sep 11, 2017
1 parent a2cecfb commit a4bd2a9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
10 changes: 4 additions & 6 deletions src/Traits/MicroServiceJsonResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,11 @@ protected function generateResponse($type, $data, $code = 200, $status = Status:
*/
protected function generatePaginatedResponse(Paginator $paginator, $type, $data, $code = 200, $status = Status::OK, $message = '')
{
$returnData = MicroServiceHelper::jsonResponseFormatter($type, $data, $code, $status, $message);

// Append the pagination response to the data.
if (is_array($data)) {
$data['pagination'] = $paginator->preparePaginationResponse()->snakeFormat();
} elseif (is_object($data)) {
$data->pagination = $paginator->preparePaginationResponse()->snakeFormat();
}
$returnData->pagination = $paginator->preparePaginationResponse()->snakeFormat();

return $this->generateResponse($type, $data, $code, $status, $message);
return response()->json($returnData, $code);
}
}
46 changes: 44 additions & 2 deletions tests/MicroServiceCoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,41 @@ protected function getExpectedServiceInfo()
*/
protected function getExpectedJsonResponse()
{
$expectedResponse = new stdClass();
$expectedResponse = new stdClass;
$expectedResponse->status = 'ok';
$expectedResponse->code = 200;
$expectedResponse->message = '';
$expectedResponse->data = new stdClass();
$expectedResponse->data = new stdClass;
$expectedResponse->data->{$this->jsonResponseDataType} = $this->jsonResponseData;

return $expectedResponse;
}

/**
* Get the expected format for an example JSON response (pre encode).
*
* @return stdClass
*/
protected function getExpectedPaginatedJsonResponse()
{
$expectedResponse = new stdClass;
$expectedResponse->status = 'ok';
$expectedResponse->code = 200;
$expectedResponse->message = '';
$expectedResponse->data = new stdClass;
$expectedResponse->data->{$this->jsonResponseDataType} = $this->jsonResponseData;
$expectedResponse->pagination = new stdClass;
$expectedResponse->pagination->total = 2;
$expectedResponse->pagination->per_page = 10;
$expectedResponse->pagination->current_page = 1;
$expectedResponse->pagination->last_page = 1;
$expectedResponse->pagination->next_page = null;
$expectedResponse->pagination->prev_page = null;

return $expectedResponse;
}


/**
* Get the expected format for an example JSON response (pre encode).
*
Expand Down Expand Up @@ -319,6 +344,23 @@ public function testPagination()
'prev_page' => 1
], (array) $responsePaginatorTwo->preparePaginationResponse()->snakeFormat());
}

/**
* Check the JSON response formatter is providing a valid response.
*
* @return void
*/
public function testPaginatedJsonResponse()
{
// Set up the paginator.
$paginator = new Paginator(count($this->jsonResponseData), 10, 1);

// Build the test response data.
$jsonResponse = MicroServiceHelper::jsonResponseFormatter($this->jsonResponseDataType, $this->jsonResponseData, 200, 'ok');
$jsonResponse->pagination = $paginator->preparePaginationResponse()->snakeFormat();

$this->assertEquals($jsonResponse, $this->getExpectedPaginatedJsonResponse());
}
}

/**
Expand Down

0 comments on commit a4bd2a9

Please sign in to comment.