From 4e0dec61f6fa305ff18e108a44f2b85c12b43ee5 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 4 Jul 2024 10:13:20 +0200 Subject: [PATCH] Let Project::listNames() handle pagination --- src/Redmine/Api/Project.php | 23 +++++-- tests/Behat/Bootstrap/ProjectContextTrait.php | 12 ++++ tests/Behat/features/projects.feature | 8 +++ tests/Unit/Api/Project/ListNamesTest.php | 65 ++++++++++++++++++- 4 files changed, 100 insertions(+), 8 deletions(-) diff --git a/src/Redmine/Api/Project.php b/src/Redmine/Api/Project.php index b6106e33..8afe5512 100755 --- a/src/Redmine/Api/Project.php +++ b/src/Redmine/Api/Project.php @@ -59,13 +59,26 @@ final public function listNames(): array $this->projectNames = []; - $list = $this->list(); + $limit = 100; + $offset = 0; - if (array_key_exists('projects', $list)) { - foreach ($list['projects'] as $issueStatus) { - $this->projectNames[(int) $issueStatus['id']] = (string) $issueStatus['name']; + do { + $list = $this->list([ + 'limit' => $limit, + 'offset' => $offset, + ]); + + $listCount = 0; + $offset += $limit; + + if (array_key_exists('projects', $list)) { + $listCount = count($list['projects']); + + foreach ($list['projects'] as $issueStatus) { + $this->projectNames[(int) $issueStatus['id']] = (string) $issueStatus['name']; + } } - } + } while ($listCount === $limit); return $this->projectNames; } diff --git a/tests/Behat/Bootstrap/ProjectContextTrait.php b/tests/Behat/Bootstrap/ProjectContextTrait.php index 33bce8cc..038e41b1 100644 --- a/tests/Behat/Bootstrap/ProjectContextTrait.php +++ b/tests/Behat/Bootstrap/ProjectContextTrait.php @@ -43,6 +43,18 @@ public function iCreateAProjectWithTheFollowingData(TableNode $table) ); } + /** + * @Given I create :count projects + */ + public function iCreateProjects(int $count) + { + while ($count > 0) { + $this->iCreateAProjectWithNameAndIdentifier('Test Project ' . $count, 'test-project-' . $count); + + $count--; + } + } + /** * @When I list all projects */ diff --git a/tests/Behat/features/projects.feature b/tests/Behat/features/projects.feature index 4f575adb..63612182 100644 --- a/tests/Behat/features/projects.feature +++ b/tests/Behat/features/projects.feature @@ -163,6 +163,14 @@ Feature: Interacting with the REST API for projects | 1 | Test Project B | | 2 | Test Project A | + Scenario: Listing of multiple project names + Given I have a "NativeCurlClient" client + And I create "108" projects + When I list all project names + Then the response has the status code "200" + And the response has the content type "application/json" + And the returned data contains "108" items + Scenario: Updating a project Given I have a "NativeCurlClient" client And I create a project with name "Test Project" and identifier "test-project" diff --git a/tests/Unit/Api/Project/ListNamesTest.php b/tests/Unit/Api/Project/ListNamesTest.php index 78dce84d..4024a96b 100644 --- a/tests/Unit/Api/Project/ListNamesTest.php +++ b/tests/Unit/Api/Project/ListNamesTest.php @@ -43,7 +43,7 @@ public static function getListNamesData(): array { return [ 'test without projects' => [ - '/projects.json', + '/projects.json?limit=100&offset=0', 201, << [ - '/projects.json', + '/projects.json?limit=100&offset=0', 201, << $i, 'name' => $name]; + } + + for ($i = 101; $i <= 200; $i++) { + $name = 'Project ' . $i; + + $assertData[$i] = $name; + $projectsRequest2[] = ['id' => $i, 'name' => $name]; + } + + $client = AssertingHttpClient::create( + $this, + [ + 'GET', + '/projects.json?limit=100&offset=0', + 'application/json', + '', + 200, + 'application/json', + json_encode(['projects' => $projectsRequest1]), + ], + [ + 'GET', + '/projects.json?limit=100&offset=100', + 'application/json', + '', + 200, + 'application/json', + json_encode(['projects' => $projectsRequest2]), + ], + [ + 'GET', + '/projects.json?limit=100&offset=200', + 'application/json', + '', + 200, + 'application/json', + json_encode(['projects' => $projectsRequest3]), + ], + ); + + // Create the object under test + $api = new Project($client); + + // Perform the tests + $this->assertSame($assertData, $api->listNames()); + } + public function testListNamesCallsHttpClientOnlyOnce() { $client = AssertingHttpClient::create( $this, [ 'GET', - '/projects.json', + '/projects.json?limit=100&offset=0', 'application/json', '', 200,