Skip to content

Commit a4da285

Browse files
authored
Merge pull request #421 from Art4/add-version-listnamesbyproject
Add `Version::listNamesByProject()` method as replacement for `Version::listing()`
2 parents 324a946 + 9424d2e commit a4da285

File tree

6 files changed

+335
-25
lines changed

6 files changed

+335
-25
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818
- New method `Redmine\Api\TimeEntryActivity::listNames()` for listing the ids and names of all time entry activities.
1919
- New method `Redmine\Api\Tracker::listNames()` for listing the ids and names of all trackers.
2020
- New method `Redmine\Api\User::listLogins()` for listing the ids and logins of all users.
21+
- New method `Redmine\Api\Version::listNamesByProject()` for listing the ids and names of all versions of a project.
2122

2223
### Deprecated
2324

@@ -30,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3031
- `Redmine\Api\TimeEntryActivity::listing()` is deprecated, use `\Redmine\Api\TimeEntryActivity::listNames()` instead.
3132
- `Redmine\Api\Tracker::listing()` is deprecated, use `\Redmine\Api\Tracker::listNames()` instead.
3233
- `Redmine\Api\User::listing()` is deprecated, use `\Redmine\Api\User::listLogins()` instead.
34+
- `Redmine\Api\Version::listing()` is deprecated, use `\Redmine\Api\Version::listNamesByProject()` instead.
3335

3436
## [v2.6.0](https://github.com/kbsali/php-redmine-api/compare/v2.5.0...v2.6.0) - 2024-03-25
3537

src/Redmine/Api/Version.php

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

