mirror of
https://github.com/predis/predis.git
synced 2026-11-08 19:06:36 +00:00
21deb140e8
Relay::select() throws a TypeError for anything but an integer, where other connections get an error response from the server. Wrap the call and return the same error response, so the existing test runs against Relay and no longer needs a Relay-only counterpart. Relay warns before sending SELECT for out-of-range databases, so that test keeps its Relay-specific counterpart.
133 lines
3.2 KiB
PHP
133 lines
3.2 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the Predis package.
|
|
*
|
|
* (c) 2009-2020 Daniele Alessandri
|
|
* (c) 2021-2026 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;
|
|
|
|
/**
|
|
* @group commands
|
|
* @group realm-connection
|
|
*/
|
|
class SELECT_Test extends PredisCommandTestCase
|
|
{
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
protected function getExpectedCommand(): string
|
|
{
|
|
return 'Predis\Command\Redis\SELECT';
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
protected function getExpectedId(): string
|
|
{
|
|
return 'SELECT';
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
*/
|
|
public function testFilterArguments(): void
|
|
{
|
|
$arguments = [10];
|
|
$expected = [10];
|
|
|
|
$command = $this->getCommand();
|
|
$command->setArguments($arguments);
|
|
|
|
$this->assertSame($expected, $command->getArguments());
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
*/
|
|
public function testParseResponse(): void
|
|
{
|
|
$this->assertSame('OK', $this->getCommand()->parseResponse('OK'));
|
|
}
|
|
|
|
/**
|
|
* @group connected
|
|
*/
|
|
public function testCanSelectDifferentDatabase(): void
|
|
{
|
|
$redis = $this->getClient();
|
|
|
|
$redis->set('foo', 'bar');
|
|
|
|
$this->assertEquals('OK', $redis->select(REDIS_SERVER_DBNUM + 1));
|
|
$this->assertSame(0, $redis->exists('foo'));
|
|
}
|
|
|
|
/**
|
|
* @group connected
|
|
* @group relay-incompatible
|
|
*/
|
|
public function testThrowsExceptionOnUnexpectedDatabaseRange(): void
|
|
{
|
|
$this->expectException('Predis\Response\ServerException');
|
|
$this->expectExceptionMessageMatches('/ERR.*DB index/');
|
|
|
|
$redis = $this->getClient();
|
|
|
|
$redis->select(100000000);
|
|
}
|
|
|
|
/**
|
|
* Relay warns before sending SELECT for indexes beyond `relay.databases`,
|
|
* which PHPUnit would turn into an exception before the server replies.
|
|
*
|
|
* @group connected
|
|
*/
|
|
public function testThrowsExceptionOnUnexpectedDatabaseRangeDespiteRelayWarning(): void
|
|
{
|
|
if (!getenv('USE_RELAY')) {
|
|
$this->markTestSkipped('Relay only warns when it is the active connection.');
|
|
}
|
|
|
|
$this->expectException('Predis\Response\ServerException');
|
|
$this->expectExceptionMessageMatches('/ERR.*DB index/');
|
|
|
|
$redis = $this->getClient();
|
|
|
|
$previous = set_error_handler(
|
|
static function ($level, $message, $file = '', $line = 0) use (&$previous) {
|
|
if (strpos($message, 'in-memory databases') !== false) {
|
|
return true;
|
|
}
|
|
|
|
return $previous ? $previous($level, $message, $file, $line) : false;
|
|
}
|
|
);
|
|
|
|
try {
|
|
$redis->select(100000000);
|
|
} finally {
|
|
restore_error_handler();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @group connected
|
|
*/
|
|
public function testThrowsExceptionOnUnexpectedDatabaseName(): void
|
|
{
|
|
$this->expectException('Predis\Response\ServerException');
|
|
$this->expectExceptionMessageMatches('/ERR.*(invalid DB index|value is not an integer or out of range)/');
|
|
|
|
$redis = $this->getClient();
|
|
|
|
$redis->select('x');
|
|
}
|
|
}
|