Skip to content

Commit

Permalink
Create IssueStatus::listNames()
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Jul 1, 2024
1 parent 9d58cf7 commit e30fcf9
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- New method `Redmine\Api\CustomField::listNames()` for listing the ids and names of all custom fields.
- 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.

### Deprecated

Expand Down
26 changes: 26 additions & 0 deletions src/Redmine/Api/IssueStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class IssueStatus extends AbstractApi
{
private $issueStatuses = [];

private $issueStatusNames = null;

/**
* List issue statuses.
*
Expand All @@ -37,6 +39,30 @@ final public function list(array $params = []): array
}
}

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

$this->issueStatusNames = [];

$list = $this->list();

if (array_key_exists('issue_statuses', $list)) {
foreach ($list['issue_statuses'] as $issueStatus) {
$this->issueStatusNames[(int) $issueStatus['id']] = (string) $issueStatus['name'];
}
}

return $this->issueStatusNames;
}

/**
* List issue statuses.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/Behat/Bootstrap/IssueStatusContextTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,18 @@ public function iListAllIssueStatuses()
$api->getLastResponse(),
);
}

/**
* @When I list all issue status names
*/
public function iListAllIssueStatusNames()
{
/** @var IssueStatus */
$api = $this->getNativeCurlClient()->getApi('issue_status');

$this->registerClientResponse(
$api->listNames(),
$api->getLastResponse(),
);
}
}
14 changes: 14 additions & 0 deletions tests/Behat/features/issue_status.feature
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,17 @@ Feature: Interacting with the REST API for issue statuses
| id | 2 |
| name | Done |
| is_closed | false |

Scenario: Listing of multiple issue status names
Given I have a "NativeCurlClient" client
And I have an issue status with the name "New"
And I have an issue status with the name "Done"
When I list all issue status names
Then the response has the status code "200"
And the response has the content type "application/json"
And the returned data is an array
And the returned data contains "2" items
And the returned data contains the following data
| property | value |
| 1 | New |
| 2 | Done |
108 changes: 108 additions & 0 deletions tests/Unit/Api/IssueStatus/ListNamesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

namespace Redmine\Tests\Unit\Api\IssueStatus;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Redmine\Api\IssueStatus;
use Redmine\Tests\Fixtures\AssertingHttpClient;

#[CoversClass(IssueStatus::class)]
class ListNamesTest extends TestCase
{
/**
* @dataProvider getListNamesData
*/
#[DataProvider('getListNamesData')]
public function testListNamesReturnsCorrectResponse($expectedPath, $responseCode, $response, $expectedResponse)
{
$client = AssertingHttpClient::create(
$this,
[
'GET',
$expectedPath,
'application/json',
'',
$responseCode,
'application/json',
$response,
],
);

// Create the object under test
$api = new IssueStatus($client);

// Perform the tests
$this->assertSame($expectedResponse, $api->listNames());
}

public static function getListNamesData(): array
{
return [
'test without issue statuses' => [
'/issue_statuses.json',
201,
<<<JSON
{
"issue_statuses": []
}
JSON,
[],
],
'test with multiple issue statuses' => [
'/issue_statuses.json',
201,
<<<JSON
{
"issue_statuses": [
{"id": 7, "name": "IssueStatus C"},
{"id": 8, "name": "IssueStatus B"},
{"id": 9, "name": "IssueStatus A"}
]
}
JSON,
[
7 => "IssueStatus C",
8 => "IssueStatus B",
9 => "IssueStatus A",
],
],
];
}

public function testListNamesCallsHttpClientOnlyOnce()
{
$client = AssertingHttpClient::create(
$this,
[
'GET',
'/issue_statuses.json',
'application/json',
'',
200,
'application/json',
<<<JSON
{
"issue_statuses": [
{
"id": 1,
"name": "IssueStatus 1"
}
]
}
JSON,
],
);

// Create the object under test
$api = new IssueStatus($client);

// Perform the tests
$this->assertSame([1 => 'IssueStatus 1'], $api->listNames());
$this->assertSame([1 => 'IssueStatus 1'], $api->listNames());
$this->assertSame([1 => 'IssueStatus 1'], $api->listNames());
}
}

0 comments on commit e30fcf9

Please sign in to comment.