1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2025 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Redis;
use Predis\Response\Status;
/**
* @group commands
* @group realm-server
*/
class COMMAND_Test extends PredisCommandTestCase
{
/**
* {@inheritdoc}
*/
protected function getExpectedCommand(): string
{
return 'Predis\Command\Redis\COMMAND';
}
/**
* {@inheritdoc}
*/
protected function getExpectedId(): string
{
return 'COMMAND';
}
/**
* @group disconnected
*/
public function testFilterArguments(): void
{
$arguments = ['INFO', 'DEL'];
$expected = ['INFO', 'DEL'];
$command = $this->getCommand();
$command->setArguments($arguments);
$this->assertSame($expected, $command->getArguments());
}
/**
* @group disconnected
*/
public function testParseResponse(): void
{
$raw = [
['get', 2, [new Status('readonly'), new Status('fast')], 1, 1, 1],
['set', -3, [new Status('write'), new Status('denyoom')], 1, 1, 1],
['watch', -2, [new Status('readonly'), new Status('noscript'), new Status('fast')], 1, -1, 1],
['unwatch', 1, [new Status('readonly'), new Status('noscript'), new Status('fast')], 0, 0, 0],
['info', -1, [new Status('readonly'), new Status('loading'), new Status('stale')], 0, 0, 0],
];
$expected = $raw;
$command = $this->getCommand();
$this->assertSame($expected, $command->parseResponse($raw));
}
/**
* @group disconnected
*/
public function testParseEmptyResponse(): void
{
$raw = [null];
$expected = [null];
$command = $this->getCommand();
$this->assertSame($expected, $command->parseResponse($raw));
}
/**
* @group connected
* @requiresRedisVersion >= 2.8.13
*/
public function testReturnsEmptyCommandInfoOnNonExistingCommand(): void
{
$redis = $this->getClient();
$this->assertCount(1, $response = $redis->command('INFO', 'FOOBAR'));
$this->assertSame([null], $response);
}
/**
* @group connected
* @requiresRedisVersion >= 6.0.0
*/
public function testReturnsEmptyCommandInfoOnNonExistingCommandResp3(): void
{
$redis = $this->getResp3Client();
$this->assertCount(1, $response = $redis->command('INFO', 'FOOBAR'));
$this->assertSame([null], $response);
}
/**
* @group connected
* @group relay-incompatible
* @group relay-fixme
* @requiresRedisVersion >= 2.8.13
*
* Relay uses RESP3 maps, the `Predis\Command\Redis\COMMAND` needs a converter.
*/
public function testReturnsCommandInfoOnExistingCommand(): void
{
$redis = $this->getClient();
$expected = [['get', 2, ['readonly', 'fast'], 1, 1, 1]];
// NOTE: starting with Redis 6.0 and the introduction of Access Control
// Lists, COMMAND INFO returns an additional array for each specified
// command in the request with a list of the ACL categories associated
// to a command. We simply append this additional array in the expected
// response if the test suite is executed against Redis >= 6.0.
if ($this->isRedisServerVersion('>=', '6.0')) {
$expected[0][] = ['@read', '@string', '@fast'];
}
// NOTE: starting with Redis 7.0 COMMAND INFO returns an additional arrays:
// - Command tips: https://redis.io/topics/command-tips.
// - Key specifications: https://redis.io/topics/key-specs.
// - Subcommands: https://redis.io/commands/command/#subcommands.
// We simply append this additional array in the expected response if the
// test suite is executed against Redis >= 7.0.
if ($this->isRedisServerVersion('>=', '7.0')) {
$expected[0][] = [];
$expected[0][] = [
[
'flags',
['RO', 'access'],
'begin_search',
['type', 'index', 'spec', ['index', 1]],
'find_keys',
['type', 'range', 'spec', ['lastkey', 0, 'keystep', 1, 'limit', 0]],
],
];
$expected[0][] = [];
}
$this->assertCount(1, $response = $redis->command('INFO', 'GET'));
// NOTE: we use assertEquals instead of assertSame because Redis returns
// flags as +STATUS responses, represented by Predis with instances of
// Predis\Response\Status instead of plain strings. This class responds
// to __toString() so the string conversion is implicit, but assertSame
// checks for strict equality while assertEquals is loose.
$this->assertEquals($expected, $response);
}
/**
* @group connected
* @requiresRedisVersion >= 2.8.13
*/
public function testReturnsListOfCommandInfoWithNoArguments(): void
{
$redis = $this->getClient();
$this->assertGreaterThan(100, count($response = $redis->command()));
}
}
|