Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 16 | class JsonApiSerializerTest extends TestCase |
||
| 17 | { |
||
| 18 | private $manager; |
||
| 19 | |||
| 20 | public function setUp(): void |
||
| 21 | { |
||
| 22 | $this->manager = new Manager(); |
||
| 23 | $this->manager->setSerializer(new JsonApiSerializer()); |
||
| 24 | } |
||
| 25 | |||
| 26 | public function testSerializeCollectionWithExtraMeta() |
||
| 27 | { |
||
| 28 | $booksData = [ |
||
| 29 | [ |
||
| 30 | 'id' => 1, |
||
| 31 | 'title' => 'Foo', |
||
| 32 | 'year' => '1991', |
||
| 33 | '_author' => [ |
||
| 34 | 'id' => 1, |
||
| 35 | 'name' => 'Dave', |
||
| 36 | ], |
||
| 37 | 'meta' => [ |
||
| 38 | 'foo' => 'bar' |
||
| 39 | ] |
||
| 40 | ], |
||
| 41 | [ |
||
| 42 | 'id' => 2, |
||
| 43 | 'title' => 'Bar', |
||
| 44 | 'year' => '1997', |
||
| 45 | '_author' => [ |
||
| 46 | 'id' => 2, |
||
| 47 | 'name' => 'Bob', |
||
| 48 | ], |
||
| 49 | 'meta' => [ |
||
| 50 | 'bar' => 'baz' |
||
| 51 | ] |
||
| 52 | ], |
||
| 53 | ]; |
||
| 54 | |||
| 55 | $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
||
| 56 | $scope = new Scope($this->manager, $resource); |
||
| 57 | |||
| 58 | $expected = [ |
||
| 59 | 'data' => [ |
||
| 60 | [ |
||
| 61 | 'type' => 'books', |
||
| 62 | 'id' => '1', |
||
| 63 | 'attributes' => [ |
||
| 64 | 'title' => 'Foo', |
||
| 65 | 'year' => 1991, |
||
| 66 | ], |
||
| 67 | 'meta' => [ |
||
| 68 | 'foo' => 'bar' |
||
| 69 | ] |
||
| 70 | ], |
||
| 71 | [ |
||
| 72 | 'type' => 'books', |
||
| 73 | 'id' => '2', |
||
| 74 | 'attributes' => [ |
||
| 75 | 'title' => 'Bar', |
||
| 76 | 'year' => 1997, |
||
| 77 | ], |
||
| 78 | 'meta' => [ |
||
| 79 | 'bar' => 'baz' |
||
| 80 | ] |
||
| 81 | ], |
||
| 82 | ], |
||
| 83 | ]; |
||
| 84 | |||
| 85 | $this->assertSame($expected, $scope->toArray()); |
||
| 86 | |||
| 87 | $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"meta":{"foo":"bar"}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"meta":{"bar":"baz"}}]}'; |
||
| 88 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 89 | } |
||
| 90 | |||
| 91 | public function testSerializingItemResourceWithHasOneInclude() |
||
| 92 | { |
||
| 93 | $this->manager->parseIncludes('author'); |
||
| 94 | |||
| 95 | $bookData = [ |
||
| 96 | 'id' => 1, |
||
| 97 | 'title' => 'Foo', |
||
| 98 | 'year' => '1991', |
||
| 99 | '_author' => [ |
||
| 100 | 'id' => 1, |
||
| 101 | 'name' => 'Dave', |
||
| 102 | ], |
||
| 103 | ]; |
||
| 104 | |||
| 105 | $resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
||
| 106 | |||
| 107 | $scope = new Scope($this->manager, $resource); |
||
| 108 | |||
| 109 | $expected = [ |
||
| 110 | 'data' => [ |
||
| 111 | 'type' => 'books', |
||
| 112 | 'id' => '1', |
||
| 113 | 'attributes' => [ |
||
| 114 | 'title' => 'Foo', |
||
| 115 | 'year' => 1991, |
||
| 116 | ], |
||
| 117 | 'relationships' => [ |
||
| 118 | 'author' => [ |
||
| 119 | 'data' => [ |
||
| 120 | 'type' => 'people', |
||
| 121 | 'id' => '1', |
||
| 122 | ], |
||
| 123 | ], |
||
| 124 | ], |
||
| 125 | ], |
||
| 126 | 'included' => [ |
||
| 127 | [ |
||
| 128 | 'type' => 'people', |
||
| 129 | 'id' => '1', |
||
| 130 | 'attributes' => [ |
||
| 131 | 'name' => 'Dave', |
||
| 132 | ], |
||
| 133 | ], |
||
| 134 | ], |
||
| 135 | ]; |
||
| 136 | |||
| 137 | $this->assertSame($expected, $scope->toArray()); |
||
| 138 | |||
| 139 | $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":{"type":"people","id":"1"}}}},"included":[{"type":"people","id":"1","attributes":{"name":"Dave"}}]}'; |
||
| 140 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 141 | } |
||
| 142 | |||
| 143 | public function testSerializingItemResourceWithMetaOnRelationship() |
||
| 144 | { |
||
| 145 | $this->manager->parseIncludes('author-with-meta'); |
||
| 146 | |||
| 147 | $bookData = [ |
||
| 148 | 'id' => 1, |
||
| 149 | 'title' => 'Foo', |
||
| 150 | 'year' => '1991', |
||
| 151 | '_author' => [ |
||
| 152 | 'id' => 1, |
||
| 153 | 'name' => 'Dave', |
||
| 154 | ], |
||
| 155 | ]; |
||
| 156 | |||
| 157 | $resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
||
| 158 | |||
| 159 | $scope = new Scope($this->manager, $resource); |
||
| 160 | |||
| 161 | $expected = [ |
||
| 162 | 'data' => [ |
||
| 163 | 'type' => 'books', |
||
| 164 | 'id' => '1', |
||
| 165 | 'attributes' => [ |
||
| 166 | 'title' => 'Foo', |
||
| 167 | 'year' => 1991, |
||
| 168 | ], |
||
| 169 | 'relationships' => [ |
||
| 170 | 'author-with-meta' => [ |
||
| 171 | 'data' => [ |
||
| 172 | 'type' => 'people', |
||
| 173 | 'id' => '1', |
||
| 174 | ], |
||
| 175 | 'meta' => [ 'foo' => 'bar' ], |
||
| 176 | ], |
||
| 177 | ], |
||
| 178 | ], |
||
| 179 | 'included' => [ |
||
| 180 | [ |
||
| 181 | 'type' => 'people', |
||
| 182 | 'id' => '1', |
||
| 183 | 'attributes' => [ |
||
| 184 | 'name' => 'Dave', |
||
| 185 | ], |
||
| 186 | ], |
||
| 187 | ], |
||
| 188 | ]; |
||
| 189 | |||
| 190 | $this->assertSame($expected, $scope->toArray()); |
||
| 191 | |||
| 192 | $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author-with-meta":{"data":{"type":"people","id":"1"},"meta":{"foo":"bar"}}}},"included":[{"type":"people","id":"1","attributes":{"name":"Dave"}}]}'; |
||
| 193 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 194 | } |
||
| 195 | |||
| 196 | View Code Duplication | public function testSerializingItemResourceWithHasOneDasherizedInclude() |
|
|
|
|||
| 197 | { |
||
| 198 | $this->manager->parseIncludes('co-author'); |
||
| 199 | |||
| 200 | $bookData = [ |
||
| 201 | 'id' => 1, |
||
| 202 | 'title' => 'Foo', |
||
| 203 | 'year' => '1991', |
||
| 204 | '_author' => [ |
||
| 205 | 'id' => 1, |
||
| 206 | 'name' => 'Dave', |
||
| 207 | ], |
||
| 208 | '_co_author' => [ |
||
| 209 | 'id' => 2, |
||
| 210 | 'name' => 'Jim', |
||
| 211 | ], |
||
| 212 | ]; |
||
| 213 | |||
| 214 | $resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
||
| 215 | |||
| 216 | $scope = new Scope($this->manager, $resource); |
||
| 217 | |||
| 218 | $expected = [ |
||
| 219 | 'data' => [ |
||
| 220 | 'type' => 'books', |
||
| 221 | 'id' => '1', |
||
| 222 | 'attributes' => [ |
||
| 223 | 'title' => 'Foo', |
||
| 224 | 'year' => 1991, |
||
| 225 | ], |
||
| 226 | 'relationships' => [ |
||
| 227 | 'co-author' => [ |
||
| 228 | 'data' => [ |
||
| 229 | 'type' => 'people', |
||
| 230 | 'id' => '2', |
||
| 231 | ], |
||
| 232 | ], |
||
| 233 | ], |
||
| 234 | ], |
||
| 235 | 'included' => [ |
||
| 236 | [ |
||
| 237 | 'type' => 'people', |
||
| 238 | 'id' => '2', |
||
| 239 | 'attributes' => [ |
||
| 240 | 'name' => 'Jim', |
||
| 241 | ], |
||
| 242 | ], |
||
| 243 | ], |
||
| 244 | ]; |
||
| 245 | |||
| 246 | $this->assertSame($expected, $scope->toArray()); |
||
| 247 | |||
| 248 | $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"co-author":{"data":{"type":"people","id":"2"}}}},"included":[{"type":"people","id":"2","attributes":{"name":"Jim"}}]}'; |
||
| 249 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 250 | } |
||
| 251 | |||
| 252 | public function testSerializingItemResourceWithEmptyHasOneInclude() |
||
| 253 | { |
||
| 254 | $this->manager->parseIncludes('author'); |
||
| 255 | |||
| 256 | $bookData = [ |
||
| 257 | 'id' => 1, |
||
| 258 | 'title' => 'Foo', |
||
| 259 | 'year' => '1991', |
||
| 260 | '_author' => null, |
||
| 261 | ]; |
||
| 262 | |||
| 263 | $resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
||
| 264 | |||
| 265 | $scope = new Scope($this->manager, $resource); |
||
| 266 | |||
| 267 | $expected = [ |
||
| 268 | 'data' => [ |
||
| 269 | 'type' => 'books', |
||
| 270 | 'id' => '1', |
||
| 271 | 'attributes' => [ |
||
| 272 | 'title' => 'Foo', |
||
| 273 | 'year' => 1991, |
||
| 274 | ], |
||
| 275 | 'relationships' => [ |
||
| 276 | 'author' => [ |
||
| 277 | 'data' => null, |
||
| 278 | ], |
||
| 279 | ], |
||
| 280 | ], |
||
| 281 | ]; |
||
| 282 | |||
| 283 | $this->assertSame($expected, $scope->toArray()); |
||
| 284 | |||
| 285 | $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":null}}}}'; |
||
| 286 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 287 | } |
||
| 288 | |||
| 289 | public function testSerializingItemResourceWithHasManyInclude() |
||
| 290 | { |
||
| 291 | $this->manager->parseIncludes('published'); |
||
| 292 | |||
| 293 | $authorData = [ |
||
| 294 | 'id' => 1, |
||
| 295 | 'name' => 'Dave', |
||
| 296 | '_published' => [ |
||
| 297 | [ |
||
| 298 | 'id' => 1, |
||
| 299 | 'title' => 'Foo', |
||
| 300 | 'year' => '1991', |
||
| 301 | ], |
||
| 302 | [ |
||
| 303 | 'id' => 2, |
||
| 304 | 'title' => 'Bar', |
||
| 305 | 'year' => '2015', |
||
| 306 | ], |
||
| 307 | ], |
||
| 308 | ]; |
||
| 309 | |||
| 310 | $resource = new Item($authorData, new JsonApiAuthorTransformer(), 'people'); |
||
| 311 | |||
| 312 | $scope = new Scope($this->manager, $resource); |
||
| 313 | |||
| 314 | $expected = [ |
||
| 315 | 'data' => [ |
||
| 316 | 'type' => 'people', |
||
| 317 | 'id' => '1', |
||
| 318 | 'attributes' => [ |
||
| 319 | 'name' => 'Dave', |
||
| 320 | ], |
||
| 321 | 'relationships' => [ |
||
| 322 | 'published' => [ |
||
| 323 | 'data' => [ |
||
| 324 | [ |
||
| 325 | 'type' => 'books', |
||
| 326 | 'id' => 1, |
||
| 327 | ], |
||
| 328 | [ |
||
| 329 | 'type' => 'books', |
||
| 330 | 'id' => 2, |
||
| 331 | ], |
||
| 332 | ], |
||
| 333 | ], |
||
| 334 | ], |
||
| 335 | ], |
||
| 336 | 'included' => [ |
||
| 337 | [ |
||
| 338 | 'type' => 'books', |
||
| 339 | 'id' => '1', |
||
| 340 | 'attributes' => [ |
||
| 341 | 'title' => 'Foo', |
||
| 342 | 'year' => 1991, |
||
| 343 | ], |
||
| 344 | ], |
||
| 345 | [ |
||
| 346 | 'type' => 'books', |
||
| 347 | 'id' => '2', |
||
| 348 | 'attributes' => [ |
||
| 349 | 'title' => 'Bar', |
||
| 350 | 'year' => 2015, |
||
| 351 | ], |
||
| 352 | ], |
||
| 353 | ], |
||
| 354 | ]; |
||
| 355 | |||
| 356 | $this->assertEquals($expected, $scope->toArray()); |
||
| 357 | |||
| 358 | $expectedJson = '{"data":{"type":"people","id":"1","attributes":{"name":"Dave"},"relationships":{"published":{"data":[{"type":"books","id":"1"},{"type":"books","id":"2"}]}}},"included":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},{"type":"books","id":"2","attributes":{"title":"Bar","year":2015}}]}'; |
||
| 359 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 360 | } |
||
| 361 | |||
| 362 | public function testSerializingItemResourceWithEmptyHasManyInclude() |
||
| 363 | { |
||
| 364 | $this->manager->parseIncludes('published'); |
||
| 365 | |||
| 366 | $authorData = [ |
||
| 367 | 'id' => 1, |
||
| 368 | 'name' => 'Dave', |
||
| 369 | '_published' => [], |
||
| 370 | ]; |
||
| 371 | |||
| 372 | $resource = new Item($authorData, new JsonApiAuthorTransformer(), 'people'); |
||
| 373 | |||
| 374 | $scope = new Scope($this->manager, $resource); |
||
| 375 | |||
| 376 | $expected = [ |
||
| 377 | 'data' => [ |
||
| 378 | 'type' => 'people', |
||
| 379 | 'id' => '1', |
||
| 380 | 'attributes' => [ |
||
| 381 | 'name' => 'Dave', |
||
| 382 | ], |
||
| 383 | 'relationships' => [ |
||
| 384 | 'published' => [ |
||
| 385 | 'data' => [], |
||
| 386 | ], |
||
| 387 | ], |
||
| 388 | ], |
||
| 389 | ]; |
||
| 390 | |||
| 391 | $this->assertSame($expected, $scope->toArray()); |
||
| 392 | |||
| 393 | $expectedJson = '{"data":{"type":"people","id":"1","attributes":{"name":"Dave"},"relationships":{"published":{"data":[]}}}}'; |
||
| 394 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 395 | } |
||
| 396 | |||
| 397 | public function testSerializingItemResourceWithoutIncludes() |
||
| 398 | { |
||
| 399 | $bookData = [ |
||
| 400 | 'id' => 1, |
||
| 401 | 'title' => 'Foo', |
||
| 402 | 'year' => '1991', |
||
| 403 | '_author' => [ |
||
| 404 | 'id' => 1, |
||
| 405 | 'name' => 'Dave', |
||
| 406 | ], |
||
| 407 | ]; |
||
| 408 | |||
| 409 | $resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
||
| 410 | |||
| 411 | $scope = new Scope($this->manager, $resource); |
||
| 412 | |||
| 413 | $expected = [ |
||
| 414 | 'data' => [ |
||
| 415 | 'type' => 'books', |
||
| 416 | 'id' => '1', |
||
| 417 | 'attributes' => [ |
||
| 418 | 'title' => 'Foo', |
||
| 419 | 'year' => 1991, |
||
| 420 | ], |
||
| 421 | ], |
||
| 422 | ]; |
||
| 423 | |||
| 424 | $this->assertSame($expected, $scope->toArray()); |
||
| 425 | |||
| 426 | $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}}}'; |
||
| 427 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 428 | } |
||
| 429 | |||
| 430 | public function testSerializingItemResourceWithMeta() |
||
| 431 | { |
||
| 432 | $bookData = [ |
||
| 433 | 'id' => 1, |
||
| 434 | 'title' => 'Foo', |
||
| 435 | 'year' => '1991', |
||
| 436 | '_author' => [ |
||
| 437 | 'id' => 1, |
||
| 438 | 'name' => 'Dave', |
||
| 439 | ], |
||
| 440 | ]; |
||
| 441 | |||
| 442 | $resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
||
| 443 | $resource->setMetaValue('foo', 'bar'); |
||
| 444 | |||
| 445 | $scope = new Scope($this->manager, $resource); |
||
| 446 | |||
| 447 | $expected = [ |
||
| 448 | 'data' => [ |
||
| 449 | 'type' => 'books', |
||
| 450 | 'id' => '1', |
||
| 451 | 'attributes' => [ |
||
| 452 | 'title' => 'Foo', |
||
| 453 | 'year' => 1991, |
||
| 454 | ], |
||
| 455 | ], |
||
| 456 | 'meta' => [ |
||
| 457 | 'foo' => 'bar', |
||
| 458 | ], |
||
| 459 | ]; |
||
| 460 | |||
| 461 | $this->assertSame($expected, $scope->toArray()); |
||
| 462 | |||
| 463 | $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},"meta":{"foo":"bar"}}'; |
||
| 464 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 465 | } |
||
| 466 | |||
| 467 | public function testSerializingItemResourceWithMetaInBody() |
||
| 468 | { |
||
| 469 | $bookData = [ |
||
| 470 | 'id' => 1, |
||
| 471 | 'title' => 'Foo', |
||
| 472 | 'year' => '1991', |
||
| 473 | '_author' => [ |
||
| 474 | 'id' => 1, |
||
| 475 | 'name' => 'Dave', |
||
| 476 | ], |
||
| 477 | 'meta' => [ |
||
| 478 | 'something' => 'something' |
||
| 479 | ] |
||
| 480 | ]; |
||
| 481 | |||
| 482 | $resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
||
| 483 | $resource->setMetaValue('foo', 'bar'); |
||
| 484 | |||
| 485 | $scope = new Scope($this->manager, $resource); |
||
| 486 | |||
| 487 | $expected = [ |
||
| 488 | 'data' => [ |
||
| 489 | 'type' => 'books', |
||
| 490 | 'id' => '1', |
||
| 491 | 'attributes' => [ |
||
| 492 | 'title' => 'Foo', |
||
| 493 | 'year' => 1991, |
||
| 494 | ], |
||
| 495 | 'meta' => [ |
||
| 496 | 'something' => 'something' |
||
| 497 | ] |
||
| 498 | ], |
||
| 499 | 'meta' => [ |
||
| 500 | 'foo' => 'bar' |
||
| 501 | ], |
||
| 502 | ]; |
||
| 503 | |||
| 504 | $this->assertSame($expected, $scope->toArray()); |
||
| 505 | |||
| 506 | $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"meta":{"something":"something"}},"meta":{"foo":"bar"}}'; |
||
| 507 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 508 | } |
||
| 509 | |||
| 510 | View Code Duplication | public function testSerializingCollectionResourceWithoutIncludes() |
|
| 511 | { |
||
| 512 | $booksData = [ |
||
| 513 | [ |
||
| 514 | 'id' => 1, |
||
| 515 | 'title' => 'Foo', |
||
| 516 | 'year' => '1991', |
||
| 517 | '_author' => [ |
||
| 518 | 'id' => 1, |
||
| 519 | 'name' => 'Dave', |
||
| 520 | ], |
||
| 521 | ], |
||
| 522 | [ |
||
| 523 | 'id' => 2, |
||
| 524 | 'title' => 'Bar', |
||
| 525 | 'year' => '1997', |
||
| 526 | '_author' => [ |
||
| 527 | 'id' => 2, |
||
| 528 | 'name' => 'Bob', |
||
| 529 | ], |
||
| 530 | ], |
||
| 531 | ]; |
||
| 532 | |||
| 533 | $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
||
| 534 | $scope = new Scope($this->manager, $resource); |
||
| 535 | |||
| 536 | $expected = [ |
||
| 537 | 'data' => [ |
||
| 538 | [ |
||
| 539 | 'type' => 'books', |
||
| 540 | 'id' => '1', |
||
| 541 | 'attributes' => [ |
||
| 542 | 'title' => 'Foo', |
||
| 543 | 'year' => 1991, |
||
| 544 | ], |
||
| 545 | ], |
||
| 546 | [ |
||
| 547 | 'type' => 'books', |
||
| 548 | 'id' => '2', |
||
| 549 | 'attributes' => [ |
||
| 550 | 'title' => 'Bar', |
||
| 551 | 'year' => 1997, |
||
| 552 | ], |
||
| 553 | ], |
||
| 554 | ], |
||
| 555 | ]; |
||
| 556 | |||
| 557 | $this->assertSame($expected, $scope->toArray()); |
||
| 558 | |||
| 559 | $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997}}]}'; |
||
| 560 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 561 | } |
||
| 562 | |||
| 563 | View Code Duplication | public function testSerializingCollectionResourceWithHasOneInclude() |
|
| 564 | { |
||
| 565 | $this->manager->parseIncludes('author'); |
||
| 566 | |||
| 567 | $booksData = [ |
||
| 568 | [ |
||
| 569 | 'id' => 1, |
||
| 570 | 'title' => 'Foo', |
||
| 571 | 'year' => '1991', |
||
| 572 | '_author' => [ |
||
| 573 | 'id' => 1, |
||
| 574 | 'name' => 'Dave', |
||
| 575 | ], |
||
| 576 | ], |
||
| 577 | [ |
||
| 578 | 'id' => 2, |
||
| 579 | 'title' => 'Bar', |
||
| 580 | 'year' => '1997', |
||
| 581 | '_author' => [ |
||
| 582 | 'id' => 2, |
||
| 583 | 'name' => 'Bob', |
||
| 584 | ], |
||
| 585 | ], |
||
| 586 | ]; |
||
| 587 | |||
| 588 | $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
||
| 589 | $scope = new Scope($this->manager, $resource); |
||
| 590 | |||
| 591 | $expected = [ |
||
| 592 | 'data' => [ |
||
| 593 | [ |
||
| 594 | 'type' => 'books', |
||
| 595 | 'id' => '1', |
||
| 596 | 'attributes' => [ |
||
| 597 | 'title' => 'Foo', |
||
| 598 | 'year' => 1991, |
||
| 599 | ], |
||
| 600 | 'relationships' => [ |
||
| 601 | 'author' => [ |
||
| 602 | 'data' => [ |
||
| 603 | 'type' => 'people', |
||
| 604 | 'id' => '1', |
||
| 605 | ], |
||
| 606 | ], |
||
| 607 | ], |
||
| 608 | ], |
||
| 609 | [ |
||
| 610 | 'type' => 'books', |
||
| 611 | 'id' => '2', |
||
| 612 | 'attributes' => [ |
||
| 613 | 'title' => 'Bar', |
||
| 614 | 'year' => 1997, |
||
| 615 | ], |
||
| 616 | 'relationships' => [ |
||
| 617 | 'author' => [ |
||
| 618 | 'data' => [ |
||
| 619 | 'type' => 'people', |
||
| 620 | 'id' => '2', |
||
| 621 | ], |
||
| 622 | ], |
||
| 623 | ], |
||
| 624 | ], |
||
| 625 | ], |
||
| 626 | 'included' => [ |
||
| 627 | [ |
||
| 628 | 'type' => 'people', |
||
| 629 | 'id' => '1', |
||
| 630 | 'attributes' => [ |
||
| 631 | 'name' => 'Dave', |
||
| 632 | ], |
||
| 633 | ], |
||
| 634 | [ |
||
| 635 | 'type' => 'people', |
||
| 636 | 'id' => '2', |
||
| 637 | 'attributes' => [ |
||
| 638 | 'name' => 'Bob', |
||
| 639 | ], |
||
| 640 | ], |
||
| 641 | ], |
||
| 642 | ]; |
||
| 643 | |||
| 644 | $this->assertSame($expected, $scope->toArray()); |
||
| 645 | |||
| 646 | $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":{"type":"people","id":"1"}}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"relationships":{"author":{"data":{"type":"people","id":"2"}}}}],"included":[{"type":"people","id":"1","attributes":{"name":"Dave"}},{"type":"people","id":"2","attributes":{"name":"Bob"}}]}'; |
||
| 647 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 648 | } |
||
| 649 | |||
| 650 | public function testSerializingCollectionResourceWithEmptyHasOneInclude() |
||
| 651 | { |
||
| 652 | $this->manager->parseIncludes('author'); |
||
| 653 | |||
| 654 | $booksData = [ |
||
| 655 | [ |
||
| 656 | 'id' => 1, |
||
| 657 | 'title' => 'Foo', |
||
| 658 | 'year' => '1991', |
||
| 659 | '_author' => null, |
||
| 660 | ], |
||
| 661 | [ |
||
| 662 | 'id' => 2, |
||
| 663 | 'title' => 'Bar', |
||
| 664 | 'year' => '1997', |
||
| 665 | '_author' => [ |
||
| 666 | 'id' => 2, |
||
| 667 | 'name' => 'Bob', |
||
| 668 | ], |
||
| 669 | ], |
||
| 670 | ]; |
||
| 671 | |||
| 672 | $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
||
| 673 | $scope = new Scope($this->manager, $resource); |
||
| 674 | |||
| 675 | $expected = [ |
||
| 676 | 'data' => [ |
||
| 677 | [ |
||
| 678 | 'type' => 'books', |
||
| 679 | 'id' => '1', |
||
| 680 | 'attributes' => [ |
||
| 681 | 'title' => 'Foo', |
||
| 682 | 'year' => 1991, |
||
| 683 | ], |
||
| 684 | 'relationships' => [ |
||
| 685 | 'author' => [ |
||
| 686 | 'data' => null, |
||
| 687 | ], |
||
| 688 | ], |
||
| 689 | ], |
||
| 690 | [ |
||
| 691 | 'type' => 'books', |
||
| 692 | 'id' => '2', |
||
| 693 | 'attributes' => [ |
||
| 694 | 'title' => 'Bar', |
||
| 695 | 'year' => 1997, |
||
| 696 | ], |
||
| 697 | 'relationships' => [ |
||
| 698 | 'author' => [ |
||
| 699 | 'data' => [ |
||
| 700 | 'type' => 'people', |
||
| 701 | 'id' => '2', |
||
| 702 | ], |
||
| 703 | ], |
||
| 704 | ], |
||
| 705 | ], |
||
| 706 | ], |
||
| 707 | 'included' => [ |
||
| 708 | [ |
||
| 709 | 'type' => 'people', |
||
| 710 | 'id' => '2', |
||
| 711 | 'attributes' => [ |
||
| 712 | 'name' => 'Bob', |
||
| 713 | ], |
||
| 714 | ], |
||
| 715 | ], |
||
| 716 | ]; |
||
| 717 | |||
| 718 | $this->assertSame($expected, $scope->toArray()); |
||
| 719 | |||
| 720 | $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":null}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"relationships":{"author":{"data":{"type":"people","id":"2"}}}}],"included":[{"type":"people","id":"2","attributes":{"name":"Bob"}}]}'; |
||
| 721 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 722 | } |
||
| 723 | |||
| 724 | public function testSerializingCollectionResourceWithHasManyInclude() |
||
| 725 | { |
||
| 726 | $this->manager->parseIncludes('published'); |
||
| 727 | |||
| 728 | $authorsData = [ |
||
| 729 | [ |
||
| 730 | 'id' => 1, |
||
| 731 | 'name' => 'Dave', |
||
| 732 | '_published' => [ |
||
| 733 | [ |
||
| 734 | 'id' => 1, |
||
| 735 | 'title' => 'Foo', |
||
| 736 | 'year' => '1991', |
||
| 737 | ], |
||
| 738 | [ |
||
| 739 | 'id' => 2, |
||
| 740 | 'title' => 'Bar', |
||
| 741 | 'year' => '2015', |
||
| 742 | ], |
||
| 743 | ], |
||
| 744 | ], |
||
| 745 | [ |
||
| 746 | 'id' => 2, |
||
| 747 | 'name' => 'Bob', |
||
| 748 | '_published' => [ |
||
| 749 | [ |
||
| 750 | 'id' => 3, |
||
| 751 | 'title' => 'Baz', |
||
| 752 | 'year' => '1995', |
||
| 753 | ], |
||
| 754 | [ |
||
| 755 | 'id' => 4, |
||
| 756 | 'title' => 'Quux', |
||
| 757 | 'year' => '2000', |
||
| 758 | ], |
||
| 759 | ], |
||
| 760 | ], |
||
| 761 | ]; |
||
| 762 | |||
| 763 | $resource = new Collection($authorsData, new JsonApiAuthorTransformer(), 'people'); |
||
| 764 | $scope = new Scope($this->manager, $resource); |
||
| 765 | |||
| 766 | $expected = [ |
||
| 767 | 'data' => [ |
||
| 768 | [ |
||
| 769 | 'type' => 'people', |
||
| 770 | 'id' => '1', |
||
| 771 | 'attributes' => [ |
||
| 772 | 'name' => 'Dave', |
||
| 773 | ], |
||
| 774 | 'relationships' => [ |
||
| 775 | 'published' => [ |
||
| 776 | 'data' => [ |
||
| 777 | [ |
||
| 778 | 'type' => 'books', |
||
| 779 | 'id' => 1, |
||
| 780 | ], |
||
| 781 | [ |
||
| 782 | 'type' => 'books', |
||
| 783 | 'id' => 2, |
||
| 784 | ], |
||
| 785 | ], |
||
| 786 | ], |
||
| 787 | ], |
||
| 788 | ], |
||
| 789 | [ |
||
| 790 | 'type' => 'people', |
||
| 791 | 'id' => '2', |
||
| 792 | 'attributes' => [ |
||
| 793 | 'name' => 'Bob', |
||
| 794 | ], |
||
| 795 | 'relationships' => [ |
||
| 796 | 'published' => [ |
||
| 797 | 'data' => [ |
||
| 798 | [ |
||
| 799 | 'type' => 'books', |
||
| 800 | 'id' => 3, |
||
| 801 | ], |
||
| 802 | [ |
||
| 803 | 'type' => 'books', |
||
| 804 | 'id' => 4, |
||
| 805 | ], |
||
| 806 | ], |
||
| 807 | ], |
||
| 808 | ], |
||
| 809 | ], |
||
| 810 | ], |
||
| 811 | 'included' => [ |
||
| 812 | [ |
||
| 813 | 'type' => 'books', |
||
| 814 | 'id' => '1', |
||
| 815 | 'attributes' => [ |
||
| 816 | 'title' => 'Foo', |
||
| 817 | 'year' => 1991, |
||
| 818 | ], |
||
| 819 | ], |
||
| 820 | [ |
||
| 821 | 'type' => 'books', |
||
| 822 | 'id' => '2', |
||
| 823 | 'attributes' => [ |
||
| 824 | 'title' => 'Bar', |
||
| 825 | 'year' => 2015, |
||
| 826 | ], |
||
| 827 | ], |
||
| 828 | [ |
||
| 829 | 'type' => 'books', |
||
| 830 | 'id' => '3', |
||
| 831 | 'attributes' => [ |
||
| 832 | 'title' => 'Baz', |
||
| 833 | 'year' => 1995, |
||
| 834 | ], |
||
| 835 | ], |
||
| 836 | [ |
||
| 837 | 'type' => 'books', |
||
| 838 | 'id' => '4', |
||
| 839 | 'attributes' => [ |
||
| 840 | 'title' => 'Quux', |
||
| 841 | 'year' => 2000, |
||
| 842 | ], |
||
| 843 | ], |
||
| 844 | ], |
||
| 845 | ]; |
||
| 846 | |||
| 847 | $this->assertEquals($expected, $scope->toArray()); |
||
| 848 | |||
| 849 | $expectedJson = '{"data":[{"type":"people","id":"1","attributes":{"name":"Dave"},"relationships":{"published":{"data":[{"type":"books","id":"1"},{"type":"books","id":"2"}]}}},{"type":"people","id":"2","attributes":{"name":"Bob"},"relationships":{"published":{"data":[{"type":"books","id":"3"},{"type":"books","id":"4"}]}}}],"included":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},{"type":"books","id":"2","attributes":{"title":"Bar","year":2015}},{"type":"books","id":"3","attributes":{"title":"Baz","year":1995}},{"type":"books","id":"4","attributes":{"title":"Quux","year":2000}}]}'; |
||
| 850 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 851 | } |
||
| 852 | |||
| 853 | public function testSerializingCollectionResourceWithEmptyHasManyInclude() |
||
| 854 | { |
||
| 855 | $this->manager->parseIncludes('published'); |
||
| 856 | |||
| 857 | $authorsData = [ |
||
| 858 | [ |
||
| 859 | 'id' => 1, |
||
| 860 | 'name' => 'Dave', |
||
| 861 | '_published' => [], |
||
| 862 | ], |
||
| 863 | [ |
||
| 864 | 'id' => 2, |
||
| 865 | 'name' => 'Bob', |
||
| 866 | '_published' => [ |
||
| 867 | [ |
||
| 868 | 'id' => 3, |
||
| 869 | 'title' => 'Baz', |
||
| 870 | 'year' => '1995', |
||
| 871 | ], |
||
| 872 | ], |
||
| 873 | ], |
||
| 874 | ]; |
||
| 875 | |||
| 876 | $resource = new Collection($authorsData, new JsonApiAuthorTransformer(), 'people'); |
||
| 877 | $scope = new Scope($this->manager, $resource); |
||
| 878 | |||
| 879 | $expected = [ |
||
| 880 | 'data' => [ |
||
| 881 | [ |
||
| 882 | 'type' => 'people', |
||
| 883 | 'id' => '1', |
||
| 884 | 'attributes' => [ |
||
| 885 | 'name' => 'Dave', |
||
| 886 | ], |
||
| 887 | 'relationships' => [ |
||
| 888 | 'published' => [ |
||
| 889 | 'data' => [], |
||
| 890 | ], |
||
| 891 | ], |
||
| 892 | ], |
||
| 893 | [ |
||
| 894 | 'type' => 'people', |
||
| 895 | 'id' => '2', |
||
| 896 | 'attributes' => [ |
||
| 897 | 'name' => 'Bob', |
||
| 898 | ], |
||
| 899 | 'relationships' => [ |
||
| 900 | 'published' => [ |
||
| 901 | 'data' => [ |
||
| 902 | [ |
||
| 903 | 'type' => 'books', |
||
| 904 | 'id' => 3, |
||
| 905 | ], |
||
| 906 | ], |
||
| 907 | ], |
||
| 908 | ], |
||
| 909 | ], |
||
| 910 | ], |
||
| 911 | 'included' => [ |
||
| 912 | [ |
||
| 913 | 'type' => 'books', |
||
| 914 | 'id' => '3', |
||
| 915 | 'attributes' => [ |
||
| 916 | 'title' => 'Baz', |
||
| 917 | 'year' => 1995, |
||
| 918 | ], |
||
| 919 | ], |
||
| 920 | ], |
||
| 921 | ]; |
||
| 922 | |||
| 923 | $this->assertEquals($expected, $scope->toArray()); |
||
| 924 | |||
| 925 | $expectedJson = '{"data":[{"type":"people","id":"1","attributes":{"name":"Dave"},"relationships":{"published":{"data":[]}}},{"type":"people","id":"2","attributes":{"name":"Bob"},"relationships":{"published":{"data":[{"type":"books","id":"3"}]}}}],"included":[{"type":"books","id":"3","attributes":{"title":"Baz","year":1995}}]}'; |
||
| 926 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 927 | } |
||
| 928 | |||
| 929 | View Code Duplication | public function testSerializingCollectionResourceWithMeta() |
|
| 930 | { |
||
| 931 | $booksData = [ |
||
| 932 | [ |
||
| 933 | 'id' => 1, |
||
| 934 | 'title' => 'Foo', |
||
| 935 | 'year' => '1991', |
||
| 936 | '_author' => [ |
||
| 937 | 'name' => 'Dave', |
||
| 938 | ], |
||
| 939 | ], |
||
| 940 | [ |
||
| 941 | 'id' => 2, |
||
| 942 | 'title' => 'Bar', |
||
| 943 | 'year' => '1997', |
||
| 944 | '_author' => [ |
||
| 945 | 'name' => 'Bob', |
||
| 946 | ], |
||
| 947 | ], |
||
| 948 | ]; |
||
| 949 | |||
| 950 | $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
||
| 951 | $resource->setMetaValue('foo', 'bar'); |
||
| 952 | |||
| 953 | $scope = new Scope($this->manager, $resource); |
||
| 954 | |||
| 955 | $expected = [ |
||
| 956 | 'data' => [ |
||
| 957 | [ |
||
| 958 | 'type' => 'books', |
||
| 959 | 'id' => '1', |
||
| 960 | 'attributes' => [ |
||
| 961 | 'title' => 'Foo', |
||
| 962 | 'year' => 1991, |
||
| 963 | ], |
||
| 964 | ], |
||
| 965 | [ |
||
| 966 | 'type' => 'books', |
||
| 967 | 'id' => '2', |
||
| 968 | 'attributes' => [ |
||
| 969 | 'title' => 'Bar', |
||
| 970 | 'year' => 1997, |
||
| 971 | ], |
||
| 972 | ], |
||
| 973 | ], |
||
| 974 | 'meta' => [ |
||
| 975 | 'foo' => 'bar', |
||
| 976 | ], |
||
| 977 | ]; |
||
| 978 | |||
| 979 | $this->assertSame($expected, $scope->toArray()); |
||
| 980 | |||
| 981 | $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997}}],"meta":{"foo":"bar"}}'; |
||
| 982 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 983 | } |
||
| 984 | |||
| 985 | public function testSerializingCollectionResourceWithDuplicatedIncludeData() |
||
| 986 | { |
||
| 987 | $this->manager->parseIncludes('author'); |
||
| 988 | |||
| 989 | $booksData = [ |
||
| 990 | [ |
||
| 991 | 'id' => 1, |
||
| 992 | 'title' => 'Foo', |
||
| 993 | 'year' => '1991', |
||
| 994 | '_author' => [ |
||
| 995 | 'id' => 1, |
||
| 996 | 'name' => 'Dave', |
||
| 997 | ], |
||
| 998 | ], |
||
| 999 | [ |
||
| 1000 | 'id' => 2, |
||
| 1001 | 'title' => 'Bar', |
||
| 1002 | 'year' => '1997', |
||
| 1003 | '_author' => [ |
||
| 1004 | 'id' => 1, |
||
| 1005 | 'name' => 'Dave', |
||
| 1006 | ], |
||
| 1007 | ], |
||
| 1008 | ]; |
||
| 1009 | |||
| 1010 | $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
||
| 1011 | $scope = new Scope($this->manager, $resource); |
||
| 1012 | |||
| 1013 | $expected = [ |
||
| 1014 | 'data' => [ |
||
| 1015 | [ |
||
| 1016 | 'type' => 'books', |
||
| 1017 | 'id' => '1', |
||
| 1018 | 'attributes' => [ |
||
| 1019 | 'title' => 'Foo', |
||
| 1020 | 'year' => 1991, |
||
| 1021 | ], |
||
| 1022 | 'relationships' => [ |
||
| 1023 | 'author' => [ |
||
| 1024 | 'data' => [ |
||
| 1025 | 'type' => 'people', |
||
| 1026 | 'id' => '1', |
||
| 1027 | ], |
||
| 1028 | ], |
||
| 1029 | ], |
||
| 1030 | ], |
||
| 1031 | [ |
||
| 1032 | 'type' => 'books', |
||
| 1033 | 'id' => '2', |
||
| 1034 | 'attributes' => [ |
||
| 1035 | 'title' => 'Bar', |
||
| 1036 | 'year' => 1997, |
||
| 1037 | ], |
||
| 1038 | 'relationships' => [ |
||
| 1039 | 'author' => [ |
||
| 1040 | 'data' => [ |
||
| 1041 | 'type' => 'people', |
||
| 1042 | 'id' => '1', |
||
| 1043 | ], |
||
| 1044 | ], |
||
| 1045 | ], |
||
| 1046 | ], |
||
| 1047 | ], |
||
| 1048 | 'included' => [ |
||
| 1049 | [ |
||
| 1050 | 'type' => 'people', |
||
| 1051 | 'id' => '1', |
||
| 1052 | 'attributes' => [ |
||
| 1053 | 'name' => 'Dave', |
||
| 1054 | ], |
||
| 1055 | ], |
||
| 1056 | ], |
||
| 1057 | ]; |
||
| 1058 | |||
| 1059 | $this->assertSame($expected, $scope->toArray()); |
||
| 1060 | |||
| 1061 | $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":{"type":"people","id":"1"}}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"relationships":{"author":{"data":{"type":"people","id":"1"}}}}],"included":[{"type":"people","id":"1","attributes":{"name":"Dave"}}]}'; |
||
| 1062 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 1063 | } |
||
| 1064 | |||
| 1065 | View Code Duplication | public function testSerializingItemResourceWithNestedIncludes() |
|
| 1066 | { |
||
| 1067 | $this->manager->parseIncludes(['author', 'author.published']); |
||
| 1068 | |||
| 1069 | $bookData = [ |
||
| 1070 | 'id' => 1, |
||
| 1071 | 'title' => 'Foo', |
||
| 1072 | 'year' => '1991', |
||
| 1073 | '_author' => [ |
||
| 1074 | 'id' => 1, |
||
| 1075 | 'name' => 'Dave', |
||
| 1076 | '_published' => [ |
||
| 1077 | [ |
||
| 1078 | 'id' => 1, |
||
| 1079 | 'title' => 'Foo', |
||
| 1080 | 'year' => '1991', |
||
| 1081 | ], |
||
| 1082 | [ |
||
| 1083 | 'id' => 2, |
||
| 1084 | 'title' => 'Bar', |
||
| 1085 | 'year' => '2015', |
||
| 1086 | ], |
||
| 1087 | ], |
||
| 1088 | ], |
||
| 1089 | ]; |
||
| 1090 | |||
| 1091 | $resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
||
| 1092 | |||
| 1093 | $scope = new Scope($this->manager, $resource); |
||
| 1094 | |||
| 1095 | $expected = [ |
||
| 1096 | 'data' => [ |
||
| 1097 | 'type' => 'books', |
||
| 1098 | 'id' => '1', |
||
| 1099 | 'attributes' => [ |
||
| 1100 | 'title' => 'Foo', |
||
| 1101 | 'year' => 1991, |
||
| 1102 | ], |
||
| 1103 | 'relationships' => [ |
||
| 1104 | 'author' => [ |
||
| 1105 | 'data' => [ |
||
| 1106 | 'type' => 'people', |
||
| 1107 | 'id' => '1', |
||
| 1108 | ], |
||
| 1109 | ], |
||
| 1110 | ], |
||
| 1111 | ], |
||
| 1112 | 'included' => [ |
||
| 1113 | [ |
||
| 1114 | 'type' => 'books', |
||
| 1115 | 'id' => '2', |
||
| 1116 | 'attributes' => [ |
||
| 1117 | 'title' => 'Bar', |
||
| 1118 | 'year' => 2015, |
||
| 1119 | ], |
||
| 1120 | ], |
||
| 1121 | [ |
||
| 1122 | 'type' => 'people', |
||
| 1123 | 'id' => '1', |
||
| 1124 | 'attributes' => [ |
||
| 1125 | 'name' => 'Dave', |
||
| 1126 | ], |
||
| 1127 | 'relationships' => [ |
||
| 1128 | 'published' => [ |
||
| 1129 | 'data' => [ |
||
| 1130 | [ |
||
| 1131 | 'type' => 'books', |
||
| 1132 | 'id' => '1', |
||
| 1133 | ], |
||
| 1134 | [ |
||
| 1135 | 'type' => 'books', |
||
| 1136 | 'id' => '2', |
||
| 1137 | ], |
||
| 1138 | ], |
||
| 1139 | ], |
||
| 1140 | ], |
||
| 1141 | ], |
||
| 1142 | ], |
||
| 1143 | ]; |
||
| 1144 | |||
| 1145 | $this->assertSame($expected, $scope->toArray()); |
||
| 1146 | |||
| 1147 | $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":{"type":"people","id":"1"}}}},"included":[{"type":"books","id":"2","attributes":{"title":"Bar","year":2015}},{"type":"people","id":"1","attributes":{"name":"Dave"},"relationships":{"published":{"data":[{"type":"books","id":"1"},{"type":"books","id":"2"}]}}}]}'; |
||
| 1148 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 1149 | } |
||
| 1150 | |||
| 1151 | public function testSerializingItemResourceWithSelfLink() |
||
| 1152 | { |
||
| 1153 | $baseUrl = 'http://example.com'; |
||
| 1154 | $this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
||
| 1155 | |||
| 1156 | $bookData = [ |
||
| 1157 | 'id' => 1, |
||
| 1158 | 'title' => 'Foo', |
||
| 1159 | 'year' => '1991', |
||
| 1160 | '_author' => [ |
||
| 1161 | 'id' => 1, |
||
| 1162 | 'name' => 'Dave', |
||
| 1163 | ], |
||
| 1164 | ]; |
||
| 1165 | |||
| 1166 | $resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
||
| 1167 | |||
| 1168 | $scope = new Scope($this->manager, $resource); |
||
| 1169 | |||
| 1170 | $expected = [ |
||
| 1171 | 'data' => [ |
||
| 1172 | 'type' => 'books', |
||
| 1173 | 'id' => '1', |
||
| 1174 | 'attributes' => [ |
||
| 1175 | 'title' => 'Foo', |
||
| 1176 | 'year' => 1991, |
||
| 1177 | ], |
||
| 1178 | 'links' => [ |
||
| 1179 | 'self' => 'http://example.com/books/1', |
||
| 1180 | ], |
||
| 1181 | 'relationships' => [ |
||
| 1182 | 'author' => [ |
||
| 1183 | 'links' => [ |
||
| 1184 | 'self' => 'http://example.com/books/1/relationships/author', |
||
| 1185 | 'related' => 'http://example.com/books/1/author', |
||
| 1186 | ], |
||
| 1187 | ], |
||
| 1188 | 'co-author' => [ |
||
| 1189 | 'links' => [ |
||
| 1190 | 'self' => 'http://example.com/books/1/relationships/co-author', |
||
| 1191 | 'related' => 'http://example.com/books/1/co-author', |
||
| 1192 | ], |
||
| 1193 | ], |
||
| 1194 | 'author-with-meta' => [ |
||
| 1195 | 'links' => [ |
||
| 1196 | 'self' => 'http://example.com/books/1/relationships/author-with-meta', |
||
| 1197 | 'related' => 'http://example.com/books/1/author-with-meta', |
||
| 1198 | ], |
||
| 1199 | ], |
||
| 1200 | ], |
||
| 1201 | ], |
||
| 1202 | ]; |
||
| 1203 | |||
| 1204 | $this->assertSame($expected, $scope->toArray()); |
||
| 1205 | |||
| 1206 | $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author","related":"http:\/\/example.com\/books\/1\/author"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/co-author","related":"http:\/\/example.com\/books\/1\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/1\/author-with-meta"}}}}}'; |
||
| 1207 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 1208 | } |
||
| 1209 | |||
| 1210 | public function testSerializingCollectionResourceWithSelfLink() |
||
| 1211 | { |
||
| 1212 | $baseUrl = 'http://example.com'; |
||
| 1213 | $this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
||
| 1214 | |||
| 1215 | $booksData = [ |
||
| 1216 | [ |
||
| 1217 | 'id' => 1, |
||
| 1218 | 'title' => 'Foo', |
||
| 1219 | 'year' => '1991', |
||
| 1220 | '_author' => [ |
||
| 1221 | 'id' => 1, |
||
| 1222 | 'name' => 'Dave', |
||
| 1223 | ], |
||
| 1224 | ], |
||
| 1225 | [ |
||
| 1226 | 'id' => 2, |
||
| 1227 | 'title' => 'Bar', |
||
| 1228 | 'year' => '1997', |
||
| 1229 | '_author' => [ |
||
| 1230 | 'id' => 2, |
||
| 1231 | 'name' => 'Bob', |
||
| 1232 | ], |
||
| 1233 | ], |
||
| 1234 | ]; |
||
| 1235 | |||
| 1236 | $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
||
| 1237 | $scope = new Scope($this->manager, $resource); |
||
| 1238 | |||
| 1239 | $expected = [ |
||
| 1240 | 'data' => [ |
||
| 1241 | [ |
||
| 1242 | 'type' => 'books', |
||
| 1243 | 'id' => '1', |
||
| 1244 | 'attributes' => [ |
||
| 1245 | 'title' => 'Foo', |
||
| 1246 | 'year' => 1991, |
||
| 1247 | ], |
||
| 1248 | 'links' => [ |
||
| 1249 | 'self' => 'http://example.com/books/1', |
||
| 1250 | ], |
||
| 1251 | 'relationships' => [ |
||
| 1252 | 'author' => [ |
||
| 1253 | 'links' => [ |
||
| 1254 | 'self' => 'http://example.com/books/1/relationships/author', |
||
| 1255 | 'related' => 'http://example.com/books/1/author', |
||
| 1256 | ], |
||
| 1257 | ], |
||
| 1258 | 'co-author' => [ |
||
| 1259 | 'links' => [ |
||
| 1260 | 'self' => 'http://example.com/books/1/relationships/co-author', |
||
| 1261 | 'related' => 'http://example.com/books/1/co-author', |
||
| 1262 | ], |
||
| 1263 | ], |
||
| 1264 | 'author-with-meta' => [ |
||
| 1265 | 'links' => [ |
||
| 1266 | 'self' => 'http://example.com/books/1/relationships/author-with-meta', |
||
| 1267 | 'related' => 'http://example.com/books/1/author-with-meta', |
||
| 1268 | ], |
||
| 1269 | ], |
||
| 1270 | ], |
||
| 1271 | ], |
||
| 1272 | [ |
||
| 1273 | 'type' => 'books', |
||
| 1274 | 'id' => '2', |
||
| 1275 | 'attributes' => [ |
||
| 1276 | 'title' => 'Bar', |
||
| 1277 | 'year' => 1997, |
||
| 1278 | ], |
||
| 1279 | 'links' => [ |
||
| 1280 | 'self' => 'http://example.com/books/2', |
||
| 1281 | ], |
||
| 1282 | 'relationships' => [ |
||
| 1283 | 'author' => [ |
||
| 1284 | 'links' => [ |
||
| 1285 | 'self' => 'http://example.com/books/2/relationships/author', |
||
| 1286 | 'related' => 'http://example.com/books/2/author' |
||
| 1287 | ], |
||
| 1288 | ], |
||
| 1289 | 'co-author' => [ |
||
| 1290 | 'links' => [ |
||
| 1291 | 'self' => 'http://example.com/books/2/relationships/co-author', |
||
| 1292 | 'related' => 'http://example.com/books/2/co-author' |
||
| 1293 | ], |
||
| 1294 | ], |
||
| 1295 | 'author-with-meta' => [ |
||
| 1296 | 'links' => [ |
||
| 1297 | 'self' => 'http://example.com/books/2/relationships/author-with-meta', |
||
| 1298 | 'related' => 'http://example.com/books/2/author-with-meta', |
||
| 1299 | ], |
||
| 1300 | ], |
||
| 1301 | ], |
||
| 1302 | ], |
||
| 1303 | ], |
||
| 1304 | ]; |
||
| 1305 | |||
| 1306 | $this->assertSame($expected, $scope->toArray()); |
||
| 1307 | |||
| 1308 | $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author","related":"http:\/\/example.com\/books\/1\/author"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/co-author","related":"http:\/\/example.com\/books\/1\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/1\/author-with-meta"}}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"links":{"self":"http:\/\/example.com\/books\/2"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author","related":"http:\/\/example.com\/books\/2\/author"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/co-author","related":"http:\/\/example.com\/books\/2\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/2\/author-with-meta"}}}}]}'; |
||
| 1309 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 1310 | } |
||
| 1311 | |||
| 1312 | public function testSerializingItemResourceWithLinksForHasOneRelationship() |
||
| 1313 | { |
||
| 1314 | $baseUrl = 'http://example.com'; |
||
| 1315 | $this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
||
| 1316 | $this->manager->parseIncludes('author'); |
||
| 1317 | |||
| 1318 | $bookData = [ |
||
| 1319 | 'id' => 1, |
||
| 1320 | 'title' => 'Foo', |
||
| 1321 | 'year' => '1991', |
||
| 1322 | '_author' => [ |
||
| 1323 | 'id' => 1, |
||
| 1324 | 'name' => 'Dave', |
||
| 1325 | ], |
||
| 1326 | ]; |
||
| 1327 | |||
| 1328 | $resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
||
| 1329 | |||
| 1330 | $scope = new Scope($this->manager, $resource); |
||
| 1331 | |||
| 1332 | $expected = [ |
||
| 1333 | 'data' => [ |
||
| 1334 | 'type' => 'books', |
||
| 1335 | 'id' => '1', |
||
| 1336 | 'attributes' => [ |
||
| 1337 | 'title' => 'Foo', |
||
| 1338 | 'year' => 1991, |
||
| 1339 | ], |
||
| 1340 | 'relationships' => [ |
||
| 1341 | 'author' => [ |
||
| 1342 | 'links' => [ |
||
| 1343 | 'self' => 'http://example.com/books/1/relationships/author', |
||
| 1344 | 'related' => 'http://example.com/books/1/author', |
||
| 1345 | ], |
||
| 1346 | 'data' => [ |
||
| 1347 | 'type' => 'people', |
||
| 1348 | 'id' => '1', |
||
| 1349 | ], |
||
| 1350 | ], |
||
| 1351 | 'co-author' => [ |
||
| 1352 | 'links' => [ |
||
| 1353 | 'self' => 'http://example.com/books/1/relationships/co-author', |
||
| 1354 | 'related' => 'http://example.com/books/1/co-author' |
||
| 1355 | ], |
||
| 1356 | ], |
||
| 1357 | 'author-with-meta' => [ |
||
| 1358 | 'links' => [ |
||
| 1359 | 'self' => 'http://example.com/books/1/relationships/author-with-meta', |
||
| 1360 | 'related' => 'http://example.com/books/1/author-with-meta' |
||
| 1361 | ], |
||
| 1362 | ], |
||
| 1363 | ], |
||
| 1364 | 'links' => [ |
||
| 1365 | 'self' => 'http://example.com/books/1', |
||
| 1366 | ], |
||
| 1367 | ], |
||
| 1368 | 'included' => [ |
||
| 1369 | [ |
||
| 1370 | 'type' => 'people', |
||
| 1371 | 'id' => '1', |
||
| 1372 | 'attributes' => [ |
||
| 1373 | 'name' => 'Dave', |
||
| 1374 | ], |
||
| 1375 | 'relationships' => [ |
||
| 1376 | 'published' => [ |
||
| 1377 | 'links' => [ |
||
| 1378 | 'self' => 'http://example.com/people/1/relationships/published', |
||
| 1379 | 'related' => 'http://example.com/people/1/published', |
||
| 1380 | ], |
||
| 1381 | ], |
||
| 1382 | ], |
||
| 1383 | 'links' => [ |
||
| 1384 | 'self' => 'http://example.com/people/1', |
||
| 1385 | ], |
||
| 1386 | ], |
||
| 1387 | ], |
||
| 1388 | ]; |
||
| 1389 | |||
| 1390 | $this->assertEquals($expected, $scope->toArray()); |
||
| 1391 | |||
| 1392 | $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author","related":"http:\/\/example.com\/books\/1\/author"},"data":{"type":"people","id":"1"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/co-author","related":"http:\/\/example.com\/books\/1\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/1\/author-with-meta"}}}},"included":[{"type":"people","id":"1","attributes":{"name":"Dave"},"links":{"self":"http:\/\/example.com\/people\/1"},"relationships":{"published":{"links":{"self":"http:\/\/example.com\/people\/1\/relationships\/published","related":"http:\/\/example.com\/people\/1\/published"}}}}]}'; |
||
| 1393 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 1394 | } |
||
| 1395 | |||
| 1396 | public function testSerializingItemResourceWithLinksForHasManyRelationship() |
||
| 1397 | { |
||
| 1398 | $baseUrl = 'http://example.com'; |
||
| 1399 | $this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
||
| 1400 | $this->manager->parseIncludes('published'); |
||
| 1401 | |||
| 1402 | $authorData = [ |
||
| 1403 | 'id' => 1, |
||
| 1404 | 'name' => 'Dave', |
||
| 1405 | '_published' => [ |
||
| 1406 | [ |
||
| 1407 | 'id' => 1, |
||
| 1408 | 'title' => 'Foo', |
||
| 1409 | 'year' => '1991', |
||
| 1410 | ], |
||
| 1411 | [ |
||
| 1412 | 'id' => 2, |
||
| 1413 | 'title' => 'Bar', |
||
| 1414 | 'year' => '2015', |
||
| 1415 | ], |
||
| 1416 | ], |
||
| 1417 | ]; |
||
| 1418 | |||
| 1419 | $resource = new Item($authorData, new JsonApiAuthorTransformer(), 'people'); |
||
| 1420 | |||
| 1421 | $scope = new Scope($this->manager, $resource); |
||
| 1422 | |||
| 1423 | $expected = [ |
||
| 1424 | 'data' => [ |
||
| 1425 | 'type' => 'people', |
||
| 1426 | 'id' => '1', |
||
| 1427 | 'attributes' => [ |
||
| 1428 | 'name' => 'Dave', |
||
| 1429 | ], |
||
| 1430 | 'relationships' => [ |
||
| 1431 | 'published' => [ |
||
| 1432 | 'links' => [ |
||
| 1433 | 'self' => 'http://example.com/people/1/relationships/published', |
||
| 1434 | 'related' => 'http://example.com/people/1/published', |
||
| 1435 | ], |
||
| 1436 | 'data' => [ |
||
| 1437 | [ |
||
| 1438 | 'type' => 'books', |
||
| 1439 | 'id' => 1, |
||
| 1440 | ], |
||
| 1441 | [ |
||
| 1442 | 'type' => 'books', |
||
| 1443 | 'id' => 2, |
||
| 1444 | ], |
||
| 1445 | ], |
||
| 1446 | ], |
||
| 1447 | ], |
||
| 1448 | 'links' => [ |
||
| 1449 | 'self' => 'http://example.com/people/1', |
||
| 1450 | ], |
||
| 1451 | ], |
||
| 1452 | 'included' => [ |
||
| 1453 | [ |
||
| 1454 | 'type' => 'books', |
||
| 1455 | 'id' => '1', |
||
| 1456 | 'attributes' => [ |
||
| 1457 | 'title' => 'Foo', |
||
| 1458 | 'year' => 1991, |
||
| 1459 | ], |
||
| 1460 | 'links' => [ |
||
| 1461 | 'self' => 'http://example.com/books/1', |
||
| 1462 | ], |
||
| 1463 | 'relationships' => [ |
||
| 1464 | 'author' => [ |
||
| 1465 | 'links' => [ |
||
| 1466 | 'self' => 'http://example.com/books/1/relationships/author', |
||
| 1467 | 'related' => 'http://example.com/books/1/author' |
||
| 1468 | ], |
||
| 1469 | ], |
||
| 1470 | 'co-author' => [ |
||
| 1471 | 'links' => [ |
||
| 1472 | 'self' => 'http://example.com/books/1/relationships/co-author', |
||
| 1473 | 'related' => 'http://example.com/books/1/co-author' |
||
| 1474 | ], |
||
| 1475 | ], |
||
| 1476 | 'author-with-meta' => [ |
||
| 1477 | 'links' => [ |
||
| 1478 | 'self' => 'http://example.com/books/1/relationships/author-with-meta', |
||
| 1479 | 'related' => 'http://example.com/books/1/author-with-meta' |
||
| 1480 | ], |
||
| 1481 | ], |
||
| 1482 | ], |
||
| 1483 | ], |
||
| 1484 | [ |
||
| 1485 | 'type' => 'books', |
||
| 1486 | 'id' => '2', |
||
| 1487 | 'attributes' => [ |
||
| 1488 | 'title' => 'Bar', |
||
| 1489 | 'year' => 2015, |
||
| 1490 | ], |
||
| 1491 | 'links' => [ |
||
| 1492 | 'self' => 'http://example.com/books/2', |
||
| 1493 | ], |
||
| 1494 | 'relationships' => [ |
||
| 1495 | 'author' => [ |
||
| 1496 | 'links' => [ |
||
| 1497 | 'self' => 'http://example.com/books/2/relationships/author', |
||
| 1498 | 'related' => 'http://example.com/books/2/author' |
||
| 1499 | ], |
||
| 1500 | ], |
||
| 1501 | 'co-author' => [ |
||
| 1502 | 'links' => [ |
||
| 1503 | 'self' => 'http://example.com/books/2/relationships/co-author', |
||
| 1504 | 'related' => 'http://example.com/books/2/co-author' |
||
| 1505 | ], |
||
| 1506 | ], |
||
| 1507 | 'author-with-meta' => [ |
||
| 1508 | 'links' => [ |
||
| 1509 | 'self' => 'http://example.com/books/2/relationships/author-with-meta', |
||
| 1510 | 'related' => 'http://example.com/books/2/author-with-meta' |
||
| 1511 | ], |
||
| 1512 | ], |
||
| 1513 | ], |
||
| 1514 | ], |
||
| 1515 | ], |
||
| 1516 | ]; |
||
| 1517 | |||
| 1518 | $this->assertEquals($expected, $scope->toArray()); |
||
| 1519 | |||
| 1520 | $expectedJson = '{"data":{"type":"people","id":"1","attributes":{"name":"Dave"},"links":{"self":"http:\/\/example.com\/people\/1"},"relationships":{"published":{"links":{"self":"http:\/\/example.com\/people\/1\/relationships\/published","related":"http:\/\/example.com\/people\/1\/published"},"data":[{"type":"books","id":"1"},{"type":"books","id":"2"}]}}},"included":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author","related":"http:\/\/example.com\/books\/1\/author"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/co-author","related":"http:\/\/example.com\/books\/1\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/1\/author-with-meta"}}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":2015},"links":{"self":"http:\/\/example.com\/books\/2"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author","related":"http:\/\/example.com\/books\/2\/author"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/co-author","related":"http:\/\/example.com\/books\/2\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/2\/author-with-meta"}}}}]}'; |
||
| 1521 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 1522 | } |
||
| 1523 | |||
| 1524 | public function testSerializingCollectionResourceWithLinksForHasOneRelationship() |
||
| 1525 | { |
||
| 1526 | $baseUrl = 'http://example.com'; |
||
| 1527 | $this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
||
| 1528 | $this->manager->parseIncludes('author'); |
||
| 1529 | |||
| 1530 | $bookData = [ |
||
| 1531 | [ |
||
| 1532 | 'id' => 1, |
||
| 1533 | 'title' => 'Foo', |
||
| 1534 | 'year' => '1991', |
||
| 1535 | '_author' => [ |
||
| 1536 | 'id' => 1, |
||
| 1537 | 'name' => 'Dave', |
||
| 1538 | ], |
||
| 1539 | ], |
||
| 1540 | [ |
||
| 1541 | 'id' => 2, |
||
| 1542 | 'title' => 'Bar', |
||
| 1543 | 'year' => '1991', |
||
| 1544 | '_author' => [ |
||
| 1545 | 'id' => 1, |
||
| 1546 | 'name' => 'Dave', |
||
| 1547 | ], |
||
| 1548 | ], |
||
| 1549 | ]; |
||
| 1550 | |||
| 1551 | $resource = new Collection($bookData, new JsonApiBookTransformer(), 'books'); |
||
| 1552 | |||
| 1553 | $scope = new Scope($this->manager, $resource); |
||
| 1554 | |||
| 1555 | $expected = [ |
||
| 1556 | 'data' => [ |
||
| 1557 | [ |
||
| 1558 | 'type' => 'books', |
||
| 1559 | 'id' => '1', |
||
| 1560 | 'attributes' => [ |
||
| 1561 | 'title' => 'Foo', |
||
| 1562 | 'year' => 1991, |
||
| 1563 | ], |
||
| 1564 | 'relationships' => [ |
||
| 1565 | 'author' => [ |
||
| 1566 | 'links' => [ |
||
| 1567 | 'self' => 'http://example.com/books/1/relationships/author', |
||
| 1568 | 'related' => 'http://example.com/books/1/author', |
||
| 1569 | ], |
||
| 1570 | 'data' => [ |
||
| 1571 | 'type' => 'people', |
||
| 1572 | 'id' => '1', |
||
| 1573 | ], |
||
| 1574 | ], |
||
| 1575 | 'co-author' => [ |
||
| 1576 | 'links' => [ |
||
| 1577 | 'self' => 'http://example.com/books/1/relationships/co-author', |
||
| 1578 | 'related' => 'http://example.com/books/1/co-author' |
||
| 1579 | ], |
||
| 1580 | ], |
||
| 1581 | 'author-with-meta' => [ |
||
| 1582 | 'links' => [ |
||
| 1583 | 'self' => 'http://example.com/books/1/relationships/author-with-meta', |
||
| 1584 | 'related' => 'http://example.com/books/1/author-with-meta' |
||
| 1585 | ], |
||
| 1586 | ], |
||
| 1587 | ], |
||
| 1588 | 'links' => [ |
||
| 1589 | 'self' => 'http://example.com/books/1', |
||
| 1590 | ], |
||
| 1591 | ], |
||
| 1592 | [ |
||
| 1593 | 'type' => 'books', |
||
| 1594 | 'id' => '2', |
||
| 1595 | 'attributes' => [ |
||
| 1596 | 'title' => 'Bar', |
||
| 1597 | 'year' => 1991, |
||
| 1598 | ], |
||
| 1599 | 'relationships' => [ |
||
| 1600 | 'author' => [ |
||
| 1601 | 'links' => [ |
||
| 1602 | 'self' => 'http://example.com/books/2/relationships/author', |
||
| 1603 | 'related' => 'http://example.com/books/2/author', |
||
| 1604 | ], |
||
| 1605 | 'data' => [ |
||
| 1606 | 'type' => 'people', |
||
| 1607 | 'id' => '1', |
||
| 1608 | ], |
||
| 1609 | ], |
||
| 1610 | 'co-author' => [ |
||
| 1611 | 'links' => [ |
||
| 1612 | 'self' => 'http://example.com/books/2/relationships/co-author', |
||
| 1613 | 'related' => 'http://example.com/books/2/co-author' |
||
| 1614 | ], |
||
| 1615 | ], |
||
| 1616 | 'author-with-meta' => [ |
||
| 1617 | 'links' => [ |
||
| 1618 | 'self' => 'http://example.com/books/2/relationships/author-with-meta', |
||
| 1619 | 'related' => 'http://example.com/books/2/author-with-meta' |
||
| 1620 | ], |
||
| 1621 | ], |
||
| 1622 | ], |
||
| 1623 | 'links' => [ |
||
| 1624 | 'self' => 'http://example.com/books/2', |
||
| 1625 | ], |
||
| 1626 | ], |
||
| 1627 | ], |
||
| 1628 | 'included' => [ |
||
| 1629 | [ |
||
| 1630 | 'type' => 'people', |
||
| 1631 | 'id' => '1', |
||
| 1632 | 'attributes' => [ |
||
| 1633 | 'name' => 'Dave', |
||
| 1634 | ], |
||
| 1635 | 'links' => [ |
||
| 1636 | 'self' => 'http://example.com/people/1', |
||
| 1637 | ], |
||
| 1638 | 'relationships' => [ |
||
| 1639 | 'published' => [ |
||
| 1640 | 'links' => [ |
||
| 1641 | 'self' => 'http://example.com/people/1/relationships/published', |
||
| 1642 | 'related' => 'http://example.com/people/1/published', |
||
| 1643 | ], |
||
| 1644 | ], |
||
| 1645 | ], |
||
| 1646 | ], |
||
| 1647 | ], |
||
| 1648 | ]; |
||
| 1649 | |||
| 1650 | $this->assertEquals($expected, $scope->toArray()); |
||
| 1651 | |||
| 1652 | $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author","related":"http:\/\/example.com\/books\/1\/author"},"data":{"type":"people","id":"1"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/co-author","related":"http:\/\/example.com\/books\/1\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/1\/author-with-meta"}}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1991},"links":{"self":"http:\/\/example.com\/books\/2"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author","related":"http:\/\/example.com\/books\/2\/author"},"data":{"type":"people","id":"1"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/co-author","related":"http:\/\/example.com\/books\/2\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/2\/author-with-meta"}}}}],"included":[{"type":"people","id":"1","attributes":{"name":"Dave"},"links":{"self":"http:\/\/example.com\/people\/1"},"relationships":{"published":{"links":{"self":"http:\/\/example.com\/people\/1\/relationships\/published","related":"http:\/\/example.com\/people\/1\/published"}}}}]}'; |
||
| 1653 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 1654 | } |
||
| 1655 | |||
| 1656 | public function testSerializingCollectionResourceWithLinksForHasManyRelationship() |
||
| 1657 | { |
||
| 1658 | $baseUrl = 'http://example.com'; |
||
| 1659 | $this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
||
| 1660 | $this->manager->parseIncludes('published'); |
||
| 1661 | |||
| 1662 | $authorData = [ |
||
| 1663 | [ |
||
| 1664 | 'id' => 1, |
||
| 1665 | 'name' => 'Dave', |
||
| 1666 | '_published' => [ |
||
| 1667 | [ |
||
| 1668 | 'id' => 1, |
||
| 1669 | 'title' => 'Foo', |
||
| 1670 | 'year' => '1991', |
||
| 1671 | ], |
||
| 1672 | [ |
||
| 1673 | 'id' => 2, |
||
| 1674 | 'title' => 'Bar', |
||
| 1675 | 'year' => '2015', |
||
| 1676 | ], |
||
| 1677 | ], |
||
| 1678 | ], |
||
| 1679 | [ |
||
| 1680 | 'id' => 2, |
||
| 1681 | 'name' => 'Bill', |
||
| 1682 | '_published' => [ |
||
| 1683 | [ |
||
| 1684 | 'id' => 1, |
||
| 1685 | 'title' => 'Foo', |
||
| 1686 | 'year' => '1991', |
||
| 1687 | ], |
||
| 1688 | [ |
||
| 1689 | 'id' => 2, |
||
| 1690 | 'title' => 'Bar', |
||
| 1691 | 'year' => '2015', |
||
| 1692 | ], |
||
| 1693 | ], |
||
| 1694 | ], |
||
| 1695 | ]; |
||
| 1696 | |||
| 1697 | $resource = new Collection($authorData, new JsonApiAuthorTransformer(), 'people'); |
||
| 1698 | |||
| 1699 | $scope = new Scope($this->manager, $resource); |
||
| 1700 | |||
| 1701 | $expected = [ |
||
| 1702 | 'data' => [ |
||
| 1703 | [ |
||
| 1704 | 'type' => 'people', |
||
| 1705 | 'id' => '1', |
||
| 1706 | 'attributes' => [ |
||
| 1707 | 'name' => 'Dave', |
||
| 1708 | ], |
||
| 1709 | 'relationships' => [ |
||
| 1710 | 'published' => [ |
||
| 1711 | 'links' => [ |
||
| 1712 | 'self' => 'http://example.com/people/1/relationships/published', |
||
| 1713 | 'related' => 'http://example.com/people/1/published', |
||
| 1714 | ], |
||
| 1715 | 'data' => [ |
||
| 1716 | [ |
||
| 1717 | 'type' => 'books', |
||
| 1718 | 'id' => 1, |
||
| 1719 | ], |
||
| 1720 | [ |
||
| 1721 | 'type' => 'books', |
||
| 1722 | 'id' => 2, |
||
| 1723 | ], |
||
| 1724 | ], |
||
| 1725 | ], |
||
| 1726 | ], |
||
| 1727 | 'links' => [ |
||
| 1728 | 'self' => 'http://example.com/people/1', |
||
| 1729 | ], |
||
| 1730 | ], |
||
| 1731 | [ |
||
| 1732 | 'type' => 'people', |
||
| 1733 | 'id' => '2', |
||
| 1734 | 'attributes' => [ |
||
| 1735 | 'name' => 'Bill', |
||
| 1736 | ], |
||
| 1737 | 'relationships' => [ |
||
| 1738 | 'published' => [ |
||
| 1739 | 'links' => [ |
||
| 1740 | 'self' => 'http://example.com/people/2/relationships/published', |
||
| 1741 | 'related' => 'http://example.com/people/2/published', |
||
| 1742 | ], |
||
| 1743 | 'data' => [ |
||
| 1744 | [ |
||
| 1745 | 'type' => 'books', |
||
| 1746 | 'id' => 1, |
||
| 1747 | ], |
||
| 1748 | [ |
||
| 1749 | 'type' => 'books', |
||
| 1750 | 'id' => 2, |
||
| 1751 | ], |
||
| 1752 | ], |
||
| 1753 | ], |
||
| 1754 | ], |
||
| 1755 | 'links' => [ |
||
| 1756 | 'self' => 'http://example.com/people/2', |
||
| 1757 | ], |
||
| 1758 | ], |
||
| 1759 | ], |
||
| 1760 | 'included' => [ |
||
| 1761 | [ |
||
| 1762 | 'type' => 'books', |
||
| 1763 | 'id' => '1', |
||
| 1764 | 'attributes' => [ |
||
| 1765 | 'title' => 'Foo', |
||
| 1766 | 'year' => 1991, |
||
| 1767 | ], |
||
| 1768 | 'links' => [ |
||
| 1769 | 'self' => 'http://example.com/books/1', |
||
| 1770 | ], |
||
| 1771 | 'relationships' => [ |
||
| 1772 | 'author' => [ |
||
| 1773 | 'links' => [ |
||
| 1774 | 'self' => 'http://example.com/books/1/relationships/author', |
||
| 1775 | 'related' => 'http://example.com/books/1/author', |
||
| 1776 | ], |
||
| 1777 | ], |
||
| 1778 | 'co-author' => [ |
||
| 1779 | 'links' => [ |
||
| 1780 | 'self' => 'http://example.com/books/1/relationships/co-author', |
||
| 1781 | 'related' => 'http://example.com/books/1/co-author' |
||
| 1782 | ], |
||
| 1783 | ], |
||
| 1784 | 'author-with-meta' => [ |
||
| 1785 | 'links' => [ |
||
| 1786 | 'self' => 'http://example.com/books/1/relationships/author-with-meta', |
||
| 1787 | 'related' => 'http://example.com/books/1/author-with-meta' |
||
| 1788 | ], |
||
| 1789 | ], |
||
| 1790 | ], |
||
| 1791 | ], |
||
| 1792 | [ |
||
| 1793 | 'type' => 'books', |
||
| 1794 | 'id' => '2', |
||
| 1795 | 'attributes' => [ |
||
| 1796 | 'title' => 'Bar', |
||
| 1797 | 'year' => 2015, |
||
| 1798 | ], |
||
| 1799 | 'links' => [ |
||
| 1800 | 'self' => 'http://example.com/books/2', |
||
| 1801 | ], |
||
| 1802 | 'relationships' => [ |
||
| 1803 | 'author' => [ |
||
| 1804 | 'links' => [ |
||
| 1805 | 'self' => 'http://example.com/books/2/relationships/author', |
||
| 1806 | 'related' => 'http://example.com/books/2/author', |
||
| 1807 | ], |
||
| 1808 | ], |
||
| 1809 | 'co-author' => [ |
||
| 1810 | 'links' => [ |
||
| 1811 | 'self' => 'http://example.com/books/2/relationships/co-author', |
||
| 1812 | 'related' => 'http://example.com/books/2/co-author' |
||
| 1813 | ], |
||
| 1814 | ], |
||
| 1815 | 'author-with-meta' => [ |
||
| 1816 | 'links' => [ |
||
| 1817 | 'self' => 'http://example.com/books/2/relationships/author-with-meta', |
||
| 1818 | 'related' => 'http://example.com/books/2/author-with-meta' |
||
| 1819 | ], |
||
| 1820 | ], |
||
| 1821 | ], |
||
| 1822 | ], |
||
| 1823 | ], |
||
| 1824 | ]; |
||
| 1825 | |||
| 1826 | $this->assertEquals($expected, $scope->toArray()); |
||
| 1827 | |||
| 1828 | $expectedJson = '{"data":[{"type":"people","id":"1","attributes":{"name":"Dave"},"links":{"self":"http:\/\/example.com\/people\/1"},"relationships":{"published":{"links":{"self":"http:\/\/example.com\/people\/1\/relationships\/published","related":"http:\/\/example.com\/people\/1\/published"},"data":[{"type":"books","id":"1"},{"type":"books","id":"2"}]}}},{"type":"people","id":"2","attributes":{"name":"Bill"},"links":{"self":"http:\/\/example.com\/people\/2"},"relationships":{"published":{"links":{"self":"http:\/\/example.com\/people\/2\/relationships\/published","related":"http:\/\/example.com\/people\/2\/published"},"data":[{"type":"books","id":"1"},{"type":"books","id":"2"}]}}}],"included":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author","related":"http:\/\/example.com\/books\/1\/author"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/co-author","related":"http:\/\/example.com\/books\/1\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/1\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/1\/author-with-meta"}}}},{"type":"books","id":"2","attributes":{"title":"Bar","year":2015},"links":{"self":"http:\/\/example.com\/books\/2"},"relationships":{"author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author","related":"http:\/\/example.com\/books\/2\/author"}},"co-author":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/co-author","related":"http:\/\/example.com\/books\/2\/co-author"}},"author-with-meta":{"links":{"self":"http:\/\/example.com\/books\/2\/relationships\/author-with-meta","related":"http:\/\/example.com\/books\/2\/author-with-meta"}}}}]}'; |
||
| 1829 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 1830 | } |
||
| 1831 | |||
| 1832 | public function testExceptionThrownIfResourceHasNoId() |
||
| 1833 | { |
||
| 1834 | $this->expectException(InvalidArgumentException::class, 'JSON API resource objects MUST have a valid id'); |
||
| 1835 | |||
| 1836 | $bookData = [ |
||
| 1837 | 'title' => 'Foo', |
||
| 1838 | 'year' => '1991', |
||
| 1839 | ]; |
||
| 1840 | |||
| 1841 | $resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
||
| 1842 | |||
| 1843 | $scope = new Scope($this->manager, $resource); |
||
| 1844 | $scope->toArray(); |
||
| 1845 | } |
||
| 1846 | |||
| 1847 | public function testSerializingItemWithReferenceToRootObject() |
||
| 1925 | |||
| 1926 | public function testSerializingCollectionWithReferenceToRootObjects() |
||
| 2037 | |||
| 2038 | public function testSerializingCollectionResourceWithPaginator() |
||
| 2178 | |||
| 2179 | View Code Duplication | public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailablePreviousLink() |
|
| 2316 | |||
| 2317 | View Code Duplication | public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailableNextLink() |
|
| 2454 | |||
| 2455 | View Code Duplication | public function testCustomLinkMerge() |
|
| 2514 | |||
| 2515 | public function testCustomLinkMergeNoLink() |
||
| 2570 | |||
| 2571 | View Code Duplication | public function testCustomSelfLinkMerge() |
|
| 2629 | |||
| 2630 | public function testEmptyAttributesIsObject() |
||
| 2645 | |||
| 2646 | /** |
||
| 2647 | * @dataProvider serializingWithFieldsetsProvider |
||
| 2648 | */ |
||
| 2649 | public function testSerializingWithFieldsets($fieldsetsToParse, $expected) |
||
| 2682 | |||
| 2683 | public function serializingWithFieldsetsProvider() |
||
| 2796 | } |
||
| 2797 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.