@@ 1943-2067 (lines=125) @@ | ||
1940 | $this->assertSame($expectedJson, $scope->toJson()); |
|
1941 | } |
|
1942 | ||
1943 | public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailablePreviousLink() |
|
1944 | { |
|
1945 | $baseUrl = 'http://example.com'; |
|
1946 | ||
1947 | $this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
|
1948 | ||
1949 | $total = 10; |
|
1950 | $count = 2; |
|
1951 | $perPage = 2; |
|
1952 | $currentPage = 1; |
|
1953 | $lastPage = 5; |
|
1954 | $currentUrl = 'http://example.com/books/?page=1'; |
|
1955 | $nextUrl = 'http://example.com/books/?page=2'; |
|
1956 | $lastUrl = 'http://example.com/books/?page=5'; |
|
1957 | ||
1958 | $paginator = Mockery::mock('League\Fractal\Pagination\PaginatorInterface'); |
|
1959 | $paginator->shouldReceive('getCurrentPage')->andReturn($currentPage); |
|
1960 | $paginator->shouldReceive('getLastPage')->andReturn($lastPage); |
|
1961 | $paginator->shouldReceive('getTotal')->andReturn($total); |
|
1962 | $paginator->shouldReceive('getCount')->andReturn($count); |
|
1963 | $paginator->shouldReceive('getPerPage')->andReturn($perPage); |
|
1964 | $paginator->shouldReceive('getUrl')->with(1)->andReturn($currentUrl); |
|
1965 | $paginator->shouldReceive('getUrl')->with(2)->andReturn($nextUrl); |
|
1966 | $paginator->shouldReceive('getUrl')->with(5)->andReturn($lastUrl); |
|
1967 | ||
1968 | $booksData = [ |
|
1969 | [ |
|
1970 | 'id' => 1, |
|
1971 | 'title' => 'Foo', |
|
1972 | 'year' => '1991', |
|
1973 | '_author' => [ |
|
1974 | 'id' => 1, |
|
1975 | 'name' => 'Dave', |
|
1976 | ], |
|
1977 | ], |
|
1978 | [ |
|
1979 | 'id' => 2, |
|
1980 | 'title' => 'Bar', |
|
1981 | 'year' => '1997', |
|
1982 | '_author' => [ |
|
1983 | 'id' => 2, |
|
1984 | 'name' => 'Bob', |
|
1985 | ], |
|
1986 | ], |
|
1987 | ]; |
|
1988 | ||
1989 | $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
|
1990 | $resource->setPaginator($paginator); |
|
1991 | $scope = new Scope($this->manager, $resource); |
|
1992 | ||
1993 | $expected = [ |
|
1994 | 'data' => [ |
|
1995 | [ |
|
1996 | 'type' => 'books', |
|
1997 | 'id' => '1', |
|
1998 | 'attributes' => [ |
|
1999 | 'title' => 'Foo', |
|
2000 | 'year' => 1991, |
|
2001 | ], |
|
2002 | 'links' => [ |
|
2003 | 'self' => 'http://example.com/books/1', |
|
2004 | ], |
|
2005 | 'relationships' => [ |
|
2006 | 'author' => [ |
|
2007 | 'links' => [ |
|
2008 | 'self' => 'http://example.com/books/1/relationships/author', |
|
2009 | 'related' => 'http://example.com/books/1/author', |
|
2010 | ], |
|
2011 | ], |
|
2012 | 'co-author' => [ |
|
2013 | 'links' => [ |
|
2014 | 'self' => 'http://example.com/books/1/relationships/co-author', |
|
2015 | 'related' => 'http://example.com/books/1/co-author' |
|
2016 | ], |
|
2017 | ], |
|
2018 | ], |
|
2019 | ], |
|
2020 | [ |
|
2021 | 'type' => 'books', |
|
2022 | 'id' => '2', |
|
2023 | 'attributes' => [ |
|
2024 | 'title' => 'Bar', |
|
2025 | 'year' => 1997, |
|
2026 | ], |
|
2027 | 'links' => [ |
|
2028 | 'self' => 'http://example.com/books/2', |
|
2029 | ], |
|
2030 | 'relationships' => [ |
|
2031 | 'author' => [ |
|
2032 | 'links' => [ |
|
2033 | 'self' => 'http://example.com/books/2/relationships/author', |
|
2034 | 'related' => 'http://example.com/books/2/author', |
|
2035 | ], |
|
2036 | ], |
|
2037 | 'co-author' => [ |
|
2038 | 'links' => [ |
|
2039 | 'self' => 'http://example.com/books/2/relationships/co-author', |
|
2040 | 'related' => 'http://example.com/books/2/co-author' |
|
2041 | ], |
|
2042 | ], |
|
2043 | ], |
|
2044 | ], |
|
2045 | ], |
|
2046 | 'meta' => [ |
|
2047 | 'pagination' => [ |
|
2048 | 'total' => 10, |
|
2049 | 'count' => 2, |
|
2050 | 'per_page' => 2, |
|
2051 | 'current_page' => 1, |
|
2052 | 'total_pages' => 5 |
|
2053 | ] |
|
2054 | ], |
|
2055 | 'links' => [ |
|
2056 | 'self' => 'http://example.com/books/?page=1', |
|
2057 | 'first' => 'http://example.com/books/?page=1', |
|
2058 | 'next' => 'http://example.com/books/?page=2', |
|
2059 | 'last' => 'http://example.com/books/?page=5' |
|
2060 | ] |
|
2061 | ]; |
|
2062 | ||
2063 | $this->assertSame($expected, $scope->toArray()); |
|
2064 | ||
2065 | $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"}}}},{"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"}}}}],"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"}}'; |
|
2066 | $this->assertSame($expectedJson, $scope->toJson()); |
|
2067 | } |
|
2068 | ||
2069 | public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailableNextLink() |
|
2070 | { |
|
@@ 2069-2193 (lines=125) @@ | ||
2066 | $this->assertSame($expectedJson, $scope->toJson()); |
|
2067 | } |
|
2068 | ||
2069 | public function testSerializingCollectionResourceWithPaginatorWithOmittedUnavailableNextLink() |
|
2070 | { |
|
2071 | $baseUrl = 'http://example.com'; |
|
2072 | ||
2073 | $this->manager->setSerializer(new JsonApiSerializer($baseUrl)); |
|
2074 | ||
2075 | $total = 10; |
|
2076 | $count = 2; |
|
2077 | $perPage = 2; |
|
2078 | $currentPage = 5; |
|
2079 | $lastPage = 5; |
|
2080 | $firstUrl = 'http://example.com/books/?page=1'; |
|
2081 | $previousUrl = 'http://example.com/books/?page=4'; |
|
2082 | $lastUrl = 'http://example.com/books/?page=5'; |
|
2083 | ||
2084 | $paginator = Mockery::mock('League\Fractal\Pagination\PaginatorInterface'); |
|
2085 | $paginator->shouldReceive('getCurrentPage')->andReturn($currentPage); |
|
2086 | $paginator->shouldReceive('getLastPage')->andReturn($lastPage); |
|
2087 | $paginator->shouldReceive('getTotal')->andReturn($total); |
|
2088 | $paginator->shouldReceive('getCount')->andReturn($count); |
|
2089 | $paginator->shouldReceive('getPerPage')->andReturn($perPage); |
|
2090 | $paginator->shouldReceive('getUrl')->with(1)->andReturn($firstUrl); |
|
2091 | $paginator->shouldReceive('getUrl')->with(4)->andReturn($previousUrl); |
|
2092 | $paginator->shouldReceive('getUrl')->with(5)->andReturn($lastUrl); |
|
2093 | ||
2094 | $booksData = [ |
|
2095 | [ |
|
2096 | 'id' => 1, |
|
2097 | 'title' => 'Foo', |
|
2098 | 'year' => '1991', |
|
2099 | '_author' => [ |
|
2100 | 'id' => 1, |
|
2101 | 'name' => 'Dave', |
|
2102 | ], |
|
2103 | ], |
|
2104 | [ |
|
2105 | 'id' => 2, |
|
2106 | 'title' => 'Bar', |
|
2107 | 'year' => '1997', |
|
2108 | '_author' => [ |
|
2109 | 'id' => 2, |
|
2110 | 'name' => 'Bob', |
|
2111 | ], |
|
2112 | ], |
|
2113 | ]; |
|
2114 | ||
2115 | $resource = new Collection($booksData, new JsonApiBookTransformer(), 'books'); |
|
2116 | $resource->setPaginator($paginator); |
|
2117 | $scope = new Scope($this->manager, $resource); |
|
2118 | ||
2119 | $expected = [ |
|
2120 | 'data' => [ |
|
2121 | [ |
|
2122 | 'type' => 'books', |
|
2123 | 'id' => '1', |
|
2124 | 'attributes' => [ |
|
2125 | 'title' => 'Foo', |
|
2126 | 'year' => 1991, |
|
2127 | ], |
|
2128 | 'links' => [ |
|
2129 | 'self' => 'http://example.com/books/1', |
|
2130 | ], |
|
2131 | 'relationships' => [ |
|
2132 | 'author' => [ |
|
2133 | 'links' => [ |
|
2134 | 'self' => 'http://example.com/books/1/relationships/author', |
|
2135 | 'related' => 'http://example.com/books/1/author', |
|
2136 | ], |
|
2137 | ], |
|
2138 | 'co-author' => [ |
|
2139 | 'links' => [ |
|
2140 | 'self' => 'http://example.com/books/1/relationships/co-author', |
|
2141 | 'related' => 'http://example.com/books/1/co-author' |
|
2142 | ], |
|
2143 | ], |
|
2144 | ], |
|
2145 | ], |
|
2146 | [ |
|
2147 | 'type' => 'books', |
|
2148 | 'id' => '2', |
|
2149 | 'attributes' => [ |
|
2150 | 'title' => 'Bar', |
|
2151 | 'year' => 1997, |
|
2152 | ], |
|
2153 | 'links' => [ |
|
2154 | 'self' => 'http://example.com/books/2', |
|
2155 | ], |
|
2156 | 'relationships' => [ |
|
2157 | 'author' => [ |
|
2158 | 'links' => [ |
|
2159 | 'self' => 'http://example.com/books/2/relationships/author', |
|
2160 | 'related' => 'http://example.com/books/2/author', |
|
2161 | ], |
|
2162 | ], |
|
2163 | 'co-author' => [ |
|
2164 | 'links' => [ |
|
2165 | 'self' => 'http://example.com/books/2/relationships/co-author', |
|
2166 | 'related' => 'http://example.com/books/2/co-author' |
|
2167 | ], |
|
2168 | ], |
|
2169 | ], |
|
2170 | ], |
|
2171 | ], |
|
2172 | 'meta' => [ |
|
2173 | 'pagination' => [ |
|
2174 | 'total' => 10, |
|
2175 | 'count' => 2, |
|
2176 | 'per_page' => 2, |
|
2177 | 'current_page' => 5, |
|
2178 | 'total_pages' => 5 |
|
2179 | ] |
|
2180 | ], |
|
2181 | 'links' => [ |
|
2182 | 'self' => 'http://example.com/books/?page=5', |
|
2183 | 'first' => 'http://example.com/books/?page=1', |
|
2184 | 'prev' => 'http://example.com/books/?page=4', |
|
2185 | 'last' => 'http://example.com/books/?page=5' |
|
2186 | ] |
|
2187 | ]; |
|
2188 | ||
2189 | $this->assertSame($expected, $scope->toArray()); |
|
2190 | ||
2191 | $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"}}}},{"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"}}}}],"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"}}'; |
|
2192 | $this->assertSame($expectedJson, $scope->toJson()); |
|
2193 | } |
|
2194 | ||
2195 | public function testCustomLinkMerge() |
|
2196 | { |