Skip to content

Commit

Permalink
Merge pull request #2 from M6Web/more-tests
Browse files Browse the repository at this point in the history
More tests
  • Loading branch information
KuiKui committed Dec 5, 2013
2 parents 1aaa5b2 + b18a557 commit cd962b4
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 38 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Redis function | Description
**ZREMRANGEBYSCORE** *key* *min* *max* | Remove all members in a sorted set within the given scores
**ZREM** *key* *member* | Remove one membner from a sorted set

It also mocks **PIPELINE** and **EXECUTE** functions but without any transaction behaviors, they just make the interface fluent.

## Tests

```shell
Expand Down
64 changes: 33 additions & 31 deletions src/M6Web/Component/RedisMock/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ public function get($key)
{
if (!isset(self::$data[$key]) || is_array(self::$data[$key]))
{
return null;
return self::$pipeline ? $this : null;
}

return self::$data[$key];
return self::$pipeline ? $this : self::$data[$key];
}

public function set($key, $value)
{
self::$data[$key] = $value;

return $this;
return self::$pipeline ? $this : 'OK';
}

public function incr($key)
Expand All @@ -68,26 +68,28 @@ public function del($key)
{
if (!isset(self::$data[$key]))
{
return 0;
return self::$pipeline ? $this : 0;
}

$deletedItems = count(self::$data[$key]);

unset(self::$data[$key]);

return $deletedItems;
return self::$pipeline ? $this : $deletedItems;
}

public function keys($pattern)
{
$pattern = preg_replace(['#\*#', '#\?#', '#(\[[^\]]+\])#'], ['.*', '.', '$1+'], $pattern);

$results = [];
foreach (self::$data as $key => $value) {
if (preg_match('#' . $pattern . '#', $key)) {
if (preg_match('#^' . $pattern . '$#', $key)) {
$results[] = $key;
}
}

return $results;
return self::$pipeline ? $this : $results;
}

// Sets
Expand All @@ -98,29 +100,29 @@ public function sadd($key, $value)

self::$data[$key][] = $value;

return $isNew;
return self::$pipeline ? $this : $isNew;
}

public function smembers($key)
{
if (!isset(self::$data[$key]))
{
return array();
return self::$pipeline ? $this : array();
}

return self::$data[$key];
return self::$pipeline ? $this : self::$data[$key];
}

public function srem($key, $value)
{
if (!isset(self::$data[$key]) || !in_array($value, self::$data[$key]))
{
return 0;
return self::$pipeline ? $this : 0;
}

self::$data[$key] = array_diff(self::$data[$key], array($value));

return 1;
return self::$pipeline ? $this : 1;
}

// Hashes
Expand All @@ -131,40 +133,40 @@ public function hset($key, $field, $value)

self::$data[$key][$field] = $value;

return $isNew;
return self::$pipeline ? $this : (int) $isNew;
}

public function hget($key, $field)
{
if (!isset(self::$data[$key][$field]))
{
return null;
return self::$pipeline ? $this : null;
}

return self::$data[$key][$field];
return self::$pipeline ? $this : self::$data[$key][$field];
}

public function hgetall($key)
{
if (!isset(self::$data[$key]))
{
return null;
return self::$pipeline ? $this : array();
}

return self::$data[$key];
return self::$pipeline ? $this : self::$data[$key];
}

public function hexists($key, $field)
{
return isset(self::$data[$key][$field]);
return self::$pipeline ? $this : (int) isset(self::$data[$key][$field]);
}

// Sorted set

public function zrangebyscore($key, $min, $max, $options = null)
{
if (!isset(self::$data[$key]) || !is_array(self::$data[$key])) {
return null;
return self::$pipeline ? $this : null;
}

if (!is_array($options) || !is_array($options['limit']) || count($options['limit']) != 2) {
Expand All @@ -183,7 +185,7 @@ public function zrangebyscore($key, $min, $max, $options = null)
});

if ($min == '-inf' && $max == '+inf') {
return array_keys(array_slice(self::$data[$key], $options['limit'][0], $options['limit'][1], true));
return self::$pipeline ? $this : array_keys(array_slice(self::$data[$key], $options['limit'][0], $options['limit'][1], true));
}

$isInfMax = function($v) use ($max) {
Expand Down Expand Up @@ -215,13 +217,13 @@ public function zrangebyscore($key, $min, $max, $options = null)
}
}

return array_values(array_slice($results, $options['limit'][0], $options['limit'][1], true));
return self::$pipeline ? $this : array_values(array_slice($results, $options['limit'][0], $options['limit'][1], true));
}

public function zrevrangebyscore($key, $max, $min, $options = null)
{
if (!isset(self::$data[$key]) || !is_array(self::$data[$key])) {
return null;
return self::$pipeline ? $this : null;
}

if (!is_array($options) || !is_array($options['limit']) || count($options['limit']) != 2) {
Expand All @@ -240,7 +242,7 @@ public function zrevrangebyscore($key, $max, $min, $options = null)
});

if ($min == '-inf' && $max == '+inf') {
return array_keys(array_slice(self::$data[$key], $options['limit'][0], $options['limit'][1], true));
return self::$pipeline ? $this : array_keys(array_slice(self::$data[$key], $options['limit'][0], $options['limit'][1], true));
}

$isInfMax = function($v) use ($max) {
Expand Down Expand Up @@ -272,43 +274,43 @@ public function zrevrangebyscore($key, $max, $min, $options = null)
}
}

return array_values(array_slice($results, $options['limit'][0], $options['limit'][1], true));
return self::$pipeline ? $this : array_values(array_slice($results, $options['limit'][0], $options['limit'][1], true));
}

public function zadd($key, $score, $member) {
if (isset(self::$data[$key]) && !is_array(self::$data[$key])) {
return null;
return self::$pipeline ? $this : null;
}

$isNew = !isset(self::$data[$key][$member]);

self::$data[$key][$member] = (int) $score;

return (int) $isNew;
return self::$pipeline ? $this : (int) $isNew;
}

public function zremrangebyscore($key, $min, $max) {
$results = [];
$remNumber = 0;

if ($toRem = $this->zrangebyscore($key, $min, $max)) {
foreach ($toRem as $member) {
if ($this->zrem($key, $member)) {
$results[] = $member;
$remNumber++;
}
}
}

return $results;
return self::$pipeline ? $this : $remNumber;
}

public function zrem($key, $member) {
if (isset(self::$data[$key]) && !is_array(self::$data[$key]) || !isset(self::$data[$key][$member])) {
return 0;
return self::$pipeline ? $this : 0;
}

unset(self::$data[$key][$member]);

return 1;
return self::$pipeline ? $this : 1;
}

// Mock
Expand Down
Loading

0 comments on commit cd962b4

Please sign in to comment.