Not working with phpredis
We discovered an issue with phpredis.
We wan't to create a Redis mock to inject them into another class that expects an instance of Redis.
Our solution now is to not use the factory but creating RedisMock directly, remove the typehint in our sut and inject the RedisMock directly.
System Informations
OS: Linux
PHP: PHP 7.1.10-1+0~20170929170631.9+jessie~1.gbp501135 (cli)
phpredis: 3.1.2
Reproduce
$factory = new \M6Web\Component\RedisMock\RedisMockFactory();
$myRedisMockClass = $factory->getAdapter(Redis::class, true);
$myRedisMock = new $myRedisMockClass();
$myRedisMock->hSetNx('foo', 'bar', 'baz');
Error
ArgumentCountError: Too few arguments to function M6Web\Component\RedisMock\RedisMock::hsetnx(), 0 passed in vendor/m6web/redis-mock/src/M6Web/Component/RedisMock/RedisMockFactory.php(226) : eval()'d code on line 370 and exactly 3 expected
vendor/m6web/redis-mock/src/M6Web/Component/RedisMock/RedisMock.php:579
More informations
we debugged a bit and found out that the reflection of Redis give no details about the parameters for most of the methods.
php > $ref = new ReflectionClass(Redis::class);
php > echo (string)$ref;
Class [ <internal:redis> class Redis ] {
...
Method [ <internal:redis> public method hGet ] {
}
Method [ <internal:redis> public method hSet ] {
}
Method [ <internal:redis> public method hSetNx ] {
}
Method [ <internal:redis> public method hDel ] {
}
...
Method [ <internal:redis> public method command ] {
}
Method [ <internal:redis> public method scan ] {
- Parameters [3] {
Parameter #0 [ <required> &$i_iterator ]
Parameter #1 [ <optional> $str_pattern ]
Parameter #2 [ <optional> $i_count ]
}
}
Method [ <internal:redis> public method hscan ] {
- Parameters [4] {
Parameter #0 [ <required> $str_key ]
Parameter #1 [ <required> &$i_iterator ]
Parameter #2 [ <optional> $str_pattern ]
Parameter #3 [ <optional> $i_count ]
}
}
...
}
Sorry I didn't read the disclaimer in the README that only predis is supported. But anyway, please see this as a feature request 😉
Proper reflection signatures were added to phpredis in https://github.com/phpredis/phpredis/issues/1055
https://github.com/TysonAndre/RedisMock/commit/a98c233df3f1b38b6b3caabf95897944077eef5b seems to be working with 3.1.2 (I haven't checked if errors were suppressed)
NOTE: I haven't gotten around to running the test suites yet, I expect there to be failures to fix
- I'm instantiating RedisMock directly instead of using RedisMockFactory
- The isOptional change seems like it could be merged on its own. It's based on pr 48 to this branch.