PHP 7.1 library providing a Redis PHP mock for your tests.
Work for now only with predis
$ composer require --dev m6web/redis-mockIt currently mocks these Redis commands :
| Redis command | Description |
|---|---|
| DBSIZE | Returns the number of keys in the selected database |
| DELkey[key ...] | Deletes one or more keys |
| DECRkey | Decrements the integer value of a key by one |
| DECRBYkeydecrement | Decrements the integer value of a key by decrement value |
| DECRBYFLOATkeydecrement | Decrements the float value of a key by decrement value |
| EXISTSkey | Determines if a key exists |
| EXPIREkeyseconds | Sets a key's time to live in seconds |
| FLUSHDB | Flushes the database |
| GETkey | Gets the value of a key |
| HDELkeyarray<fields> | Delete hash fields |
| HEXISTSkeyfield | Determines if a hash field exists |
| HMGETkeyarray<field> | Gets the values of multiple hash fields |
| HGETkeyfield | Gets the value of a hash field |
| HGETALLkey | Gets all the fields and values in a hash |
| HKEYSkey | Gets all the fields in a hash |
| HLENkey | Gets the number of fields in a hash |
| HMSETkeyarray<field, value> | Sets each value in the corresponding field |
| HSETkeyfieldvalue | Sets the string value of a hash field |
| HSETNXkeyfieldvalue | Sets field in the hash stored at key to value, only if field does not yet exist |
| HINCRBYkeyfieldincrement | Increments the integer stored at field in the hash stored at key by increment. |
| INCRkey | Increments the integer value of a key by one |
| INCRBYkeyincrement | Increments the integer value of a key by increment value |
| INCRBYFLOATkeyincrement | Increments the float value of a key by increment value |
| KEYSpattern | Finds all keys matching the given pattern |
| LINDEXkeyindex | Returns the element at index index in the list stored at key |
| LLENkey | Returns the length of the list stored at key |
| LPUSHkeyvalue[value ...] | Pushs values at the head of a list |
| LPOPkey | Pops values at the head of a list |
| LREMkeyvaluecount | Removes count instances of value from the head of a list (follows the predis parameters order) |
| LTRIMkeystartstop | Removes the values of the key list which are outside the range start...stop |
| LRANGEkeystartstop | Gets a range of elements from a list |
| MGETarray<field> | Gets the values of multiple keys |
| MSETarray<field, value> | Sets the string values of multiple keys |
| QUIT | Quit the REDIS |
| RPUSHkeyvalue | Pushs values at the tail of a list |
| RPOPkey | Pops values at the tail of a list |
| SCAN | Iterates the set of keys in the currently selected Redis database. |
| SSCAN | Iterates elements of Sets types. |
| SETkeyvalue | Sets the string value of a key |
| SETEXkeysecondsvalue | Sets the value and expiration of a key |
| SETNXkeyvalue | Sets key to hold value if key does not exist |
| SADDkeymember[member ...] | Adds one or more members to a set |
| SDIFFkeykey[key ...] | Returns the members of the set resulting from the difference between the first set and all the successive sets. |
| SISMEMBERkeymember | Determines if a member is in a set |
| SMEMBERSkey | Gets all the members in a set |
| SUNIONkey[key ...] | Returns the members of the set resulting from the union of all the given sets. |
| SINTERkey[key ...] | Returns the members of the set resulting from the intersection of all the given sets. |
| SCARDkey | Get cardinality of set (count of members) |
| SREMkeymember[member ...] | Removes one or more members from a set |
| TTLkey | Gets the time to live for a key |
| TYPEkey | Returns the string representation of the type of the value stored at key. |
| ZADDkeyscoremember | Adds one member to a sorted set, or update its score if it already exists |
| ZINCRBYkeyincrementmember | Increments the score of member in the sorted set stored at key by increment |
| ZCARDkey | Returns the sorted set cardinality (number of elements) of the sorted set stored at key |
| ZCOUNTkeyminmax | Returns the number of elements in the stored set at key with a score between min and max. |
| ZRANGEkeystartstop[withscores] | Returns the specified range of members in a sorted set |
| ZRANGEBYSCOREkeyminmaxoptions | Returns a range of members in a sorted set, by score |
| ZRANKkeymember | Returns the rank of member in the sorted set stored at key, with the scores ordered from low to high |
| ZREVRANKkeymember | Returns the rank of member in the sorted set stored at key, with the scores ordered from high to low |
| ZREMkeymember | Removes one membner from a sorted set |
| ZREMRANGEBYSCOREkeyminmax | Removes all members in a sorted set within the given scores |
| ZREVRANGEkeystartstop[withscores] | Returns the specified range of members in a sorted set, with scores ordered from high to low |
| ZREVRANGEBYSCOREkeyminmaxoptions | Returns a range of members in a sorted set, by score, with scores ordered from high to low |
| ZSCAN | Iterates elements of Sorted Sets types. |
| ZSCOREkeymember | Returns the score of member in the sorted set at key |
| ZUNIONSTOREdestnumkeyskey ... [weights ...][aggregate SUM/MIN/MAX] | Computes the union of the stored sets given by the specified keys, store the result in the destination key, and returns the number of elements of the new sorted set. |
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. EVAL, EVALSHA, WATCH and UNWATCH are just stubs—they won't execute anything
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.
- Storage is static and therefore shared by all instances.
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.
The storage in the RedisMock class is organized by named areas. The default area's name is the empty string '' but you can
specify an alternate area name when calling the factory's getAdapter method.
getAdapter($classToExtend, $failOnlyAtRuntime = false, $ignoreConstructor = true, $storage = '')This enables the mocking of several remote Redis servers, each one with its own storage area.
However, one same area remains statically shared across all the instances bound to it.
The development environment is provided by Vagrant and the Xotelia box.
$ cp Vagrantfile.dist Vagrantfile
$ vagrant up
$ vagrant ssh$ cd /vagrant
$ composer install
$ ./vendor/bin/atoumDevelopped by the Cytron Team of M6 Web.
Tested with atoum.
RedisMock is licensed under the MIT license.