Code Duplication    Length = 137-137 lines in 2 locations

test/JsonApiSerializerTest.php 2 locations

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