1212namespace Tobyz \Tests \JsonApiServer \specification ;
1313
1414use Tobyz \JsonApiServer \JsonApi ;
15+ use Tobyz \JsonApiServer \Schema \Type ;
1516use Tobyz \Tests \JsonApiServer \AbstractTestCase ;
1617use Tobyz \Tests \JsonApiServer \MockAdapter ;
1718
1819/**
19- * @see https://jsonapi.org/format/1.0/#fetching-pagination
20- * @todo Create a profile for offset pagination strategy
20+ * @see https://jsonapi.org/format/1.1/#fetching-pagination
2121 */
2222class OffsetPaginationTest extends AbstractTestCase
2323{
@@ -26,60 +26,84 @@ class OffsetPaginationTest extends AbstractTestCase
2626 */
2727 private $ api ;
2828
29- /**
30- * @var MockAdapter
31- */
32- private $ adapter ;
33-
3429 public function setUp (): void
3530 {
3631 $ this ->api = new JsonApi ('http://example.com ' );
3732
38- $ this ->adapter = new MockAdapter ();
33+ $ adapter = new MockAdapter (
34+ array_map (function ($ i ) {
35+ return (object ) ['id ' => (string ) $ i ];
36+ }, range (1 , 100 ))
37+ );
38+
39+ $ this ->api ->resourceType ('articles ' , $ adapter , function (Type $ type ) {
40+ $ type ->paginate (20 );
41+ });
3942 }
4043
4144 public function test_can_request_limit_on_resource_collection ()
4245 {
43- $ this ->markTestIncomplete ();
46+ $ response = $ this ->api ->handle (
47+ $ this ->buildRequest ('GET ' , '/articles ' )
48+ ->withQueryParams (['page ' => ['limit ' => '10 ' ]])
49+ );
50+
51+ $ data = json_decode ($ response ->getBody (), true )['data ' ] ?? null ;
52+
53+ $ this ->assertCount (10 , $ data );
4454 }
4555
4656 public function test_can_request_offset_on_resource_collection ()
4757 {
48- $ this ->markTestIncomplete ();
49- }
58+ $ response = $ this ->api ->handle (
59+ $ this ->buildRequest ('GET ' , '/articles ' )
60+ ->withQueryParams (['page ' => ['offset ' => '5 ' ]])
61+ );
5062
51- public function test_first_pagination_link_is_correct ()
52- {
53- $ this ->markTestIncomplete ();
54- }
63+ $ data = json_decode ($ response ->getBody (), true )['data ' ] ?? null ;
5564
56- public function test_last_pagination_link_is_correct ()
57- {
58- $ this ->markTestIncomplete ();
65+ $ this ->assertEquals ('6 ' , $ data [0 ]['id ' ] ?? null );
5966 }
6067
61- public function test_next_pagination_link_is_correct ()
68+ public function test_pagination_links_are_correct_and_retain_query_parameters ()
6269 {
63- $ this ->markTestIncomplete ();
70+ $ response = $ this ->api ->handle (
71+ $ this ->buildRequest ('GET ' , '/articles ' )
72+ ->withQueryParams ([
73+ 'page ' => ['offset ' => '40 ' ],
74+ 'otherParam ' => 'value ' ,
75+ ])
76+ );
77+
78+ $ links = json_decode ($ response ->getBody (), true )['links ' ] ?? null ;
79+
80+ $ this ->assertEquals ('/articles?otherParam=value ' , $ links ['first ' ] ?? null );
81+ $ this ->assertEquals ('/articles?otherParam=value&page%5Boffset%5D=80 ' , $ links ['last ' ] ?? null );
82+ $ this ->assertEquals ('/articles?otherParam=value&page%5Boffset%5D=60 ' , $ links ['next ' ] ?? null );
83+ $ this ->assertEquals ('/articles?otherParam=value&page%5Boffset%5D=20 ' , $ links ['prev ' ] ?? null );
6484 }
6585
6686 public function test_next_pagination_link_is_not_included_on_last_page ()
6787 {
68- $ this ->markTestIncomplete ();
69- }
88+ $ response = $ this ->api ->handle (
89+ $ this ->buildRequest ('GET ' , '/articles ' )
90+ ->withQueryParams (['page ' => ['offset ' => '80 ' ]])
91+ );
7092
71- public function test_prev_pagination_link_is_correct ()
72- {
73- $ this ->markTestIncomplete ();
74- }
93+ $ links = json_decode ($ response ->getBody (), true )['links ' ] ?? null ;
7594
76- public function test_prev_pagination_link_is_not_included_on_last_page ()
77- {
78- $ this ->markTestIncomplete ();
95+ $ this ->assertNull ($ links ['next ' ] ?? null );
7996 }
8097
81- public function test_pagination_links_retain_other_query_parameters ()
98+ public function test_prev_pagination_link_is_not_included_on_first_page ()
8299 {
83- $ this ->markTestIncomplete ();
100+ $ response = $ this ->api ->handle (
101+ $ this ->buildRequest ('GET ' , '/articles ' )
102+ ->withQueryParams (['page ' => ['offset ' => '0 ' ]])
103+ );
104+
105+ $ links = json_decode ($ response ->getBody (), true )['links ' ] ?? null ;
106+
107+ $ this ->assertNull ($ links ['prev ' ] ?? null );
84108 }
85109}
0 commit comments