Skip to content

Commit

Permalink
test(LaravelDataTest): Enhance resource collection tests
Browse files Browse the repository at this point in the history
- Update tests for user collection and resource
- Use UserResource for success response
- Include user collection for paginated results
- Ensure JSON snapshots reflect changes in user data representation
  • Loading branch information
guanguans committed Nov 11, 2024
1 parent ea76f1b commit dea1331
Show file tree
Hide file tree
Showing 8 changed files with 809 additions and 211 deletions.
74 changes: 57 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,63 @@ class Controller extends \App\Http\Controllers\Controller

### FAQ

<details>
<summary>How to get `\Guanguans\LaravelApiResponse\ApiResponse` instance</summary>

```php
<?php

namespace App\Http\Controllers\Api;

use Guanguans\LaravelApiResponse\Contracts\ApiResponseContract;
use Guanguans\LaravelApiResponse\Facades\ApiResponseFacade;
use Guanguans\LaravelApiResponse\Support\Traits\ApiResponseFactory;
use Illuminate\Http\JsonResponse;

class Controller extends \App\Http\Controllers\Controller
{
use ApiResponseFactory;

public function getInstance(): JsonResponse
{
/** @var \Guanguans\LaravelApiResponse\ApiResponse $apiResponse */
// $apiResponse = ApiResponseFacade::getFacadeRoot();
// $apiResponse = resolve(ApiResponseContract::class);
// $apiResponse = app(ApiResponseContract::class);
$apiResponse = $this->apiResponse();

return $apiResponse->ok();
}
}
```

</details>

<details>
<summary>How to specify api paths to automatically handle exception</summary>

* Reference to the [ApiPathsRenderUsing.php](src/RenderUsings/ApiPathsRenderUsing.php)
* Update the configuration `api-response.render_using`

</details>

<details>
<summary>How to always respond with successful http status code</summary>

* Reference to the [StatusCodePipe.php](src/Pipes/StatusCodePipe.php)
* Remove the configuration `api-response.pipes.StatusCodePipe`

</details>

<details>
<summary>How to localize message</summary>

