Skip to content

Add Project::listNames() method as replacement for Project::listing() #411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ 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.

### Deprecated

- `Redmine\Api\CustomField::listing()` is deprecated, use `\Redmine\Api\CustomField::listNames()` instead.
- `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.

## [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
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
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
167 changes: 167 additions & 0 deletions tests/Unit/Api/Project/ListNamesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php

declare(strict_types=1);

namespace Redmine\Tests\Unit\Api\Project;

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

#[CoversClass(Project::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 Project($client);

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

public static function getListNamesData(): array
{
return [
'test without projects' => [
'/projects.json?limit=100&offset=0',
201,
<<<JSON
{
"projects": []
}
JSON,
[],
],
'test with multiple projects' => [
'/projects.json?limit=100&offset=0',
201,
<<<JSON
{
"projects": [
{"id": 7, "name": "Project C"},
{"id": 8, "name": "Project B"},
{"id": 9, "name": "Project A"}
]
}
JSON,
[
7 => "Project C",
8 => "Project B",
9 => "Project A",
],
],
];
}

public function testListNamesWithALotOfProjectsHandlesPagination()
{
$assertData = [];
$projectsRequest1 = [];
$projectsRequest2 = [];
$projectsRequest3 = [];

for ($i = 1; $i <= 100; $i++) {
$name = 'Project ' . $i;

$assertData[$i] = $name;
$projectsRequest1[] = ['id' => $i, 'name' => $name];
}

for ($i = 101; $i <= 200; $i++) {
$name = 'Project ' . $i;

$assertData[$i] = $name;
$projectsRequest2[] = ['id' => $i, 'name' => $name];
}

$client = AssertingHttpClient::create(
$this,
[
'GET',
'/projects.json?limit=100&offset=0',
'application/json',
'',
200,
'application/json',
json_encode(['projects' => $projectsRequest1]),
],
[
'GET',
'/projects.json?limit=100&offset=100',
'application/json',
'',
200,
'application/json',
json_encode(['projects' => $projectsRequest2]),
],
[
'GET',
'/projects.json?limit=100&offset=200',
'application/json',
'',
200,
'application/json',
json_encode(['projects' => $projectsRequest3]),
],
);

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

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

public function testListNamesCallsHttpClientOnlyOnce()
{
$client = AssertingHttpClient::create(
$this,
[
'GET',
'/projects.json?limit=100&offset=0',
'application/json',
'',
200,
'application/json',
<<<JSON
{
"projects": [
{
"id": 1,
"name": "Project 1"
}
]
}
JSON,
],
);

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

// Perform the tests
$this->assertSame([1 => 'Project 1'], $api->listNames());
$this->assertSame([1 => 'Project 1'], $api->listNames());
$this->assertSame([1 => 'Project 1'], $api->listNames());
}
}
32 changes: 32 additions & 0 deletions tests/Unit/Api/ProjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,38 @@ public function testListingCallsGetEveryTimeWithForceUpdate()
$this->assertSame($expectedReturn, $api->listing(true));
}

/**
* Test listing().
*/
public function testListingTriggersDeprecationWarning()
{
$client = $this->createMock(Client::class);
$client->method('requestGet')
->willReturn(true);
$client->method('getLastResponseBody')
->willReturn('{"projects":[{"id":1,"name":"Project 1"},{"id":5,"name":"Project 5"}]}');
$client->method('getLastResponseContentType')
->willReturn('application/json');

$api = new Project($client);

// PHPUnit 10 compatible way to test trigger_error().
set_error_handler(
function ($errno, $errstr): bool {
$this->assertSame(
'`Redmine\Api\Project::listing()` is deprecated since v2.7.0, use `Redmine\Api\Project::listNames()` instead.',
$errstr,
);

restore_error_handler();
return true;
},
E_USER_DEPRECATED,
);

$api->listing();
}

/**
* Test getIdByName().
*/
Expand Down
Loading