Skip to content

Handle protected command #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
11 changes: 10 additions & 1 deletion src/Redis/Initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
use MacFJA\RediSearch\Redis\Command\TagVals;
use Predis\Profile\RedisProfile;
use Rediska_Commands;
use Throwable;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
Expand Down Expand Up @@ -139,7 +140,15 @@ 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 (false === stripos($exception->getMessage(), 'unknown command')) {
throw $exception;
}

return null;
}

foreach ($modules as $module) {
$data = array_column(
Expand Down
55 changes: 55 additions & 0 deletions tests/fixes/PR42Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

/*
* Copyright MacFJA
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

namespace MacFJA\RediSearch\tests\fixes;

use Exception;
use MacFJA\RediSearch\Redis\Client;
use MacFJA\RediSearch\Redis\Initializer;
use PHPUnit\Framework\TestCase;

/**
* @covers \MacFJA\RediSearch\Redis\Initializer
*
* @internal
*/
class PR42Test extends TestCase
{
public function testUnavailableCommand(): void
{
$client = $this->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);
}
}