Skip to content

Commit

Permalink
Merge pull request BedrockStreaming#21 from dchancogne/master
Browse files Browse the repository at this point in the history
Add support for SETEX command
  • Loading branch information
fdubost committed Feb 18, 2014
2 parents 6496b9d + f71700d commit fc977ca
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Redis PHP Mock [![Build Status](https://secure.travis-ci.org/M6Web/RedisMock.png?branch=master)](http://travis-ci.org/M6Web/RedisMock)

PHP 5.3 library providing a Redis PHP mock for your tests.
PHP 5.3 library providing a Redis PHP mock for your tests.

## Installation

Expand Down Expand Up @@ -35,6 +35,7 @@ Redis command | Description
**GET** *key* | Gets the value of a key
**INCR** *key* | Increments the integer value of a key by one
**SET** *key* *value* | Sets the string value of a key
**SETEX** *key* *seconds* *value* | Sets the value and expiration of a key
**SADD** *key* *member* | Adds one member to a set
**SISMEMBER** *key* *member* | Determines if a member is in a set
**SMEMBERS** *key* | Gets all the members in a set
Expand Down Expand Up @@ -81,7 +82,7 @@ $myRedisMock = $factory->getAdapter('My\Redis\Library');

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

*Note : the factory will throw an exception by default if your parent class implements unsupported commands. If you want even so partially use the mock, you can specify the second parameter when you build it `$factory->getAdapterClass('My\Redis\Library', true)`. The exception will then thrown only when the command is called.*
*Note : the factory will throw an exception by default if your parent class implements unsupported commands. If you want even so partially use the mock, you can specify the second parameter when you build it `$factory->getAdapter('My\Redis\Library', true)`. The exception will then thrown only when the command is called.*

## Tests

Expand Down
5 changes: 5 additions & 0 deletions src/M6Web/Component/RedisMock/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public function set($key, $value, $seconds = null)
return $this->returnPipedInfo('OK');
}

public function setex($key, $seconds, $value)
{
return $this->set($key, $value, $seconds);
}

public function ttl($key)
{
if (!array_key_exists($key, $this->data) || $this->deleteOnTtlExpired($key)) {
Expand Down
41 changes: 35 additions & 6 deletions tests/units/RedisMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function testSetGetDelExists()
->isNull()
->integer($redisMock->del('test'))
->isEqualTo(0)

->string($redisMock->set('test', 'something'))
->isEqualTo('OK')
->string($redisMock->type('test'))
Expand All @@ -37,6 +38,24 @@ public function testSetGetDelExists()
->isEqualTo('none')
->boolean($redisMock->exists('test'))
->isFalse()

->string($redisMock->setex('test1', 5, 'something'))
->isEqualTo('OK')
->string($redisMock->type('test1'))
->isEqualTo('string')
->boolean($redisMock->exists('test1'))
->isTrue()
->string($redisMock->get('test1'))
->isEqualTo('something')
->integer($redisMock->del('test1'))
->isEqualTo(1)
->variable($redisMock->get('test1'))
->isNull()
->string($redisMock->type('test1'))
->isEqualTo('none')
->boolean($redisMock->exists('test1'))
->isFalse()

->string($redisMock->set('test1', 'something'))
->isEqualTo('OK')
->string($redisMock->set('test2', 'something else'))
Expand All @@ -49,16 +68,26 @@ public function testSetGetDelExists()
->isEqualTo('OK')
->integer($redisMock->del(array('test1', 'test2')))
->isEqualTo(2)
->string($redisMock->set('test', 'something', 1))

->string($redisMock->set('test3', 'something', 1))
->isEqualTo('OK')
->integer($redisMock->ttl('test'))
->string($redisMock->setex('test4', 2, 'something else'))
->isEqualTo('OK')
->integer($redisMock->ttl('test3'))
->isEqualTo(1)
->string($redisMock->get('test'))
->isEqualTo('something');
sleep(2);
->integer($redisMock->ttl('test4'))
->isEqualTo(2)
->string($redisMock->get('test3'))
->isEqualTo('something')
->string($redisMock->get('test4'))
->isEqualTo('something else');
sleep(3);
$this->assert
->variable($redisMock->get('test'))
->variable($redisMock->get('test3'))
->isNull()
->variable($redisMock->get('test4'))
->isNull()

->string($redisMock->set('test', 'something', 1))
->isEqualTo('OK')
->string($redisMock->type('test'))
Expand Down

0 comments on commit fc977ca

Please sign in to comment.