Skip to content

Commit

Permalink
Merge pull request #94 from asilgalis/fix-hdel
Browse files Browse the repository at this point in the history
Update hdel method to support deleting multiple hash fields
  • Loading branch information
lnahiro authored Jan 4, 2022
2 parents 3f99a16 + 759f696 commit 8884530
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Redis command | Description
**EXPIRE** *key* *seconds* | Sets a key's time to live in seconds
**FLUSHDB** | Flushes the database
**GET** *key* | Gets the value of a key
**HDEL** *key* *field* | Delete one hash fields
**HDEL** *key* *array\<fields\>* | Delete hash fields
**HEXISTS** *key* *field* | Determines if a hash field exists
**HMGET** *key* *array\<field\>* | Gets the values of multiple hash fields
**HGET** *key* *field* | Gets the value of a hash field
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"autoload": {
"psr-0": {"M6Web\\Component\\RedisMock": "src/"}
},
"autoload-dev": {
"psr-4": {"M6Web\\Component\\RedisMock\\Tests\\Units\\": "tests/units/"}
},
"require": {
"php": ">=7.1.0"
},
Expand Down
25 changes: 15 additions & 10 deletions src/M6Web/Component/RedisMock/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -739,10 +739,10 @@ public function hmget($key, $fields)
return $this->returnPipedInfo($result);
}

public function hdel($key, $field)
public function hdel($key, $fields)
{
if (func_num_args() > 2) {
throw new UnsupportedException('In RedisMock, `hdel` command can not delete more than one entry at once.');
throw new UnsupportedException('In RedisMock, `hdel` command does not accept more than two arguments.');
}

if (isset(self::$dataValues[$this->storage][$key]) && !is_array(self::$dataValues[$this->storage][$key])) {
Expand All @@ -753,16 +753,21 @@ public function hdel($key, $field)
return $this->returnPipedInfo(0);
}

if (array_key_exists($field, self::$dataValues[$this->storage][$key])) {
unset(self::$dataValues[$this->storage][$key][$field]);
if (0 === count(self::$dataValues[$this->storage][$key])) {
unset(self::$dataTypes[$this->storage][$key]);
}
$fields = is_array($fields) ? $fields : [$fields];
$info = 0;

return $this->returnPipedInfo(1);
} else {
return $this->returnPipedInfo(0);
foreach ($fields as $field) {
if (array_key_exists($field, self::$dataValues[$this->storage][$key])) {
unset(self::$dataValues[$this->storage][$key][$field]);
if (0 === count(self::$dataValues[$this->storage][$key])) {
unset(self::$dataTypes[$this->storage][$key]);
}

$info++;
}
}

return $this->returnPipedInfo($info);
}

public function hkeys($key)
Expand Down
14 changes: 10 additions & 4 deletions src/M6Web/Component/RedisMock/RedisMockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,19 @@ protected function getMethodSignature(\ReflectionMethod $method)
$signatures = array();
foreach ($method->getParameters() as $parameter) {
$signature = '';
$parameterType = $parameter->getType();
$isReflectionNamedType = $parameterType instanceof \ReflectionNamedType;
// typeHint
if ($parameter->isArray()) {
if ($isReflectionNamedType && $parameterType->getName() === 'array') {
$signature .= 'array ';
} elseif (method_exists($parameter, 'isCallable') && $parameter->isCallable()) {
} elseif (
method_exists($parameter, 'isCallable')
&& $isReflectionNamedType
&& $parameterType->getName() === 'callable'
) {
$signature .= 'callable ';
} elseif ($parameter->getClass()) {
$signature .= sprintf('\%s ', $parameter->getClass());
} elseif ($isReflectionNamedType && $parameterType->getName() === 'object') {
$signature .= sprintf('\%s ', get_class($parameter));
}
// reference
if ($parameter->isPassedByReference()) {
Expand Down
12 changes: 11 additions & 1 deletion tests/units/RedisMock.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace M6Web\Component\RedisMock\tests\units;
namespace M6Web\Component\RedisMock\Tests\Units;

use atoum;
use M6Web\Component\RedisMock\RedisMock as Redis;
Expand Down Expand Up @@ -1247,12 +1247,22 @@ public function testHSetHMSetHGetHDelHExistsHKeysHLenHGetAll()
->isEqualTo(1)
->integer($redisMock->hset('test', 'test2', 'something else'))
->isEqualTo(1)
->integer($redisMock->hset('test', 'test4', 'something else 4'))
->isEqualTo(1)
->integer($redisMock->hset('test', 'test5', 'something else 5'))
->isEqualTo(1)
->integer($redisMock->hset('test', 'test6', 'something else 6'))
->isEqualTo(1)
->integer($redisMock->hdel('test', 'test2'))
->isEqualTo(1)
->integer($redisMock->hdel('test', 'test3'))
->isEqualTo(0)
->integer($redisMock->hdel('raoul', 'test2'))
->isEqualTo(0)
->integer($redisMock->hdel('test', ['test4']))
->isEqualTo(1)
->integer($redisMock->hdel('test', ['test5', 'test6']))
->isEqualTo(2)
->string($redisMock->type('test'))
->isEqualTo('hash')
->integer($redisMock->hdel('test', 'test1'))
Expand Down
3 changes: 1 addition & 2 deletions tests/units/RedisMockFactory.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php

namespace M6Web\Component\RedisMock\tests\units;
namespace M6Web\Component\RedisMock\Tests\Units;

use M6Web\Component\RedisMock\RedisMockFactory as Factory;
use M6Web\Component\RedisMock\RedisMock as Mock;
use atoum;

/**
Expand Down

0 comments on commit 8884530

Please sign in to comment.