diff --git a/README.md b/README.md index 3c72b65..ffc2336 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 diff --git a/src/M6Web/Component/RedisMock/RedisMock.php b/src/M6Web/Component/RedisMock/RedisMock.php index f5c0434..56388eb 100644 --- a/src/M6Web/Component/RedisMock/RedisMock.php +++ b/src/M6Web/Component/RedisMock/RedisMock.php @@ -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)) { diff --git a/tests/units/RedisMock.php b/tests/units/RedisMock.php index 686961f..9416e3b 100644 --- a/tests/units/RedisMock.php +++ b/tests/units/RedisMock.php @@ -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')) @@ -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')) @@ -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'))