Code Duplication    Length = 137-137 lines in 2 locations

test/Serializer/JsonApiSerializerTest.php 2 locations

@@ 2179-2315 (lines=137) @@
2176
        $this->assertSame($expectedJson, $scope->toJson());
2177
    }
2178
2179
    public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailablePreviousLink()
2180
    {
2181
        $baseUrl = 'http://example.com';
2182
2183
        $this->manager->setSerializer(new JsonApiSerializer($baseUrl));
2184
2185
        $total = 10;
2186
        $count = 2;
2187
        $perPage = 2;
2188
        $currentPage = 1;
2189
        $lastPage = 5;
2190
        $currentUrl = 'http://example.com/books/?page=1';
2191
        $nextUrl = 'http://example.com/books/?page=2';
2192
        $lastUrl = 'http://example.com/books/?page=5';
2193
2194
        $paginator = Mockery::mock('League\Fractal\Pagination\PaginatorInterface');
2195
        $paginator->shouldReceive('getCurrentPage')->andReturn($currentPage);
2196
        $paginator->shouldReceive('getLastPage')->andReturn($lastPage);
2197
        $paginator->shouldReceive('getTotal')->andReturn($total);
2198
        $paginator->shouldReceive('getCount')->andReturn($count);
2199
        $paginator->shouldReceive('getPerPage')->andReturn($perPage);
2200
        $paginator->shouldReceive('getUrl')->with(1)->andReturn($currentUrl);
2201
        $paginator->shouldReceive('getUrl')->with(2)->andReturn($nextUrl);
2202
        $paginator->shouldReceive('getUrl')->with(5)->andReturn($lastUrl);
2203
2204
        $booksData = [
2205
            [
2206
                'id' => 1,
2207
                'title' => 'Foo',
2208
                'year' => '1991',
2209
                '_author' => [
2210
                    'id' => 1,
2211
                    'name' => 'Dave',
2212
                ],
2213
            ],
2214
            [
2215
                'id' => 2,
2216
                'title' => 'Bar',
2217
                'year' => '1997',
2218
                '_author' => [
2219
                    'id' => 2,
2220
                    'name' => 'Bob',
2221
                ],
2222
            ],
2223
        ];
2224
2225
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
2226
        $resource->setPaginator($paginator);
2227
        $scope = new Scope($this->manager, $resource);
2228
2229
        $expected = [
2230
            'data' => [
2231
                [
2232
                    'type' => 'books',
2233
                    'id' => '1',
2234
                    'attributes' => [
2235
                        'title' => 'Foo',
2236
                        'year' => 1991,
2237
                    ],
2238
                    'links' => [
2239
                        'self' => 'http://example.com/books/1',
2240
                    ],
2241
                    'relationships' => [
2242
                        'author' => [
2243
                            'links' => [
2244
                                'self' => 'http://example.com/books/1/relationships/author',
2245
                                'related' => 'http://example.com/books/1/author',
2246
                            ],
2247
                        ],
2248
                        'co-author' => [
2249
                            'links' => [
2250
                                'self' => 'http://example.com/books/1/relationships/co-author',
2251
                                'related' => 'http://example.com/books/1/co-author'
2252
                            ],
2253
                        ],
2254
                        'author-with-meta' => [
2255
                            'links' => [
2256
                                'self' => 'http://example.com/books/1/relationships/author-with-meta',
2257
                                'related' => 'http://example.com/books/1/author-with-meta'
2258
                            ],
2259
                        ],
2260
                    ],
2261
                ],
2262
                [
2263
                    'type' => 'books',
2264
                    'id' => '2',
2265
                    'attributes' => [
2266
                        'title' => 'Bar',
2267
                        'year' => 1997,
2268
                    ],
2269
                    'links' => [
2270
                        'self' => 'http://example.com/books/2',
2271
                    ],
2272
                    'relationships' => [
2273
                        'author' => [
2274
                            'links' => [
2275
                                'self' => 'http://example.com/books/2/relationships/author',
2276
                                'related' => 'http://example.com/books/2/author',
2277
                            ],
2278
                        ],
2279
                        'co-author' => [
2280
                            'links' => [
2281
                                'self' => 'http://example.com/books/2/relationships/co-author',
2282
                                'related' => 'http://example.com/books/2/co-author'
2283
                            ],
2284
                        ],
2285
                        'author-with-meta' => [
2286
                            'links' => [
2287
                                'self' => 'http://example.com/books/2/relationships/author-with-meta',
2288
                                'related' => 'http://example.com/books/2/author-with-meta'
2289
                            ],
2290
                        ],
2291
                    ],
2292
                ],
2293
            ],
2294
            'meta' => [
2295
                'pagination' => [
2296
                    'total' =>  10,
2297
                    'count' => 2,
2298
                    'per_page' => 2,
2299
                    'current_page' => 1,
2300
                    'total_pages' => 5
2301
                ]
2302
            ],
2303
            'links' => [
2304
                'self' => 'http://example.com/books/?page=1',
2305
                'first' => 'http://example.com/books/?page=1',
2306
                'next' => 'http://example.com/books/?page=2',
2307
                'last' => 'http://example.com/books/?page=5'
2308
            ]
2309
        ];
2310
2311
        $this->assertSame($expected, $scope->toArray());
2312
2313
        $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"}}}}],"meta":{"pagination":{"total":10,"count":2,"per_page":2,"current_page":1,"total_pages":5}},"links":{"self":"http:\/\/example.com\/books\/?page=1","first":"http:\/\/example.com\/books\/?page=1","next":"http:\/\/example.com\/books\/?page=2","last":"http:\/\/example.com\/books\/?page=5"}}';
2314
        $this->assertSame($expectedJson, $scope->toJson());
2315
    }
