Skip to content

Commit 7483628

Browse files
authored
Merge pull request #53 from MacFJA/handle-protected-command-pr-42
Handle protected command
2 parents 6bf7ed5 + 029853f commit 7483628

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2424
- Missing default values on highlight option ([Issue#38])
2525
- Don't throw an exception if the result of the index definition is incorrect ([Issue#46])
2626
- Phpredis extension don't return raw response ([Issue#46])
27+
- Error with version reading if `MODULE LIST` command is protected ([PR#42])
2728

2829
### Changed
2930

@@ -247,3 +248,4 @@ First version
247248
[PR#14]: https://github.com/MacFJA/php-redisearch/pull/14
248249
[PR#15]: https://github.com/MacFJA/php-redisearch/pull/15
249250
[PR#17]: https://github.com/MacFJA/php-redisearch/pull/17
251+
[PR#42]: https://github.com/MacFJA/php-redisearch/pull/42

src/Redis/Initializer.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
use MacFJA\RediSearch\Redis\Command\TagVals;
5353
use Predis\Profile\RedisProfile;
5454
use Rediska_Commands;
55+
use Throwable;
5556

5657
/**
5758
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -139,7 +140,15 @@ public static function registerCommandsRediska(): void
139140

140141
public static function getRediSearchVersion(Client $client): ?string
141142
{
142-
$modules = $client->executeRaw('module', 'list') ?? [];
143+
try {
144+
$modules = $client->executeRaw('module', 'list') ?? [];
145+
} catch (Throwable $exception) {
146+
if (false === stripos($exception->getMessage(), 'unknown command')) {
147+
throw $exception;
148+
}
149+
150+
return null;
151+
}
143152

144153
foreach ($modules as $module) {
145154
$data = array_column(

tests/fixes/PR42Test.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* Copyright MacFJA
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
9+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
10+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
14+
* Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
namespace MacFJA\RediSearch\tests\fixes;
23+
24+
use Exception;
25+
use MacFJA\RediSearch\Redis\Client;
26+
use MacFJA\RediSearch\Redis\Initializer;
27+
use PHPUnit\Framework\TestCase;
28+
29+
/**
30+
* @covers \MacFJA\RediSearch\Redis\Initializer
31+
*
32+
* @internal
33+
*/
34+
class PR42Test extends TestCase
35+
{
36+
public function testUnavailableCommand(): void
37+
{
38+
$client = $this->createMock(Client::class);
39+
$client->method('executeRaw')->willThrowException(new Exception('ERR unknown command `module`, with args beginning with: `list`'));
40+
41+
$response = Initializer::getRediSearchVersion($client);
42+
43+
static::assertNull($response);
44+
}
45+
46+
public function testUnknownError(): void
47+
{
48+
$this->expectException(Exception::class);
49+
50+
$client = $this->createMock(Client::class);
51+
$client->method('executeRaw')->willThrowException(new Exception('ERR unknown server error'));
52+
53+
Initializer::getRediSearchVersion($client);
54+
}
55+
}

0 commit comments

Comments
 (0)