Skip to content

Repository files navigation

Redis PHP Mock Build Status

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

Installation

Add this line in your composer.json :

{
"require": {
"m6web/redis-mock": "~1.2"
}
}

Update your vendors :

$ composer update m6web/redis-mock

Functions

It currently mocks these Redis commands :

Redis commandDescription
DELkey[key ...]Deletes one or more keys
EXISTSkeyDetermines if a key exists
EXPIREkeysecondsSets a key's time to live in seconds
KEYSpatternFinds all keys matching the given pattern
TTLkey Gets the time to live for a key
TYPEkeyReturns the string representation of the type of the value stored at key.
GETkeyGets the value of a key
INCRkeyIncrements the integer value of a key by one
INCRBYkeyincrementIncrements the integer value of a key by increment value
DECRkeyDecrements the integer value of a key by one
DECRBYkeydecrementDecrements the integer value of a key by decrement value
SETkeyvalueSets the string value of a key
SETEXkeysecondsvalueSets the value and expiration of a key
SADDkeymemberAdds one member to a set
SISMEMBERkeymemberDetermines if a member is in a set
SMEMBERSkeyGets all the members in a set
SREMkeymemberRemoves one member from a set
HDELkeyfieldDelete one hash fields
HEXISTSkeyfieldDetermines if a hash field exists
HGETkeyfieldGets the value of a hash field
HGETALLkeyGets all the fields and values in a hash
HMSETkeyarray<field, value>Sets each value in the corresponding field
HSETkeyfieldvalueSets the string value of a hash field
LPUSHkeyvaluePushs values at the head of a list
LPOPkeyPops values at the head of a list
LREMkeycountvalueRemoves count instances of value from the head of a list
LTRIMkeystartstopRemoves the values of the key list which are outside the range start...stop
RPUSHkeyvaluePushs values at the tail of a list
RPOPkeyPops values at the tail of a list
ZADDkeyscorememberAdds one member to a sorted set, or update its score if it already exists
ZRANGEkeystartstopReturns the specified range of members in a sorted set
ZRANGEBYSCOREkeyminmaxoptionsReturns a range of members in a sorted set, by score
ZREMkeymemberRemoves one membner from a sorted set
ZREMRANGEBYSCOREkeyminmaxRemoves all members in a sorted set within the given scores
ZREVRANGEkeystartstopReturns the specified range of members in a sorted set, with scores ordered from high to low
ZREVRANGEBYSCOREkeyminmaxoptionsReturns a range of members in a sorted set, by score, with scores ordered from high to low
DBSIZEReturns the number of keys in the selected database
FLUSHDBFlushes 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. PIPELINE and EXECUTE pseudo commands (client pipelining) are also mocked.

Usage

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

$factory = new \M6Web\Component\RedisMock\RedisMockFactory();
$myRedisMockClass = $factory->getAdapterClass('My\Redis\Library');
$myRedisMock = new$myRedisMockClass($myParameters);

In a simpler way, if you don't need to instanciate the mocked class with custom parameters (e.g. to easier inject the mock using Symfony config file), you can use getAdapter instead of getAdapterClass to directly create the adapter :

$factory = new \M6Web\Component\RedisMock\RedisMockFactory();
$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->getAdapter('My\Redis\Library', true). The exception will then thrown only when the command is called.

Tests

$ curl -sS https://getcomposer.org/installer | php
$ php composer.phar install
$ ./vendor/bin/atoum

Credits

Developped by the Cytron Team of M6 Web. Tested with atoum.

License

RedisMock is licensed under the MIT license.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages