Skip to content

Commit

Permalink
Deprecate User::getIdByUsername()
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Jul 9, 2024
1 parent 5913938 commit 4441921
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Redmine\Api\Tracker::listing()` is deprecated, use `\Redmine\Api\Tracker::listNames()` instead.
- `Redmine\Api\Tracker::getIdByName()` is deprecated, use `\Redmine\Api\Tracker::listNames()` instead.
- `Redmine\Api\User::listing()` is deprecated, use `\Redmine\Api\User::listLogins()` instead.
- `Redmine\Api\User::getIdByUsername()` 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
5 changes: 5 additions & 0 deletions src/Redmine/Api/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,18 @@ public function getCurrentUser(array $params = [])
/**
* Get a user id given its username.
*
* @deprecated v2.7.0 Use listLogins() instead.
* @see User::listLogins()
*
* @param string $username
* @param array $params to allow offset/limit (and more) to be passed
*
* @return int|bool
*/
public function getIdByUsername($username, array $params = [])
{
@trigger_error('`' . __METHOD__ . '()` is deprecated since v2.7.0, use `' . __CLASS__ . '::listLogins()` instead.', E_USER_DEPRECATED);

$arr = $this->doListing(false, $params);

if (!isset($arr[$username])) {
Expand Down
53 changes: 41 additions & 12 deletions tests/Unit/Api/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function testGetCurrentUserReturnsClientGetResponse()
public function testGetIdByUsernameMakesGetRequest()
{
// Test values
$response = '{"users":[{"id":5,"login":"User 5"}]}';
$response = '{"users":[{"id":5,"login":"user_5"}]}';

// Create the used mock objects
$client = $this->createMock(Client::class);
Expand All @@ -79,8 +79,37 @@ public function testGetIdByUsernameMakesGetRequest()
$api = new User($client);

// Perform the tests
$this->assertFalse($api->getIdByUsername('User 1'));
$this->assertSame(5, $api->getIdByUsername('User 5'));
$this->assertFalse($api->getIdByUsername('user_1'));
$this->assertSame(5, $api->getIdByUsername('user_5'));
}

public function testGetIdByUsernameTriggersDeprecationWarning()
{
$client = $this->createMock(Client::class);
$client->method('requestGet')
->willReturn(true);
$client->method('getLastResponseBody')
->willReturn('{"users":[{"id":1,"login":"user_1"},{"id":5,"login":"user_5"}]}');
$client->method('getLastResponseContentType')
->willReturn('application/json');

$api = new User($client);

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

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

$api->getIdByUsername('user_5');
}

/**
Expand Down Expand Up @@ -189,10 +218,10 @@ public function testAllReturnsClientGetResponseWithParameters()
public function testListingReturnsNameIdArray()
{
// Test values
$response = '{"users":[{"id":1,"login":"User 1"},{"id":5,"login":"User 5"}]}';
$response = '{"users":[{"id":1,"login":"user_1"},{"id":5,"login":"user_5"}]}';
$expectedReturn = [
'User 1' => 1,
'User 5' => 5,
'user_1' => 1,
'user_5' => 5,
];

// Create the used mock objects
Expand Down Expand Up @@ -223,10 +252,10 @@ public function testListingReturnsNameIdArray()
public function testListingCallsGetOnlyTheFirstTime()
{
// Test values
$response = '{"users":[{"id":1,"login":"User 1"},{"id":5,"login":"User 5"}]}';
$response = '{"users":[{"id":1,"login":"user_1"},{"id":5,"login":"user_5"}]}';
$expectedReturn = [
'User 1' => 1,
'User 5' => 5,
'user_1' => 1,
'user_5' => 5,
];

// Create the used mock objects
Expand Down Expand Up @@ -258,10 +287,10 @@ public function testListingCallsGetOnlyTheFirstTime()
public function testListingCallsGetEveryTimeWithForceUpdate()
{
// Test values
$response = '{"users":[{"id":1,"login":"User 1"},{"id":5,"login":"User 5"}]}';
$response = '{"users":[{"id":1,"login":"user_1"},{"id":5,"login":"user_5"}]}';
$expectedReturn = [
'User 1' => 1,
'User 5' => 5,
'user_1' => 1,
'user_5' => 5,
];

// Create the used mock objects
Expand Down

0 comments on commit 4441921

Please sign in to comment.