Skip to content

Commit 81b7023

Browse files
committed
Add Tracker::listNames()
1 parent 791ca5f commit 81b7023

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
- New method `Redmine\Api\Project::listNames()` for listing the ids and names of all projects.
1717
- New method `Redmine\Api\Role::listNames()` for listing the ids and names of all roles.
1818
- New method `Redmine\Api\TimeEntryActivity::listNames()` for listing the ids and names of all time entry activities.
19+
- New method `Redmine\Api\Tracker::listNames()` for listing the ids and names of all trackers.
1920

2021
### Deprecated
2122

src/Redmine/Api/Tracker.php

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

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

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

tests/Behat/Bootstrap/TrackerContextTrait.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,18 @@ public function iListAllTrackers()
4040
$api->getLastResponse(),
4141
);
4242
}
43+
44+
/**
45+
* @When I list all tracker names
46+
*/
47+
public function iListAllTrackerNames()
48+
{
49+
/** @var Tracker */
50+
$api = $this->getNativeCurlClient()->getApi('tracker');
51+
52+
$this->registerClientResponse(
53+
$api->listNames(),
54+
$api->getLastResponse(),
55+
);
56+
}
4357
}

tests/Behat/features/tracker.feature

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,17 @@ Feature: Interacting with the REST API for trackers
7878
| 6 | estimated_hours |
7979
| 7 | done_ratio |
8080
| 8 | description |
81+
82+
Scenario: Listing of multiple tracker names
83+
Given I have a "NativeCurlClient" client
84+
And I have an issue status with the name "New"
85+
And I have a tracker with the name "Feature" and default status id "1"
86+
And I have a tracker with the name "Defect" and default status id "1"
87+
When I list all tracker names
88+
Then the response has the status code "200"
89+
And the response has the content type "application/json"
90+
And the returned data contains "2" items
91+
And the returned data contains the following data
92+
| property | value |
93+
| 1 | Feature |
94+
| 2 | Defect |
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\Tracker;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\TestCase;
10+
use Redmine\Api\Tracker;
11+
use Redmine\Tests\Fixtures\AssertingHttpClient;
12+
13+
#[CoversClass(Tracker::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 Tracker($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 trackers' => [
46+
'/trackers.json',
47+
201,
48+
<<<JSON
49+
{
50+
"trackers": []
51+
}
52+
JSON,
53+
[],
54+
],
55+
'test with multiple trackers' => [
56+
'/trackers.json',
57+
201,
58+
<<<JSON
59+
{
60+
"trackers": [
61+
{"id": 7, "name": "Tracker 3"},
62+
{"id": 8, "name": "Tracker 2"},
63+
{"id": 9, "name": "Tracker 1"}
64+
]
65+
}
66+
JSON,
67+
[
68+
7 => "Tracker 3",
69+
8 => "Tracker 2",
70+
9 => "Tracker 1",
71+
],
72+
],
73+
];
74+
}
75+
76+
public function testListNamesCallsHttpClientOnlyOnce()
77+
{
78+
$client = AssertingHttpClient::create(
79+
$this,
80+
[
81+
'GET',
82+
'/trackers.json',
83+
'application/json',
84+
'',
85+
200,
86+
'application/json',
87+
<<<JSON
88+
{
89+
"trackers": [
90+
{
91+
"id": 1,
92+
"name": "Tracker 1"
93+
}
94+
]
95+
}
96+
JSON,
97+
],
98+
);
99+
100+
// Create the object under test
101+
$api = new Tracker($client);
102+
103+
// Perform the tests
104+
$this->assertSame([1 => 'Tracker 1'], $api->listNames());
105+
$this->assertSame([1 => 'Tracker 1'], $api->listNames());
106+
$this->assertSame([1 => 'Tracker 1'], $api->listNames());
107+
}
108+
}

0 commit comments

Comments
 (0)