From bf7b7fb47de566f89878315039ed64e46580a35b Mon Sep 17 00:00:00 2001 From: David Chancogne Date: Fri, 14 Feb 2014 17:06:50 -0500 Subject: [PATCH 1/5] Add support for SETEX command --- .gitignore | 1 + src/M6Web/Component/RedisMock/RedisMock.php | 9 +++++++-- tests/units/RedisMock.php | 6 ++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index d1502b0..164c346 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ vendor/ composer.lock +composer.phar diff --git a/src/M6Web/Component/RedisMock/RedisMock.php b/src/M6Web/Component/RedisMock/RedisMock.php index 5a4a934..d720fcb 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)) { @@ -181,7 +186,7 @@ public function sadd($key, $member) if ($isNew) { $this->data[$key][] = $member; } - + $this->dataTypes[$key] = 'set'; if (array_key_exists($key, $this->dataTtl)) { @@ -275,7 +280,7 @@ public function lpush($key, $value) if (isset($this->data[$key]) && !is_array($this->data[$key])) { return $this->returnPipedInfo(null); - } + } array_unshift($this->data[$key], $value); diff --git a/tests/units/RedisMock.php b/tests/units/RedisMock.php index 50652d5..6776bc6 100644 --- a/tests/units/RedisMock.php +++ b/tests/units/RedisMock.php @@ -23,6 +23,8 @@ public function testSetGetDelExists() ->isEqualTo(0) ->string($redisMock->set('test', 'something')) ->isEqualTo('OK') + ->string($redisMock->setex('test', 10, 'something')) + ->isEqualTo('OK') ->string($redisMock->type('test')) ->isEqualTo('string') ->boolean($redisMock->exists('test')) @@ -51,6 +53,8 @@ public function testSetGetDelExists() ->isEqualTo(1) ->string($redisMock->set('test', 'something', 1)) ->isEqualTo('OK') + ->string($redisMock->setex('test2', 1, 'something')) + ->isEqualTo('OK') ->integer($redisMock->ttl('test')) ->isEqualTo(1) ->string($redisMock->get('test')) @@ -59,6 +63,8 @@ public function testSetGetDelExists() $this->assert ->variable($redisMock->get('test')) ->isNull() + ->variable($redisMock->get('test2')) + ->isNull() ->string($redisMock->set('test', 'something', 1)) ->isEqualTo('OK') ->string($redisMock->type('test')) From 369fe18b347e4ebb1745e7549ed596cc44740c2d Mon Sep 17 00:00:00 2001 From: David Chancogne Date: Sun, 16 Feb 2014 17:57:00 -0500 Subject: [PATCH 2/5] Update README Update README to add support for SETEX redis command. Fix README to mention ability to create mock class without throwing an exception (issue #20) --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d356430..ab3812d 100644 --- a/README.md +++ b/README.md @@ -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* | Set 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 From 74147482ed7a6ba9ab66845f79b5d027a69f18bc Mon Sep 17 00:00:00 2001 From: David Chancogne Date: Mon, 17 Feb 2014 10:25:37 -0500 Subject: [PATCH 3/5] Cleanup and enhance unit tests Cleanup and enhance unit tests with better use case testing for SETEX. Fix README. --- README.md | 8 ++++---- tests/units/RedisMock.php | 43 ++++++++++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index ab3812d..28173fb 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,7 +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* | Set the value and expiration 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 @@ -60,7 +60,7 @@ Redis command | Description **DBSIZE** | Returns the number of keys in the selected database **FLUSHDB** | Flushes the database -It mocks **MULTI**, **DISCARD** and **EXEC** commands but without any transaction behaviors, they just make the interface fluent and return each command results. +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. ## Usage @@ -94,7 +94,7 @@ $ ./vendor/bin/atoum ## Credits -Developped by the [Cytron Team](http://cytron.fr/) of [M6 Web](http://tech.m6web.fr/). +Developped by the [Cytron Team](http://cytron.fr/) of [M6 Web](http://tech.m6web.fr/). Tested with [atoum](http://atoum.org). ## License diff --git a/tests/units/RedisMock.php b/tests/units/RedisMock.php index 6776bc6..0f1cf5a 100644 --- a/tests/units/RedisMock.php +++ b/tests/units/RedisMock.php @@ -21,10 +21,9 @@ public function testSetGetDelExists() ->isNull() ->integer($redisMock->del('test')) ->isEqualTo(0) + ->string($redisMock->set('test', 'something')) ->isEqualTo('OK') - ->string($redisMock->setex('test', 10, 'something')) - ->isEqualTo('OK') ->string($redisMock->type('test')) ->isEqualTo('string') ->boolean($redisMock->exists('test')) @@ -39,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')) @@ -51,20 +68,26 @@ public function testSetGetDelExists() ->isEqualTo(1) ->integer($redisMock->del('test2')) ->isEqualTo(1) - ->string($redisMock->set('test', 'something', 1)) + + ->string($redisMock->set('test3', 'something', 1)) ->isEqualTo('OK') - ->string($redisMock->setex('test2', 1, 'something')) + ->string($redisMock->setex('test4', 2, 'something else')) ->isEqualTo('OK') - ->integer($redisMock->ttl('test')) + ->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('test2')) + ->variable($redisMock->get('test4')) ->isNull() + ->string($redisMock->set('test', 'something', 1)) ->isEqualTo('OK') ->string($redisMock->type('test')) From 8dcf35ea03ea08440aa4d96ad051a5cbbc5faa78 Mon Sep 17 00:00:00 2001 From: David Chancogne Date: Mon, 17 Feb 2014 10:56:18 -0500 Subject: [PATCH 4/5] Fix markdown --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 28173fb..f343ff3 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ Redis command | Description **DBSIZE** | Returns the number of keys in the selected database **FLUSHDB** | Flushes the database -It mocks **MULTI**, **DISCARD** and **EXEC** commands but without any transaction behaviors, they just make the interface fluent and return each command results. +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. ## Usage @@ -94,7 +94,7 @@ $ ./vendor/bin/atoum ## Credits -Developped by the [Cytron Team](http://cytron.fr/) of [M6 Web](http://tech.m6web.fr/). +Developped by the [Cytron Team](http://cytron.fr/) of [M6 Web](http://tech.m6web.fr/). Tested with [atoum](http://atoum.org). ## License From a6745b572b633a455c49b5f0df12971e596a3180 Mon Sep 17 00:00:00 2001 From: David Chancogne Date: Mon, 17 Feb 2014 10:58:16 -0500 Subject: [PATCH 5/5] Fix README markdown --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f343ff3..36969b5 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ Redis command | Description **DBSIZE** | Returns the number of keys in the selected database **FLUSHDB** | Flushes the database -It mocks **MULTI**, **DISCARD** and **EXEC** commands but without any transaction behaviors, they just make the interface fluent and return each command results. +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. ## Usage @@ -94,7 +94,7 @@ $ ./vendor/bin/atoum ## Credits -Developped by the [Cytron Team](http://cytron.fr/) of [M6 Web](http://tech.m6web.fr/). +Developped by the [Cytron Team](http://cytron.fr/) of [M6 Web](http://tech.m6web.fr/). Tested with [atoum](http://atoum.org). ## License