Skip to content

Commit

Permalink
Merge branch 'del-multiple' of github.com:dchancogne/RedisMock
Browse files Browse the repository at this point in the history
Conflicts:
	tests/units/RedisMock.php
  • Loading branch information
dchancogne committed Feb 17, 2014
2 parents a6745b5 + 29235cd commit f71700d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
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
28 changes: 15 additions & 13 deletions src/M6Web/Component/RedisMock/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,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
16 changes: 8 additions & 8 deletions tests/units/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,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('test3', 'something', 1))
->isEqualTo('OK')
Expand Down

0 comments on commit f71700d

Please sign in to comment.