Skip to content

Commit 4e0dec6

Browse files
committed
Let Project::listNames() handle pagination
1 parent 352a8dc commit 4e0dec6

File tree

4 files changed

+100
-8
lines changed

4 files changed

+100
-8
lines changed

src/Redmine/Api/Project.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,26 @@ final public function listNames(): array
5959

6060
$this->projectNames = [];
6161

62-
$list = $this->list();
62+
$limit = 100;
63+
$offset = 0;
6364

64-
if (array_key_exists('projects', $list)) {
65-
foreach ($list['projects'] as $issueStatus) {
66-
$this->projectNames[(int) $issueStatus['id']] = (string) $issueStatus['name'];
65+
do {
66+
$list = $this->list([
67+
'limit' => $limit,
68+
'offset' => $offset,
69+
]);
70+
71+
$listCount = 0;
72+
$offset += $limit;
73+
74+
if (array_key_exists('projects', $list)) {
75+
$listCount = count($list['projects']);
76+
77+
foreach ($list['projects'] as $issueStatus) {
78+
$this->projectNames[(int) $issueStatus['id']] = (string) $issueStatus['name'];
79+
}
6780
}
68-
}
81+
} while ($listCount === $limit);
6982

7083
return $this->projectNames;
7184
}

tests/Behat/Bootstrap/ProjectContextTrait.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ public function iCreateAProjectWithTheFollowingData(TableNode $table)
4343
);
4444
}
4545

46+
/**
47+
* @Given I create :count projects
48+
*/
49+
public function iCreateProjects(int $count)
50+
{
51+
while ($count > 0) {
52+
$this->iCreateAProjectWithNameAndIdentifier('Test Project ' . $count, 'test-project-' . $count);
53+
54+
$count--;
55+
}
56+
}
57+
4658
/**
4759
* @When I list all projects
4860
*/

tests/Behat/features/projects.feature

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ Feature: Interacting with the REST API for projects
163163
| 1 | Test Project B |
164164
| 2 | Test Project A |
165165

166+
Scenario: Listing of multiple project names
167+
Given I have a "NativeCurlClient" client
168+
And I create "108" projects
169+
When I list all project names
170+
Then the response has the status code "200"
171+
And the response has the content type "application/json"
172+
And the returned data contains "108" items
173+
166174
Scenario: Updating a project
167175
Given I have a "NativeCurlClient" client
168176
And I create a project with name "Test Project" and identifier "test-project"

tests/Unit/Api/Project/ListNamesTest.php

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static function getListNamesData(): array
4343
{
4444
return [
4545
'test without projects' => [
46-
'/projects.json',
46+
'/projects.json?limit=100&offset=0',
4747
201,
4848
<<<JSON
4949
{
@@ -53,7 +53,7 @@ public static function getListNamesData(): array
5353
[],
5454
],
5555
'test with multiple projects' => [
56-
'/projects.json',
56+
'/projects.json?limit=100&offset=0',
5757
201,
5858
<<<JSON
5959
{
@@ -73,13 +73,72 @@ public static function getListNamesData(): array
7373
];
7474
}
7575

76+
public function testListNamesWithALotOfProjectsHandlesPagination()
77+
{
78+
$assertData = [];
79+
$projectsRequest1 = [];
80+
$projectsRequest2 = [];
81+
$projectsRequest3 = [];
82+
83+
for ($i = 1; $i <= 100; $i++) {
84+
$name = 'Project ' . $i;
85+
86+
$assertData[$i] = $name;
87+
$projectsRequest1[] = ['id' => $i, 'name' => $name];
88+
}
89+
90+
for ($i = 101; $i <= 200; $i++) {
91+
$name = 'Project ' . $i;
92+
93+
$assertData[$i] = $name;
94+
$projectsRequest2[] = ['id' => $i, 'name' => $name];
95+
}
96+
97+
$client = AssertingHttpClient::create(
98+
$this,
99+
[
100+
'GET',
101+
'/projects.json?limit=100&offset=0',
102+
'application/json',
103+
'',
104+
200,
105+
'application/json',
106+
json_encode(['projects' => $projectsRequest1]),
107+
],
108+
[
109+
'GET',
110+
'/projects.json?limit=100&offset=100',
111+
'application/json',
112+
'',
113+
200,
114+
'application/json',
115+
json_encode(['projects' => $projectsRequest2]),
116+
],
117+
[
118+
'GET',
119+
'/projects.json?limit=100&offset=200',
120+
'application/json',
121+
'',
122+
200,
123+
'application/json',
124+
json_encode(['projects' => $projectsRequest3]),
125+
],
126+
);
127+
128+
// Create the object under test
129+
$api = new Project($client);
130+
131+
// Perform the tests
132+
$this->assertSame($assertData, $api->listNames());
133+
}
134+
76135
public function testListNamesCallsHttpClientOnlyOnce()
77136
{
78137
$client = AssertingHttpClient::create(
79138
$this,
80139
[
81140
'GET',
82-
'/projects.json',
141+
'/projects.json?limit=100&offset=0',
83142
'application/json',
84143
'',
85144
200,

0 commit comments

Comments
 (0)