26+
private $versionNames = [];
27+
2628
/**
2729
* List versions of a project.
2830
*
@@ -51,6 +53,41 @@ final public function listByProject($projectIdentifier, array $params = []): arr
5153
}
5254
}
5355

56+
/**
57+
* Returns an array of all versions by a project with id/name pairs.
58+
*
59+
* @param string|int $projectIdentifier project id or literal identifier
60+
*
61+
* @throws InvalidParameterException if $projectIdentifier is not of type int or string
62+
*
63+
* @return array<int,string> list of version names (id => name)
64+
*/
65+
final public function listNamesByProject($projectIdentifier): array
66+
{
67+
if (! is_int($projectIdentifier) && ! is_string($projectIdentifier)) {
68+
throw new InvalidParameterException(sprintf(
69+
'%s(): Argument #1 ($projectIdentifier) must be of type int or string',
70+
__METHOD__,
71+
));
72+
}
73+
74+
if (array_key_exists($projectIdentifier, $this->versionNames)) {
75+
return $this->versionNames[$projectIdentifier];
76+
}
77+
78+
$this->versionNames[$projectIdentifier] = [];
79+
80+
$list = $this->listByProject($projectIdentifier);
81+
82+
if (array_key_exists('versions', $list)) {
83+
foreach ($list['versions'] as $version) {
84+
$this->versionNames[$projectIdentifier][(int) $version['id']] = $version['name'];
85+
}
86+
}
87+
88+
return $this->versionNames[$projectIdentifier];
89+
}
90+
5491
/**
5592
* List versions.
5693
*
@@ -88,6 +125,9 @@ public function all($project, array $params = [])
88125
/**
89126
* Returns an array of name/id pairs (or id/name if not $reverse) of versions for $project.
90127
*
128+
* @deprecated v2.7.0 Use listNamesByProject() instead.
129+
* @see Version::listNamesByProject()
130+
*
91131
* @param string|int $project project id or literal identifier
92132
* @param bool $forceUpdate to force the update of the projects var
93133
* @param bool $reverse to return an array indexed by name rather than id
@@ -97,6 +137,8 @@ public function all($project, array $params = [])
97137
*/
98138
public function listing($project, $forceUpdate = false, $reverse = true, array $params = [])
99139
{
140+
@trigger_error('`' . __METHOD__ . '()` is deprecated since v2.7.0, use `' . __CLASS__ . '::listNamesByProject()` instead.', E_USER_DEPRECATED);
141+
100142
if (true === $forceUpdate || empty($this->versions)) {
101143
$this->versions = $this->listByProject($project, $params);
102144
}

tests/Behat/Bootstrap/VersionContextTrait.php

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,35 +45,63 @@ public function iCreateAVersionWithProjectIdentifierAndWithTheFollowingData(stri
4545
}
4646

4747
/**
48-
* @When I update the version with id :id and the following data
48+
* @When I show the version with id :versionId
4949
*/
50-
public function iUpdateTheVersionWithIdAndTheFollowingData($id, TableNode $table)
50+
public function iShowTheVersionWithId(int $versionId)
5151
{
52-
$data = [];
52+
/** @var Version */
53+
$api = $this->getNativeCurlClient()->getApi('version');
5354

54-
foreach ($table as $row) {
55-
$data[$row['property']] = $row['value'];
56-
}
55+
$this->registerClientResponse(
56+
$api->show($versionId),
57+
$api->getLastResponse(),
58+
);
59+
}
5760

61+
/**
62+
* @When I list all versions for project identifier :identifier
63+
*/
64+
public function iListAllVersionsForProjectIdentifier($identifier)
65+
{
5866
/** @var Version */
5967
$api = $this->getNativeCurlClient()->getApi('version');
6068

6169
$this->registerClientResponse(
62-
$api->update($id, $data),
70+
$api->listByProject($identifier),
6371
$api->getLastResponse(),
6472
);
6573
}
6674

6775
/**
68-
* @When I show the version with id :versionId
76+
* @When I list all version names for project identifier :identifier
6977
*/
70-
public function iShowTheVersionWithId(int $versionId)
78+
public function iListAllVersionNamesForProjectIdentifier($identifier)
7179
{
7280
/** @var Version */
7381
$api = $this->getNativeCurlClient()->getApi('version');
7482

7583
$this->registerClientResponse(
76-
$api->show($versionId),
84+
$api->listNamesByProject($identifier),
85+
$api->getLastResponse(),
86+
);
87+
}
88+
89+
/**
90+
* @When I update the version with id :id and the following data
91+
*/
92+
public function iUpdateTheVersionWithIdAndTheFollowingData($id, TableNode $table)
93+
{
94+
$data = [];
95+
96+
foreach ($table as $row) {
97+
$data[$row['property']] = $row['value'];
98+
}
99+
100+
/** @var Version */
101+
$api = $this->getNativeCurlClient()->getApi('version');
102+
103+
$this->registerClientResponse(
104+
$api->update($id, $data),
77105
$api->getLastResponse(),
78106
);
79107
}

tests/Behat/features/version.feature

Lines changed: 93 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,6 @@ Feature: Interacting with the REST API for versions
5252
| id | 1 |
5353
| name | Test Project |
5454

55-
Scenario: Updating a version
56-
Given I have a "NativeCurlClient" client
57-
And I create a project with name "Test Project" and identifier "test-project"
58-
And I create a version with project identifier "test-project" with the following data
59-
| property | value |
60-
| name | Test-Version |
61-
When I update the version with id "1" and the following data
62-
| property | value |
63-
| name | New Version name |
64-
Then the response has the status code "204"
65-
And the response has an empty content type
66-
And the response has the content ""
67-
And the returned data is exactly ""
68-
6955
Scenario: Showing a version
7056
Given I have a "NativeCurlClient" client
7157
And I create a project with name "Test Project" and identifier "test-project"
@@ -115,6 +101,99 @@ Feature: Interacting with the REST API for versions
115101
And the response has the content ""
116102
And the returned data is false
117103

104+
Scenario: Listing of zero versions
105+
Given I have a "NativeCurlClient" client
106+
And I create a project with name "Test Project" and identifier "test-project"
107+
When I list all versions for project identifier "test-project"
108+
Then the response has the status code "200"
109+
And the response has the content type "application/json"
110+
And the returned data has only the following properties
111+
"""
112+
versions
113+
total_count
114+
"""
115+
And the returned data contains the following data
116+
| property | value |
117+
| versions | [] |
118+
| total_count | 0 |
119+
120+
Scenario: Listing of multiple versions
121+
Given I have a "NativeCurlClient" client
122+
And I create a project with name "Test Project" and identifier "test-project"
123+
And I create a version with name "Test-Version B" and project identifier "test-project"
124+
And I create a version with name "Test-Version A" and project identifier "test-project"
125+
When I list all versions for project identifier "test-project"
126+
Then the response has the status code "200"
127+
And the response has the content type "application/json"
128+
And the returned data has only the following properties
129+
"""
130+
versions
131+
total_count
132+
"""
133+
And the returned data contains the following data
134+
| property | value |
135+
| total_count | 2 |
136+
And the returned data "versions" property is an array
137+
And the returned data "versions" property contains "2" items
138+
And the returned data "versions.0" property is an array
139+
And the returned data "versions.0" property has only the following properties
140+
"""
141+
id
142+
project
143+
name
144+
description
145+
status
146+
due_date
147+
sharing
148+
wiki_page_title
149+
created_on
150+
updated_on
151+
"""
152+
And the returned data "versions.0" property contains the following data
153+
| property | value |
154+
| id | 1 |
155+
| name | Test-Version B |
156+
| description | |
157+
| status | open |
158+
| sharing | none |
159+
| wiki_page_title | null |
160+
And the returned data "versions.0.project" property contains the following data
161+
| property | value |
162+
| id | 1 |
163+
| name | Test Project |
164+
165+
@wip
166+
Scenario: Listing of multiple version names
167+
Given I have a "NativeCurlClient" client
168+
And I create a project with name "Test Project 1" and identifier "test-project-1"
169+
And I create a project with name "Test Project 2" and identifier "test-project-2"
170+
And I create a version with name "Test-Version 1B" and project identifier "test-project-1"
171+
And I create a version with name "Test-Version 1A" and project identifier "test-project-1"
172+
And I create a version with name "Test-Version 2B" and project identifier "test-project-2"
173+
And I create a version with name "Test-Version 2A" and project identifier "test-project-2"
174+
When I list all version names for project identifier "test-project-2"
175+
Then the response has the status code "200"
176+
And the response has the content type "application/json"
177+
And the returned data contains "2" items
178+
And the returned data contains the following data
179+
| property | value |
180+
| 3 | Test-Version 2B |
181+
| 4 | Test-Version 2A |
182+
183+
Scenario: Updating a version
184+
Given I have a "NativeCurlClient" client
185+
And I create a project with name "Test Project" and identifier "test-project"
186+
And I create a version with project identifier "test-project" with the following data
187+
| property | value |
188+
| name | Test-Version |
189+
When I update the version with id "1" and the following data
190+
| property | value |
191+
| name | New Version name |
192+
Then the response has the status code "204"
193+
And the response has an empty content type
194+
And the response has the content ""
195+
And the returned data is exactly ""
196+
118197
Scenario: Removing a version
119198
Given I have a "NativeCurlClient" client
120199
And I create a project with name "Test Project" and identifier "test-project"

0 commit comments

Comments
 (0)