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 |
||
| 11 | class JsonApiSerializerTest extends PHPUnit_Framework_TestCase |
||
| 12 | { |
||
| 13 | private $manager; |
||
| 14 | |||
| 15 | public function setUp() |
||
| 16 | { |
||
| 17 | $this->manager = new Manager(); |
||
| 18 | $this->manager->setSerializer(new JsonApiSerializer()); |
||
| 19 | } |
||
| 20 | |||
| 21 | public function testSerializingItemResourceWithHasOneInclude() |
||
| 22 | { |
||
| 23 | $this->manager->parseIncludes('author'); |
||
| 24 | |||
| 25 | $bookData = [ |
||
| 26 | 'id' => 1, |
||
| 27 | 'title' => 'Foo', |
||
| 28 | 'year' => '1991', |
||
| 29 | '_author' => [ |
||
| 30 | 'id' => 1, |
||
| 31 | 'name' => 'Dave', |
||
| 32 | ], |
||
| 33 | ]; |
||
| 34 | |||
| 35 | $resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
||
| 36 | |||
| 37 | $scope = new Scope($this->manager, $resource); |
||
| 38 | |||
| 39 | $expected = [ |
||
| 40 | 'data' => [ |
||
| 41 | 'type' => 'books', |
||
| 42 | 'id' => '1', |
||
| 43 | 'attributes' => [ |
||
| 44 | 'title' => 'Foo', |
||
| 45 | 'year' => 1991, |
||
| 46 | ], |
||
| 47 | 'relationships' => [ |
||
| 48 | 'author' => [ |
||
| 49 | 'data' => [ |
||
| 50 | 'type' => 'people', |
||
| 51 | 'id' => '1', |
||
| 52 | ], |
||
| 53 | ], |
||
| 54 | ], |
||
| 55 | ], |
||
| 56 | 'included' => [ |
||
| 57 | [ |
||
| 58 | 'type' => 'people', |
||
| 59 | 'id' => '1', |
||
| 60 | 'attributes' => [ |
||
| 61 | 'name' => 'Dave', |
||
| 62 | ], |
||
| 63 | ], |
||
| 64 | ], |
||
| 65 | ]; |
||
| 66 | |||
| 67 | $this->assertSame($expected, $scope->toArray()); |
||
| 68 | |||
| 69 | $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"}}]}'; |
||
| 70 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 71 | } |
||
| 72 | |||
| 73 | View Code Duplication | public function testSerializingItemResourceWithHasOneDasherizedInclude() |
|
| 74 | { |
||
| 75 | $this->manager->parseIncludes('co-author'); |
||
| 76 | |||
| 77 | $bookData = [ |
||
| 78 | 'id' => 1, |
||
| 79 | 'title' => 'Foo', |
||
| 80 | 'year' => '1991', |
||
| 81 | '_author' => [ |
||
| 82 | 'id' => 1, |
||
| 83 | 'name' => 'Dave', |
||
| 84 | ], |
||
| 85 | '_co_author' => [ |
||
| 86 | 'id' => 2, |
||
| 87 | 'name' => 'Jim', |
||
| 88 | ], |
||
| 89 | ]; |
||
| 90 | |||
| 91 | $resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
||
| 92 | |||
| 93 | $scope = new Scope($this->manager, $resource); |
||
| 94 | |||
| 95 | $expected = [ |
||
| 96 | 'data' => [ |
||
| 97 | 'type' => 'books', |
||
| 98 | 'id' => '1', |
||
| 99 | 'attributes' => [ |
||
| 100 | 'title' => 'Foo', |
||
| 101 | 'year' => 1991, |
||
| 102 | ], |
||
| 103 | 'relationships' => [ |
||
| 104 | 'co-author' => [ |
||
| 105 | 'data' => [ |
||
| 106 | 'type' => 'people', |
||
| 107 | 'id' => '2', |
||
| 108 | ], |
||
| 109 | ], |
||
| 110 | ], |
||
| 111 | ], |
||
| 112 | 'included' => [ |
||
| 113 | [ |
||
| 114 | 'type' => 'people', |
||
| 115 | 'id' => '2', |
||
| 116 | 'attributes' => [ |
||
| 117 | 'name' => 'Jim', |
||
| 118 | ], |
||
| 119 | ], |
||
| 120 | ], |
||
| 121 | ]; |
||
| 122 | |||
| 123 | $this->assertSame($expected, $scope->toArray()); |
||
| 124 | |||
| 125 | $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"}}]}'; |
||
| 126 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 127 | } |
||
| 128 | |||
| 129 | public function testSerializingItemResourceWithEmptyHasOneInclude() |
||
| 130 | { |
||
| 131 | $this->manager->parseIncludes('author'); |
||
| 132 | |||
| 133 | $bookData = [ |
||
| 134 | 'id' => 1, |
||
| 135 | 'title' => 'Foo', |
||
| 136 | 'year' => '1991', |
||
| 137 | '_author' => null, |
||
| 138 | ]; |
||
| 139 | |||
| 140 | $resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
||
| 141 | |||
| 142 | $scope = new Scope($this->manager, $resource); |
||
| 143 | |||
| 144 | $expected = [ |
||
| 145 | 'data' => [ |
||
| 146 | 'type' => 'books', |
||
| 147 | 'id' => '1', |
||
| 148 | 'attributes' => [ |
||
| 149 | 'title' => 'Foo', |
||
| 150 | 'year' => 1991, |
||
| 151 | ], |
||
| 152 | 'relationships' => [ |
||
| 153 | 'author' => [ |
||
| 154 | 'data' => null, |
||
| 155 | ], |
||
| 156 | ], |
||
| 157 | ], |
||
| 158 | ]; |
||
| 159 | |||
| 160 | $this->assertSame($expected, $scope->toArray()); |
||
| 161 | |||
| 162 | $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"relationships":{"author":{"data":null}}}}'; |
||
| 163 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 164 | } |
||
| 165 | |||
| 166 | public function testSerializingItemResourceWithHasManyInclude() |
||
| 167 | { |
||
| 168 | $this->manager->parseIncludes('published'); |
||
| 169 | |||
| 170 | $authorData = [ |
||
| 171 | 'id' => 1, |
||
| 172 | 'name' => 'Dave', |
||
| 173 | '_published' => [ |
||
| 174 | [ |
||
| 175 | 'id' => 1, |
||
| 176 | 'title' => 'Foo', |
||
| 177 | 'year' => '1991', |
||
| 178 | ], |
||
| 179 | [ |
||
| 180 | 'id' => 2, |
||
| 181 | 'title' => 'Bar', |
||
| 182 | 'year' => '2015', |
||
| 183 | ], |
||
| 184 | ], |
||
| 185 | ]; |
||
| 186 | |||
| 187 | $resource = new Item($authorData, new JsonApiAuthorTransformer(), 'people'); |
||
| 188 | |||
| 189 | $scope = new Scope($this->manager, $resource); |
||
| 190 | |||
| 191 | $expected = [ |
||
| 192 | 'data' => [ |
||
| 193 | 'type' => 'people', |
||
| 194 | 'id' => '1', |
||
| 195 | 'attributes' => [ |
||
| 196 | 'name' => 'Dave', |
||
| 197 | ], |
||
| 198 | 'relationships' => [ |
||
| 199 | 'published' => [ |
||
| 200 | 'data' => [ |
||
| 201 | [ |
||
| 202 | 'type' => 'books', |
||
| 203 | 'id' => 1, |
||
| 204 | ], |
||
| 205 | [ |
||
| 206 | 'type' => 'books', |
||
| 207 | 'id' => 2, |
||
| 208 | ], |
||
| 209 | ], |
||
| 210 | ], |
||
| 211 | ], |
||
| 212 | ], |
||
| 213 | 'included' => [ |
||
| 214 | [ |
||
| 215 | 'type' => 'books', |
||
| 216 | 'id' => '1', |
||
| 217 | 'attributes' => [ |
||
| 218 | 'title' => 'Foo', |
||
| 219 | 'year' => 1991, |
||
| 220 | ], |
||
| 221 | ], |
||
| 222 | [ |
||
| 223 | 'type' => 'books', |
||
| 224 | 'id' => '2', |
||
| 225 | 'attributes' => [ |
||
| 226 | 'title' => 'Bar', |
||
| 227 | 'year' => 2015, |
||
| 228 | ], |
||
| 229 | ], |
||
| 230 | ], |
||
| 231 | ]; |
||
| 232 | |||
| 233 | $this->assertEquals($expected, $scope->toArray()); |
||
| 234 | |||
| 235 | $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}}]}'; |
||
| 236 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 237 | } |
||
| 238 | |||
| 239 | public function testSerializingItemResourceWithEmptyHasManyInclude() |
||
| 240 | { |
||
| 241 | $this->manager->parseIncludes('published'); |
||
| 242 | |||
| 243 | $authorData = [ |
||
| 244 | 'id' => 1, |
||
| 245 | 'name' => 'Dave', |
||
| 246 | '_published' => [], |
||
| 247 | ]; |
||
| 248 | |||
| 249 | $resource = new Item($authorData, new JsonApiAuthorTransformer(), 'people'); |
||
| 250 | |||
| 251 | $scope = new Scope($this->manager, $resource); |
||
| 252 | |||
| 253 | $expected = [ |
||
| 254 | 'data' => [ |
||
| 255 | 'type' => 'people', |
||
| 256 | 'id' => '1', |
||
| 257 | 'attributes' => [ |
||
| 258 | 'name' => 'Dave', |
||
| 259 | ], |
||
| 260 | 'relationships' => [ |
||
| 261 | 'published' => [ |
||
| 262 | 'data' => [], |
||
| 263 | ], |
||
| 264 | ], |
||
| 265 | ], |
||
| 266 | ]; |
||
| 267 | |||
| 268 | $this->assertSame($expected, $scope->toArray()); |
||
| 269 | |||
| 270 | $expectedJson = '{"data":{"type":"people","id":"1","attributes":{"name":"Dave"},"relationships":{"published":{"data":[]}}}}'; |
||
| 271 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 272 | } |
||
| 273 | |||
| 274 | public function testSerializingItemResourceWithoutIncludes() |
||
| 275 | { |
||
| 276 | $bookData = [ |
||
| 277 | 'id' => 1, |
||
| 278 | 'title' => 'Foo', |
||
| 279 | 'year' => '1991', |
||
| 280 | '_author' => [ |
||
| 281 | 'id' => 1, |
||
| 282 | 'name' => 'Dave', |
||
| 283 | ], |
||
| 284 | ]; |
||
| 285 | |||
| 286 | $resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
||
| 287 | |||
| 288 | $scope = new Scope($this->manager, $resource); |
||
| 289 | |||
| 290 | $expected = [ |
||
| 291 | 'data' => [ |
||
| 292 | 'type' => 'books', |
||
| 293 | 'id' => '1', |
||
| 294 | 'attributes' => [ |
||
| 295 | 'title' => 'Foo', |
||
| 296 | 'year' => 1991, |
||
| 297 | ], |
||
| 298 | ], |
||
| 299 | ]; |
||
| 300 | |||
| 301 | $this->assertSame($expected, $scope->toArray()); |
||
| 302 | |||
| 303 | $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}}}'; |
||
| 304 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 305 | } |
||
| 306 | |||
| 307 | View Code Duplication | public function testSerializingItemResourceWithMeta() |
|
| 308 | { |
||
| 309 | $bookData = [ |
||
| 310 | 'id' => 1, |
||
| 311 | 'title' => 'Foo', |
||
| 312 | 'year' => '1991', |
||
| 313 | '_author' => [ |
||
| 314 | 'id' => 1, |
||
| 315 | 'name' => 'Dave', |
||
| 316 | ], |
||
| 317 | ]; |
||
| 318 | |||
| 319 | $resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
||
| 320 | $resource->setMetaValue('foo', 'bar'); |
||
| 321 | |||
| 322 | $scope = new Scope($this->manager, $resource); |
||
| 323 | |||
| 324 | $expected = [ |
||
| 325 | 'data' => [ |
||
| 326 | 'type' => 'books', |
||
| 327 | 'id' => '1', |
||
| 328 | 'attributes' => [ |
||
| 329 | 'title' => 'Foo', |
||
| 330 | 'year' => 1991, |
||
| 331 | ], |
||
| 332 | ], |
||
| 333 | 'meta' => [ |
||
| 334 | 'foo' => 'bar', |
||
| 335 | ], |
||
| 336 | ]; |
||
| 337 | |||
| 338 | $this->assertSame($expected, $scope->toArray()); |
||
| 339 | |||
| 340 | $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},"meta":{"foo":"bar"}}'; |
||
| 341 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 342 | } |
||
| 343 | |||
| 344 | View Code Duplication | public function testSerializingCollectionResourceWithoutIncludes() |
|
| 345 | { |
||
| 346 | $booksData = [ |
||
| 347 | [ |
||
| 348 | 'id' => 1, |
||
| 349 | 'title' => 'Foo', |
||
| 350 | 'year' => '1991', |
||
| 351 | '_author' => [ |
||
| 352 | 'id' => 1, |
||
| 353 | 'name' => 'Dave', |
||
| 354 | ], |
||
| 355 | ], |
||
| 356 | [ |
||
| 357 | 'id' => 2, |
||
| 358 | 'title' => 'Bar', |
||
| 359 | 'year' => '1997', |
||
| 360 | '_author' => [ |
||
| 361 | 'id' => 2, |
||
| 362 | 'name' => 'Bob', |
||
| 363 | ], |
||
| 364 | ], |
||
| 365 | ]; |
||
| 366 | |||
| 367 | $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
||
| 368 | $scope = new Scope($this->manager, $resource); |
||
| 369 | |||
| 370 | $expected = [ |
||
| 371 | 'data' => [ |
||
| 372 | [ |
||
| 373 | 'type' => 'books', |
||
| 374 | 'id' => '1', |
||
| 375 | 'attributes' => [ |
||
| 376 | 'title' => 'Foo', |
||
| 377 | 'year' => 1991, |
||
| 378 | ], |
||
| 379 | ], |
||
| 380 | [ |
||
| 381 | 'type' => 'books', |
||
| 382 | 'id' => '2', |
||
| 383 | 'attributes' => [ |
||
| 384 | 'title' => 'Bar', |
||
| 385 | 'year' => 1997, |
||
| 386 | ], |
||
| 387 | ], |
||
| 388 | ], |
||
| 389 | ]; |
||
| 390 | |||
| 391 | $this->assertSame($expected, $scope->toArray()); |
||
| 392 | |||
| 393 | $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997}}]}'; |
||
| 394 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 395 | } |
||
| 396 | |||
| 397 | View Code Duplication | public function testSerializingCollectionResourceWithHasOneInclude() |
|
| 398 | { |
||
| 399 | $this->manager->parseIncludes('author'); |
||
| 400 | |||
| 401 | $booksData = [ |
||
| 402 | [ |
||
| 403 | 'id' => 1, |
||
| 404 | 'title' => 'Foo', |
||
| 405 | 'year' => '1991', |
||
| 406 | '_author' => [ |
||
| 407 | 'id' => 1, |
||
| 408 | 'name' => 'Dave', |
||
| 409 | ], |
||
| 410 | ], |
||
| 411 | [ |
||
| 412 | 'id' => 2, |
||
| 413 | 'title' => 'Bar', |
||
| 414 | 'year' => '1997', |
||
| 415 | '_author' => [ |
||
| 416 | 'id' => 2, |
||
| 417 | 'name' => 'Bob', |
||
| 418 | ], |
||
| 419 | ], |
||
| 420 | ]; |
||
| 421 | |||
| 422 | $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
||
| 423 | $scope = new Scope($this->manager, $resource); |
||
| 424 | |||
| 425 | $expected = [ |
||
| 426 | 'data' => [ |
||
| 427 | [ |
||
| 428 | 'type' => 'books', |
||
| 429 | 'id' => '1', |
||
| 430 | 'attributes' => [ |
||
| 431 | 'title' => 'Foo', |
||
| 432 | 'year' => 1991, |
||
| 433 | ], |
||
| 434 | 'relationships' => [ |
||
| 435 | 'author' => [ |
||
| 436 | 'data' => [ |
||
| 437 | 'type' => 'people', |
||
| 438 | 'id' => '1', |
||
| 439 | ], |
||
| 440 | ], |
||
| 441 | ], |
||
| 442 | ], |
||
| 443 | [ |
||
| 444 | 'type' => 'books', |
||
| 445 | 'id' => '2', |
||
| 446 | 'attributes' => [ |
||
| 447 | 'title' => 'Bar', |
||
| 448 | 'year' => 1997, |
||
| 449 | ], |
||
| 450 | 'relationships' => [ |
||
| 451 | 'author' => [ |
||
| 452 | 'data' => [ |
||
| 453 | 'type' => 'people', |
||
| 454 | 'id' => '2', |
||
| 455 | ], |
||
| 456 | ], |
||
| 457 | ], |
||
| 458 | ], |
||
| 459 | ], |
||
| 460 | 'included' => [ |
||
| 461 | [ |
||
| 462 | 'type' => 'people', |
||
| 463 | 'id' => '1', |
||
| 464 | 'attributes' => [ |
||
| 465 | 'name' => 'Dave', |
||
| 466 | ], |
||
| 467 | ], |
||
| 468 | [ |
||
| 469 | 'type' => 'people', |
||
| 470 | 'id' => '2', |
||
| 471 | 'attributes' => [ |
||
| 472 | 'name' => 'Bob', |
||
| 473 | ], |
||
| 474 | ], |
||
| 475 | ], |
||
| 476 | ]; |
||
| 477 | |||
| 478 | $this->assertSame($expected, $scope->toArray()); |
||
| 479 | |||
| 480 | $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"}}]}'; |
||
| 481 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 482 | } |
||
| 483 | |||
| 484 | public function testSerializingCollectionResourceWithEmptyHasOneInclude() |
||
| 485 | { |
||
| 486 | $this->manager->parseIncludes('author'); |
||
| 487 | |||
| 488 | $booksData = [ |
||
| 489 | [ |
||
| 490 | 'id' => 1, |
||
| 491 | 'title' => 'Foo', |
||
| 492 | 'year' => '1991', |
||
| 493 | '_author' => null, |
||
| 494 | ], |
||
| 495 | [ |
||
| 496 | 'id' => 2, |
||
| 497 | 'title' => 'Bar', |
||
| 498 | 'year' => '1997', |
||
| 499 | '_author' => [ |
||
| 500 | 'id' => 2, |
||
| 501 | 'name' => 'Bob', |
||
| 502 | ], |
||
| 503 | ], |
||
| 504 | ]; |
||
| 505 | |||
| 506 | $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
||
| 507 | $scope = new Scope($this->manager, $resource); |
||
| 508 | |||
| 509 | $expected = [ |
||
| 510 | 'data' => [ |
||
| 511 | [ |
||
| 512 | 'type' => 'books', |
||
| 513 | 'id' => '1', |
||
| 514 | 'attributes' => [ |
||
| 515 | 'title' => 'Foo', |
||
| 516 | 'year' => 1991, |
||
| 517 | ], |
||
| 518 | 'relationships' => [ |
||
| 519 | 'author' => [ |
||
| 520 | 'data' => null, |
||
| 521 | ], |
||
| 522 | ], |
||
| 523 | ], |
||
| 524 | [ |
||
| 525 | 'type' => 'books', |
||
| 526 | 'id' => '2', |
||
| 527 | 'attributes' => [ |
||
| 528 | 'title' => 'Bar', |
||
| 529 | 'year' => 1997, |
||
| 530 | ], |
||
| 531 | 'relationships' => [ |
||
| 532 | 'author' => [ |
||
| 533 | 'data' => [ |
||
| 534 | 'type' => 'people', |
||
| 535 | 'id' => '2', |
||
| 536 | ], |
||
| 537 | ], |
||
| 538 | ], |
||
| 539 | ], |
||
| 540 | ], |
||
| 541 | 'included' => [ |
||
| 542 | [ |
||
| 543 | 'type' => 'people', |
||
| 544 | 'id' => '2', |
||
| 545 | 'attributes' => [ |
||
| 546 | 'name' => 'Bob', |
||
| 547 | ], |
||
| 548 | ], |
||
| 549 | ], |
||
| 550 | ]; |
||
| 551 | |||
| 552 | $this->assertSame($expected, $scope->toArray()); |
||
| 553 | |||
| 554 | $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"}}]}'; |
||
| 555 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 556 | } |
||
| 557 | |||
| 558 | public function testSerializingCollectionResourceWithHasManyInclude() |
||
| 559 | { |
||
| 560 | $this->manager->parseIncludes('published'); |
||
| 561 | |||
| 562 | $authorsData = [ |
||
| 563 | [ |
||
| 564 | 'id' => 1, |
||
| 565 | 'name' => 'Dave', |
||
| 566 | '_published' => [ |
||
| 567 | [ |
||
| 568 | 'id' => 1, |
||
| 569 | 'title' => 'Foo', |
||
| 570 | 'year' => '1991', |
||
| 571 | ], |
||
| 572 | [ |
||
| 573 | 'id' => 2, |
||
| 574 | 'title' => 'Bar', |
||
| 575 | 'year' => '2015', |
||
| 576 | ], |
||
| 577 | ], |
||
| 578 | ], |
||
| 579 | [ |
||
| 580 | 'id' => 2, |
||
| 581 | 'name' => 'Bob', |
||
| 582 | '_published' => [ |
||
| 583 | [ |
||
| 584 | 'id' => 3, |
||
| 585 | 'title' => 'Baz', |
||
| 586 | 'year' => '1995', |
||
| 587 | ], |
||
| 588 | [ |
||
| 589 | 'id' => 4, |
||
| 590 | 'title' => 'Quux', |
||
| 591 | 'year' => '2000', |
||
| 592 | ], |
||
| 593 | ], |
||
| 594 | ], |
||
| 595 | ]; |
||
| 596 | |||
| 597 | $resource = new Collection($authorsData, new JsonApiAuthorTransformer(), 'people'); |
||
| 598 | $scope = new Scope($this->manager, $resource); |
||
| 599 | |||
| 600 | $expected = [ |
||
| 601 | 'data' => [ |
||
| 602 | [ |
||
| 603 | 'type' => 'people', |
||
| 604 | 'id' => '1', |
||
| 605 | 'attributes' => [ |
||
| 606 | 'name' => 'Dave', |
||
| 607 | ], |
||
| 608 | 'relationships' => [ |
||
| 609 | 'published' => [ |
||
| 610 | 'data' => [ |
||
| 611 | [ |
||
| 612 | 'type' => 'books', |
||
| 613 | 'id' => 1, |
||
| 614 | ], |
||
| 615 | [ |
||
| 616 | 'type' => 'books', |
||
| 617 | 'id' => 2, |
||
| 618 | ], |
||
| 619 | ], |
||
| 620 | ], |
||
| 621 | ], |
||
| 622 | ], |
||
| 623 | [ |
||
| 624 | 'type' => 'people', |
||
| 625 | 'id' => '2', |
||
| 626 | 'attributes' => [ |
||
| 627 | 'name' => 'Bob', |
||
| 628 | ], |
||
| 629 | 'relationships' => [ |
||
| 630 | 'published' => [ |
||
| 631 | 'data' => [ |
||
| 632 | [ |
||
| 633 | 'type' => 'books', |
||
| 634 | 'id' => 3, |
||
| 635 | ], |
||
| 636 | [ |
||
| 637 | 'type' => 'books', |
||
| 638 | 'id' => 4, |
||
| 639 | ], |
||
| 640 | ], |
||
| 641 | ], |
||
| 642 | ], |
||
| 643 | ], |
||
| 644 | ], |
||
| 645 | 'included' => [ |
||
| 646 | [ |
||
| 647 | 'type' => 'books', |
||
| 648 | 'id' => '1', |
||
| 649 | 'attributes' => [ |
||
| 650 | 'title' => 'Foo', |
||
| 651 | 'year' => 1991, |
||
| 652 | ], |
||
| 653 | ], |
||
| 654 | [ |
||
| 655 | 'type' => 'books', |
||
| 656 | 'id' => '2', |
||
| 657 | 'attributes' => [ |
||
| 658 | 'title' => 'Bar', |
||
| 659 | 'year' => 2015, |
||
| 660 | ], |
||
| 661 | ], |
||
| 662 | [ |
||
| 663 | 'type' => 'books', |
||
| 664 | 'id' => '3', |
||
| 665 | 'attributes' => [ |
||
| 666 | 'title' => 'Baz', |
||
| 667 | 'year' => 1995, |
||
| 668 | ], |
||
| 669 | ], |
||
| 670 | [ |
||
| 671 | 'type' => 'books', |
||
| 672 | 'id' => '4', |
||
| 673 | 'attributes' => [ |
||
| 674 | 'title' => 'Quux', |
||
| 675 | 'year' => 2000, |
||
| 676 | ], |
||
| 677 | ], |
||
| 678 | ], |
||
| 679 | ]; |
||
| 680 | |||
| 681 | $this->assertEquals($expected, $scope->toArray()); |
||
| 682 | |||
| 683 | $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}}]}'; |
||
| 684 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 685 | } |
||
| 686 | |||
| 687 | public function testSerializingCollectionResourceWithEmptyHasManyInclude() |
||
| 688 | { |
||
| 689 | $this->manager->parseIncludes('published'); |
||
| 690 | |||
| 691 | $authorsData = [ |
||
| 692 | [ |
||
| 693 | 'id' => 1, |
||
| 694 | 'name' => 'Dave', |
||
| 695 | '_published' => [], |
||
| 696 | ], |
||
| 697 | [ |
||
| 698 | 'id' => 2, |
||
| 699 | 'name' => 'Bob', |
||
| 700 | '_published' => [ |
||
| 701 | [ |
||
| 702 | 'id' => 3, |
||
| 703 | 'title' => 'Baz', |
||
| 704 | 'year' => '1995', |
||
| 705 | ], |
||
| 706 | ], |
||
| 707 | ], |
||
| 708 | ]; |
||
| 709 | |||
| 710 | $resource = new Collection($authorsData, new JsonApiAuthorTransformer(), 'people'); |
||
| 711 | $scope = new Scope($this->manager, $resource); |
||
| 712 | |||
| 713 | $expected = [ |
||
| 714 | 'data' => [ |
||
| 715 | [ |
||
| 716 | 'type' => 'people', |
||
| 717 | 'id' => '1', |
||
| 718 | 'attributes' => [ |
||
| 719 | 'name' => 'Dave', |
||
| 720 | ], |
||
| 721 | 'relationships' => [ |
||
| 722 | 'published' => [ |
||
| 723 | 'data' => [], |
||
| 724 | ], |
||
| 725 | ], |
||
| 726 | ], |
||
| 727 | [ |
||
| 728 | 'type' => 'people', |
||
| 729 | 'id' => '2', |
||
| 730 | 'attributes' => [ |
||
| 731 | 'name' => 'Bob', |
||
| 732 | ], |
||
| 733 | 'relationships' => [ |
||
| 734 | 'published' => [ |
||
| 735 | 'data' => [ |
||
| 736 | [ |
||
| 737 | 'type' => 'books', |
||
| 738 | 'id' => 3, |
||
| 739 | ], |
||
| 740 | ], |
||
| 741 | ], |
||
| 742 | ], |
||
| 743 | ], |
||
| 744 | ], |
||
| 745 | 'included' => [ |
||
| 746 | [ |
||
| 747 | 'type' => 'books', |
||
| 748 | 'id' => '3', |
||
| 749 | 'attributes' => [ |
||
| 750 | 'title' => 'Baz', |
||
| 751 | 'year' => 1995, |
||
| 752 | ], |
||
| 753 | ], |
||
| 754 | ], |
||
| 755 | ]; |
||
| 756 | |||
| 757 | $this->assertEquals($expected, $scope->toArray()); |
||
| 758 | |||
| 759 | $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}}]}'; |
||
| 760 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 761 | } |
||
| 762 | |||
| 763 | View Code Duplication | public function testSerializingCollectionResourceWithMeta() |
|
| 764 | { |
||
| 765 | $booksData = [ |
||
| 766 | [ |
||
| 767 | 'id' => 1, |
||
| 768 | 'title' => 'Foo', |
||
| 769 | 'year' => '1991', |
||
| 770 | '_author' => [ |
||
| 771 | 'name' => 'Dave', |
||
| 772 | ], |
||
| 773 | ], |
||
| 774 | [ |
||
| 775 | 'id' => 2, |
||
| 776 | 'title' => 'Bar', |
||
| 777 | 'year' => '1997', |
||
| 778 | '_author' => [ |
||
| 779 | 'name' => 'Bob', |
||
| 780 | ], |
||
| 781 | ], |
||
| 782 | ]; |
||
| 783 | |||
| 784 | $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
||
| 785 | $resource->setMetaValue('foo', 'bar'); |
||
| 786 | |||
| 787 | $scope = new Scope($this->manager, $resource); |
||
| 788 | |||
| 789 | $expected = [ |
||
| 790 | 'data' => [ |
||
| 791 | [ |
||
| 792 | 'type' => 'books', |
||
| 793 | 'id' => '1', |
||
| 794 | 'attributes' => [ |
||
| 795 | 'title' => 'Foo', |
||
| 796 | 'year' => 1991, |
||
| 797 | ], |
||
| 798 | ], |
||
| 799 | [ |
||
| 800 | 'type' => 'books', |
||
| 801 | 'id' => '2', |
||
| 802 | 'attributes' => [ |
||
| 803 | 'title' => 'Bar', |
||
| 804 | 'year' => 1997, |
||
| 805 | ], |
||
| 806 | ], |
||
| 807 | ], |
||
| 808 | 'meta' => [ |
||
| 809 | 'foo' => 'bar', |
||
| 810 | ], |
||
| 811 | ]; |
||
| 812 | |||
| 813 | $this->assertSame($expected, $scope->toArray()); |
||
| 814 | |||
| 815 | $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997}}],"meta":{"foo":"bar"}}'; |
||
| 816 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 817 | } |
||
| 818 | |||
| 819 | public function testSerializingCollectionResourceWithDuplicatedIncludeData() |
||
| 820 | { |
||
| 821 | $this->manager->parseIncludes('author'); |
||
| 822 | |||
| 823 | $booksData = [ |
||
| 824 | [ |
||
| 825 | 'id' => 1, |
||
| 826 | 'title' => 'Foo', |
||
| 827 | 'year' => '1991', |
||
| 828 | '_author' => [ |
||
| 829 | 'id' => 1, |
||
| 830 | 'name' => 'Dave', |
||
| 831 | ], |
||
| 832 | ], |
||
| 833 | [ |
||
| 834 | 'id' => 2, |
||
| 835 | 'title' => 'Bar', |
||
| 836 | 'year' => '1997', |
||
| 837 | '_author' => [ |
||
| 838 | 'id' => 1, |
||
| 839 | 'name' => 'Dave', |
||
| 840 | ], |
||
| 841 | ], |
||
| 842 | ]; |
||
| 843 | |||
| 844 | $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
||
| 845 | $scope = new Scope($this->manager, $resource); |
||
| 846 | |||
| 847 | $expected = [ |
||
| 848 | 'data' => [ |
||
| 849 | [ |
||
| 850 | 'type' => 'books', |
||
| 851 | 'id' => '1', |
||
| 852 | 'attributes' => [ |
||
| 853 | 'title' => 'Foo', |
||
| 854 | 'year' => 1991, |
||
| 855 | ], |
||
| 856 | 'relationships' => [ |
||
| 857 | 'author' => [ |
||
| 858 | 'data' => [ |
||
| 859 | 'type' => 'people', |
||
| 860 | 'id' => '1', |
||
| 861 | ], |
||
| 862 | ], |
||
| 863 | ], |
||
| 864 | ], |
||
| 865 | [ |
||
| 866 | 'type' => 'books', |
||
| 867 | 'id' => '2', |
||
| 868 | 'attributes' => [ |
||
| 869 | 'title' => 'Bar', |
||
| 870 | 'year' => 1997, |
||
| 871 | ], |
||
| 872 | 'relationships' => [ |
||
| 873 | 'author' => [ |
||
| 874 | 'data' => [ |
||
| 875 | 'type' => 'people', |
||
| 876 | 'id' => '1', |
||
| 877 | ], |
||
| 878 | ], |
||
| 879 | ], |
||
| 880 | ], |
||
| 881 | ], |
||
| 882 | 'included' => [ |
||
| 883 | [ |
||
| 884 | 'type' => 'people', |
||
| 885 | 'id' => '1', |
||
| 886 | 'attributes' => [ |
||
| 887 | 'name' => 'Dave', |
||
| 888 | ], |
||
| 889 | ], |
||
| 890 | ], |
||
| 891 | ]; |
||
| 892 | |||
| 893 | $this->assertSame($expected, $scope->toArray()); |
||
| 894 | |||
| 895 | $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"}}]}'; |
||
| 896 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 897 | } |
||
| 898 | |||
| 899 | View Code Duplication | public function testSerializingItemResourceWithNestedIncludes() |
|
| 900 | { |
||
| 901 | $this->manager->parseIncludes(['author', 'author.published']); |
||
| 902 | |||
| 903 | $bookData = [ |
||
| 904 | 'id' => 1, |
||
| 905 | 'title' => 'Foo', |
||
| 906 | 'year' => '1991', |
||
| 907 | '_author' => [ |
||
| 908 | 'id' => 1, |
||
| 909 | 'name' => 'Dave', |
||
| 910 | '_published' => [ |
||
| 911 | [ |
||
| 912 | 'id' => 1, |
||
| 913 | 'title' => 'Foo', |
||
| 914 | 'year' => '1991', |
||
| 915 | ], |
||
| 916 | [ |
||
| 917 | 'id' => 2, |
||
| 918 | 'title' => 'Bar', |
||
| 919 | 'year' => '2015', |
||
| 920 | ], |
||
| 921 | ], |
||
| 922 | ], |
||
| 923 | ]; |
||
| 924 | |||
| 925 | $resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
||
| 926 | |||
| 927 | $scope = new Scope($this->manager, $resource); |
||
| 928 | |||
| 929 | $expected = [ |
||
| 930 | 'data' => [ |
||
| 931 | 'type' => 'books', |
||
| 932 | 'id' => '1', |
||
| 933 | 'attributes' => [ |
||
| 934 | 'title' => 'Foo', |
||
| 935 | 'year' => 1991, |
||
| 936 | ], |
||
| 937 | 'relationships' => [ |
||
| 938 | 'author' => [ |
||
| 939 | 'data' => [ |
||
| 940 | 'type' => 'people', |
||
| 941 | 'id' => '1', |
||
| 942 | ], |
||
| 943 | ], |
||
| 944 | ], |
||
| 945 | ], |
||
| 946 | 'included' => [ |
||
| 947 | [ |
||
| 948 | 'type' => 'books', |
||
| 949 | 'id' => '2', |
||
| 950 | 'attributes' => [ |
||
| 951 | 'title' => 'Bar', |
||
| 952 | 'year' => 2015, |
||
| 953 | ], |
||
| 954 | ], |
||
| 955 | [ |
||
| 956 | 'type' => 'people', |
||
| 957 | 'id' => '1', |
||
| 958 | 'attributes' => [ |
||
| 959 | 'name' => 'Dave', |
||
| 960 | ], |
||
| 961 | 'relationships' => [ |
||
| 962 | 'published' => [ |
||
| 963 | 'data' => [ |
||
| 964 | [ |
||
| 965 | 'type' => 'books', |
||
| 966 | 'id' => '1', |
||
| 967 | ], |
||
| 968 | [ |
||
| 969 | 'type' => 'books', |
||
| 970 | 'id' => '2', |
||
| 971 | ], |
||
| 972 | ], |
||
| 973 | ], |
||
| 974 | ], |
||
| 975 | ], |
||
| 976 | ], |
||
| 977 | ]; |
||
| 978 | |||
| 979 | $this->assertSame($expected, $scope->toArray()); |
||
| 980 | |||
| 981 | $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"}]}}}]}'; |
||
| 982 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 983 | } |
||
| 984 | |||
| 985 | View Code Duplication | public function testSerializingItemResourceWithSelfLink() |
|
| 986 | { |
||
| 987 | $baseUrl = 'http://example.com'; |
||
| 988 | $this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
||
| 989 | |||
| 990 | $bookData = [ |
||
| 991 | 'id' => 1, |
||
| 992 | 'title' => 'Foo', |
||
| 993 | 'year' => '1991', |
||
| 994 | '_author' => [ |
||
| 995 | 'id' => 1, |
||
| 996 | 'name' => 'Dave', |
||
| 997 | ], |
||
| 998 | ]; |
||
| 999 | |||
| 1000 | $resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
||
| 1001 | |||
| 1002 | $scope = new Scope($this->manager, $resource); |
||
| 1003 | |||
| 1004 | $expected = [ |
||
| 1005 | 'data' => [ |
||
| 1006 | 'type' => 'books', |
||
| 1007 | 'id' => '1', |
||
| 1008 | 'attributes' => [ |
||
| 1009 | 'title' => 'Foo', |
||
| 1010 | 'year' => 1991, |
||
| 1011 | ], |
||
| 1012 | 'links' => [ |
||
| 1013 | 'self' => 'http://example.com/books/1', |
||
| 1014 | ], |
||
| 1015 | ], |
||
| 1016 | ]; |
||
| 1017 | |||
| 1018 | $this->assertSame($expected, $scope->toArray()); |
||
| 1019 | |||
| 1020 | $expectedJson = '{"data":{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"}}}'; |
||
| 1021 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 1022 | } |
||
| 1023 | |||
| 1024 | View Code Duplication | public function testSerializingCollectionResourceWithSelfLink() |
|
| 1025 | { |
||
| 1026 | $baseUrl = 'http://example.com'; |
||
| 1027 | $this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
||
| 1028 | |||
| 1029 | $booksData = [ |
||
| 1030 | [ |
||
| 1031 | 'id' => 1, |
||
| 1032 | 'title' => 'Foo', |
||
| 1033 | 'year' => '1991', |
||
| 1034 | '_author' => [ |
||
| 1035 | 'id' => 1, |
||
| 1036 | 'name' => 'Dave', |
||
| 1037 | ], |
||
| 1038 | ], |
||
| 1039 | [ |
||
| 1040 | 'id' => 2, |
||
| 1041 | 'title' => 'Bar', |
||
| 1042 | 'year' => '1997', |
||
| 1043 | '_author' => [ |
||
| 1044 | 'id' => 2, |
||
| 1045 | 'name' => 'Bob', |
||
| 1046 | ], |
||
| 1047 | ], |
||
| 1048 | ]; |
||
| 1049 | |||
| 1050 | $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
||
| 1051 | $scope = new Scope($this->manager, $resource); |
||
| 1052 | |||
| 1053 | $expected = [ |
||
| 1054 | 'data' => [ |
||
| 1055 | [ |
||
| 1056 | 'type' => 'books', |
||
| 1057 | 'id' => '1', |
||
| 1058 | 'attributes' => [ |
||
| 1059 | 'title' => 'Foo', |
||
| 1060 | 'year' => 1991, |
||
| 1061 | ], |
||
| 1062 | 'links' => [ |
||
| 1063 | 'self' => 'http://example.com/books/1', |
||
| 1064 | ], |
||
| 1065 | ], |
||
| 1066 | [ |
||
| 1067 | 'type' => 'books', |
||
| 1068 | 'id' => '2', |
||
| 1069 | 'attributes' => [ |
||
| 1070 | 'title' => 'Bar', |
||
| 1071 | 'year' => 1997, |
||
| 1072 | ], |
||
| 1073 | 'links' => [ |
||
| 1074 | 'self' => 'http://example.com/books/2', |
||
| 1075 | ], |
||
| 1076 | ], |
||
| 1077 | ], |
||
| 1078 | ]; |
||
| 1079 | |||
| 1080 | $this->assertSame($expected, $scope->toArray()); |
||
| 1081 | |||
| 1082 | $expectedJson = '{"data":[{"type":"books","id":"1","attributes":{"title":"Foo","year":1991},"links":{"self":"http:\/\/example.com\/books\/1"}},{"type":"books","id":"2","attributes":{"title":"Bar","year":1997},"links":{"self":"http:\/\/example.com\/books\/2"}}]}'; |
||
| 1083 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 1084 | } |
||
| 1085 | |||
| 1086 | View Code Duplication | public function testSerializingItemResourceWithLinksForHasOneRelationship() |
|
| 1087 | { |
||
| 1088 | $baseUrl = 'http://example.com'; |
||
| 1089 | $this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
||
| 1090 | $this->manager->parseIncludes('author'); |
||
| 1091 | |||
| 1092 | $bookData = [ |
||
| 1093 | 'id' => 1, |
||
| 1094 | 'title' => 'Foo', |
||
| 1095 | 'year' => '1991', |
||
| 1096 | '_author' => [ |
||
| 1097 | 'id' => 1, |
||
| 1098 | 'name' => 'Dave', |
||
| 1099 | ], |
||
| 1100 | ]; |
||
| 1101 | |||
| 1102 | $resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
||
| 1103 | |||
| 1104 | $scope = new Scope($this->manager, $resource); |
||
| 1105 | |||
| 1106 | $expected = [ |
||
| 1107 | 'data' => [ |
||
| 1108 | 'type' => 'books', |
||
| 1109 | 'id' => '1', |
||
| 1110 | 'attributes' => [ |
||
| 1111 | 'title' => 'Foo', |
||
| 1112 | 'year' => 1991, |
||
| 1113 | ], |
||
| 1114 | 'relationships' => [ |
||
| 1115 | 'author' => [ |
||
| 1116 | 'links' => [ |
||
| 1117 | 'self' => 'http://example.com/books/1/relationships/author', |
||
| 1118 | 'related' => 'http://example.com/books/1/author', |
||
| 1119 | ], |
||
| 1120 | 'data' => [ |
||
| 1121 | 'type' => 'people', |
||
| 1122 | 'id' => '1', |
||
| 1123 | ], |
||
| 1124 | ], |
||
| 1125 | ], |
||
| 1126 | 'links' => [ |
||
| 1127 | 'self' => 'http://example.com/books/1', |
||
| 1128 | ], |
||
| 1129 | ], |
||
| 1130 | 'included' => [ |
||
| 1131 | [ |
||
| 1132 | 'type' => 'people', |
||
| 1133 | 'id' => '1', |
||
| 1134 | 'attributes' => [ |
||
| 1135 | 'name' => 'Dave', |
||
| 1136 | ], |
||
| 1137 | 'links' => [ |
||
| 1138 | 'self' => 'http://example.com/people/1', |
||
| 1139 | ], |
||
| 1140 | ], |
||
| 1141 | ], |
||
| 1142 | ]; |
||
| 1143 | |||
| 1144 | $this->assertEquals($expected, $scope->toArray()); |
||
| 1145 | |||
| 1146 | $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"}}}},"included":[{"type":"people","id":"1","attributes":{"name":"Dave"},"links":{"self":"http:\/\/example.com\/people\/1"}}]}'; |
||
| 1147 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 1148 | } |
||
| 1149 | |||
| 1150 | public function testSerializingItemResourceWithLinksForHasManyRelationship() |
||
| 1151 | { |
||
| 1152 | $baseUrl = 'http://example.com'; |
||
| 1153 | $this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
||
| 1154 | $this->manager->parseIncludes('published'); |
||
| 1155 | |||
| 1156 | $authorData = [ |
||
| 1157 | 'id' => 1, |
||
| 1158 | 'name' => 'Dave', |
||
| 1159 | '_published' => [ |
||
| 1160 | [ |
||
| 1161 | 'id' => 1, |
||
| 1162 | 'title' => 'Foo', |
||
| 1163 | 'year' => '1991', |
||
| 1164 | ], |
||
| 1165 | [ |
||
| 1166 | 'id' => 2, |
||
| 1167 | 'title' => 'Bar', |
||
| 1168 | 'year' => '2015', |
||
| 1169 | ], |
||
| 1170 | ], |
||
| 1171 | ]; |
||
| 1172 | |||
| 1173 | $resource = new Item($authorData, new JsonApiAuthorTransformer(), 'people'); |
||
| 1174 | |||
| 1175 | $scope = new Scope($this->manager, $resource); |
||
| 1176 | |||
| 1177 | $expected = [ |
||
| 1178 | 'data' => [ |
||
| 1179 | 'type' => 'people', |
||
| 1180 | 'id' => '1', |
||
| 1181 | 'attributes' => [ |
||
| 1182 | 'name' => 'Dave', |
||
| 1183 | ], |
||
| 1184 | 'relationships' => [ |
||
| 1185 | 'published' => [ |
||
| 1186 | 'links' => [ |
||
| 1187 | 'self' => 'http://example.com/people/1/relationships/published', |
||
| 1188 | 'related' => 'http://example.com/people/1/published', |
||
| 1189 | ], |
||
| 1190 | 'data' => [ |
||
| 1191 | [ |
||
| 1192 | 'type' => 'books', |
||
| 1193 | 'id' => 1, |
||
| 1194 | ], |
||
| 1195 | [ |
||
| 1196 | 'type' => 'books', |
||
| 1197 | 'id' => 2, |
||
| 1198 | ], |
||
| 1199 | ], |
||
| 1200 | ], |
||
| 1201 | ], |
||
| 1202 | 'links' => [ |
||
| 1203 | 'self' => 'http://example.com/people/1', |
||
| 1204 | ], |
||
| 1205 | ], |
||
| 1206 | 'included' => [ |
||
| 1207 | [ |
||
| 1208 | 'type' => 'books', |
||
| 1209 | 'id' => '1', |
||
| 1210 | 'attributes' => [ |
||
| 1211 | 'title' => 'Foo', |
||
| 1212 | 'year' => 1991, |
||
| 1213 | ], |
||
| 1214 | 'links' => [ |
||
| 1215 | 'self' => 'http://example.com/books/1', |
||
| 1216 | ], |
||
| 1217 | ], |
||
| 1218 | [ |
||
| 1219 | 'type' => 'books', |
||
| 1220 | 'id' => '2', |
||
| 1221 | 'attributes' => [ |
||
| 1222 | 'title' => 'Bar', |
||
| 1223 | 'year' => 2015, |
||
| 1224 | ], |
||
| 1225 | 'links' => [ |
||
| 1226 | 'self' => 'http://example.com/books/2', |
||
| 1227 | ], |
||
| 1228 | ], |
||
| 1229 | ], |
||
| 1230 | ]; |
||
| 1231 | |||
| 1232 | $this->assertEquals($expected, $scope->toArray()); |
||
| 1233 | |||
| 1234 | $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"}},{"type":"books","id":"2","attributes":{"title":"Bar","year":2015},"links":{"self":"http:\/\/example.com\/books\/2"}}]}'; |
||
| 1235 | $this->assertSame($expectedJson, $scope->toJson()); |
||
| 1236 | } |
||
| 1237 | |||
| 1238 | public function testSerializingCollectionResourceWithLinksForHasOneRelationship() |
||
| 1337 | |||
| 1338 | public function testSerializingCollectionResourceWithLinksForHasManyRelationship() |
||
| 1473 | |||
| 1474 | /** |
||
| 1475 | * @expectedException InvalidArgumentException |
||
| 1476 | * @expectedExceptionMessage JSON API resource objects MUST have a valid id |
||
| 1477 | */ |
||
| 1478 | public function testExceptionThrownIfResourceHasNoId() |
||
| 1479 | { |
||
| 1480 | $bookData = [ |
||
| 1481 | 'title' => 'Foo', |
||
| 1482 | 'year' => '1991', |
||
| 1483 | ]; |
||
| 1484 | |||
| 1485 | $resource = new Item($bookData, new JsonApiBookTransformer(), 'books'); |
||
| 1486 | |||
| 1487 | $scope = new Scope($this->manager, $resource); |
||
| 1488 | $scope->toArray(); |
||
| 1489 | } |
||
| 1490 | |||
| 1491 | public function testSerializingItemWithReferenceToRootObject() |
||
| 1492 | { |
||
| 1493 | $this->manager->parseIncludes('published.author'); |
||
| 1494 | |||
| 1569 | |||
| 1570 | public function testSerializingCollectionWithReferenceToRootObjects() |
||
| 1681 | |||
| 1682 | public function testSerializingCollectionResourceWithPaginator() |
||
| 1782 | |||
| 1783 | View Code Duplication | public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailablePreviousLink() |
|
| 1880 | |||
| 1881 | View Code Duplication | public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailableNextLink() |
|
| 1978 | |||
| 1979 | public function testCustomLinkMerge() |
||
| 2018 | |||
| 2019 | public function testCustomLinkMergeNoLink() |
||
| 2054 | |||
| 2055 | public function tearDown() |
||
| 2059 | } |
||
| 2060 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.