1+ <?php
2+
3+ namespace Aternos \SpigotApi \Tests \Unit \Client \List ;
4+
5+ use Aternos \SpigotApi \Client \Category ;
6+ use Aternos \SpigotApi \Client \List \PaginatedCategoryProjectList ;
7+ use Aternos \SpigotApi \Client \Project ;
8+ use Aternos \SpigotApi \Client \SpigotAPIClient ;
9+ use Aternos \SpigotApi \Model \Resource ;
10+ use Aternos \SpigotApi \Model \ResourceCategory ;
11+ use Aternos \SpigotApi \Tests \TestCase ;
12+
13+ class PaginatedCategoryProjectListTest extends TestCase
14+ {
15+ protected function createResource (int $ id , Category $ category = Category::SPIGOT ): Resource
16+ {
17+ return new Resource ([
18+ 'id ' => $ id ,
19+ 'category ' =>
20+ new ResourceCategory ([
21+ 'id ' => $ category ->value ,
22+ 'title ' => $ category ->name
23+ ])
24+ ]);
25+ }
26+
27+ public function testConstructionTransformsResourcesToProjects (): void
28+ {
29+ $ resources = [
30+ $ this ->createResource (1 ),
31+ $ this ->createResource (2 ),
32+ $ this ->createResource (3 ),
33+ ];
34+
35+ $ list = new PaginatedCategoryProjectList ($ this ->apiClient , Category::SPIGOT , 1 , $ resources );
36+
37+ $ this ->assertCount (3 , $ list );
38+ $ this ->assertSame (3 , $ list ->getHits ());
39+ $ this ->assertInstanceOf (Project::class, $ list [0 ]);
40+ $ this ->assertInstanceOf (Project::class, $ list [1 ]);
41+ $ this ->assertInstanceOf (Project::class, $ list [2 ]);
42+ }
43+
44+ public function testArrayAccessAndIteration (): void
45+ {
46+ $ resources = [
47+ $ this ->createResource (10 ),
48+ $ this ->createResource (11 ),
49+ ];
50+
51+ $ list = new PaginatedCategoryProjectList ($ this ->apiClient , Category::SPIGOT , 2 , $ resources );
52+
53+ // ArrayAccess
54+ $ this ->assertTrue (isset ($ list [0 ]));
55+ $ this ->assertTrue (isset ($ list [1 ]));
56+ $ this ->assertFalse (isset ($ list [2 ]));
57+
58+ // Iteration
59+ $ collected = [];
60+ foreach ($ list as $ project ) {
61+ $ collected [] = $ project ;
62+ }
63+ $ this ->assertCount (2 , $ collected );
64+ $ this ->assertEquals ($ list [0 ], $ collected [0 ]);
65+ $ this ->assertEquals ($ list [1 ], $ collected [1 ]);
66+
67+ // Rewind + key/valid
68+ $ list ->rewind ();
69+ $ this ->assertTrue ($ list ->valid ());
70+ $ this ->assertSame (0 , $ list ->key ());
71+ $ list ->next ();
72+ $ this ->assertSame (1 , $ list ->key ());
73+ }
74+
75+ public function testGetNextPageUsesClient (): void
76+ {
77+ $ mockedApiClient = $ this ->createMock (SpigotAPIClient::class);
78+
79+ $ page1 = new PaginatedCategoryProjectList ($ mockedApiClient , Category::SPIGOT , 1 , [
80+ $ this ->createResource (1 , Category::SPIGOT )
81+ ]);
82+ $ page2 = new PaginatedCategoryProjectList ($ mockedApiClient , Category::SPIGOT , 2 , [
83+ $ this ->createResource (2 , Category::SPIGOT )
84+ ]);
85+
86+ $ mockedApiClient ->expects ($ this ->once ())
87+ ->method ('listProjectsForCategory ' )
88+ ->with (Category::SPIGOT , 2 )
89+ ->willReturn ($ page2 );
90+
91+ $ next = $ page1 ->getNextPage ();
92+ $ this ->assertSame ($ page2 , $ next );
93+ }
94+
95+ public function testGetPreviousPage (): void
96+ {
97+ $ mockedApiClient = $ this ->createMock (SpigotAPIClient::class);
98+
99+ $ page1 = new PaginatedCategoryProjectList ($ mockedApiClient , Category::SPIGOT , 1 , [$ this ->createResource (1 , Category::SPIGOT )]);
100+ $ this ->assertFalse ($ page1 ->hasPreviousPage ());
101+ $ this ->assertNull ($ page1 ->getPreviousPage ());
102+
103+ $ page2 = new PaginatedCategoryProjectList ($ mockedApiClient , Category::SPIGOT , 2 , [$ this ->createResource (2 , Category::SPIGOT )]);
104+
105+ $ mockedApiClient ->expects ($ this ->once ())
106+ ->method ('listProjectsForCategory ' )
107+ ->with (Category::SPIGOT , 1 )
108+ ->willReturn ($ page1 );
109+
110+ $ previous = $ page2 ->getPreviousPage ();
111+ $ this ->assertSame ($ page1 , $ previous );
112+ }
113+
114+ public function testGetResultsFromFollowingPagesAggregatesUntilEmptyPage (): void
115+ {
116+ $ mockedApiClient = $ this ->createMock (SpigotAPIClient::class);
117+
118+ $ page1 = new PaginatedCategoryProjectList ($ mockedApiClient , Category::SPIGOT , 1 , [
119+ $ this ->createResource (10 , Category::SPIGOT ),
120+ ]);
121+ $ page2 = new PaginatedCategoryProjectList ($ mockedApiClient , Category::SPIGOT , 2 , [
122+ $ this ->createResource (20 , Category::SPIGOT ),
123+ $ this ->createResource (21 , Category::SPIGOT ),
124+ ]);
125+ // Empty page (hits = 0) stops the aggregation loop
126+ $ page3 = new PaginatedCategoryProjectList ($ mockedApiClient , Category::SPIGOT , 3 , []);
127+
128+ $ mockedApiClient ->expects ($ this ->exactly (2 ))
129+ ->method ('listProjectsForCategory ' )
130+ ->willReturnOnConsecutiveCalls ($ page2 , $ page3 );
131+
132+ $ all = $ page1 ->getResultsFromFollowingPages ();
133+ $ this ->assertCount (3 , $ all ); // 1 from page1 + 2 from page2
134+ $ this ->assertContainsOnlyInstancesOf (Project::class, $ all );
135+ }
136+ }
0 commit comments