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() |
||
| 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() |
|
| 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 | ]; |
||
| 1023 | |||
| 1024 | View Code Duplication | public function testSerializingCollectionResourceWithSelfLink() |
|
| 1085 | |||
| 1086 | View Code Duplication | public function testSerializingItemResourceWithLinksForHasOneRelationship() |
|
| 1149 | |||
| 1150 | public function testSerializingItemResourceWithLinksForHasManyRelationship() |
||
| 1237 | |||
| 1238 | /** |
||
| 1239 | * @expectedException InvalidArgumentException |
||
| 1240 | * @expectedExceptionMessage JSON API resource objects MUST have a valid id |
||
| 1241 | */ |
||
| 1242 | public function testExceptionThrownIfResourceHasNoId() |
||
| 1254 | |||
| 1255 | public function testSerializingItemWithReferenceToRootObject() |
||
| 1333 | |||
| 1334 | public function testSerializingCollectionWithReferenceToRootObjects() |
||
| 1445 | |||
| 1446 | public function testSerializingCollectionResourceWithPaginator() |
||
| 1546 | |||
| 1547 | View Code Duplication | public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailablePreviousLink() |
|
| 1644 | |||
| 1645 | View Code Duplication | public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailableNextLink() |
|
| 1742 | |||
| 1743 | /** |
||
| 1744 | * @dataProvider serializingWithFieldsetsProvider |
||
| 1745 | */ |
||
| 1746 | public function testSerializingWithFieldsets($fieldsetsToParse, $expected) |
||
| 1779 | |||
| 1780 | public function serializingWithFieldsetsProvider() |
||
| 1893 | |||
| 1894 | public function tearDown() |
||
| 1898 | } |
||
| 1899 |