Skip to content

Commit e30fcf9

Browse files
committed
Create IssueStatus::listNames()
1 parent 9d58cf7 commit e30fcf9

File tree

5 files changed

+163
-0
lines changed

5 files changed

+163
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- New method `Redmine\Api\CustomField::listNames()` for listing the ids and names of all custom fields.
1313
- New method `Redmine\Api\Group::listNames()` for listing the ids and names of all groups.
1414
- New method `Redmine\Api\IssueCategory::listNamesByProject()` for listing the ids and names of all issue categories of a project.
15+
- New method `Redmine\Api\IssueStatus::listNames()` for listing the ids and names of all issue statuses.
1516

1617
### Deprecated
1718

src/Redmine/Api/IssueStatus.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class IssueStatus extends AbstractApi
1717
{
1818
private $issueStatuses = [];
1919

20+
private $issueStatusNames = null;
21+
2022
/**
2123
* List issue statuses.
2224
*
@@ -37,6 +39,30 @@ final public function list(array $params = []): array
3739
}
3840
}
3941

42+
/**
43+
* Returns an array of all issue statuses with id/name pairs.
44+
*
45+
* @return array<int,string> list of issue statuses (id => name)
46+
*/
47+
final public function listNames(): array
48+
{
49+
if ($this->issueStatusNames !== null) {
50+
return $this->issueStatusNames;
51+
}
52+
53+
$this->issueStatusNames = [];
54+
55+
$list = $this->list();
56+
57+
if (array_key_exists('issue_statuses', $list)) {
58+
foreach ($list['issue_statuses'] as $issueStatus) {
59+
$this->issueStatusNames[(int) $issueStatus['id']] = (string) $issueStatus['name'];
60+
}
61+
}
62+
63+
return $this->issueStatusNames;
64+
}
65+
4066
/**
4167
* List issue statuses.
4268
*

tests/Behat/Bootstrap/IssueStatusContextTrait.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,18 @@ public function iListAllIssueStatuses()
3838
$api->getLastResponse(),
3939
);
4040
}
41+
42+
/**
43+
* @When I list all issue status names
44+
*/
45+
public function iListAllIssueStatusNames()
46+
{
47+
/** @var IssueStatus */
48+
$api = $this->getNativeCurlClient()->getApi('issue_status');
49+
50+
$this->registerClientResponse(
51+
$api->listNames(),
52+
$api->getLastResponse(),
53+
);
54+
}
4155
}

tests/Behat/features/issue_status.feature

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,17 @@ Feature: Interacting with the REST API for issue statuses
5353
| id | 2 |
5454
| name | Done |
5555
| is_closed | false |
56+
57+
Scenario: Listing of multiple issue status names
58+
Given I have a "NativeCurlClient" client
59+
And I have an issue status with the name "New"
60+
And I have an issue status with the name "Done"
61+
When I list all issue status names
62+
Then the response has the status code "200"
63+
And the response has the content type "application/json"
64+
And the returned data is an array
65+
And the returned data contains "2" items
66+
And the returned data contains the following data
67+
| property | value |
68+
| 1 | New |
69+
| 2 | Done |
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Redmine\Tests\Unit\Api\IssueStatus;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\TestCase;
10+
use Redmine\Api\IssueStatus;
11+
use Redmine\Tests\Fixtures\AssertingHttpClient;
12+
13+
#[CoversClass(IssueStatus::class)]
14+
class ListNamesTest extends TestCase
15+
{
16+
/**
17+
* @dataProvider getListNamesData
18+
*/
19+
#[DataProvider('getListNamesData')]
20+
public function testListNamesReturnsCorrectResponse($expectedPath, $responseCode, $response, $expectedResponse)
21+
{
22+
$client = AssertingHttpClient::create(
23+
$this,
24+
[
25+
'GET',
26+
$expectedPath,
27+
'application/json',
28+
'',
29+
$responseCode,
30+
'application/json',
31+
$response,
32+
],
33+
);
34+
35+
// Create the object under test
36+
$api = new IssueStatus($client);
37+
38+
// Perform the tests
39+
$this->assertSame($expectedResponse, $api->listNames());
40+
}
41+
42+
public static function getListNamesData(): array
43+
{
44+
return [
45+
'test without issue statuses' => [
46+
'/issue_statuses.json',
47+
201,
48+
<<<JSON
49+
{
50+
"issue_statuses": []
51+
}
52+
JSON,
53+
[],
54+
],
55+
'test with multiple issue statuses' => [
56+
'/issue_statuses.json',
57+
201,
58+
<<<JSON
59+
{
60+
"issue_statuses": [
61+
{"id": 7, "name": "IssueStatus C"},
62+
{"id": 8, "name": "IssueStatus B"},
63+
{"id": 9, "name": "IssueStatus A"}
64+
]
65+
}
66+
JSON,
67+
[
68+
7 => "IssueStatus C",
69+
8 => "IssueStatus B",
70+
9 => "IssueStatus A",
71+
],
72+
],
73+
];
74+
}
75+
76+
public function testListNamesCallsHttpClientOnlyOnce()
77+
{
78+
$client = AssertingHttpClient::create(
79+
$this,
80+
[
81+
'GET',
82+
'/issue_statuses.json',
83+
'application/json',
84+
'',
85+
200,
86+
'application/json',
87+
<<<JSON
88+
{
89+
"issue_statuses": [
90+
{
91+
"id": 1,
92+
"name": "IssueStatus 1"
93+
}
94+
]
95+
}
96+
JSON,
97+
],
98+
);
99+
100+
// Create the object under test
101+
$api = new IssueStatus($client);
102+
103+
// Perform the tests
104+
$this->assertSame([1 => 'IssueStatus 1'], $api->listNames());
105+
$this->assertSame([1 => 'IssueStatus 1'], $api->listNames());
106+
$this->assertSame([1 => 'IssueStatus 1'], $api->listNames());
107+
}
108+
}

0 commit comments

Comments
 (0)