12
12
namespace Tobyz \Tests \JsonApiServer \specification ;
13
13
14
14
use Tobyz \JsonApiServer \JsonApi ;
15
+ use Tobyz \JsonApiServer \Schema \Type ;
15
16
use Tobyz \Tests \JsonApiServer \AbstractTestCase ;
16
17
use Tobyz \Tests \JsonApiServer \MockAdapter ;
17
18
18
19
/**
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
21
21
*/
22
22
class OffsetPaginationTest extends AbstractTestCase
23
23
{
@@ -26,60 +26,84 @@ class OffsetPaginationTest extends AbstractTestCase
26
26
*/
27
27
private $ api ;
28
28
29
- /**
30
- * @var MockAdapter
31
- */
32
- private $ adapter ;
33
-
34
29
public function setUp (): void
35
30
{
36
31
$ this ->api = new JsonApi ('http://example.com ' );
37
32
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
+ });
39
42
}
40
43
41
44
public function test_can_request_limit_on_resource_collection ()
42
45
{
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 );
44
54
}
45
55
46
56
public function test_can_request_offset_on_resource_collection ()
47
57
{
48
- $ this ->markTestIncomplete ();
49
- }
58
+ $ response = $ this ->api ->handle (
59
+ $ this ->buildRequest ('GET ' , '/articles ' )
60
+ ->withQueryParams (['page ' => ['offset ' => '5 ' ]])
61
+ );
50
62
51
- public function test_first_pagination_link_is_correct ()
52
- {
53
- $ this ->markTestIncomplete ();
54
- }
63
+ $ data = json_decode ($ response ->getBody (), true )['data ' ] ?? null ;
55
64
56
- public function test_last_pagination_link_is_correct ()
57
- {
58
- $ this ->markTestIncomplete ();
65
+ $ this ->assertEquals ('6 ' , $ data [0 ]['id ' ] ?? null );
59
66
}
60
67
61
- public function test_next_pagination_link_is_correct ()
68
+ public function test_pagination_links_are_correct_and_retain_query_parameters ()
62
69
{
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 );
64
84
}
65
85
66
86
public function test_next_pagination_link_is_not_included_on_last_page ()
67
87
{
68
- $ this ->markTestIncomplete ();
69
- }
88
+ $ response = $ this ->api ->handle (
89
+ $ this ->buildRequest ('GET ' , '/articles ' )
90
+ ->withQueryParams (['page ' => ['offset ' => '80 ' ]])
91
+ );
70
92
71
- public function test_prev_pagination_link_is_correct ()
72
- {
73
- $ this ->markTestIncomplete ();
74
- }
93
+ $ links = json_decode ($ response ->getBody (), true )['links ' ] ?? null ;
75
94
76
- public function test_prev_pagination_link_is_not_included_on_last_page ()
77
- {
78
- $ this ->markTestIncomplete ();
95
+ $ this ->assertNull ($ links ['next ' ] ?? null );
79
96
}
80
97
81
- public function test_pagination_links_retain_other_query_parameters ()
98
+ public function test_prev_pagination_link_is_not_included_on_first_page ()
82
99
{
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 );
84
108
}
85
109
}
0 commit comments