Skip to content

Commit

Permalink
Merge pull request BedrockStreaming#22 from dchancogne/del-multiple
Browse files Browse the repository at this point in the history
Add multiple key delete support
  • Loading branch information
KuiKui committed Feb 17, 2014
2 parents 9d39047 + 29235cd commit 6496b9d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor/
composer.lock
composer.phar
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ It currently mocks these Redis commands :

Redis command | Description
-------------------------------------------------|------------
**DEL** *key* | Deletes a key
**DEL** *key* *[key ...]* | Deletes one or more keys
**EXISTS** *key* | Determines if a key exists
**EXPIRE** *key* *seconds* | Sets a key's time to live in seconds
**KEYS** *pattern* | Finds all keys matching the given pattern
Expand Down
32 changes: 17 additions & 15 deletions src/M6Web/Component/RedisMock/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,23 +129,25 @@ public function exists($key)

public function del($key)
{
if (func_num_args() > 1) {
throw new UnsupportedException('In RedisMock, `del` command can not remove more than one key at once.');
}

if (!isset($this->data[$key])) {
return $this->returnPipedInfo(0);
if ( is_array($key) ) {
$keys = $key;
} else {
$keys = func_get_args();
}

$deletedItems = count($this->data[$key]);

unset($this->data[$key]);
unset($this->dataTypes[$key]);
if (array_key_exists($key, $this->dataTtl)) {
unset($this->dataTtl[$key]);
$deletedKeyCount = 0;
foreach ( $keys as $k ) {
if ( isset($this->data[$k]) ) {
$deletedKeyCount += count($this->data[$k]);
unset($this->data[$k]);
unset($this->dataTypes[$k]);
if (array_key_exists($k, $this->dataTtl)) {
unset($this->dataTtl[$k]);
}
}
}

return $this->returnPipedInfo($deletedItems);
return $this->returnPipedInfo($deletedKeyCount);
}

public function keys($pattern)
Expand Down Expand Up @@ -181,7 +183,7 @@ public function sadd($key, $member)
if ($isNew) {
$this->data[$key][] = $member;
}

$this->dataTypes[$key] = 'set';

if (array_key_exists($key, $this->dataTtl)) {
Expand Down Expand Up @@ -275,7 +277,7 @@ public function lpush($key, $value)

if (isset($this->data[$key]) && !is_array($this->data[$key])) {
return $this->returnPipedInfo(null);
}
}

array_unshift($this->data[$key], $value);

Expand Down
16 changes: 8 additions & 8 deletions tests/units/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public function testSetGetDelExists()
->isEqualTo('OK')
->string($redisMock->set('test2', 'something else'))
->isEqualTo('OK')
->exception(function() use ($redisMock) {
$redisMock->del('test1', 'test2');
})
->isInstanceOf('\M6Web\Component\RedisMock\UnsupportedException')
->integer($redisMock->del('test1'))
->isEqualTo(1)
->integer($redisMock->del('test2'))
->isEqualTo(1)
->integer($redisMock->del('test1', 'test2'))
->isEqualTo(2)
->string($redisMock->set('test1', 'something'))
->isEqualTo('OK')
->string($redisMock->set('test2', 'something else'))
->isEqualTo('OK')
->integer($redisMock->del(array('test1', 'test2')))
->isEqualTo(2)
->string($redisMock->set('test', 'something', 1))
->isEqualTo('OK')
->integer($redisMock->ttl('test'))
Expand Down

0 comments on commit 6496b9d

Please sign in to comment.