diff --git a/src/Traits/MicroServiceJsonResponseTrait.php b/src/Traits/MicroServiceJsonResponseTrait.php index 4667d4f..4c3b6ee 100644 --- a/src/Traits/MicroServiceJsonResponseTrait.php +++ b/src/Traits/MicroServiceJsonResponseTrait.php @@ -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); } } \ No newline at end of file diff --git a/tests/MicroServiceCoreTest.php b/tests/MicroServiceCoreTest.php index f0a1859..3633cc9 100644 --- a/tests/MicroServiceCoreTest.php +++ b/tests/MicroServiceCoreTest.php @@ -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). * @@ -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()); + } } /**