Skip to content

Commit

Permalink
Merge pull request #78 from tomlankhorst/feature/sdiff
Browse files Browse the repository at this point in the history
Add SDIFF support
  • Loading branch information
omansour authored May 22, 2019
2 parents d460453 + 574f5e7 commit 6d9351a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions src/M6Web/Component/RedisMock/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = array_values(array_diff(...$result));

$this->restorePipeline();

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

public function smembers($key)
{
if (!isset(self::$dataValues[$this->storage][$key]) || $this->deleteOnTtlExpired($key)) {
Expand Down
12 changes: 12 additions & 0 deletions tests/units/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(['b', 'd']);
}

public function testSAddSMembersSIsMemberSRem()
{
$redisMock = new Redis();
Expand Down

0 comments on commit 6d9351a

Please sign in to comment.