Skip to content

Commit 8884530

Browse files
authored
Merge pull request #94 from asilgalis/fix-hdel
Update hdel method to support deleting multiple hash fields
2 parents 3f99a16 + 759f696 commit 8884530

File tree

6 files changed

+41
-18
lines changed

6 files changed

+41
-18
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Redis command | Description
2525
**EXPIRE** *key* *seconds* | Sets a key's time to live in seconds
2626
**FLUSHDB** | Flushes the database
2727
**GET** *key* | Gets the value of a key
28-
**HDEL** *key* *field* | Delete one hash fields
28+
**HDEL** *key* *array\<fields\>* | Delete hash fields
2929
**HEXISTS** *key* *field* | Determines if a hash field exists
3030
**HMGET** *key* *array\<field\>* | Gets the values of multiple hash fields
3131
**HGET** *key* *field* | Gets the value of a hash field

composer.json

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"autoload": {
1515
"psr-0": {"M6Web\\Component\\RedisMock": "src/"}
1616
},
17+
"autoload-dev": {
18+
"psr-4": {"M6Web\\Component\\RedisMock\\Tests\\Units\\": "tests/units/"}
19+
},
1720
"require": {
1821
"php": ">=7.1.0"
1922
},

src/M6Web/Component/RedisMock/RedisMock.php

+15-10
Original file line numberDiff line numberDiff line change
@@ -739,10 +739,10 @@ public function hmget($key, $fields)
739739
return $this->returnPipedInfo($result);
740740
}
741741

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

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

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

762-
return $this->returnPipedInfo(1);
763-
} else {
764-
return $this->returnPipedInfo(0);
759+
foreach ($fields as $field) {
760+
if (array_key_exists($field, self::$dataValues[$this->storage][$key])) {
761+
unset(self::$dataValues[$this->storage][$key][$field]);
762+
if (0 === count(self::$dataValues[$this->storage][$key])) {
763+
unset(self::$dataTypes[$this->storage][$key]);
764+
}
765+
766+
$info++;
767+
}
765768
}
769+
770+
return $this->returnPipedInfo($info);
766771
}
767772

768773
public function hkeys($key)

src/M6Web/Component/RedisMock/RedisMockFactory.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,19 @@ protected function getMethodSignature(\ReflectionMethod $method)
300300
$signatures = array();
301301
foreach ($method->getParameters() as $parameter) {
302302
$signature = '';
303+
$parameterType = $parameter->getType();
304+
$isReflectionNamedType = $parameterType instanceof \ReflectionNamedType;
303305
// typeHint
304-
if ($parameter->isArray()) {
306+
if ($isReflectionNamedType && $parameterType->getName() === 'array') {
305307
$signature .= 'array ';
306-
} elseif (method_exists($parameter, 'isCallable') && $parameter->isCallable()) {
308+
} elseif (
309+
method_exists($parameter, 'isCallable')
310+
&& $isReflectionNamedType
311+
&& $parameterType->getName() === 'callable'
312+
) {
307313
$signature .= 'callable ';
308-
} elseif ($parameter->getClass()) {
309-
$signature .= sprintf('\%s ', $parameter->getClass());
314+
} elseif ($isReflectionNamedType && $parameterType->getName() === 'object') {
315+
$signature .= sprintf('\%s ', get_class($parameter));
310316
}
311317
// reference
312318
if ($parameter->isPassedByReference()) {

tests/units/RedisMock.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace M6Web\Component\RedisMock\tests\units;
3+
namespace M6Web\Component\RedisMock\Tests\Units;
44

55
use atoum;
66
use M6Web\Component\RedisMock\RedisMock as Redis;
@@ -1247,12 +1247,22 @@ public function testHSetHMSetHGetHDelHExistsHKeysHLenHGetAll()
12471247
->isEqualTo(1)
12481248
->integer($redisMock->hset('test', 'test2', 'something else'))
12491249
->isEqualTo(1)
1250+
->integer($redisMock->hset('test', 'test4', 'something else 4'))
1251+
->isEqualTo(1)
1252+
->integer($redisMock->hset('test', 'test5', 'something else 5'))
1253+
->isEqualTo(1)
1254+
->integer($redisMock->hset('test', 'test6', 'something else 6'))
1255+
->isEqualTo(1)
12501256
->integer($redisMock->hdel('test', 'test2'))
12511257
->isEqualTo(1)
12521258
->integer($redisMock->hdel('test', 'test3'))
12531259
->isEqualTo(0)
12541260
->integer($redisMock->hdel('raoul', 'test2'))
12551261
->isEqualTo(0)
1262+
->integer($redisMock->hdel('test', ['test4']))
1263+
->isEqualTo(1)
1264+
->integer($redisMock->hdel('test', ['test5', 'test6']))
1265+
->isEqualTo(2)
12561266
->string($redisMock->type('test'))
12571267
->isEqualTo('hash')
12581268
->integer($redisMock->hdel('test', 'test1'))

tests/units/RedisMockFactory.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<?php
22

3-
namespace M6Web\Component\RedisMock\tests\units;
3+
namespace M6Web\Component\RedisMock\Tests\Units;
44

55
use M6Web\Component\RedisMock\RedisMockFactory as Factory;
6-
use M6Web\Component\RedisMock\RedisMock as Mock;
76
use atoum;
87

98
/**

0 commit comments

Comments
 (0)