Skip to content

Commit 420baa0

Browse files
committed
Create TimeEntryActivity::listNames()
1 parent 7620e52 commit 420baa0

File tree

5 files changed

+162
-1
lines changed

5 files changed

+162
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- New method `Redmine\Api\IssueStatus::listNames()` for listing the ids and names of all issue statuses.
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.
18+
- New method `Redmine\Api\TimeEntryActivity::listNames()` for listing the ids and names of all time entry activities.
1819

1920
### Deprecated
2021

src/Redmine/Api/TimeEntryActivity.php

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

20+
private $timeEntryActivityNames = null;
21+
2022
/**
2123
* List time entry activities.
2224
*
@@ -35,6 +37,29 @@ final public function list(array $params = []): array
3537
}
3638
}
3739

40+
/**
41+
* Returns an array of all time entry activities with id/name pairs.
42+
*
43+
* @return array<int,string> list of time entry activities (id => name)
44+
*/
45+
final public function listNames(): array
46+
{
47+
if ($this->timeEntryActivityNames !== null) {
48+
return $this->timeEntryActivityNames;
49+
}
50+
51+
$this->timeEntryActivityNames = [];
52+
$list = $this->list();
53+
54+
if (array_key_exists('time_entry_activities', $list)) {
55+
foreach ($list['time_entry_activities'] as $activity) {
56+
$this->timeEntryActivityNames[(int) $activity['id']] = $activity['name'];
57+
}
58+
}
59+
60+
return $this->timeEntryActivityNames;
61+
}
62+
3863
/**
3964
* List time entry activities.
4065
*

tests/Behat/Bootstrap/TimeEntryActivityContextTrait.php

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

tests/Behat/features/time_entry_activity.feature

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ Feature: Interacting with the REST API for time entry activities
1616
And the returned data "time_entry_activities" property is an array
1717
And the returned data "time_entry_activities" property contains "0" items
1818

19-
@wip
2019
Scenario: Listing of multiple time entry activities
2120
Given I have a "NativeCurlClient" client
2221
And I have a time entry activiy with name "Development"
@@ -59,3 +58,17 @@ Feature: Interacting with the REST API for time entry activities
5958
| is_default | false |
6059
| active | true |
6160

61+
Scenario: Listing of multiple time entry activity names
62+
Given I have a "NativeCurlClient" client
63+
And I have a time entry activiy with name "Development"
64+
And I have a time entry activiy with name "Support"
65+
When I list all time entry activity names
66+
Then the response has the status code "200"
67+
And the response has the content type "application/json"
68+
And the returned data is an array
69+
And the returned data contains "2" items
70+
And the returned data contains the following data
71+
| property | value |
72+
| 1 | Development |
73+
| 2 | Support |
74+
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\TimeEntryActivity;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\TestCase;
10+
use Redmine\Api\TimeEntryActivity;
11+
use Redmine\Tests\Fixtures\AssertingHttpClient;
12+
13+
#[CoversClass(TimeEntryActivity::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 TimeEntryActivity($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 time entry activities' => [
46+
'/enumerations/time_entry_activities.json',
47+
201,
48+
<<<JSON
49+
{
50+
"time_entry_activities": []
51+
}
52+
JSON,
53+
[],
54+
],
55+
'test with multiple time entry activities' => [
56+
'/enumerations/time_entry_activities.json',
57+
201,
58+
<<<JSON
59+
{
60+
"time_entry_activities": [
61+
{"id": 7, "name": "TimeEntryActivity 3"},
62+
{"id": 8, "name": "TimeEntryActivity 2"},
63+
{"id": 9, "name": "TimeEntryActivity 1"}
64+
]
65+
}
66+
JSON,
67+
[
68+
7 => "TimeEntryActivity 3",
69+
8 => "TimeEntryActivity 2",
70+
9 => "TimeEntryActivity 1",
71+
],
72+
],
73+
];
74+
}
75+
76+
public function testListNamesCallsHttpClientOnlyOnce()
77+
{
78+
$client = AssertingHttpClient::create(
79+
$this,
80+
[
81+
'GET',
82+
'/enumerations/time_entry_activities.json',
83+
'application/json',
84+
'',
85+
200,
86+
'application/json',
87+
<<<JSON
88+
{
89+
"time_entry_activities": [
90+
{
91+
"id": 1,
92+
"name": "TimeEntryActivity 1"
93+
}
94+
]
95+
}
96+
JSON,
97+
],
98+
);
99+
100+
// Create the object under test
101+
$api = new TimeEntryActivity($client);
102+
103+
// Perform the tests
104+
$this->assertSame([1 => 'TimeEntryActivity 1'], $api->listNames());
105+
$this->assertSame([1 => 'TimeEntryActivity 1'], $api->listNames());
106+
$this->assertSame([1 => 'TimeEntryActivity 1'], $api->listNames());
107+
}
108+
}

0 commit comments

Comments
 (0)