* Reference to the [MessagePipe.php](src/Pipes/MessagePipe.php)
* Add localize status code message [install [Laravel-Lang/http-statuses](https://github.com/Laravel-Lang/http-statuses) `composer require --dev laravel-lang/http-statuses` or create lang files `resources/lang/***/http-statuses.php`]
* Add localize message [`resources/lang/xxx.json`]

</details>

<details>
<summary>How to customize pipe</summary>

Expand Down Expand Up @@ -1178,23 +1235,6 @@ class Controller extends \App\Http\Controllers\Controller

</details>

<details>
<summary>How to always respond with successful http status code</summary>

* Reference to the [StatusCodePipe.php](src/Pipes/StatusCodePipe.php)
* Remove the configuration `api-response.pipes.StatusCodePipe`

</details>

<details>
<summary>How to localize message</summary>

* Reference to the [MessagePipe.php](src/Pipes/MessagePipe.php)
* Add localize status code message [install [Laravel-Lang/http-statuses](https://github.com/Laravel-Lang/http-statuses) `composer require --dev laravel-lang/http-statuses` or create lang files `resources/lang/***/http-statuses.php`]
* Add localize message [`resources/lang/xxx.json`]

</details>

<details>
<summary>Shortcut methods of http status</summary>

Expand Down
16 changes: 12 additions & 4 deletions tests/Feature/LaravelDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,23 @@
})->group(__DIR__, __FILE__);

it('is resource collection', function (?string $wrap): void {
$userCollection = UserCollection::make(User::query()->with(['country', 'posts'])->get());
assertMatchesJsonSnapshot($this->apiResponse()->success($userCollection)->content());
$users = User::query()->with(['country', 'posts'])->get();
assertMatchesJsonSnapshot($this->apiResponse()->success(UserCollection::make($users))->content());
assertMatchesJsonSnapshot($this->apiResponse()->success(UserResource::collection($users))->content());

$userCollection = UserCollection::make(User::query()->with(['country', 'posts'])->simplePaginate(3));
$simplePaginate = User::query()->with(['country', 'posts'])->simplePaginate(3);
assertMatchesJsonSnapshot(
$this
->apiResponse()
->unshiftPipes(PaginatorDataPipe::with($wrap))
->success(UserCollection::make($simplePaginate))
->content()
);
assertMatchesJsonSnapshot(
$this
->apiResponse()
->unshiftPipes(PaginatorDataPipe::with($wrap))
->success($userCollection)
->success(UserResource::collection($simplePaginate))
->content()
);
})->group(__DIR__, __FILE__)->with('wraps');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,116 +2,157 @@
"status": true,
"code": 200,
"message": "OK",
"data": {
"data": [
{
"data": [
{
"id": 1,
"name": "John",
"country_id": 1,
"created_at": "2024-01-01 00:00:01",
"updated_at": "2024-01-01 00:00:01",
"country": {
"id": 1,
"name": "John",
"country_id": 1,
"name": "China",
"created_at": "2024-01-01 00:00:01",
"updated_at": "2024-01-01 00:00:01",
"country": {
"updated_at": "2024-01-01 00:00:01"
},
"posts": [
{
"id": 1,
"name": "China",
"title": "PHP is the best language!",
"user_id": 1,
"created_at": "2024-01-01 00:00:01",
"updated_at": "2024-01-01 00:00:01"
},
"posts": [
{
"id": 1,
"title": "PHP is the best language!",
"user_id": 1,
"created_at": "2024-01-01 00:00:01",
"updated_at": "2024-01-01 00:00:01"
},
{
"id": 2,
"title": "JAVA is the best language!",
"user_id": 1,
"created_at": "2024-01-01 00:00:02",
"updated_at": "2024-01-01 00:00:02"
},
{
"id": 3,
"title": "Python is the best language!",
"user_id": 1,
"created_at": "2024-01-01 00:00:03",
"updated_at": "2024-01-01 00:00:03"
}
]
},
{
"id": 2,
"name": "Tom",
"country_id": 2,
"created_at": "2024-01-01 00:00:02",
"updated_at": "2024-01-01 00:00:02",
"country": {
{
"id": 2,
"name": "USA",
"title": "JAVA is the best language!",
"user_id": 1,
"created_at": "2024-01-01 00:00:02",
"updated_at": "2024-01-01 00:00:02"
},
"posts": [
{
"id": 4,
"title": "Go is the best language!",
"user_id": 2,
"created_at": "2024-01-01 00:00:04",
"updated_at": "2024-01-01 00:00:04"
},
{
"id": 5,
"title": "JavaScript is the best language!",
"user_id": 2,
"created_at": "2024-01-01 00:00:05",
"updated_at": "2024-01-01 00:00:05"
},
{
"id": 6,
"title": "Ruby is the best language!",
"user_id": 2,
"created_at": "2024-01-01 00:00:06",
"updated_at": "2024-01-01 00:00:06"
}
]
},
{
"id": 3,
"name": "Jerry",
"country_id": 3,
"created_at": "2024-01-01 00:00:03",
"updated_at": "2024-01-01 00:00:03",
"country": {
{
"id": 3,
"name": "Japan",
"title": "Python is the best language!",
"user_id": 1,
"created_at": "2024-01-01 00:00:03",
"updated_at": "2024-01-01 00:00:03"
}
]
},
{
"id": 2,
"name": "Tom",
"country_id": 2,
"created_at": "2024-01-01 00:00:02",
"updated_at": "2024-01-01 00:00:02",
"country": {
"id": 2,
"name": "USA",
"created_at": "2024-01-01 00:00:02",
"updated_at": "2024-01-01 00:00:02"
},
"posts": [
{
"id": 4,
"title": "Go is the best language!",
"user_id": 2,
"created_at": "2024-01-01 00:00:04",
"updated_at": "2024-01-01 00:00:04"
},
{
"id": 5,
"title": "JavaScript is the best language!",
"user_id": 2,
"created_at": "2024-01-01 00:00:05",
"updated_at": "2024-01-01 00:00:05"
},
"posts": [
{
"id": 7,
"title": "C is the best language!",
"user_id": 3,
"created_at": "2024-01-01 00:00:07",
"updated_at": "2024-01-01 00:00:07"
}
]
}
],
"links": {
"first": "http:\/\/localhost?page=1",
"last": null,
"prev": null,
"next": "http:\/\/localhost?page=2"
{
"id": 6,
"title": "Ruby is the best language!",
"user_id": 2,
"created_at": "2024-01-01 00:00:06",
"updated_at": "2024-01-01 00:00:06"
}
]
},
"meta": {
"current_page": 1,
"from": 1,
"path": "http:\/\/localhost",
"per_page": 3,
"to": 3
{
"id": 3,
"name": "Jerry",
"country_id": 3,
"created_at": "2024-01-01 00:00:03",
"updated_at": "2024-01-01 00:00:03",
"country": {
"id": 3,
"name": "Japan",
"created_at": "2024-01-01 00:00:03",
"updated_at": "2024-01-01 00:00:03"
},
"posts": [
{
"id": 7,
"title": "C is the best language!",
"user_id": 3,
"created_at": "2024-01-01 00:00:07",
"updated_at": "2024-01-01 00:00:07"
}
]
},
{
"id": 4,
"name": "Jack",
"country_id": 4,
"created_at": "2024-01-01 00:00:04",
"updated_at": "2024-01-01 00:00:04",
"country": {
"id": 4,
"name": "Korea",
"created_at": "2024-01-01 00:00:04",
"updated_at": "2024-01-01 00:00:04"
},
"posts": []
},
{
"id": 5,
"name": "Rose",
"country_id": 5,
"created_at": "2024-01-01 00:00:05",
"updated_at": "2024-01-01 00:00:05",
"country": {
"id": 5,
"name": "UK",
"created_at": "2024-01-01 00:00:05",
"updated_at": "2024-01-01 00:00:05"
},
"posts": []
},
{
"id": 6,
"name": "Lucy",
"country_id": 6,
"created_at": "2024-01-01 00:00:06",
"updated_at": "2024-01-01 00:00:06",
"country": {
"id": 6,
"name": "France",
"created_at": "2024-01-01 00:00:06",
"updated_at": "2024-01-01 00:00:06"
},
"posts": []
},
{
"id": 7,
"name": "Lily",
"country_id": 7,
"created_at": "2024-01-01 00:00:07",
"updated_at": "2024-01-01 00:00:07",
"country": {
"id": 7,
"name": "Germany",
"created_at": "2024-01-01 00:00:07",
"updated_at": "2024-01-01 00:00:07"
},
"posts": []
}
},
],
"error": {}
}
Loading

0 comments on commit dea1331

Please sign in to comment.