Skip to content

Commit 7f94154

Browse files
committed
Use internal cache for group names
1 parent 1e881ae commit 7f94154

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

src/Redmine/Api/Group.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class Group extends AbstractApi
2323
{
2424
private $groups = [];
2525

26+
private $groupNames = null;
27+
2628
/**
2729
* List groups.
2830
*
@@ -50,17 +52,17 @@ final public function list(array $params = []): array
5052
*/
5153
final public function listNames(): array
5254
{
53-
if (empty($this->groups)) {
54-
$this->groups = $this->list();
55+
if ($this->groupNames !== null) {
56+
return $this->groupNames;
5557
}
5658

57-
$list = [];
59+
$this->groupNames = [];
5860

59-
foreach ($this->groups['groups'] as $e) {
60-
$list[(int) $e['id']] = $e['name'];
61+
foreach ($this->list()['groups'] as $group) {
62+
$this->groupNames[(int) $group['id']] = $group['name'];
6163
}
6264

63-
return $list;
65+
return $this->groupNames;
6466
}
6567

6668
/**

tests/Unit/Api/Group/ListNamesTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,37 @@ public static function getListNamesData(): array
6161
],
6262
];
6363
}
64+
65+
public function testListNamesCallsHttpClientOnlyOnce()
66+
{
67+
$client = AssertingHttpClient::create(
68+
$this,
69+
[
70+
'GET',
71+
'/groups.json',
72+
'application/json',
73+
'',
74+
200,
75+
'application/json',
76+
<<<JSON
77+
{
78+
"groups": [
79+
{
80+
"id": 1,
81+
"name": "Group 1"
82+
}
83+
]
84+
}
85+
JSON,
86+
]
87+
);
88+
89+
// Create the object under test
90+
$api = new Group($client);
91+
92+
// Perform the tests
93+
$this->assertSame([1 => 'Group 1'], $api->listNames());
94+
$this->assertSame([1 => 'Group 1'], $api->listNames());
95+
$this->assertSame([1 => 'Group 1'], $api->listNames());
96+
}
6497
}

0 commit comments

Comments
 (0)