mirror of
https://github.com/predis/predis.git
synced 2026-08-26 08:39:55 +00:00
cc2a7657db
We have renamed most methods to drop the "command" suffix as it is quite redundant. Due to this change and thanks to variadic methods introduced with PHP 5.6 we took the opportunity to replace both "supportsCommand()" and "supportsCommands()" with a single new method "supports()". Added more stringent typehints for method arguments and typehints for return values now that we do not need to support anything below PHP 7.2. Also moved from using array() to [] in source code of class involved.
227 lines
6.9 KiB
PHP
227 lines
6.9 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the Predis package.
|
|
*
|
|
* (c) Daniele Alessandri <suppakilla@gmail.com>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Predis\Connection;
|
|
|
|
use PredisTestCase;
|
|
|
|
/**
|
|
* @group ext-curl
|
|
* @group ext-phpiredis
|
|
* @group realm-connection
|
|
* @group realm-webdis
|
|
* @requires extension phpiredis
|
|
* @requires extension curl
|
|
*/
|
|
class WebdisConnectionTest extends PredisTestCase
|
|
{
|
|
/**
|
|
* @group disconnected
|
|
*/
|
|
public function testIsConnectedAlwaysReturnsTrue()
|
|
{
|
|
$connection = $this->createConnection();
|
|
|
|
$this->assertTrue($connection->isConnected());
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
*/
|
|
public function testSupportsSchemeUnix()
|
|
{
|
|
$connection = $this->createConnectionWithParams(array('scheme' => 'http'));
|
|
|
|
$this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
*/
|
|
public function testThrowsExceptionOnInvalidScheme()
|
|
{
|
|
$this->expectException('InvalidArgumentException');
|
|
$this->expectExceptionMessage("Invalid scheme: 'tcp'");
|
|
|
|
$connection = $this->createConnectionWithParams(array('scheme' => 'tcp'));
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
*/
|
|
public function testWritingCommandsIsNotSupported()
|
|
{
|
|
$this->expectException('Predis\NotSupportedException');
|
|
$this->expectExceptionMessage("The method Predis\Connection\WebdisConnection::writeRequest() is not supported");
|
|
|
|
$connection = $this->createConnection();
|
|
$connection->writeRequest($this->getCommandFactory()->create('ping'));
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
*/
|
|
public function testReadingResponsesIsNotSupported()
|
|
{
|
|
$this->expectException('Predis\NotSupportedException');
|
|
$this->expectExceptionMessage("The method Predis\Connection\WebdisConnection::readResponse() is not supported");
|
|
|
|
$connection = $this->createConnection();
|
|
$connection->readResponse($this->getCommandFactory()->create('ping'));
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
*/
|
|
public function testReadingFromConnectionIsNotSupported()
|
|
{
|
|
$this->expectException('Predis\NotSupportedException');
|
|
$this->expectExceptionMessage("The method Predis\Connection\WebdisConnection::read() is not supported");
|
|
|
|
$connection = $this->createConnection();
|
|
$connection->read();
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
*/
|
|
public function testAddingConnectCommandsIsNotSupported()
|
|
{
|
|
$this->expectException('Predis\NotSupportedException');
|
|
$this->expectExceptionMessage("The method Predis\Connection\WebdisConnection::addConnectCommand() is not supported");
|
|
|
|
$connection = $this->createConnection();
|
|
$connection->addConnectCommand($this->getCommandFactory()->create('ping'));
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
*/
|
|
public function testRejectCommandSelect()
|
|
{
|
|
$this->expectException('Predis\NotSupportedException');
|
|
$this->expectExceptionMessage("Command 'SELECT' is not allowed by Webdis");
|
|
|
|
$connection = $this->createConnection();
|
|
$connection->executeCommand($this->getCommandFactory()->create('select', array(0)));
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
*/
|
|
public function testRejectCommandAuth()
|
|
{
|
|
$this->expectException('Predis\NotSupportedException');
|
|
$this->expectExceptionMessage("Command 'AUTH' is not allowed by Webdis");
|
|
|
|
$connection = $this->createConnection();
|
|
$connection->executeCommand($this->getCommandFactory()->create('auth', array('foobar')));
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
*/
|
|
public function testCanBeSerialized()
|
|
{
|
|
$parameters = $this->getParameters(array(
|
|
'alias' => 'redis',
|
|
'read_write_timeout' => 10,
|
|
));
|
|
|
|
$connection = $this->createConnectionWithParams($parameters);
|
|
|
|
$unserialized = unserialize(serialize($connection));
|
|
|
|
$this->assertInstanceOf('Predis\Connection\WebdisConnection', $unserialized);
|
|
$this->assertEquals($parameters, $unserialized->getParameters());
|
|
}
|
|
|
|
// ******************************************************************** //
|
|
// ---- INTEGRATION TESTS --------------------------------------------- //
|
|
// ******************************************************************** //
|
|
|
|
/**
|
|
* @group connected
|
|
*/
|
|
public function testExecutesMultipleCommandsOnServer()
|
|
{
|
|
$commands = $this->getCommandFactory();
|
|
|
|
$cmdPing = $commands->create('ping');
|
|
$cmdEcho = $commands->create('echo', array('echoed'));
|
|
$cmdGet = $commands->create('get', array('foobar'));
|
|
$cmdRpush = $commands->create('rpush', array('metavars', 'foo', 'hoge', 'lol'));
|
|
$cmdLrange = $commands->create('lrange', array('metavars', 0, -1));
|
|
|
|
$connection = $this->createConnection(true);
|
|
|
|
$this->assertEquals('PONG', $connection->executeCommand($cmdPing));
|
|
$this->assertSame('echoed', $connection->executeCommand($cmdEcho));
|
|
$this->assertNull($connection->executeCommand($cmdGet));
|
|
$this->assertSame(3, $connection->executeCommand($cmdRpush));
|
|
$this->assertSame(array('foo', 'hoge', 'lol'), $connection->executeCommand($cmdLrange));
|
|
}
|
|
|
|
/**
|
|
* @medium
|
|
* @group disconnected
|
|
* @group slow
|
|
*/
|
|
public function testThrowExceptionWhenUnableToConnect()
|
|
{
|
|
$this->expectException('Predis\Connection\ConnectionException');
|
|
|
|
$connection = $this->createConnectionWithParams(array('host' => '169.254.10.10'));
|
|
$connection->executeCommand($this->getCommandFactory()->create('ping'));
|
|
}
|
|
|
|
// ******************************************************************** //
|
|
// ---- HELPER METHODS ------------------------------------------------ //
|
|
// ******************************************************************** //
|
|
|
|
/**
|
|
* Returns a named array with the default connection parameters and their values.
|
|
*
|
|
* @return array Default connection parameters.
|
|
*/
|
|
protected function getDefaultParametersArray()
|
|
{
|
|
return array(
|
|
'scheme' => 'http',
|
|
'host' => WEBDIS_SERVER_HOST,
|
|
'port' => WEBDIS_SERVER_PORT,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
protected function createConnection()
|
|
{
|
|
return $this->createConnectionWithParams(array());
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
protected function createConnectionWithParams($parameters)
|
|
{
|
|
if (!$parameters instanceof ParametersInterface) {
|
|
$parameters = $this->getParameters($parameters);
|
|
}
|
|
|
|
$connection = new WebdisConnection($parameters);
|
|
$connection->executeCommand($this->getCommandFactory()->create('flushdb'));
|
|
|
|
return $connection;
|
|
}
|
|
}
|