Skip to content

Commit

Permalink
Merge branch 'v2.x' into add-role-listnames
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Jul 5, 2024
2 parents 05bc969 + 8fab7fb commit a2ea0d4
Show file tree
Hide file tree
Showing 8 changed files with 336 additions and 23 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- New method `Redmine\Api\Group::listNames()` for listing the ids and names of all groups.
- New method `Redmine\Api\IssueCategory::listNamesByProject()` for listing the ids and names of all issue categories of a project.
- New method `Redmine\Api\IssueStatus::listNames()` for listing the ids and names of all issue statuses.
- New method `Redmine\Api\Project::listNames()` for listing the ids and names of all projects.
- New method `Redmine\Api\Role::listNames()` for listing the ids and names of all roles.

### Deprecated
Expand All @@ -21,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Redmine\Api\Group::listing()` is deprecated, use `\Redmine\Api\Group::listNames()` instead.
- `Redmine\Api\IssueCategory::listing()` is deprecated, use `\Redmine\Api\IssueCategory::listNamesByProject()` instead.
- `Redmine\Api\IssueStatus::listing()` is deprecated, use `\Redmine\Api\IssueStatus::listNamesByProject()` instead.
- `Redmine\Api\Project::listing()` is deprecated, use `\Redmine\Api\Project::listNamesByProject()` instead.
- `Redmine\Api\Role::listing()` is deprecated, use `\Redmine\Api\Role::listNamesByProject()` instead.

## [v2.6.0](https://github.com/kbsali/php-redmine-api/compare/v2.5.0...v2.6.0) - 2024-03-25
Expand Down
44 changes: 44 additions & 0 deletions src/Redmine/Api/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class Project extends AbstractApi
{
private $projects = [];

private $projectNames = null;

/**
* List projects.
*
Expand All @@ -44,6 +46,43 @@ final public function list(array $params = []): array
}
}

/**
* Returns an array of all projects with id/name pairs.
*
* @return array<int,string> list of projects (id => name)
*/
final public function listNames(): array
{
if ($this->projectNames !== null) {
return $this->projectNames;
}

$this->projectNames = [];

$limit = 100;
$offset = 0;

do {
$list = $this->list([
'limit' => $limit,
'offset' => $offset,
]);

$listCount = 0;
$offset += $limit;

if (array_key_exists('projects', $list)) {
$listCount = count($list['projects']);

foreach ($list['projects'] as $issueStatus) {
$this->projectNames[(int) $issueStatus['id']] = (string) $issueStatus['name'];
}
}
} while ($listCount === $limit);

return $this->projectNames;
}

/**
* List projects.
*
Expand Down Expand Up @@ -80,6 +119,9 @@ public function all(array $params = [])
/**
* Returns an array of projects with name/id pairs (or id/name if $reserse is false).
*
* @deprecated v2.7.0 Use listNames() instead.
* @see Project::listNames()
*
* @param bool $forceUpdate to force the update of the projects var
* @param bool $reverse to return an array indexed by name rather than id
* @param array $params to allow offset/limit (and more) to be passed
Expand All @@ -88,6 +130,8 @@ public function all(array $params = [])
*/
public function listing($forceUpdate = false, $reverse = true, array $params = [])
{
@trigger_error('`' . __METHOD__ . '()` is deprecated since v2.7.0, use `' . __CLASS__ . '::listNames()` instead.', E_USER_DEPRECATED);

if (true === $forceUpdate || empty($this->projects)) {
$this->projects = $this->list($params);
}
Expand Down
28 changes: 28 additions & 0 deletions tests/Behat/Bootstrap/IssueCategoryContextTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,34 @@

trait IssueCategoryContextTrait
{
/**
* @Given I create :count issue categories for project identifier :identifier
*/
public function iCreateIssueCategoriesForProjectIdentifier(int $count, $identifier)
{
while ($count > 0) {
$this->iCreateAnIssueCategoryForProjectIdentifierAndWithTheName(
$identifier,
'Issue Category ' . $count,
);

$count--;
}
}

/**
* @When I create an issue category for project identifier :identifier and with the name :name
*/
public function iCreateAnIssueCategoryForProjectIdentifierAndWithTheName($identifier, $name)
{
$table = new TableNode([
['property', 'value'],
['name', $name],
]);

$this->iCreateAnIssueCategoryForProjectIdentifierAndWithTheFollowingData($identifier, $table);
}

/**
* @When I create an issue category for project identifier :identifier and with the following data
*/
Expand Down
26 changes: 26 additions & 0 deletions tests/Behat/Bootstrap/ProjectContextTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ public function iCreateAProjectWithTheFollowingData(TableNode $table)
);
}

/**
* @Given I create :count projects
*/
public function iCreateProjects(int $count)
{
while ($count > 0) {
$this->iCreateAProjectWithNameAndIdentifier('Test Project ' . $count, 'test-project-' . $count);

$count--;
}
}

/**
* @When I list all projects
*/
Expand All @@ -57,6 +69,20 @@ public function iListAllProjects()
);
}

/**
* @When I list all project names
*/
public function iListAllProjectNames()
{
/** @var Project */
$api = $this->getNativeCurlClient()->getApi('project');

$this->registerClientResponse(
$api->listNames(),
$api->getLastResponse(),
);
}

/**
* @When I show the project with identifier :identifier
*/
Expand Down
39 changes: 16 additions & 23 deletions tests/Behat/features/issue_category.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ Feature: Interacting with the REST API for issue categories
Scenario: Creating an issue category with miminal data
Given I have a "NativeCurlClient" client
And I create a project with name "Test Project" and identifier "test-project"
When I create an issue category for project identifier "test-project" and with the following data
| property | value |
| name | Category name |
When I create an issue category for project identifier "test-project" and with the name "Category name"
Then the response has the status code "201"
And the response has the content type "application/xml"
And the returned data is an instance of "SimpleXMLElement"
Expand Down Expand Up @@ -101,12 +99,8 @@ Feature: Interacting with the REST API for issue categories
Scenario: Listing of multiple issue categories
Given I have a "NativeCurlClient" client
And I create a project with name "Test Project" and identifier "test-project"
And I create an issue category for project identifier "test-project" and with the following data
| property | value |
| name | Category name B |
And I create an issue category for project identifier "test-project" and with the following data
| property | value |
| name | Category name A |
And I create an issue category for project identifier "test-project" and with the name "Category name B"
And I create an issue category for project identifier "test-project" and with the name "Category name A"
When I list all issue categories for project identifier "test-project"
Then the response has the status code "200"
And the response has the content type "application/json"
Expand Down Expand Up @@ -154,12 +148,8 @@ Feature: Interacting with the REST API for issue categories
Scenario: Listing of multiple issue category names
Given I have a "NativeCurlClient" client
And I create a project with name "Test Project" and identifier "test-project"
And I create an issue category for project identifier "test-project" and with the following data
| property | value |
| name | Category name B |
And I create an issue category for project identifier "test-project" and with the following data
| property | value |
| name | Category name A |
And I create an issue category for project identifier "test-project" and with the name "Category name B"
And I create an issue category for project identifier "test-project" and with the name "Category name A"
When I list all issue category names for project identifier "test-project"
Then the response has the status code "200"
And the response has the content type "application/json"
Expand All @@ -168,13 +158,19 @@ Feature: Interacting with the REST API for issue categories
| 1 | Category name B |
| 2 | Category name A |

Scenario: Listing a lot of issue category names
Given I have a "NativeCurlClient" client
And I create a project with name "Test Project" and identifier "test-project"
And I create "108" issue categories for project identifier "test-project"
When I list all issue category names for project identifier "test-project"
Then the response has the status code "200"
And the response has the content type "application/json"
And the returned data contains "108" items

Scenario: Updating an issue category with all data
Given I have a "NativeCurlClient" client
And I create a project with name "Test Project" and identifier "test-project"
And I create an issue category for project identifier "test-project" and with the following data
| property | value |
| name | Category name |
| assigned_to_id | 1 |
And I create an issue category for project identifier "test-project" and with the name "Category name"
When I update the issue category with id "1" and the following data
| property | value |
| name | New category name |
Expand All @@ -187,10 +183,7 @@ Feature: Interacting with the REST API for issue categories
Scenario: Deleting an issue category
Given I have a "NativeCurlClient" client
And I create a project with name "Test Project" and identifier "test-project"
And I create an issue category for project identifier "test-project" and with the following data
| property | value |
| name | Category name |
| assigned_to_id | 1 |
And I create an issue category for project identifier "test-project" and with the name "Category name"
When I remove the issue category with id "1"
Then the response has the status code "204"
And the response has an empty content type
Expand Down
21 changes: 21 additions & 0 deletions tests/Behat/features/projects.feature
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,27 @@ Feature: Interacting with the REST API for projects
updated_on
"""

Scenario: Listing of multiple project names
Given I have a "NativeCurlClient" client
And I create a project with name "Test Project B" and identifier "test-project1"
And I create a project with name "Test Project A" and identifier "test-project2"
When I list all project names
Then the response has the status code "200"
And the response has the content type "application/json"
And the returned data contains "2" items
And the returned data has proterties with the following data
| property | value |
| 1 | Test Project B |
| 2 | Test Project A |

Scenario: Listing of multiple project names
Given I have a "NativeCurlClient" client
And I create "108" projects
When I list all project names
Then the response has the status code "200"
And the response has the content type "application/json"
And the returned data contains "108" items

Scenario: Updating a project
Given I have a "NativeCurlClient" client
And I create a project with name "Test Project" and identifier "test-project"
Expand Down
Loading

0 comments on commit a2ea0d4

Please sign in to comment.