fix: Allow num_buckets to be equal as min_bucket

Signed-off-by: Kostiantyn Miakshyn <molodchick@gmail.com>
This commit is contained in:
Kostiantyn Miakshyn 2025-12-07 22:59:42 +01:00
parent bbca4fe56e
commit 7848dfb294
2 changed files with 6 additions and 2 deletions

View file

@ -30,6 +30,10 @@ class Mapper {
? (int)$this->config['arguments']['min_bucket']
: 0;
if ($minBucket === $numBuckets) {
return (string)$minBucket;
}
$hash = md5($this->user->getUID());
$num = hexdec(substr($hash, 0, 4));
return (string)(($num % ($numBuckets - $minBucket)) + $minBucket);

View file

@ -24,6 +24,7 @@ class MapperTest extends \Test\TestCase {
public static function dataGetBucket(): array {
return [
['user', 64, 0, '17'],
['user', 64, 64, '64'],
['USER', 64, 0, '0'],
['bc0e8b52-a66c-1035-90c6-d9663bda9e3f', 64, 0, '56'],
['user', 8, 0, '1'],
@ -41,8 +42,7 @@ class MapperTest extends \Test\TestCase {
#[\PHPUnit\Framework\Attributes\DataProvider('dataGetBucket')]
public function testGetBucket($username, $numBuckets, $bucketShift, $expectedBucket): void {
$mapper = new Mapper($this->user, ['arguments' => ['min_bucket' => $bucketShift]]);
$this->user->expects($this->once())
->method('getUID')
$this->user->method('getUID')
->willReturn($username);
$result = $mapper->getBucket($numBuckets);