From 65828ebe523ac9fdf87bc9dd5aa98a8a2ff016d6 Mon Sep 17 00:00:00 2001 From: Tom Lankhorst Date: Sun, 19 May 2019 12:29:44 +0200 Subject: [PATCH 1/3] Add SDIFF support --- README.md | 1 + src/M6Web/Component/RedisMock/RedisMock.php | 15 +++++++++++++++ tests/units/RedisMock.php | 12 ++++++++++++ 3 files changed, 28 insertions(+) diff --git a/README.md b/README.md index 6cd0a48..2918ceb 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,7 @@ Redis command | Description **SETEX** *key* *seconds* *value* | Sets the value and expiration of a key **SETNX** *key* *value* | Sets key to hold value if key does not exist **SADD** *key* *member* *[member ...]* | Adds one or more members to a set +**SDIFF** *key* *key* *[key ...]* | Returns the members of the set resulting from the difference between the first set and all the successive sets. **SISMEMBER** *key* *member* | Determines if a member is in a set **SMEMBERS** *key* | Gets all the members in a set **SUNION** *key* *[key ...]* | Returns the members of the set resulting from the union of all the given sets. diff --git a/src/M6Web/Component/RedisMock/RedisMock.php b/src/M6Web/Component/RedisMock/RedisMock.php index ceb7cb8..e35475a 100644 --- a/src/M6Web/Component/RedisMock/RedisMock.php +++ b/src/M6Web/Component/RedisMock/RedisMock.php @@ -333,6 +333,21 @@ public function sadd($key, $members) } + public function sdiff($key) + { + $this->stopPipeline(); + $keys = is_array($key) ? $key : func_get_args(); + $result = []; + foreach ($keys as $key) { + $result[] = $this->smembers($key); + } + $result = call_user_func_array('array_diff', $result); + + $this->restorePipeline(); + + return $this->returnPipedInfo($result); + } + public function smembers($key) { if (!isset(self::$dataValues[$this->storage][$key]) || $this->deleteOnTtlExpired($key)) { diff --git a/tests/units/RedisMock.php b/tests/units/RedisMock.php index af77f6b..f15bec3 100644 --- a/tests/units/RedisMock.php +++ b/tests/units/RedisMock.php @@ -470,6 +470,18 @@ public function testSCard() ->isEqualTo(0); } + public function testSDiff() + { + $redisMock = new Redis(); + $redisMock->sadd('key1', 'a', 'b', 'c', 'd'); + $redisMock->sadd('key2', 'c'); + $redisMock->sadd('key3', 'a', 'c', 'e'); + + $this->assert + ->array($redisMock->sdiff('key1', 'key2', 'key3')) + ->isEqualTo([1 => 'b', 3 => 'd']); + } + public function testSAddSMembersSIsMemberSRem() { $redisMock = new Redis(); From 7d565d374884fb701041405f1e7f6cc6a21246f7 Mon Sep 17 00:00:00 2001 From: Tom Lankhorst Date: Sun, 19 May 2019 12:32:36 +0200 Subject: [PATCH 2/3] Drop array indices --- src/M6Web/Component/RedisMock/RedisMock.php | 2 +- tests/units/RedisMock.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/M6Web/Component/RedisMock/RedisMock.php b/src/M6Web/Component/RedisMock/RedisMock.php index e35475a..6fe0d0a 100644 --- a/src/M6Web/Component/RedisMock/RedisMock.php +++ b/src/M6Web/Component/RedisMock/RedisMock.php @@ -341,7 +341,7 @@ public function sdiff($key) foreach ($keys as $key) { $result[] = $this->smembers($key); } - $result = call_user_func_array('array_diff', $result); + $result = array_values(call_user_func_array('array_diff', $result)); $this->restorePipeline(); diff --git a/tests/units/RedisMock.php b/tests/units/RedisMock.php index f15bec3..db62e7b 100644 --- a/tests/units/RedisMock.php +++ b/tests/units/RedisMock.php @@ -479,7 +479,7 @@ public function testSDiff() $this->assert ->array($redisMock->sdiff('key1', 'key2', 'key3')) - ->isEqualTo([1 => 'b', 3 => 'd']); + ->isEqualTo(['b', 'd']); } public function testSAddSMembersSIsMemberSRem() From 574f5e7923f4316d9cf456e152c2f0258f8bd0da Mon Sep 17 00:00:00 2001 From: Tom Lankhorst Date: Mon, 20 May 2019 09:52:03 +0200 Subject: [PATCH 3/3] Use ... token for var-length args --- src/M6Web/Component/RedisMock/RedisMock.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/M6Web/Component/RedisMock/RedisMock.php b/src/M6Web/Component/RedisMock/RedisMock.php index 6fe0d0a..677b893 100644 --- a/src/M6Web/Component/RedisMock/RedisMock.php +++ b/src/M6Web/Component/RedisMock/RedisMock.php @@ -341,7 +341,7 @@ public function sdiff($key) foreach ($keys as $key) { $result[] = $this->smembers($key); } - $result = array_values(call_user_func_array('array_diff', $result)); + $result = array_values(array_diff(...$result)); $this->restorePipeline();