From e30fcf96e5d1ff54c84021441a12a1bcc8edcde3 Mon Sep 17 00:00:00 2001 From: Art4 Date: Mon, 1 Jul 2024 13:11:40 +0200 Subject: [PATCH] Create IssueStatus::listNames() --- CHANGELOG.md | 1 + src/Redmine/Api/IssueStatus.php | 26 +++++ .../Bootstrap/IssueStatusContextTrait.php | 14 +++ tests/Behat/features/issue_status.feature | 14 +++ tests/Unit/Api/IssueStatus/ListNamesTest.php | 108 ++++++++++++++++++ 5 files changed, 163 insertions(+) create mode 100644 tests/Unit/Api/IssueStatus/ListNamesTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 14d56264..909e3315 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - New method `Redmine\Api\CustomField::listNames()` for listing the ids and names of all custom fields. - New method `Redmine\Api\Group::listNames()` for listing the ids and names of all groups. - New method `Redmine\Api\IssueCategory::listNamesByProject()` for listing the ids and names of all issue categories of a project. +- New method `Redmine\Api\IssueStatus::listNames()` for listing the ids and names of all issue statuses. ### Deprecated diff --git a/src/Redmine/Api/IssueStatus.php b/src/Redmine/Api/IssueStatus.php index a7c076a1..e47c7224 100644 --- a/src/Redmine/Api/IssueStatus.php +++ b/src/Redmine/Api/IssueStatus.php @@ -17,6 +17,8 @@ class IssueStatus extends AbstractApi { private $issueStatuses = []; + private $issueStatusNames = null; + /** * List issue statuses. * @@ -37,6 +39,30 @@ final public function list(array $params = []): array } } + /** + * Returns an array of all issue statuses with id/name pairs. + * + * @return array list of issue statuses (id => name) + */ + final public function listNames(): array + { + if ($this->issueStatusNames !== null) { + return $this->issueStatusNames; + } + + $this->issueStatusNames = []; + + $list = $this->list(); + + if (array_key_exists('issue_statuses', $list)) { + foreach ($list['issue_statuses'] as $issueStatus) { + $this->issueStatusNames[(int) $issueStatus['id']] = (string) $issueStatus['name']; + } + } + + return $this->issueStatusNames; + } + /** * List issue statuses. * diff --git a/tests/Behat/Bootstrap/IssueStatusContextTrait.php b/tests/Behat/Bootstrap/IssueStatusContextTrait.php index bcf2f056..d0a5f401 100644 --- a/tests/Behat/Bootstrap/IssueStatusContextTrait.php +++ b/tests/Behat/Bootstrap/IssueStatusContextTrait.php @@ -38,4 +38,18 @@ public function iListAllIssueStatuses() $api->getLastResponse(), ); } + + /** + * @When I list all issue status names + */ + public function iListAllIssueStatusNames() + { + /** @var IssueStatus */ + $api = $this->getNativeCurlClient()->getApi('issue_status'); + + $this->registerClientResponse( + $api->listNames(), + $api->getLastResponse(), + ); + } } diff --git a/tests/Behat/features/issue_status.feature b/tests/Behat/features/issue_status.feature index ffeaaf94..540cd4ff 100644 --- a/tests/Behat/features/issue_status.feature +++ b/tests/Behat/features/issue_status.feature @@ -53,3 +53,17 @@ Feature: Interacting with the REST API for issue statuses | id | 2 | | name | Done | | is_closed | false | + + Scenario: Listing of multiple issue status names + Given I have a "NativeCurlClient" client + And I have an issue status with the name "New" + And I have an issue status with the name "Done" + When I list all issue status names + Then the response has the status code "200" + And the response has the content type "application/json" + And the returned data is an array + And the returned data contains "2" items + And the returned data contains the following data + | property | value | + | 1 | New | + | 2 | Done | diff --git a/tests/Unit/Api/IssueStatus/ListNamesTest.php b/tests/Unit/Api/IssueStatus/ListNamesTest.php new file mode 100644 index 00000000..231e4fc5 --- /dev/null +++ b/tests/Unit/Api/IssueStatus/ListNamesTest.php @@ -0,0 +1,108 @@ +assertSame($expectedResponse, $api->listNames()); + } + + public static function getListNamesData(): array + { + return [ + 'test without issue statuses' => [ + '/issue_statuses.json', + 201, + << [ + '/issue_statuses.json', + 201, + << "IssueStatus C", + 8 => "IssueStatus B", + 9 => "IssueStatus A", + ], + ], + ]; + } + + public function testListNamesCallsHttpClientOnlyOnce() + { + $client = AssertingHttpClient::create( + $this, + [ + 'GET', + '/issue_statuses.json', + 'application/json', + '', + 200, + 'application/json', + <<assertSame([1 => 'IssueStatus 1'], $api->listNames()); + $this->assertSame([1 => 'IssueStatus 1'], $api->listNames()); + $this->assertSame([1 => 'IssueStatus 1'], $api->listNames()); + } +}