Skip to content

Commit

Permalink
Merge pull request #86 from magnetik/zadd-scores
Browse files Browse the repository at this point in the history
Redis zadd scores are not always integer
  • Loading branch information
omansour authored Aug 20, 2019
2 parents 3f308e4 + 28ec63b commit e10199c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/M6Web/Component/RedisMock/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -988,10 +988,14 @@ public function zadd($key, ...$args) {

$isNew = !isset(self::$dataValues[$this->storage][$key][$member]);

self::$dataValues[$this->storage][$key][$member] = (int) $score;
self::$dataTypes[$this->storage][$key] = 'zset';
if (array_key_exists($key, self::$dataTtl[$this->storage]))
{
if (!is_numeric($score)) {
throw new \InvalidArgumentException('Score should be either an integer or a float.');
}
$score += 0; // convert potential string value to int or float

self::$dataValues[$this->storage][$key][$member] = $score;
self::$dataTypes[$this->storage][$key] = 'zset';
if (array_key_exists($key, self::$dataTtl[$this->storage])) {
unset(self::$dataTtl[$this->storage][$key]);
}

Expand Down
14 changes: 13 additions & 1 deletion tests/units/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -753,10 +753,22 @@ public function testZAddWithArray()
$redisMock = new Redis();
$redisMock->zadd('test', ['test1' => 1]);
$redisMock->zadd('test', ['test2' => 10]);
$redisMock->zadd('test', ['test3' => 1.5]);
$redisMock->zadd('test', ['test4' => '10.5']);

$this->assert
->integer($redisMock->zcard('test'))
->isEqualTo(2);
->isEqualTo(4);
}

public function testZAddDoNotAcceptNonNumericValue()
{
$redisMock = new Redis();
$this->exception(
function () use ($redisMock) {
$redisMock->zadd('test', ['test1' => 'NotANumeric']);
}
)->isInstanceOf(\InvalidArgumentException::class);
}

public function testZIncrBy()
Expand Down

0 comments on commit e10199c

Please sign in to comment.