Skip to content

Commit a4bd2a9

Browse files
author
Dan Richards
committed
Resolved issue with pagination attachment to response.
Added test for pagination response attachment.
1 parent a2cecfb commit a4bd2a9

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

src/Traits/MicroServiceJsonResponseTrait.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,11 @@ protected function generateResponse($type, $data, $code = 200, $status = Status:
5959
*/
6060
protected function generatePaginatedResponse(Paginator $paginator, $type, $data, $code = 200, $status = Status::OK, $message = '')
6161
{
62+
$returnData = MicroServiceHelper::jsonResponseFormatter($type, $data, $code, $status, $message);
63+
6264
// Append the pagination response to the data.
63-
if (is_array($data)) {
64-
$data['pagination'] = $paginator->preparePaginationResponse()->snakeFormat();
65-
} elseif (is_object($data)) {
66-
$data->pagination = $paginator->preparePaginationResponse()->snakeFormat();
67-
}
65+
$returnData->pagination = $paginator->preparePaginationResponse()->snakeFormat();
6866

69-
return $this->generateResponse($type, $data, $code, $status, $message);
67+
return response()->json($returnData, $code);
7068
}
7169
}

tests/MicroServiceCoreTest.php

+44-2
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,41 @@ protected function getExpectedServiceInfo()
6868
*/
6969
protected function getExpectedJsonResponse()
7070
{
71-
$expectedResponse = new stdClass();
71+
$expectedResponse = new stdClass;
7272
$expectedResponse->status = 'ok';
7373
$expectedResponse->code = 200;
7474
$expectedResponse->message = '';
75-
$expectedResponse->data = new stdClass();
75+
$expectedResponse->data = new stdClass;
7676
$expectedResponse->data->{$this->jsonResponseDataType} = $this->jsonResponseData;
7777

7878
return $expectedResponse;
7979
}
8080

81+
/**
82+
* Get the expected format for an example JSON response (pre encode).
83+
*
84+
* @return stdClass
85+
*/
86+
protected function getExpectedPaginatedJsonResponse()
87+
{
88+
$expectedResponse = new stdClass;
89+
$expectedResponse->status = 'ok';
90+
$expectedResponse->code = 200;
91+
$expectedResponse->message = '';
92+
$expectedResponse->data = new stdClass;
93+
$expectedResponse->data->{$this->jsonResponseDataType} = $this->jsonResponseData;
94+
$expectedResponse->pagination = new stdClass;
95+
$expectedResponse->pagination->total = 2;
96+
$expectedResponse->pagination->per_page = 10;
97+
$expectedResponse->pagination->current_page = 1;
98+
$expectedResponse->pagination->last_page = 1;
99+
$expectedResponse->pagination->next_page = null;
100+
$expectedResponse->pagination->prev_page = null;
101+
102+
return $expectedResponse;
103+
}
104+
105+
81106
/**
82107
* Get the expected format for an example JSON response (pre encode).
83108
*
@@ -319,6 +344,23 @@ public function testPagination()
319344
'prev_page' => 1
320345
], (array) $responsePaginatorTwo->preparePaginationResponse()->snakeFormat());
321346
}
347+
348+
/**
349+
* Check the JSON response formatter is providing a valid response.
350+
*
351+
* @return void
352+
*/
353+
public function testPaginatedJsonResponse()
354+
{
355+
// Set up the paginator.
356+
$paginator = new Paginator(count($this->jsonResponseData), 10, 1);
357+
358+
// Build the test response data.
359+
$jsonResponse = MicroServiceHelper::jsonResponseFormatter($this->jsonResponseDataType, $this->jsonResponseData, 200, 'ok');
360+
$jsonResponse->pagination = $paginator->preparePaginationResponse()->snakeFormat();
361+
362+
$this->assertEquals($jsonResponse, $this->getExpectedPaginatedJsonResponse());
363+
}
322364
}
323365

324366
/**

0 commit comments

Comments
 (0)