Skip to content

Commit

Permalink
Merge pull request BedrockStreaming#6 from M6Web/feature/atoum-adapte…
Browse files Browse the repository at this point in the history
…r-no-trait

Feature/atoum adapter no trait (thanks to @omansour and @adriensamson)
  • Loading branch information
KuiKui committed Dec 18, 2013
2 parents ad8af63 + 6de48f1 commit 4e03972
Show file tree
Hide file tree
Showing 6 changed files with 596 additions and 43 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ Redis function | Description

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

## Usage

RedisMock library provides a factory able to build a mocked class of your Redis library that can be directly injected in your application :

```php
$factory = new \M6Web\Component\RedisMockFactory();
$myRedisMock = $factory->getAdapter('My\Redis\Library', new \M6Web\Component\RedisMock\RedisMock());
```

**WARNING !** *RedisMock doesn't implement all Redis features and commands. The mock can have undesired behavior if your parent class uses unsupported features.*

## Tests

```shell
Expand Down
92 changes: 64 additions & 28 deletions src/M6Web/Component/RedisMock/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public function getData()

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

Expand All @@ -46,16 +45,11 @@ public function set($key, $value)

public function incr($key)
{
if (!isset(self::$data[$key]))
{
if (!isset(self::$data[$key])) {
self::$data[$key] = 1;
}
elseif (!is_integer(self::$data[$key]))
{
} elseif (!is_integer(self::$data[$key])) {
return self::$pipeline ? $this : null;
}
else
{
} else {
self::$data[$key]++;
}

Expand All @@ -71,8 +65,11 @@ public function exists($key)

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

if (!isset(self::$data[$key])) {
return self::$pipeline ? $this : 0;
}

Expand Down Expand Up @@ -101,17 +98,26 @@ public function keys($pattern)

public function sadd($key, $member)
{
$isNew = !isset(self::$data[$key]);
if (func_num_args() > 2) {
throw new UnsupportedException('In RedisMock, `sadd` command can not set more than one member at once.');
}

if (isset(self::$data[$key]) && !is_array(self::$data[$key])) {
return self::$pipeline ? $this : null;
}

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

self::$data[$key][] = $member;
if ($isNew) {
self::$data[$key][] = $member;
}

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

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

Expand All @@ -120,8 +126,11 @@ public function smembers($key)

public function srem($key, $member)
{
if (!isset(self::$data[$key]) || !in_array($member, self::$data[$key]))
{
if (func_num_args() > 2) {
throw new UnsupportedException('In RedisMock, `srem` command can not remove more than one member at once.');
}

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

Expand All @@ -132,8 +141,7 @@ public function srem($key, $member)

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

Expand All @@ -144,7 +152,11 @@ public function sismember($key, $member)

public function hset($key, $field, $value)
{
$isNew = !isset(self::$data[$key][$field]);
if (isset(self::$data[$key]) && !is_array(self::$data[$key])) {
return self::$pipeline ? $this : null;
}

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

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

Expand Down Expand Up @@ -178,8 +190,12 @@ public function hexists($key, $field)

// Sorted set

public function zrange($key, $start, $stop)
public function zrange($key, $start, $stop, $withscores = false)
{
if ($withscores) {
throw new UnsupportedException('Parameter `withscores` is not supported by RedisMock for `zrange` command.');
}

$set = $this->zrangebyscore($key, '-inf', '+inf');

if ($start < 0) {
Expand All @@ -203,8 +219,12 @@ public function zrange($key, $start, $stop)
return self::$pipeline ? $this : array_slice($set, $start, $length);
}

public function zrevrange($key, $start, $stop)
public function zrevrange($key, $start, $stop, $withscores = false)
{
if ($withscores) {
throw new UnsupportedException('Parameter `withscores` is not supported by RedisMock for `zrevrange` command.');
}

$set = $this->zrevrangebyscore($key, '+inf', '-inf');

if ($start < 0){
Expand All @@ -228,13 +248,17 @@ public function zrevrange($key, $start, $stop)
return self::$pipeline ? $this : array_slice($set, $start, $length);
}

public function zrangebyscore($key, $min, $max, $options = null)
public function zrangebyscore($key, $min, $max, array $options = [])
{
if (!empty($options['withscores'])) {
throw new UnsupportedException('Parameter `withscores` is not supported by RedisMock for `zrangebyscore` command.');
}

if (!isset(self::$data[$key]) || !is_array(self::$data[$key])) {
return self::$pipeline ? $this : null;
}

if (!is_array($options) || !is_array($options['limit']) || count($options['limit']) != 2) {
if (!isset($options['limit']) || !is_array($options['limit']) || count($options['limit']) != 2) {
$options['limit'] = [0, count(self::$data[$key])];
}

Expand Down Expand Up @@ -285,13 +309,17 @@ public function zrangebyscore($key, $min, $max, $options = null)
return self::$pipeline ? $this : array_values(array_slice($results, $options['limit'][0], $options['limit'][1], true));
}

public function zrevrangebyscore($key, $max, $min, $options = null)
public function zrevrangebyscore($key, $max, $min, array $options = [])
{
if (!empty($options['withscores'])) {
throw new UnsupportedException('Parameter `withscores` is not supported by RedisMock for `zrevrangebyscore` command.');
}

if (!isset(self::$data[$key]) || !is_array(self::$data[$key])) {
return self::$pipeline ? $this : null;
}

if (!is_array($options) || !is_array($options['limit']) || count($options['limit']) != 2) {
if (!isset($options['limit']) || !is_array($options['limit']) || count($options['limit']) != 2) {
$options['limit'] = [0, count(self::$data[$key])];
}

Expand Down Expand Up @@ -343,6 +371,10 @@ public function zrevrangebyscore($key, $max, $min, $options = null)
}

public function zadd($key, $score, $member) {
if (func_num_args() > 3) {
throw new UnsupportedException('In RedisMock, `zadd` command can not set more than one member at once.');
}

if (isset(self::$data[$key]) && !is_array(self::$data[$key])) {
return self::$pipeline ? $this : null;
}
Expand All @@ -369,6 +401,10 @@ public function zremrangebyscore($key, $min, $max) {
}

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

if (isset(self::$data[$key]) && !is_array(self::$data[$key]) || !isset(self::$data[$key][$member])) {
return self::$pipeline ? $this : 0;
}
Expand Down
Loading

0 comments on commit 4e03972

Please sign in to comment.