Skip to content

Commit

Permalink
Merge branch 'v2.x' into add-user-listlogins
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Jul 8, 2024
2 parents 154903f + dfa6b49 commit 5da7204
Show file tree
Hide file tree
Showing 6 changed files with 296 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- 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.
- New method `Redmine\Api\TimeEntryActivity::listNames()` for listing the ids and names of all time entry activities.
- New method `Redmine\Api\Tracker::listNames()` for listing the ids and names of all trackers.
- New method `Redmine\Api\User::listLogins()` for listing the ids and logins of all users.

### Deprecated
Expand All @@ -27,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Redmine\Api\Project::listing()` is deprecated, use `\Redmine\Api\Project::listNames()` instead.
- `Redmine\Api\Role::listing()` is deprecated, use `\Redmine\Api\Role::listNames()` instead.
- `Redmine\Api\TimeEntryActivity::listing()` is deprecated, use `\Redmine\Api\TimeEntryActivity::listNames()` instead.
- `Redmine\Api\Tracker::listing()` is deprecated, use `\Redmine\Api\Tracker::listNames()` instead.
- `Redmine\Api\User::listing()` is deprecated, use `\Redmine\Api\User::listLogins()` instead.

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

private $trackerNames = null;

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

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

$this->trackerNames = [];
$list = $this->list();

if (array_key_exists('trackers', $list)) {
foreach ($list['trackers'] as $role) {
$this->trackerNames[(int) $role['id']] = $role['name'];
}
}

return $this->trackerNames;
}

/**
* List trackers.
*
Expand Down Expand Up @@ -73,12 +98,17 @@ public function all(array $params = [])
/**
* Returns an array of trackers with name/id pairs.
*
* @deprecated v2.7.0 Use listNames() instead.
* @see Tracker::listNames()
*
* @param bool $forceUpdate to force the update of the trackers var
*
* @return array list of trackers (id => name)
*/
public function listing($forceUpdate = false)
{
@trigger_error('`' . __METHOD__ . '()` is deprecated since v2.7.0, use `' . __CLASS__ . '::listNames()` instead.', E_USER_DEPRECATED);

if (empty($this->trackers) || $forceUpdate) {
$this->trackers = $this->list();
}
Expand Down
30 changes: 30 additions & 0 deletions tests/Behat/Bootstrap/TrackerContextTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Redmine\Tests\Behat\Bootstrap;

use Redmine\Api\Tracker;

trait TrackerContextTrait
{
/**
Expand All @@ -24,4 +26,32 @@ public function iHaveATrackerWithTheNameAndDefaultStatusId(string $trackerName,
],
);
}

/**
* @When I list all trackers
*/
public function iListAllTrackers()
{
/** @var Tracker */
$api = $this->getNativeCurlClient()->getApi('tracker');

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

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

$this->registerClientResponse(
$api->listNames(),
$api->getLastResponse(),
);
}
}
94 changes: 94 additions & 0 deletions tests/Behat/features/tracker.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
@tracker
Feature: Interacting with the REST API for trackers
In order to interact with REST API for trackers
As a user
I want to make sure the Redmine server replies with the correct response

Scenario: Listing of zero trackers
Given I have a "NativeCurlClient" client
When I list all trackers
Then the response has the status code "200"
And the response has the content type "application/json"
And the returned data has only the following properties
"""
trackers
"""
And the returned data "trackers" property is an array
And the returned data "trackers" property contains "0" items

Scenario: Listing of multiple trackers
Given I have a "NativeCurlClient" client
And I have an issue status with the name "New"
And I have a tracker with the name "Feature" and default status id "1"
And I have a tracker with the name "Defect" and default status id "1"
When I list all trackers
Then the response has the status code "200"
And the response has the content type "application/json"
And the returned data has only the following properties
"""
trackers
"""
And the returned data "trackers" property is an array
And the returned data "trackers" property contains "2" items
And the returned data "trackers.0" property is an array
And the returned data "trackers.0" property has only the following properties with Redmine version ">= 5.0.0"
"""
id
name
default_status
description
enabled_standard_fields
"""
But the returned data "trackers.0" property has only the following properties with Redmine version "< 5.0.0"
"""
id
name
default_status
description
"""
And the returned data "trackers.0" property contains the following data
| property | value |
| id | 1 |
| name | Feature |
| description | null |
And the returned data "trackers.0.default_status" property contains the following data
| property | value |
| id | 1 |
| name | New |
And the returned data "trackers.0.enabled_standard_fields" property contains the following data with Redmine version ">= 5.1.0"
| property | value |
| 0 | assigned_to_id |
| 1 | category_id |
| 2 | fixed_version_id |
| 3 | parent_issue_id |
| 4 | start_date |
| 5 | due_date |
| 6 | estimated_hours |
| 7 | done_ratio |
| 8 | description |
| 9 | priority_id |
But the returned data "trackers.0.enabled_standard_fields" property contains the following data with Redmine version ">= 5.0.0"
| property | value |
| 0 | assigned_to_id |
| 1 | category_id |
| 2 | fixed_version_id |
| 3 | parent_issue_id |
| 4 | start_date |
| 5 | due_date |
| 6 | estimated_hours |
| 7 | done_ratio |
| 8 | description |

Scenario: Listing of multiple tracker names
Given I have a "NativeCurlClient" client
And I have an issue status with the name "New"
And I have a tracker with the name "Feature" and default status id "1"
And I have a tracker with the name "Defect" and default status id "1"
When I list all tracker 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 contains the following data
| property | value |
| 1 | Feature |
| 2 | Defect |
108 changes: 108 additions & 0 deletions tests/Unit/Api/Tracker/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\Tracker;

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

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

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

public static function getListNamesData(): array
{
return [
'test without trackers' => [
'/trackers.json',
201,
<<<JSON
{
"trackers": []
}
JSON,
[],
],
'test with multiple trackers' => [
'/trackers.json',
201,
<<<JSON
{
"trackers": [
{"id": 7, "name": "Tracker 3"},
{"id": 8, "name": "Tracker 2"},
{"id": 9, "name": "Tracker 1"}
]
}
JSON,
[
7 => "Tracker 3",
8 => "Tracker 2",
9 => "Tracker 1",
],
],
];
}

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

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

// Perform the tests
$this->assertSame([1 => 'Tracker 1'], $api->listNames());
$this->assertSame([1 => 'Tracker 1'], $api->listNames());
$this->assertSame([1 => 'Tracker 1'], $api->listNames());
}
}
32 changes: 32 additions & 0 deletions tests/Unit/Api/TrackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,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('{"trackers":[{"id":1,"name":"Tracker 1"},{"id":5,"name":"Tracker 5"}]}');
$client->method('getLastResponseContentType')
->willReturn('application/json');

$api = new Tracker($client);

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

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

$api->listing();
}

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

0 comments on commit 5da7204

Please sign in to comment.