Skip to content

Commit 066212f

Browse files
authored
Merge pull request #409 from Art4/add-issuestatus-listnames
Add `IssueStatus::listNames()` method as replacement for `IssueStatus::listing()`
2 parents 35bb710 + a426080 commit 066212f

File tree

6 files changed

+272
-0
lines changed

6 files changed

+272
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ 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

1819
- `Redmine\Api\CustomField::listing()` is deprecated, use `\Redmine\Api\CustomField::listNames()` instead.
1920
- `Redmine\Api\Group::listing()` is deprecated, use `\Redmine\Api\Group::listNames()` instead.
2021
- `Redmine\Api\IssueCategory::listing()` is deprecated, use `\Redmine\Api\IssueCategory::listNamesByProject()` instead.
22+
- `Redmine\Api\IssueStatus::listing()` is deprecated, use `\Redmine\Api\IssueStatus::listNamesByProject()` instead.
2123

2224
## [v2.6.0](https://github.com/kbsali/php-redmine-api/compare/v2.5.0...v2.6.0) - 2024-03-25
2325

src/Redmine/Api/IssueStatus.php

Lines changed: 31 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
*
@@ -73,12 +99,17 @@ public function all(array $params = [])
7399
/**
74100
* Returns an array of issue statuses with name/id pairs.
75101
*
102+
* @deprecated v2.7.0 Use listNames() instead.
103+
* @see IssueStatus::listNames()
104+
*
76105
* @param bool $forceUpdate to force the update of the statuses var
77106
*
78107
* @return array list of issue statuses (id => name)
79108
*/
80109
public function listing($forceUpdate = false)
81110
{
111+
@trigger_error('`' . __METHOD__ . '()` is deprecated since v2.7.0, use `' . __CLASS__ . '::listNames()` instead.', E_USER_DEPRECATED);
112+
82113
if (empty($this->issueStatuses) || $forceUpdate) {
83114
$this->issueStatuses = $this->list();
84115
}

tests/Behat/Bootstrap/IssueStatusContextTrait.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Redmine\Tests\Behat\Bootstrap;
66

7+
use Redmine\Api\IssueStatus;
8+
79
trait IssueStatusContextTrait
810
{
911
/**
@@ -22,4 +24,32 @@ public function iHaveAnIssueStatusWithTheName($issueStatusName)
2224
],
2325
);
2426
}
27+
28+
/**
29+
* @When I list all issue statuses
30+
*/
31+
public function iListAllIssueStatuses()
32+
{
33+
/** @var IssueStatus */
34+
$api = $this->getNativeCurlClient()->getApi('issue_status');
35+
36+
$this->registerClientResponse(
37+
$api->list(),
38+
$api->getLastResponse(),
39+
);
40+
}
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+
}
2555
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
@issue_status
2+
Feature: Interacting with the REST API for issue statuses
3+
In order to interact with REST API for issue statuses
4+
As a user
5+
I want to make sure the Redmine server replies with the correct response
6+
7+
Scenario: Listing of zero issue statuses
8+
Given I have a "NativeCurlClient" client
9+
When I list all issue statuses
10+
Then the response has the status code "200"
11+
And the response has the content type "application/json"
12+
And the returned data has only the following properties
13+
"""
14+
issue_statuses
15+
"""
16+
And the returned data "issue_statuses" property is an array
17+
And the returned data "issue_statuses" property contains "0" items
18+
19+
Scenario: Listing of multiple issue statuses
20+
Given I have a "NativeCurlClient" client
21+
And I have an issue status with the name "New"
22+
And I have an issue status with the name "Done"
23+
When I list all issue statuses
24+
Then the response has the status code "200"
25+
And the response has the content type "application/json"
26+
And the returned data has only the following properties
27+
"""
28+
issue_statuses
29+
"""
30+
And the returned data "issue_statuses" property is an array
31+
And the returned data "issue_statuses" property contains "2" items
32+
# field 'description' was added in Redmine 5.1.0, see https://www.redmine.org/issues/2568
33+
And the returned data "issue_statuses.0" property contains the following data with Redmine version ">= 5.1.0"
34+
| property | value |
35+
| id | 1 |
36+
| name | New |
37+
| is_closed | false |
38+
| description | null |
39+
But the returned data "issue_statuses.0" property contains the following data with Redmine version "< 5.1.0"
40+
| property | value |
41+
| id | 1 |
42+
| name | New |
43+
| is_closed | false |
44+
# field 'description' was added in Redmine 5.1.0, see https://www.redmine.org/issues/2568
45+
And the returned data "issue_statuses.1" property contains the following data with Redmine version ">= 5.1.0"
46+
| property | value |
47+
| id | 2 |
48+
| name | Done |
49+
| is_closed | false |
50+
| description | null |
51+
But the returned data "issue_statuses.1" property contains the following data with Redmine version "< 5.1.0"
52+
| property | value |
53+
| id | 2 |
54+
| name | Done |
55+
| 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+
}

tests/Unit/Api/IssueStatusTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,38 @@ public function testListingCallsGetEveryTimeWithForceUpdate()
215215
$this->assertSame($expectedReturn, $api->listing(true));
216216
}
217217

218+
/**
219+
* Test listing().
220+
*/
221+
public function testListingTriggersDeprecationWarning()
222+
{
223+
$client = $this->createMock(Client::class);
224+
$client->method('requestGet')
225+
->willReturn(true);
226+
$client->method('getLastResponseBody')
227+
->willReturn('{"issue_statuses":[{"id":1,"name":"IssueStatus 1"},{"id":5,"name":"IssueStatus 5"}]}');
228+
$client->method('getLastResponseContentType')
229+
->willReturn('application/json');
230+
231+
$api = new IssueStatus($client);
232+
233+
// PHPUnit 10 compatible way to test trigger_error().
234+
set_error_handler(
235+
function ($errno, $errstr): bool {
236+
$this->assertSame(
237+
'`Redmine\Api\IssueStatus::listing()` is deprecated since v2.7.0, use `Redmine\Api\IssueStatus::listNames()` instead.',
238+
$errstr,
239+
);
240+
241+
restore_error_handler();
242+
return true;
243+
},
244+
E_USER_DEPRECATED,
245+
);
246+
247+
$api->listing();
248+
}
249+
218250
/**
219251
* Test getIdByName().
220252
*/

0 commit comments

Comments
 (0)