Code Duplication    Length = 137-137 lines in 2 locations

test/JsonApiSerializerTest.php 2 locations

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