123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628 |
- <?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 realm-connection
- */
- abstract class PredisConnectionTestCase extends PredisTestCase
- {
- /**
- * @group disconnected
- */
- public function testConstructorDoesNotOpenConnection()
- {
- $connection = $this->createConnection();
- $this->assertFalse($connection->isConnected());
- }
- /**
- * @group disconnected
- */
- public function testSupportsSchemeTCP()
- {
- $connection = $this->createConnectionWithParams(array('scheme' => 'tcp'));
- $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
- }
- /**
- * @group disconnected
- */
- public function testSupportsSchemeRedis()
- {
- $connection = $this->createConnectionWithParams(array('scheme' => 'redis'));
- $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
- }
- /**
- * @group disconnected
- */
- public function testSupportsSchemeTls()
- {
- $connection = $this->createConnectionWithParams(array('scheme' => 'tls'));
- $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
- }
- /**
- * @group disconnected
- */
- public function testSupportsSchemeRediss()
- {
- $connection = $this->createConnectionWithParams(array('scheme' => 'rediss'));
- $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
- }
- /**
- * @group disconnected
- */
- public function testSupportsSchemeUnix()
- {
- $connection = $this->createConnectionWithParams(array('scheme' => 'unix'));
- $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
- }
- /**
- * @group disconnected
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage Invalid scheme: 'udp'.
- */
- public function testThrowsExceptionOnInvalidScheme()
- {
- $this->createConnectionWithParams(array('scheme' => 'udp'));
- }
- /**
- * @group disconnected
- */
- public function testExposesParameters()
- {
- $parameters = $this->getParameters();
- $connection = $this->createConnectionWithParams($parameters);
- $this->assertSame($parameters, $connection->getParameters());
- }
- /**
- * @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(static::CONNECTION_CLASS, $unserialized);
- $this->assertEquals($parameters, $unserialized->getParameters());
- }
- // ******************************************************************** //
- // ---- INTEGRATION TESTS --------------------------------------------- //
- // ******************************************************************** //
- /**
- * @group connected
- * @requires PHP 5.4
- */
- public function testAcceptsTcpNodelayParameter()
- {
- $connection = $this->createConnectionWithParams(array('tcp_nodelay' => false));
- $connection->connect();
- $this->assertTrue($connection->isConnected());
- $connection = $this->createConnectionWithParams(array('tcp_nodelay' => true));
- $connection->connect();
- $this->assertTrue($connection->isConnected());
- }
- /**
- * @group connected
- */
- public function testConnectForcesConnection()
- {
- $connection = $this->createConnection();
- $this->assertFalse($connection->isConnected());
- $connection->connect();
- $this->assertTrue($connection->isConnected());
- }
- /**
- * @group connected
- */
- public function testDoesNotThrowExceptionOnConnectWhenAlreadyConnected()
- {
- $connection = $this->createConnection();
- $connection->connect();
- $this->assertTrue($connection->isConnected());
- $connection->connect();
- $this->assertTrue($connection->isConnected());
- }
- /**
- * @group connected
- */
- public function testDisconnectForcesDisconnection()
- {
- $connection = $this->createConnection();
- $connection->connect();
- $this->assertTrue($connection->isConnected());
- $connection->disconnect();
- $this->assertFalse($connection->isConnected());
- }
- /**
- * @group disconnected
- */
- public function testDoesNotThrowExceptionOnDisconnectWhenAlreadyDisconnected()
- {
- $connection = $this->createConnection();
- $this->assertFalse($connection->isConnected());
- $connection->disconnect();
- $this->assertFalse($connection->isConnected());
- }
- /**
- * @group connected
- */
- public function testGetResourceForcesConnection()
- {
- $connection = $this->createConnection();
- $this->assertFalse($connection->isConnected());
- $this->assertInternalType('resource', $connection->getResource());
- $this->assertTrue($connection->isConnected());
- }
- /**
- * @group connected
- */
- public function testSendingCommandForcesConnection()
- {
- $connection = $this->createConnection();
- $commands = $this->getCommandFactory();
- $cmdPing = $commands->createCommand('ping');
- $this->assertEquals('PONG', $connection->executeCommand($cmdPing));
- $this->assertTrue($connection->isConnected());
- }
- /**
- * @group connected
- */
- public function testExecutesCommandOnServer()
- {
- $commands = $this->getCommandFactory();
- $cmdPing = $this->getMock($commands->getCommandClass('ping'), array('parseResponse'));
- $cmdPing->expects($this->never())
- ->method('parseResponse');
- $connection = $this->createConnection();
- $this->assertEquals('PONG', $connection->executeCommand($cmdPing));
- }
- /**
- * @group connected
- */
- public function testExecutesCommandWithHolesInArguments()
- {
- $commands = $this->getCommandFactory();
- $cmdDel = $commands->createCommand('mget', array(0 => 'key:0', 2 => 'key:2'));
- $connection = $this->createConnection();
- $this->assertSame(array(null, null), $connection->executeCommand($cmdDel));
- }
- /**
- * @group connected
- */
- public function testExecutesMultipleCommandsOnServer()
- {
- $commands = $this->getCommandFactory();
- $cmdPing = $commands->createCommand('ping');
- $cmdEcho = $commands->createCommand('echo', array('echoed'));
- $cmdGet = $commands->createCommand('get', array('foobar'));
- $cmdRpush = $commands->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol'));
- $cmdLrange = $commands->createCommand('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));
- }
- /**
- * @group connected
- */
- public function testWritesCommandToServer()
- {
- $commands = $this->getCommandFactory();
- $cmdEcho = $this->getMock($commands->getCommandClass('echo'), array('parseResponse'));
- $cmdEcho->setArguments(array('ECHOED'));
- $cmdEcho
- ->expects($this->never())
- ->method('parseResponse');
- $connection = $this->createConnection();
- $connection->writeRequest($cmdEcho);
- $connection->disconnect();
- }
- /**
- * @group connected
- */
- public function testReadsCommandFromServer()
- {
- $commands = $this->getCommandFactory();
- $cmdEcho = $this->getMock($commands->getCommandClass('echo'), array('parseResponse'));
- $cmdEcho->setArguments(array('ECHOED'));
- $cmdEcho
- ->expects($this->never())
- ->method('parseResponse');
- $connection = $this->createConnection();
- $connection->writeRequest($cmdEcho);
- $this->assertSame('ECHOED', $connection->readResponse($cmdEcho));
- }
- /**
- * @group connected
- */
- public function testIsAbleToWriteMultipleCommandsAndReadThemBackForPipelining()
- {
- $commands = $this->getCommandFactory();
- $cmdPing = $this->getMock($commands->getCommandClass('ping'), array('parseResponse'));
- $cmdPing->expects($this->never())
- ->method('parseResponse');
- $cmdEcho = $this->getMock($commands->getCommandClass('echo'), array('parseResponse'));
- $cmdEcho->setArguments(array('ECHOED'));
- $cmdEcho->expects($this->never())
- ->method('parseResponse');
- $connection = $this->createConnection();
- $connection->writeRequest($cmdPing);
- $connection->writeRequest($cmdEcho);
- $this->assertEquals('PONG', $connection->readResponse($cmdPing));
- $this->assertSame('ECHOED', $connection->readResponse($cmdEcho));
- }
- /**
- * @group connected
- */
- public function testSendsInitializationCommandsOnConnection()
- {
- $commands = $this->getCommandFactory();
- $cmdPing = $this->getMock($commands->getCommandClass('ping'), array('getArguments'));
- $cmdPing
- ->expects($this->once())
- ->method('getArguments')
- ->will($this->returnValue(array()));
- $cmdEcho = $this->getMock($commands->getCommandClass('echo'), array('getArguments'));
- $cmdEcho
- ->expects($this->once())
- ->method('getArguments')
- ->will($this->returnValue(array('ECHOED')));
- $connection = $this->createConnection();
- $connection->addConnectCommand($cmdPing);
- $connection->addConnectCommand($cmdEcho);
- $connection->connect();
- }
- /**
- * @group connected
- */
- public function testReadsStatusResponses()
- {
- $commands = $this->getCommandFactory();
- $connection = $this->createConnection(true);
- $connection->writeRequest($commands->createCommand('set', array('foo', 'bar')));
- $this->assertInstanceOf('Predis\Response\Status', $connection->read());
- $connection->writeRequest($commands->createCommand('ping'));
- $this->assertInstanceOf('Predis\Response\Status', $connection->read());
- $connection->writeRequest($commands->createCommand('multi'));
- $connection->writeRequest($commands->createCommand('ping'));
- $this->assertInstanceOf('Predis\Response\Status', $connection->read());
- $this->assertInstanceOf('Predis\Response\Status', $connection->read());
- }
- /**
- * @group connected
- */
- public function testReadsBulkResponses()
- {
- $commands = $this->getCommandFactory();
- $connection = $this->createConnection(true);
- $connection->executeCommand($commands->createCommand('set', array('foo', 'bar')));
- $connection->writeRequest($commands->createCommand('get', array('foo')));
- $this->assertSame('bar', $connection->read());
- $connection->writeRequest($commands->createCommand('get', array('hoge')));
- $this->assertNull($connection->read());
- }
- /**
- * @group connected
- */
- public function testReadsIntegerResponses()
- {
- $commands = $this->getCommandFactory();
- $connection = $this->createConnection(true);
- $connection->executeCommand($commands->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol')));
- $connection->writeRequest($commands->createCommand('llen', array('metavars')));
- $this->assertSame(3, $connection->read());
- }
- /**
- * @group connected
- */
- public function testReadsErrorResponsesAsResponseErrorObjects()
- {
- $commands = $this->getCommandFactory();
- $connection = $this->createConnection(true);
- $connection->executeCommand($commands->createCommand('set', array('foo', 'bar')));
- $connection->writeRequest($commands->createCommand('rpush', array('foo', 'baz')));
- $this->assertInstanceOf('Predis\Response\Error', $error = $connection->read());
- $this->assertRegExp('/[ERR|WRONGTYPE] Operation against a key holding the wrong kind of value/', $error->getMessage());
- }
- /**
- * @group connected
- */
- public function testReadsMultibulkResponsesAsArrays()
- {
- $commands = $this->getCommandFactory();
- $connection = $this->createConnection(true);
- $connection->executeCommand($commands->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol')));
- $connection->writeRequest($commands->createCommand('lrange', array('metavars', 0, -1)));
- $this->assertSame(array('foo', 'hoge', 'lol'), $connection->read());
- }
- /**
- * @group connected
- * @group slow
- * @expectedException \Predis\Connection\ConnectionException
- * @expectedExceptionMessageRegExp /.* \[tcp:\/\/169.254.10.10:6379\]/
- */
- public function testThrowsExceptionOnConnectionTimeout()
- {
- // TODO: float timeouts for connect() under HHVM 3.6.6 are broken and,
- // unfortunately, this is the version still being used by Travis CI.
- if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.6.6', '<=')) {
- $timeout = 1;
- } else {
- $timeout = 0.1;
- }
- $connection = $this->createConnectionWithParams(array(
- 'host' => '169.254.10.10',
- 'timeout' => $timeout,
- ), false);
- $connection->connect();
- }
- /**
- * @group connected
- * @group slow
- * @expectedException \Predis\Connection\ConnectionException
- * @expectedExceptionMessageRegExp /.* \[tcp:\/\/\[0:0:0:0:0:ffff:a9fe:a0a\]:6379\]/
- */
- public function testThrowsExceptionOnConnectionTimeoutIPv6()
- {
- // TODO: float timeouts for connect() under HHVM 3.6.6 are broken and,
- // unfortunately, this is the version still being used by Travis CI.
- if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.6.6', '<=')) {
- $timeout = 1;
- } else {
- $timeout = 0.1;
- }
- $connection = $this->createConnectionWithParams(array(
- 'host' => '0:0:0:0:0:ffff:a9fe:a0a',
- 'timeout' => $timeout,
- ), false);
- $connection->connect();
- }
- /**
- * @group connected
- * @group slow
- * @expectedException \Predis\Connection\ConnectionException
- * @expectedExceptionMessageRegExp /.* \[unix:\/tmp\/nonexistent\/redis\.sock]/
- */
- public function testThrowsExceptionOnUnixDomainSocketNotFound()
- {
- $connection = $this->createConnectionWithParams(array(
- 'scheme' => 'unix',
- 'path' => '/tmp/nonexistent/redis.sock',
- ), false);
- $connection->connect();
- }
- /**
- * @group connected
- * @group slow
- * @expectedException \Predis\Connection\ConnectionException
- */
- public function testThrowsExceptionOnReadWriteTimeout()
- {
- $commands = $this->getCommandFactory();
- $connection = $this->createConnectionWithParams(array(
- 'read_write_timeout' => 0.5,
- ), true);
- $connection->executeCommand($commands->createCommand('brpop', array('foo', 3)));
- }
- /**
- * @medium
- * @group connected
- * @expectedException \Predis\Protocol\ProtocolException
- */
- public function testThrowsExceptionOnProtocolDesynchronizationErrors()
- {
- $connection = $this->createConnection();
- $stream = $connection->getResource();
- $connection->writeRequest($this->getCommandFactory()->createCommand('ping'));
- fread($stream, 1);
- $connection->read();
- }
- // ******************************************************************** //
- // ---- 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' => 'tcp',
- 'host' => REDIS_SERVER_HOST,
- 'port' => REDIS_SERVER_PORT,
- 'database' => REDIS_SERVER_DBNUM,
- 'read_write_timeout' => 2,
- );
- }
- /**
- * Asserts that the connection is using a persistent resource stream.
- *
- * This assertion will trigger a connect() operation if the connection has
- * not been open yet.
- *
- * @param NodeConnectionInterface $connection Connection instance.
- */
- protected function assertPersistentConnection(NodeConnectionInterface $connection)
- {
- if (version_compare(PHP_VERSION, '5.4.0') < 0 || $this->isHHVM()) {
- $this->markTestSkipped('This test does not currently work on HHVM.');
- }
- $this->assertSame('persistent stream', get_resource_type($connection->getResource()));
- }
- /**
- * Asserts that the connection is not using a persistent resource stream.
- *
- * This assertion will trigger a connect() operation if the connection has
- * not been open yet.
- *
- * @param NodeConnectionInterface $connection Connection instance.
- */
- protected function assertNonPersistentConnection(NodeConnectionInterface $connection)
- {
- if (version_compare(PHP_VERSION, '5.4.0') < 0 || $this->isHHVM()) {
- $this->markTestSkipped('This test does not currently work on HHVM.');
- }
- $this->assertSame('stream', get_resource_type($connection->getResource()));
- }
- /**
- * Creates a new connection instance.
- *
- * @param bool $initialize Push default initialization commands (SELECT and FLUSHDB).
- *
- * @return NodeConnectionInterface
- */
- protected function createConnection($initialize = false)
- {
- return $this->createConnectionWithParams(array(), $initialize);
- }
- /**
- * Creates a new connection instance using additional connection parameters.
- *
- * @param mixed $parameters Additional connection parameters.
- * @param bool $initialize Push default initialization commands (SELECT and FLUSHDB).
- *
- * @return NodeConnectionInterface
- */
- protected function createConnectionWithParams($parameters, $initialize = false)
- {
- $class = static::CONNECTION_CLASS;
- $commands = $this->getCommandFactory();
- if (!$parameters instanceof ParametersInterface) {
- $parameters = $this->getParameters($parameters);
- }
- $connection = new $class($parameters);
- if ($initialize) {
- $connection->addConnectCommand(
- $commands->createCommand('select', array($parameters->database))
- );
- $connection->addConnectCommand(
- $commands->createCommand('flushdb')
- );
- }
- return $connection;
- }
- }
|