|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Test\Unit; |
| 4 | + |
| 5 | +use Ark4ne\JsonApi\Resource\JsonApiCollection; |
| 6 | +use Ark4ne\JsonApi\Resource\Resourceable; |
| 7 | +use Illuminate\Database\Eloquent\Model; |
| 8 | +use Illuminate\Http\Request; |
| 9 | +use Illuminate\Http\Resources\Json\JsonResource; |
| 10 | +use Test\Support\Stub; |
| 11 | +use Test\TestCase; |
| 12 | + |
| 13 | +class JsonApiCollectionTest extends TestCase |
| 14 | +{ |
| 15 | + public function testToArrayBasic() |
| 16 | + { |
| 17 | + foreach (range(1, 3) as $idx) { |
| 18 | + $models[] = Stub::model(['id' => $idx]); |
| 19 | + } |
| 20 | + |
| 21 | + $collection = new class(collect($models), null) extends JsonApiCollection { |
| 22 | + }; |
| 23 | + |
| 24 | + $request = new Request(); |
| 25 | + |
| 26 | + $collect = [ |
| 27 | + ['id' => 1], |
| 28 | + ['id' => 2], |
| 29 | + ['id' => 3], |
| 30 | + ]; |
| 31 | + $this->assertEquals($collect, $collection->toArray($request)); |
| 32 | + $this->assertEquals([], $collection->with($request)); |
| 33 | + $this->assertEquals([ |
| 34 | + 'data' => $collect |
| 35 | + ], $collection->toResponse($request)->getData(true)); |
| 36 | + } |
| 37 | + |
| 38 | + public function testToArrayResource() |
| 39 | + { |
| 40 | + foreach (range(1, 3) as $idx) { |
| 41 | + $resources[] = $this->getResource(Stub::model(['id' => $idx])); |
| 42 | + } |
| 43 | + |
| 44 | + $collection = new class(collect($resources), null) extends JsonApiCollection { |
| 45 | + }; |
| 46 | + |
| 47 | + $request = new Request(); |
| 48 | + |
| 49 | + $collect = [ |
| 50 | + ['type' => 'my-model', 'id' => 1], |
| 51 | + ['type' => 'my-model', 'id' => 2], |
| 52 | + ['type' => 'my-model', 'id' => 3], |
| 53 | + ]; |
| 54 | + $this->assertEquals($collect, $collection->toArray($request)); |
| 55 | + $this->assertEquals([ |
| 56 | + 'meta' => ['foo' => ['bar-1', 'bar-2', 'bar-3']], |
| 57 | + 'included' => $collect |
| 58 | + ], $collection->with($request)); |
| 59 | + |
| 60 | + $collection->with = []; |
| 61 | + $this->assertEquals([ |
| 62 | + 'data' => $collect, |
| 63 | + 'meta' => ['foo' => ['bar-1', 'bar-2', 'bar-3']], |
| 64 | + 'included' => $collect |
| 65 | + ], $collection->toResponse($request)->getData(true)); |
| 66 | + } |
| 67 | + |
| 68 | + public function testToArrayResourceMinimal() |
| 69 | + { |
| 70 | + foreach (range(1, 3) as $idx) { |
| 71 | + $resources[] = $this->getResource(Stub::model(['id' => $idx])); |
| 72 | + } |
| 73 | + |
| 74 | + $collection = new class(collect($resources), null) extends JsonApiCollection { |
| 75 | + public function toArray($request, bool $minimal = false): array |
| 76 | + { |
| 77 | + $this->with = ['other' => 'slug']; |
| 78 | + return parent::toArray($request, true); |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + $request = new Request(); |
| 83 | + |
| 84 | + $collect = [ |
| 85 | + ['type' => 'my-model', 'id' => 1], |
| 86 | + ['type' => 'my-model', 'id' => 2], |
| 87 | + ['type' => 'my-model', 'id' => 3], |
| 88 | + ]; |
| 89 | + $this->assertEquals($collect, $collection->toArray($request, true)); |
| 90 | + $this->assertEquals([ |
| 91 | + 'meta' => [ |
| 92 | + 'foo' => ['bar-1', 'bar-2', 'bar-3'] |
| 93 | + ], |
| 94 | + 'other' => 'slug' |
| 95 | + ], $collection->with($request)); |
| 96 | + $collection->with = []; |
| 97 | + $this->assertEquals([ |
| 98 | + 'data' => $collect, |
| 99 | + 'meta' => ['foo' => ['bar-1', 'bar-2', 'bar-3']], |
| 100 | + 'other' => 'slug' |
| 101 | + ], $collection->toResponse($request)->getData(true)); |
| 102 | + } |
| 103 | + |
| 104 | + private function getResource(Model $model) |
| 105 | + { |
| 106 | + return new class($model) extends JsonResource implements Resourceable { |
| 107 | + public function toArray($request, bool $minimal = false): array |
| 108 | + { |
| 109 | + $this->with['bar'] = []; |
| 110 | + $this->with['meta']['foo'] = "bar-{$this->id}"; |
| 111 | + $this->with['included'][] = [ |
| 112 | + 'type' => 'my-model', |
| 113 | + 'id' => $this->id, |
| 114 | + ] + $this->resource->toArray(); |
| 115 | + |
| 116 | + return [ |
| 117 | + 'type' => 'my-model', |
| 118 | + 'id' => $this->id, |
| 119 | + ]; |
| 120 | + } |
| 121 | + }; |
| 122 | + } |
| 123 | +} |
0 commit comments