Skip to content

Commit 2bade9c

Browse files
committed
Add method Group::listNames()
1 parent ece897d commit 2bade9c

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

src/Redmine/Api/Group.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@ final public function list(array $params = []): array
4343
}
4444
}
4545

46+
/**
47+
* Returns an array of all groups with id/name pairs.
48+
*
49+
* @return array<int,string> list of groups (id => name)
50+
*/
51+
final public function listNames(): array
52+
{
53+
if (empty($this->groups)) {
54+
$this->groups = $this->list();
55+
}
56+
57+
$list = [];
58+
59+
foreach ($this->groups['groups'] as $e) {
60+
$list[(int) $e['id']] = $e['name'];
61+
}
62+
63+
return $list;
64+
}
65+
4666
/**
4767
* List groups.
4868
*
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Redmine\Tests\Unit\Api\Group;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\TestCase;
10+
use Redmine\Api\Group;
11+
use Redmine\Exception\MissingParameterException;
12+
use Redmine\Http\HttpClient;
13+
use Redmine\Tests\Fixtures\AssertingHttpClient;
14+
use SimpleXMLElement;
15+
16+
#[CoversClass(Group::class)]
17+
class ListNamesTest extends TestCase
18+
{
19+
/**
20+
* @dataProvider getListNamesData
21+
*/
22+
#[DataProvider('getListNamesData')]
23+
public function testListNamesReturnsCorrectResponse($expectedPath, $responseCode, $response, $expectedResponse)
24+
{
25+
$client = AssertingHttpClient::create(
26+
$this,
27+
[
28+
'GET',
29+
$expectedPath,
30+
'application/json',
31+
'',
32+
$responseCode,
33+
'application/json',
34+
$response,
35+
]
36+
);
37+
38+
// Create the object under test
39+
$api = new Group($client);
40+
41+
// Perform the tests
42+
$this->assertSame($expectedResponse, $api->listNames());
43+
}
44+
45+
public static function getListNamesData(): array
46+
{
47+
return [
48+
'test with minimal parameters' => [
49+
'/groups.json',
50+
201,
51+
<<<JSON
52+
{
53+
"groups": [
54+
{
55+
"id": 1,
56+
"name": "Group 1"
57+
}
58+
]
59+
}
60+
JSON,
61+
[
62+
1 => "Group 1",
63+
]
64+
],
65+
];
66+
}
67+
}

0 commit comments

Comments
 (0)