Skip to content

Commit 957ab4b

Browse files
committed
Add Role::listNames()
1 parent fab4c42 commit 957ab4b

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
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
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.
1515
- New method `Redmine\Api\IssueStatus::listNames()` for listing the ids and names of all issue statuses.
16+
- New method `Redmine\Api\Role::listNames()` for listing the ids and names of all roles.
1617

1718
### Deprecated
1819

src/Redmine/Api/Role.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class Role extends AbstractApi
1919
{
2020
private $roles = [];
2121

22+
private $roleNames = null;
23+
2224
/**
2325
* List roles.
2426
*
@@ -39,6 +41,29 @@ final public function list(array $params = []): array
3941
}
4042
}
4143

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

tests/Behat/Bootstrap/RoleContextTrait.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,18 @@ public function iListAllRoles()
4444
$api->getLastResponse(),
4545
);
4646
}
47+
48+
/**
49+
* @When I list all role names
50+
*/
51+
public function iListAllRoleNames()
52+
{
53+
/** @var Role */
54+
$api = $this->getNativeCurlClient()->getApi('role');
55+
56+
$this->registerClientResponse(
57+
$api->listNames(),
58+
$api->getLastResponse(),
59+
);
60+
}
4761
}

tests/Behat/features/role.feature

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,17 @@ Feature: Interacting with the REST API for roles
3939
| property | value |
4040
| id | 4 |
4141
| name | Developer |
42+
43+
Scenario: Listing of multiple role names
44+
Given I have a "NativeCurlClient" client
45+
And I have a role with the name "Reporter"
46+
And I have a role with the name "Developer"
47+
When I list all role names
48+
Then the response has the status code "200"
49+
And the response has the content type "application/json"
50+
And the returned data is an array
51+
And the returned data contains "2" items
52+
And the returned data contains the following data
53+
| property | value |
54+
| 3 | Reporter |
55+
| 4 | Developer |

tests/Unit/Api/Role/ListNamesTest.php

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\Role;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\TestCase;
10+
use Redmine\Api\Role;
11+
use Redmine\Tests\Fixtures\AssertingHttpClient;
12+
13+
#[CoversClass(Role::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 Role($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 roles' => [
46+
'/roles.json',
47+
201,
48+
<<<JSON
49+
{
50+
"roles": []
51+
}
52+
JSON,
53+
[],
54+
],
55+
'test with multiple roles' => [
56+
'/roles.json',
57+
201,
58+
<<<JSON
59+
{
60+
"roles": [
61+
{"id": 7, "name": "Role 3"},
62+
{"id": 8, "name": "Role 2"},
63+
{"id": 9, "name": "Role 1"}
64+
]
65+
}
66+
JSON,
67+
[
68+
7 => "Role 3",
69+
8 => "Role 2",
70+
9 => "Role 1",
71+
],
72+
],
73+
];
74+
}
75+
76+
public function testListNamesCallsHttpClientOnlyOnce()
77+
{
78+
$client = AssertingHttpClient::create(
79+
$this,
80+
[
81+
'GET',
82+
'/roles.json',
83+
'application/json',
84+
'',
85+
200,
86+
'application/json',
87+
<<<JSON
88+
{
89+
"roles": [
90+
{
91+
"id": 1,
92+
"name": "Role 1"
93+
}
94+
]
95+
}
96+
JSON,
97+
],
98+
);
99+
100+
// Create the object under test
101+
$api = new Role($client);
102+
103+
// Perform the tests
104+
$this->assertSame([1 => 'Role 1'], $api->listNames());
105+
$this->assertSame([1 => 'Role 1'], $api->listNames());
106+
$this->assertSame([1 => 'Role 1'], $api->listNames());
107+
}
108+
}

0 commit comments

Comments
 (0)