|
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
|
|
|
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 = (new 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
|
|
|
|