From fe519c52b4277377bd1345e3622ee82cab6ed416 Mon Sep 17 00:00:00 2001 From: Vincent Radstake Date: Tue, 14 Jun 2022 16:44:22 +0200 Subject: [PATCH 1/4] Return empty modules array when module command is not available --- src/Redis/Initializer.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Redis/Initializer.php b/src/Redis/Initializer.php index 865f3af..7c5d0a9 100644 --- a/src/Redis/Initializer.php +++ b/src/Redis/Initializer.php @@ -139,7 +139,14 @@ public static function registerCommandsRediska(): void public static function getRediSearchVersion(Client $client): ?string { - $modules = $client->executeRaw('module', 'list') ?? []; + try { + $modules = $client->executeRaw('module', 'list') ?? []; + } catch (\Throwable $exception) { + if (strpos($exception->getMessage(), 'unknown command') === false) { + throw $exception; + } + $modules = []; + } foreach ($modules as $module) { $data = array_column( From dbf3e4a5baa833ab226b3ec625b87f854e207283 Mon Sep 17 00:00:00 2001 From: Vincent Radstake Date: Tue, 14 Jun 2022 17:00:17 +0200 Subject: [PATCH 2/4] Update Initializer.php --- src/Redis/Initializer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Redis/Initializer.php b/src/Redis/Initializer.php index 7c5d0a9..a15de99 100644 --- a/src/Redis/Initializer.php +++ b/src/Redis/Initializer.php @@ -142,7 +142,7 @@ public static function getRediSearchVersion(Client $client): ?string try { $modules = $client->executeRaw('module', 'list') ?? []; } catch (\Throwable $exception) { - if (strpos($exception->getMessage(), 'unknown command') === false) { + if (strpos($exception->getMessage(), 'unknown command') !== false) { throw $exception; } $modules = []; From f807d02cac347ce0a24d1cc21f8c2bef59c24c6c Mon Sep 17 00:00:00 2001 From: Vincent Radstake Date: Tue, 14 Jun 2022 18:00:11 +0200 Subject: [PATCH 3/4] Update Initializer.php --- src/Redis/Initializer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Redis/Initializer.php b/src/Redis/Initializer.php index a15de99..7c5d0a9 100644 --- a/src/Redis/Initializer.php +++ b/src/Redis/Initializer.php @@ -142,7 +142,7 @@ public static function getRediSearchVersion(Client $client): ?string try { $modules = $client->executeRaw('module', 'list') ?? []; } catch (\Throwable $exception) { - if (strpos($exception->getMessage(), 'unknown command') !== false) { + if (strpos($exception->getMessage(), 'unknown command') === false) { throw $exception; } $modules = []; From 029853fcc4f83548eb13980e72b50563e533f672 Mon Sep 17 00:00:00 2001 From: MacFJA Date: Fri, 5 Aug 2022 23:32:55 +0200 Subject: [PATCH 4/4] Add UT + update Changelog --- CHANGELOG.md | 2 ++ src/Redis/Initializer.php | 8 +++--- tests/fixes/PR42Test.php | 55 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 tests/fixes/PR42Test.php diff --git a/CHANGELOG.md b/CHANGELOG.md index e36b147..e052485 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Missing default values on highlight option ([Issue#38]) - Don't throw an exception if the result of the index definition is incorrect ([Issue#46]) - Phpredis extension don't return raw response ([Issue#46]) +- Error with version reading if `MODULE LIST` command is protected ([PR#42]) ### Changed @@ -247,3 +248,4 @@ First version [PR#14]: https://github.com/MacFJA/php-redisearch/pull/14 [PR#15]: https://github.com/MacFJA/php-redisearch/pull/15 [PR#17]: https://github.com/MacFJA/php-redisearch/pull/17 +[PR#42]: https://github.com/MacFJA/php-redisearch/pull/42 diff --git a/src/Redis/Initializer.php b/src/Redis/Initializer.php index 7c5d0a9..8c72dbf 100644 --- a/src/Redis/Initializer.php +++ b/src/Redis/Initializer.php @@ -52,6 +52,7 @@ use MacFJA\RediSearch\Redis\Command\TagVals; use Predis\Profile\RedisProfile; use Rediska_Commands; +use Throwable; /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) @@ -141,11 +142,12 @@ public static function getRediSearchVersion(Client $client): ?string { try { $modules = $client->executeRaw('module', 'list') ?? []; - } catch (\Throwable $exception) { - if (strpos($exception->getMessage(), 'unknown command') === false) { + } catch (Throwable $exception) { + if (false === stripos($exception->getMessage(), 'unknown command')) { throw $exception; } - $modules = []; + + return null; } foreach ($modules as $module) { diff --git a/tests/fixes/PR42Test.php b/tests/fixes/PR42Test.php new file mode 100644 index 0000000..07063cf --- /dev/null +++ b/tests/fixes/PR42Test.php @@ -0,0 +1,55 @@ +createMock(Client::class); + $client->method('executeRaw')->willThrowException(new Exception('ERR unknown command `module`, with args beginning with: `list`')); + + $response = Initializer::getRediSearchVersion($client); + + static::assertNull($response); + } + + public function testUnknownError(): void + { + $this->expectException(Exception::class); + + $client = $this->createMock(Client::class); + $client->method('executeRaw')->willThrowException(new Exception('ERR unknown server error')); + + Initializer::getRediSearchVersion($client); + } +}