Skip to content

Commit

Permalink
Merge branch 'v2.x' into 417-deprecate-all-redmineapigetidbyname-methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Jul 8, 2024
2 parents 71655dc + a4da285 commit 5cc6d37
Show file tree
Hide file tree
Showing 13 changed files with 781 additions and 75 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- 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.
- New method `Redmine\Api\Version::listNamesByProject()` for listing the ids and names of all versions of a project.

### Deprecated

Expand All @@ -29,6 +31,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `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.
- `Redmine\Api\Version::listing()` is deprecated, use `\Redmine\Api\Version::listNamesByProject()` instead.

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

Expand Down
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@
"sort-packages": true
},
"scripts": {
"behat": "behat --config tests/Behat/behat.yml --format progress",
"bdt": [
"@behat --format=progress --suite=redmine_50103",
"@behat --format=progress --suite=redmine_50009",
"@behat --format=progress --suite=redmine_40210"
],
"behat": "behat --config tests/Behat/behat.yml",
"codestyle": "php-cs-fixer fix",
"coverage": "phpunit --coverage-html=\".phpunit.cache/code-coverage\"",
"phpstan": "phpstan analyze --memory-limit 512M --configuration .phpstan.neon",
Expand Down
44 changes: 44 additions & 0 deletions src/Redmine/Api/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class User extends AbstractApi
{
private $users = [];

private $userLogins = null;

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

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

$this->userLogins = [];

$limit = 100;
$offset = 0;

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

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

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

foreach ($list['users'] as $user) {
$this->userLogins[(int) $user['id']] = (string) $user['login'];
}
}
} while ($listCount === $limit);

return $this->userLogins;
}

/**
* List users.
*
Expand Down Expand Up @@ -79,13 +118,18 @@ public function all(array $params = [])
/**
* Returns an array of users with login/id pairs.
*
* @deprecated v2.7.0 Use listLogins() instead.
* @see User::listLogins()
*
* @param bool $forceUpdate to force the update of the users var
* @param array $params to allow offset/limit (and more) to be passed
*
* @return array list of users (id => username)
*/
public function listing($forceUpdate = false, array $params = [])
{
@trigger_error('`' . __METHOD__ . '()` is deprecated since v2.7.0, use `' . __CLASS__ . '::listLogins()` instead.', E_USER_DEPRECATED);

if (empty($this->users) || $forceUpdate) {
$this->users = $this->list($params);
}
Expand Down
42 changes: 42 additions & 0 deletions src/Redmine/Api/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Version extends AbstractApi
{
private $versions = [];

private $versionNames = [];

/**
* List versions of a project.
*
Expand Down Expand Up @@ -51,6 +53,41 @@ final public function listByProject($projectIdentifier, array $params = []): arr
}
}

/**
* Returns an array of all versions by a project with id/name pairs.
*
* @param string|int $projectIdentifier project id or literal identifier
*
* @throws InvalidParameterException if $projectIdentifier is not of type int or string
*
* @return array<int,string> list of version names (id => name)
*/
final public function listNamesByProject($projectIdentifier): array
{
if (! is_int($projectIdentifier) && ! is_string($projectIdentifier)) {
throw new InvalidParameterException(sprintf(
'%s(): Argument #1 ($projectIdentifier) must be of type int or string',
__METHOD__,
));
}

if (array_key_exists($projectIdentifier, $this->versionNames)) {
return $this->versionNames[$projectIdentifier];
}

$this->versionNames[$projectIdentifier] = [];

$list = $this->listByProject($projectIdentifier);

if (array_key_exists('versions', $list)) {
foreach ($list['versions'] as $version) {
$this->versionNames[$projectIdentifier][(int) $version['id']] = $version['name'];
}
}

return $this->versionNames[$projectIdentifier];
}

/**
* List versions.
*
Expand Down Expand Up @@ -88,6 +125,9 @@ public function all($project, array $params = [])
/**
* Returns an array of name/id pairs (or id/name if not $reverse) of versions for $project.
*
* @deprecated v2.7.0 Use listNamesByProject() instead.
* @see Version::listNamesByProject()
*
* @param string|int $project project id or literal identifier
* @param bool $forceUpdate to force the update of the projects var
* @param bool $reverse to return an array indexed by name rather than id
Expand All @@ -97,6 +137,8 @@ public function all($project, array $params = [])
*/
public function listing($project, $forceUpdate = false, $reverse = true, array $params = [])
{
@trigger_error('`' . __METHOD__ . '()` is deprecated since v2.7.0, use `' . __CLASS__ . '::listNamesByProject()` instead.', E_USER_DEPRECATED);

if (true === $forceUpdate || empty($this->versions)) {
$this->versions = $this->listByProject($project, $params);
}
Expand Down
68 changes: 58 additions & 10 deletions tests/Behat/Bootstrap/UserContextTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@

trait UserContextTrait
{
/**
* @Given I create :count users
*/
public function iCreateUsers(int $count)
{
while ($count > 0) {
$table = new TableNode([
['property', 'value'],
['login', 'testuser_' . $count],
['firstname', 'first'],
['lastname', 'last'],
['mail', 'mail.' . $count . '@example.net'],
]);

$this->iCreateAUserWithTheFollowingData($table);

$count--;
}
}

/**
* @When I create a user with the following data
*/
Expand All @@ -30,35 +50,63 @@ public function iCreateAUserWithTheFollowingData(TableNode $table)
}

/**
* @When I update the user with id :id and the following data
* @When I show the user with id :userId
*/
public function iUpdateTheUserWithIdAndTheFollowingData($id, TableNode $table)
public function iShowTheUserWithId(int $userId)
{
$data = [];
/** @var User */
$api = $this->getNativeCurlClient()->getApi('user');

foreach ($table as $row) {
$data[$row['property']] = $row['value'];
}
$this->registerClientResponse(
$api->show($userId),
$api->getLastResponse(),
);
}

/**
* @When I list all users
*/
public function iListAllUsers()
{
/** @var User */
$api = $this->getNativeCurlClient()->getApi('user');

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

/**
* @When I list all user logins
*/
public function iListAllUserLogins()
{
/** @var User */
$api = $this->getNativeCurlClient()->getApi('user');

$this->registerClientResponse(
$api->update($id, $data),
$api->listLogins(),
$api->getLastResponse(),
);
}

/**
* @When I show the user with id :userId
* @When I update the user with id :id and the following data
*/
public function iShowTheUserWithId(int $userId)
public function iUpdateTheUserWithIdAndTheFollowingData($id, TableNode $table)
{
$data = [];

foreach ($table as $row) {
$data[$row['property']] = $row['value'];
}

/** @var User */
$api = $this->getNativeCurlClient()->getApi('user');

$this->registerClientResponse(
$api->show($userId),
$api->update($id, $data),
$api->getLastResponse(),
);
}
Expand Down
48 changes: 38 additions & 10 deletions tests/Behat/Bootstrap/VersionContextTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,63 @@ public function iCreateAVersionWithProjectIdentifierAndWithTheFollowingData(stri
}

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

foreach ($table as $row) {
$data[$row['property']] = $row['value'];
}
$this->registerClientResponse(
$api->show($versionId),
$api->getLastResponse(),
);
}

/**
* @When I list all versions for project identifier :identifier
*/
public function iListAllVersionsForProjectIdentifier($identifier)
{
/** @var Version */
$api = $this->getNativeCurlClient()->getApi('version');

$this->registerClientResponse(
$api->update($id, $data),
$api->listByProject($identifier),
$api->getLastResponse(),
);
}

/**
* @When I show the version with id :versionId
* @When I list all version names for project identifier :identifier
*/
public function iShowTheVersionWithId(int $versionId)
public function iListAllVersionNamesForProjectIdentifier($identifier)
{
/** @var Version */
$api = $this->getNativeCurlClient()->getApi('version');

$this->registerClientResponse(
$api->show($versionId),
$api->listNamesByProject($identifier),
$api->getLastResponse(),
);
}

/**
* @When I update the version with id :id and the following data
*/
public function iUpdateTheVersionWithIdAndTheFollowingData($id, TableNode $table)
{
$data = [];

foreach ($table as $row) {
$data[$row['property']] = $row['value'];
}

/** @var Version */
$api = $this->getNativeCurlClient()->getApi('version');

$this->registerClientResponse(
$api->update($id, $data),
$api->getLastResponse(),
);
}
Expand Down
Loading

0 comments on commit 5cc6d37

Please sign in to comment.