2316
2317
    public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailableNextLink()
2318
    {
@@ 2317-2453 (lines=137) @@
2314
        $this->assertSame($expectedJson, $scope->toJson());
2315
    }
2316
2317
    public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailableNextLink()
2318
    {
2319
        $baseUrl = 'http://example.com';
2320
2321
        $this->manager->setSerializer(new JsonApiSerializer($baseUrl));
2322
2323
        $total = 10;
2324
        $count = 2;
2325
        $perPage = 2;
2326
        $currentPage = 5;
2327
        $lastPage = 5;
2328
        $firstUrl = 'http://example.com/books/?page=1';
2329
        $previousUrl = 'http://example.com/books/?page=4';
2330
        $lastUrl = 'http://example.com/books/?page=5';
2331
2332
        $paginator = Mockery::mock('League\Fractal\Pagination\PaginatorInterface');
2333
        $paginator->shouldReceive('getCurrentPage')->andReturn($currentPage);
2334
        $paginator->shouldReceive('getLastPage')->andReturn($lastPage);
2335
        $paginator->shouldReceive('getTotal')->andReturn($total);
2336
        $paginator->shouldReceive('getCount')->andReturn($count);
2337
        $paginator->shouldReceive('getPerPage')->andReturn($perPage);
2338
        $paginator->shouldReceive('getUrl')->with(1)->andReturn($firstUrl);
2339
        $paginator->shouldReceive('getUrl')->with(4)->andReturn($previousUrl);
2340
        $paginator->shouldReceive('getUrl')->with(5)->andReturn($lastUrl);
2341
2342
        $booksData = [
2343
            [
2344
                'id' => 1,
2345
                'title' => 'Foo',
2346
                'year' => '1991',
2347
                '_author' => [
2348
                    'id' => 1,
2349
                    'name' => 'Dave',
2350
                ],
2351
            ],
2352
            [
2353
                'id' => 2,
2354
                'title' => 'Bar',
2355
                'year' => '1997',
2356
                '_author' => [
2357
                    'id' => 2,
2358
                    'name' => 'Bob',
2359
                ],
2360
            ],
2361
        ];
2362
2363
        $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books');
2364
        $resource->setPaginator($paginator);
2365
        $scope = new Scope($this->manager, $resource);
2366
2367
        $expected = [
2368
            'data' => [
2369
                [
2370
                    'type' => 'books',
2371
                    'id' => '1',
2372
                    'attributes' => [
2373
                        'title' => 'Foo',
2374
                        'year' => 1991,
2375
                    ],
2376
                    'links' => [
2377
                        'self' => 'http://example.com/books/1',
2378
                    ],
2379
                    'relationships' => [
2380
                        'author' => [
2381
                            'links' => [
2382
                                'self' => 'http://example.com/books/1/relationships/author',
2383
                                'related' => 'http://example.com/books/1/author',
2384
                            ],
2385
                        ],
2386
                        'co-author' => [
2387
                            'links' => [
2388
                                'self' => 'http://example.com/books/1/relationships/co-author',
2389
                                'related' => 'http://example.com/books/1/co-author'
2390
                            ],
2391
                        ],
2392
                        'author-with-meta' => [
2393
                            'links' => [
2394
                                'self' => 'http://example.com/books/1/relationships/author-with-meta',
2395
                                'related' => 'http://example.com/books/1/author-with-meta'
2396
                            ],
2397
                        ],
2398
                    ],
2399
                ],
2400
                [
2401
                    'type' => 'books',
2402
                    'id' => '2',
2403
                    'attributes' => [
2404
                        'title' => 'Bar',
2405
                        'year' => 1997,
2406
                    ],
2407
                    'links' => [
2408
                        'self' => 'http://example.com/books/2',
2409
                    ],
2410
                    'relationships' => [
2411
                        'author' => [
2412
                            'links' => [
2413
                                'self' => 'http://example.com/books/2/relationships/author',
2414
                                'related' => 'http://example.com/books/2/author',
2415
                            ],
2416
                        ],
2417
                        'co-author' => [
2418
                            'links' => [
2419
                                'self' => 'http://example.com/books/2/relationships/co-author',
2420
                                'related' => 'http://example.com/books/2/co-author'
2421
                            ],
2422
                        ],
2423
                        'author-with-meta' => [
2424
                            'links' => [
2425
                                'self' => 'http://example.com/books/2/relationships/author-with-meta',
2426
                                'related' => 'http://example.com/books/2/author-with-meta'
2427
                            ],
2428
                        ],
2429
                    ],
2430
                ],
2431
            ],
2432
            'meta' => [
2433
                'pagination' => [
2434
                    'total' =>  10,
2435
                    'count' => 2,
2436
                    'per_page' => 2,
2437
                    'current_page' => 5,
2438
                    'total_pages' => 5
2439
                ]
2440
            ],
2441
            'links' => [
2442
                'self' => 'http://example.com/books/?page=5',
2443
                'first' => 'http://example.com/books/?page=1',
2444
                'prev' => 'http://example.com/books/?page=4',
2445
                'last' => 'http://example.com/books/?page=5'
2446
            ]
2447
        ];
2448
2449
        $this->assertSame($expected, $scope->toArray());
2450
2451
        $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"}}}}],"meta":{"pagination":{"total":10,"count":2,"per_page":2,"current_page":5,"total_pages":5}},"links":{"self":"http:\/\/example.com\/books\/?page=5","first":"http:\/\/example.com\/books\/?page=1","prev":"http:\/\/example.com\/books\/?page=4","last":"http:\/\/example.com\/books\/?page=5"}}';
2452
        $this->assertSame($expectedJson, $scope->toJson());
2453
    }
2454
2455
    public function testCustomLinkMerge()
2456
    {