Skip to content

Commit

Permalink
Adjustement
Browse files Browse the repository at this point in the history
  • Loading branch information
Florent DUBOST committed Jan 17, 2014
1 parent 8111050 commit 95fc6a8
Show file tree
Hide file tree
Showing 3 changed files with 261 additions and 89 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ Redis command | Description
**HEXISTS** *key* *field* | Determines if a hash field exists
**HGET** *key* *field* | Gets the value of a hash field
**HGETALL** *key* | Gets all the fields and values in a hash
**HSET** *key* *field* *value* | Sets the string value of a hash field
**HMSET** *key* *array<field, value>* | Sets each value in the corresponding field
**HSET** *key* *field* *value* | Sets the string value of a hash field
**LPUSH** *key* *value* | Pushs values at the head of a list
**LREM** *key* *count* *value* | Removes `count` instances of `value` from the head of a list
**LTRIM** *key* *start* *stop* | Removes the values of the `key` list which are outside the range `start`...`stop`
**RPUSH** *key* *value* | Pushs values at the tail of a list
**ZADD** *key* *score* *member* | Adds one member to a sorted set, or update its score if it already exists
**ZRANGE** *key* *start* *stop* | Returns the specified range of members in a sorted set
**ZRANGEBYSCORE** *key* *min* *max* *options* | Returns a range of members in a sorted set, by score
Expand All @@ -54,10 +58,6 @@ Redis command | Description
**ZREVRANGEBYSCORE** *key* *min* *max* *options* | Returns a range of members in a sorted set, by score, with scores ordered from high to low
**DBSIZE** | Returns the number of keys in the selected database
**FLUSHDB** | Flushes the database
**LPUSH** *key* *value* [*value*] [...] | Push values at the head of a list
**RPUSH** *key* *value* [*value*] [...] | Push values at the tail of a list
**LREM** *key* *count* *value* | Remove `count` instances of `value` from the head of a list
**LTRIM** *key* *start* *stop* | Remove the values of the `key` list which are outside the range `start`...`stop`

It mocks **MULTI**, **DISCARD** and **EXEC** commands but without any transaction behaviors, they just make the interface fluent and return each command results.
**PIPELINE** and **EXECUTE** pseudo commands (client pipelining) are also mocked.
Expand Down
131 changes: 90 additions & 41 deletions src/M6Web/Component/RedisMock/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,25 @@ public function srem($key, $member)
return $this->returnPipedInfo(1);
}

public function sismember($key, $member)
{
if (!isset($this->data[$key]) || !in_array($member, $this->data[$key]) || $this->deleteOnTtlExpired($key)) {
return $this->returnPipedInfo(0);
}

return $this->returnPipedInfo(1);
}

// Lists

public function lrem($key, $count, $value)
{
$this->deleteOnTtlExpired($key);

if (!isset($this->data[$key]) || !in_array($value, $this->data[$key]) || $this->deleteOnTtlExpired($key)) {
return $this->returnPipedInfo(0);
}

$arr = $this->data[$key];
$reversed = false;

Expand All @@ -233,9 +250,9 @@ public function lrem($key, $count, $value)
}

$arr = array_filter($arr, function ($curValue) use (&$count, $value) {
if (!!$count && ($curValue == $value)) {
return false;
if ($count && ($curValue == $value)) {
$count--;
return false;
}

return true;
Expand All @@ -247,91 +264,123 @@ public function lrem($key, $count, $value)
$arr = array_reverse($arr);
}

$this->data[$key] = array_reverse($arr);
$this->data[$key] = array_values($arr);

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

public function sismember($key, $member)
public function lpush($key, $value)
{
if (!isset($this->data[$key]) || !in_array($member, $this->data[$key]) || $this->deleteOnTtlExpired($key)) {
return $this->returnPipedInfo(0);
$this->deleteOnTtlExpired($key);

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

return $this->returnPipedInfo(1);
}
if (!isset($this->data[$key])) {
$this->data[$key] = array();
}

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

public function hset($key, $field, $value)
return $this->returnPipedInfo(count($this->data[$key]));
}

public function rpush($key, $value)
{
$this->deleteOnTtlExpired($key);

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

$isNew = !isset($this->data[$key]) || !isset($this->data[$key][$field]);

$this->data[$key][$field] = $value;
$this->dataTypes[$key] = 'hash';
if (array_key_exists($key, $this->dataTtl)) {
unset($this->dataTtl[$key]);
if (!isset($this->data[$key])) {
$this->data[$key] = array();
}

return $this->returnPipedInfo((int) $isNew);
}
array_push($this->data[$key], $value);

public function hmset($key, $pairs)
{
foreach ($pairs as $field => $value) {
$this->hset($key, $field, $value);
}

return $this->returnPipedInfo('OK');
return $this->returnPipedInfo(count($this->data[$key]));
}

public function lpush($key)
public function ltrim($key, $start, $stop)
{
$this->deleteOnTtlExpired($key);

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

$values = array_slice(func_get_args(), 1);
if ($start < 0) {
if (abs($start) > count($this->data[$key])) {
$start = 0;
} else {
$start = count($this->data[$key]) + $start;
}
}

foreach ($values as $value) {
array_unshift($this->data[$key], $value);
if ($stop >= 0) {
$length = $stop - $start + 1;
} else {
if ($stop == -1) {
$length = NULL;
} else {
$length = $stop + 1;
}
}

return $this->returnPipedInfo(count($this->data[$key]));
$this->data[$key] = array_slice($this->data[$key], $start, $length);

if (!count($this->data[$key])) {
$this->stopPipeline();
$this->del($key);
$this->restorePipeline();
}

return $this->returnPipedInfo('OK');
}

public function rpush($key)
// Hashes

public function hset($key, $field, $value)
{
if (!isset($this->data[$key])) {
$this->data[$key] = array();
}
$this->deleteOnTtlExpired($key);

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

$values = array_slice(func_get_args(), 1);
$isNew = !isset($this->data[$key]) || !isset($this->data[$key][$field]);

foreach ($values as $value) {
array_push($this->data[$key], $value);
$this->data[$key][$field] = $value;
$this->dataTypes[$key] = 'hash';
if (array_key_exists($key, $this->dataTtl)) {
unset($this->dataTtl[$key]);
}

return $this->returnPipedInfo(count($this->data[$key]));
return $this->returnPipedInfo((int) $isNew);
}

public function ltrim($key, $start, $stop)
public function hmset($key, $pairs)
{
$this->data[$key] = array_slice($this->data[$key], $start, ( $stop > 0 ? $stop - $start : $stop));
$this->deleteOnTtlExpired($key);

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

$this->stopPipeline();
foreach ($pairs as $field => $value) {
$this->hset($key, $field, $value);
}
$this->restorePipeline();

return $this->returnPipedInfo('OK');
}


public function hget($key, $field)
{
if (!isset($this->data[$key][$field]) || $this->deleteOnTtlExpired($key)) {
Expand Down
Loading

0 comments on commit 95fc6a8

Please sign in to comment.