|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Test\Unit; |
| 4 | + |
| 5 | +use Ark4ne\JsonApi\Resource\Relationship; |
| 6 | +use Ark4ne\JsonApi\Resource\Resourceable; |
| 7 | +use Illuminate\Http\Request; |
| 8 | +use Illuminate\Http\Resources\Json\JsonResource; |
| 9 | +use Illuminate\Http\Resources\Json\ResourceCollection; |
| 10 | +use Illuminate\Http\Resources\MissingValue; |
| 11 | +use Test\TestCase; |
| 12 | + |
| 13 | +class RelationshipTest extends TestCase |
| 14 | +{ |
| 15 | + public function testToArrayResource() |
| 16 | + { |
| 17 | + $resource = $this->getJsonResource(); |
| 18 | + |
| 19 | + $relation = new Relationship($resource); |
| 20 | + |
| 21 | + $this->assertEquals([ |
| 22 | + 'data' => [ |
| 23 | + 'data' => [ |
| 24 | + 'type' => 'my-model', |
| 25 | + 'id' => 1, |
| 26 | + ] |
| 27 | + ], |
| 28 | + 'included' => [ |
| 29 | + [ |
| 30 | + 'type' => 'my-model', |
| 31 | + 'id' => 1, |
| 32 | + 'attributes' => [ |
| 33 | + 'foo' => 'bar' |
| 34 | + ] |
| 35 | + ] |
| 36 | + ] |
| 37 | + ], $relation->toArray(new Request())); |
| 38 | + } |
| 39 | + |
| 40 | + public function testToArrayResourceMinimal() |
| 41 | + { |
| 42 | + $resource = $this->getJsonResource(); |
| 43 | + |
| 44 | + $relation = (new Relationship($resource))->withLinks([ |
| 45 | + 'self' => 'link' |
| 46 | + ])->withMeta([ |
| 47 | + 'hash' => 'azerty' |
| 48 | + ]); |
| 49 | + |
| 50 | + $this->assertEquals([ |
| 51 | + 'data' => [ |
| 52 | + 'data' => [ |
| 53 | + 'type' => 'my-model', |
| 54 | + 'id' => 1, |
| 55 | + ], |
| 56 | + 'links' => ['self' => 'link'], |
| 57 | + 'meta' => ['hash' => 'azerty'], |
| 58 | + ], |
| 59 | + ], $relation->toArray(new Request(), true)); |
| 60 | + } |
| 61 | + |
| 62 | + public function testToArrayCollection() |
| 63 | + { |
| 64 | + $resource = $this->getResourceCollection(); |
| 65 | + $relation = new Relationship($resource); |
| 66 | + |
| 67 | + $this->assertEquals([ |
| 68 | + 'data' => [ |
| 69 | + 'data' => [ |
| 70 | + [ |
| 71 | + 'type' => 'my-model', |
| 72 | + 'id' => 1, |
| 73 | + ], |
| 74 | + [ |
| 75 | + 'type' => 'my-model', |
| 76 | + 'id' => 2, |
| 77 | + ] |
| 78 | + ] |
| 79 | + ], |
| 80 | + 'included' => [ |
| 81 | + [ |
| 82 | + 'type' => 'my-model', |
| 83 | + 'id' => 1, |
| 84 | + 'attributes' => [ |
| 85 | + 'foo' => 'bar' |
| 86 | + ] |
| 87 | + ], |
| 88 | + [ |
| 89 | + 'type' => 'my-model', |
| 90 | + 'id' => 2, |
| 91 | + 'attributes' => [ |
| 92 | + 'baz' => 'tar' |
| 93 | + ] |
| 94 | + ] |
| 95 | + ] |
| 96 | + ], $relation->toArray(new Request())); |
| 97 | + } |
| 98 | + |
| 99 | + public function testToArrayCollectionMinimal() |
| 100 | + { |
| 101 | + $resource = $this->getResourceCollection(); |
| 102 | + $relation = (new Relationship($resource))->withLinks([ |
| 103 | + 'self' => 'link' |
| 104 | + ])->withMeta([ |
| 105 | + 'total' => 2 |
| 106 | + ]); |
| 107 | + |
| 108 | + $this->assertEquals([ |
| 109 | + 'data' => [ |
| 110 | + 'data' => [ |
| 111 | + [ |
| 112 | + 'type' => 'my-model', |
| 113 | + 'id' => 1, |
| 114 | + ], |
| 115 | + [ |
| 116 | + 'type' => 'my-model', |
| 117 | + 'id' => 2, |
| 118 | + ] |
| 119 | + ], |
| 120 | + 'links' => ['self' => 'link'], |
| 121 | + 'meta' => ['total' => 2], |
| 122 | + ], |
| 123 | + ], $relation->toArray(new Request(), true)); |
| 124 | + } |
| 125 | + |
| 126 | + public function testToArrayMissingValue() |
| 127 | + { |
| 128 | + $resource = $this->getJsonResourceMissingValue(); |
| 129 | + $relation = (new Relationship($resource))->withLinks([ |
| 130 | + 'self' => 'link' |
| 131 | + ])->withMeta([ |
| 132 | + 'hash' => 'azerty' |
| 133 | + ]); |
| 134 | + |
| 135 | + $this->assertEquals([ |
| 136 | + 'data' => [ |
| 137 | + 'links' => ['self' => 'link'], |
| 138 | + 'meta' => ['hash' => 'azerty'], |
| 139 | + ], |
| 140 | + ], $relation->toArray(new Request(), true)); |
| 141 | + } |
| 142 | + |
| 143 | + private function getJsonResource() |
| 144 | + { |
| 145 | + return new class((object)['id' => 1]) extends JsonResource implements Resourceable { |
| 146 | + public function toArray($request, bool $minimal = false): array |
| 147 | + { |
| 148 | + return [ |
| 149 | + 'type' => 'my-model', |
| 150 | + 'id' => $this->id, |
| 151 | + 'attributes' => [ |
| 152 | + 'foo' => 'bar' |
| 153 | + ] |
| 154 | + ]; |
| 155 | + } |
| 156 | + }; |
| 157 | + } |
| 158 | + |
| 159 | + private function getJsonResourceMissingValue() |
| 160 | + { |
| 161 | + return new class(new MissingValue()) extends JsonResource implements Resourceable { |
| 162 | + public function toArray($request, bool $minimal = false): array |
| 163 | + { |
| 164 | + return [ |
| 165 | + 'type' => 'my-model', |
| 166 | + 'id' => $this->id, |
| 167 | + 'attributes' => [ |
| 168 | + 'foo' => 'bar' |
| 169 | + ] |
| 170 | + ]; |
| 171 | + } |
| 172 | + }; |
| 173 | + } |
| 174 | + |
| 175 | + private function getResourceCollection() |
| 176 | + { |
| 177 | + return new class(collect()) extends ResourceCollection implements Resourceable { |
| 178 | + public function toArray($request, bool $minimal = false): array |
| 179 | + { |
| 180 | + return [ |
| 181 | + [ |
| 182 | + 'type' => 'my-model', |
| 183 | + 'id' => 1, |
| 184 | + 'attributes' => [ |
| 185 | + 'foo' => 'bar' |
| 186 | + ] |
| 187 | + ], |
| 188 | + [ |
| 189 | + 'type' => 'my-model', |
| 190 | + 'id' => 2, |
| 191 | + 'attributes' => [ |
| 192 | + 'baz' => 'tar' |
| 193 | + ] |
| 194 | + ] |
| 195 | + ]; |
| 196 | + } |
| 197 | + }; |
| 198 | + } |
| 199 | + |
| 200 | +} |
0 commit comments