1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Tests\Unit\Http\Resources; |
6
|
|
|
|
7
|
|
|
use Illuminate\Foundation\Testing\WithFaker; |
8
|
|
|
use Illuminate\Http\JsonResponse; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
use Illuminate\Support\Collection; |
11
|
|
|
use Longman\LaravelLodash\Http\Resources\ArrayResource; |
12
|
|
|
use Longman\LaravelLodash\Http\Resources\JsonResourceCollection; |
13
|
|
|
use Tests\Unit\TestCase; |
14
|
|
|
|
15
|
|
|
use function app; |
16
|
|
|
use function array_fill; |
17
|
|
|
|
18
|
|
|
class JsonResourceCollectionTest extends TestCase |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
use WithFaker; |
21
|
|
|
|
22
|
|
|
/** @test */ |
23
|
|
|
public function it_should_transform_collection_of_arrays(): void |
24
|
|
|
{ |
25
|
|
|
$collection = new Collection(array_fill(0, 3, [ |
26
|
|
|
'aa' => 1, |
27
|
|
|
'aa2' => 2, |
28
|
|
|
'aa3' => 3, |
29
|
|
|
])); |
30
|
|
|
|
31
|
|
|
$resource = new JsonResourceCollection($collection, ArrayResource::class); |
32
|
|
|
|
33
|
|
|
$request = app(Request::class); |
34
|
|
|
$response = $resource->additional(['custom' => '1'])->toResponse($request); |
35
|
|
|
$expected = '{"data":[{"id":null,"type":"object","attributes":{"aa":1,"aa2":2,"aa3":3}},{"id":null,"type":"object","attributes":{"aa":1,"aa2":2,"aa3":3}},{"id":null,"type":"object","attributes":{"aa":1,"aa2":2,"aa3":3}}],"custom":"1"}'; |
36
|
|
|
|
37
|
|
|
$this->assertInstanceOf(JsonResponse::class, $response); |
38
|
|
|
$this->assertSame($expected, $response->content()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** @test */ |
42
|
|
View Code Duplication |
public function it_should_transform_collection_of_models(): void |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$models = []; |
45
|
|
|
for ($i = 1; $i <= 3; $i++) { |
46
|
|
|
$model = new User([ |
47
|
|
|
'id' => $i, |
48
|
|
|
'name' => 'Custom Name ' . $i, |
49
|
|
|
'mail' => 'Custom Mail ' . $i, |
50
|
|
|
'home_address' => 'Custom Address ' . $i, |
51
|
|
|
]); |
52
|
|
|
$models[] = $model; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$collection = new Collection($models); |
56
|
|
|
|
57
|
|
|
$resource = new JsonResourceCollection($collection, UserResource::class); |
58
|
|
|
|
59
|
|
|
$request = app(Request::class); |
60
|
|
|
$response = $resource->additional(['custom' => '1'])->toResponse($request); |
61
|
|
|
$expected = '{"data":[{"id":"1","type":"User","attributes":{"name":"Custom Name 1","mail":"Custom Mail 1","homeAddress":"Custom Address 1","calculatedField":7}},{"id":"2","type":"User","attributes":{"name":"Custom Name 2","mail":"Custom Mail 2","homeAddress":"Custom Address 2","calculatedField":7}},{"id":"3","type":"User","attributes":{"name":"Custom Name 3","mail":"Custom Mail 3","homeAddress":"Custom Address 3","calculatedField":7}}],"custom":"1"}'; |
62
|
|
|
$this->assertInstanceOf(JsonResponse::class, $response); |
63
|
|
|
$this->assertSame($expected, $response->content()); |
64
|
|
|
|
65
|
|
|
$array = [ |
66
|
|
|
'name' => 'Custom Name 1', |
67
|
|
|
'mail' => 'Custom Mail 1', |
68
|
|
|
'homeAddress' => 'Custom Address 1', |
69
|
|
|
'calculatedField' => 7, |
70
|
|
|
]; |
71
|
|
|
$transformed = UserResource::transformToInternal($array); |
72
|
|
|
|
73
|
|
|
$this->assertSame([ |
74
|
|
|
'name' => 'Custom Name 1', |
75
|
|
|
'mail' => 'Custom Mail 1', |
76
|
|
|
'home_address' => 'Custom Address 1', |
77
|
|
|
'calculated_field' => 7, |
78
|
|
|
], $transformed); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** @test */ |
82
|
|
View Code Duplication |
public function it_should_transform_collection_of_models_with_hidden_properties(): void |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$models = []; |
85
|
|
|
for ($i = 1; $i <= 3; $i++) { |
86
|
|
|
$model = new User([ |
87
|
|
|
'id' => $i, |
88
|
|
|
'name' => 'Custom Name ' . $i, |
89
|
|
|
'mail' => 'Custom Mail ' . $i, |
90
|
|
|
'home_address' => 'Custom Address ' . $i, |
91
|
|
|
]); |
92
|
|
|
// Hide home_address |
93
|
|
|
$model->makeHidden(['home_address']); |
94
|
|
|
$models[] = $model; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$collection = new Collection($models); |
98
|
|
|
|
99
|
|
|
$resource = new JsonResourceCollection($collection, UserResource::class); |
100
|
|
|
|
101
|
|
|
$request = app(Request::class); |
102
|
|
|
$response = $resource->additional(['custom' => '1'])->toResponse($request); |
103
|
|
|
$expected = '{"data":[{"id":"1","type":"User","attributes":{"name":"Custom Name 1","mail":"Custom Mail 1","calculatedField":7}},{"id":"2","type":"User","attributes":{"name":"Custom Name 2","mail":"Custom Mail 2","calculatedField":7}},{"id":"3","type":"User","attributes":{"name":"Custom Name 3","mail":"Custom Mail 3","calculatedField":7}}],"custom":"1"}'; |
104
|
|
|
$this->assertInstanceOf(JsonResponse::class, $response); |
105
|
|
|
$this->assertSame($expected, $response->content()); |
106
|
|
|
|
107
|
|
|
$array = [ |
108
|
|
|
'name' => 'Custom Name 1', |
109
|
|
|
'mail' => 'Custom Mail 1', |
110
|
|
|
'homeAddress' => 'Custom Address 1', |
111
|
|
|
'calculatedField' => 7, |
112
|
|
|
]; |
113
|
|
|
$transformed = UserResource::transformToInternal($array); |
114
|
|
|
|
115
|
|
|
$this->assertSame([ |
116
|
|
|
'name' => 'Custom Name 1', |
117
|
|
|
'mail' => 'Custom Mail 1', |
118
|
|
|
'home_address' => 'Custom Address 1', |
119
|
|
|
'calculated_field' => 7, |
120
|
|
|
], $transformed); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** @test */ |
124
|
|
View Code Duplication |
public function it_should_transform_collection_of_models_with_hidden_properties_in_output(): void |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
$models = []; |
127
|
|
|
for ($i = 1; $i <= 3; $i++) { |
128
|
|
|
$model = new User([ |
129
|
|
|
'id' => $i, |
130
|
|
|
'name' => 'Custom Name ' . $i, |
131
|
|
|
'mail' => 'Custom Mail ' . $i, |
132
|
|
|
'home_address' => 'Custom Address ' . $i, |
133
|
|
|
]); |
134
|
|
|
$models[] = $model; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$collection = new Collection($models); |
138
|
|
|
|
139
|
|
|
// Use resiurce where property "mail" is hidden from output |
140
|
|
|
$resource = new JsonResourceCollection($collection, UserResourceWithHidden::class); |
141
|
|
|
|
142
|
|
|
$request = app(Request::class); |
143
|
|
|
$response = $resource->additional(['custom' => '1'])->toResponse($request); |
144
|
|
|
$expected = '{"data":[{"id":"1","type":"User","attributes":{"name":"Custom Name 1","homeAddress":"Custom Address 1","calculatedField":7}},{"id":"2","type":"User","attributes":{"name":"Custom Name 2","homeAddress":"Custom Address 2","calculatedField":7}},{"id":"3","type":"User","attributes":{"name":"Custom Name 3","homeAddress":"Custom Address 3","calculatedField":7}}],"custom":"1"}'; |
145
|
|
|
$this->assertInstanceOf(JsonResponse::class, $response); |
146
|
|
|
$this->assertSame($expected, $response->content()); |
147
|
|
|
|
148
|
|
|
$array = [ |
149
|
|
|
'name' => 'Custom Name 1', |
150
|
|
|
'mail' => 'Custom Mail 1', |
151
|
|
|
'homeAddress' => 'Custom Address 1', |
152
|
|
|
'calculatedField' => 7, |
153
|
|
|
]; |
154
|
|
|
$transformed = UserResource::transformToInternal($array); |
155
|
|
|
|
156
|
|
|
$this->assertSame([ |
157
|
|
|
'name' => 'Custom Name 1', |
158
|
|
|
'mail' => 'Custom Mail 1', |
159
|
|
|
'home_address' => 'Custom Address 1', |
160
|
|
|
'calculated_field' => 7, |
161
|
|
|
], $transformed); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|