From c7cae66a9715c4b361acb536dd8e02d366e10901 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Sat, 25 Jul 2015 20:33:00 +0200 Subject: [PATCH] [tests] Improve code-reuse in tests for the Predis\Connection namespace. --- tests/PHPUnit/PredisConnectionTestCase.php | 215 ++++++++++++++---- tests/PHPUnit/PredisTestCase.php | 14 ++ .../CompositeStreamConnectionTest.php | 122 ++-------- .../PhpiredisSocketConnectionTest.php | 88 +------ .../PhpiredisStreamConnectionTest.php | 152 +++---------- .../Connection/StreamConnectionTest.php | 133 +---------- .../Connection/WebdisConnectionTest.php | 86 ++++--- 7 files changed, 314 insertions(+), 496 deletions(-) diff --git a/tests/PHPUnit/PredisConnectionTestCase.php b/tests/PHPUnit/PredisConnectionTestCase.php index 9df8a230..63c71ddd 100644 --- a/tests/PHPUnit/PredisConnectionTestCase.php +++ b/tests/PHPUnit/PredisConnectionTestCase.php @@ -11,7 +11,6 @@ namespace Predis\Connection; -use Predis\Profile; use PredisTestCase; /** @@ -19,6 +18,84 @@ use PredisTestCase; */ 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 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()); + } + /** * @group disconnected * @group slow @@ -27,20 +104,35 @@ abstract class PredisConnectionTestCase extends PredisTestCase public function testThrowExceptionWhenUnableToConnect() { $parameters = array('host' => '169.254.10.10', 'timeout' => 0.5); - $connection = $this->getConnection($profile, false, $parameters); - $connection->executeCommand($this->getProfile()->createCommand('ping')); + $connection = $this->createConnectionWithParams($parameters, false); + $connection->executeCommand($this->getCurrentProfile()->createCommand('ping')); } // ******************************************************************** // // ---- 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->getConnection(); + $connection = $this->createConnection(); $this->assertFalse($connection->isConnected()); $connection->connect(); @@ -52,7 +144,7 @@ abstract class PredisConnectionTestCase extends PredisTestCase */ public function testDoesNotThrowExceptionOnConnectWhenAlreadyConnected() { - $connection = $this->getConnection(); + $connection = $this->createConnection(); $connection->connect(); $this->assertTrue($connection->isConnected()); @@ -66,7 +158,7 @@ abstract class PredisConnectionTestCase extends PredisTestCase */ public function testDisconnectForcesDisconnection() { - $connection = $this->getConnection(); + $connection = $this->createConnection(); $connection->connect(); $this->assertTrue($connection->isConnected()); @@ -80,7 +172,7 @@ abstract class PredisConnectionTestCase extends PredisTestCase */ public function testDoesNotThrowExceptionOnDisconnectWhenAlreadyDisconnected() { - $connection = $this->getConnection(); + $connection = $this->createConnection(); $this->assertFalse($connection->isConnected()); $connection->disconnect(); @@ -92,7 +184,7 @@ abstract class PredisConnectionTestCase extends PredisTestCase */ public function testGetResourceForcesConnection() { - $connection = $this->getConnection(); + $connection = $this->createConnection(); $this->assertFalse($connection->isConnected()); $this->assertInternalType('resource', $connection->getResource()); @@ -104,7 +196,9 @@ abstract class PredisConnectionTestCase extends PredisTestCase */ public function testSendingCommandForcesConnection() { - $connection = $this->getConnection($profile); + $connection = $this->createConnection(); + $profile = $this->getCurrentProfile(); + $cmdPing = $profile->createCommand('ping'); $this->assertEquals('PONG', $connection->executeCommand($cmdPing)); @@ -116,13 +210,14 @@ abstract class PredisConnectionTestCase extends PredisTestCase */ public function testExecutesCommandOnServer() { - $profile = $this->getProfile(); - $connection = $this->getConnection($profile); + $profile = $this->getCurrentProfile(); $cmdPing = $this->getMock($profile->getCommandClass('ping'), array('parseResponse')); $cmdPing->expects($this->never()) ->method('parseResponse'); + $connection = $this->createConnection(); + $this->assertEquals('PONG', $connection->executeCommand($cmdPing)); } @@ -131,7 +226,7 @@ abstract class PredisConnectionTestCase extends PredisTestCase */ public function testExecutesMultipleCommandsOnServer() { - $connection = $this->getConnection($profile, true); + $profile = $this->getCurrentProfile(); $cmdPing = $profile->createCommand('ping'); $cmdEcho = $profile->createCommand('echo', array('echoed')); @@ -139,6 +234,8 @@ abstract class PredisConnectionTestCase extends PredisTestCase $cmdRpush = $profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol')); $cmdLrange = $profile->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)); @@ -151,14 +248,14 @@ abstract class PredisConnectionTestCase extends PredisTestCase */ public function testWritesCommandToServer() { - $profile = $this->getProfile(); - $connection = $this->getConnection($profile); + $profile = $this->getCurrentProfile(); $cmdEcho = $this->getMock($profile->getCommandClass('echo'), array('parseResponse')); $cmdEcho->setArguments(array('ECHOED')); $cmdEcho->expects($this->never()) ->method('parseResponse'); + $connection = $this->createConnection(); $connection->writeRequest($cmdEcho); $connection->disconnect(); } @@ -168,15 +265,16 @@ abstract class PredisConnectionTestCase extends PredisTestCase */ public function testReadsCommandFromServer() { - $profile = $this->getProfile(); - $connection = $this->getConnection($profile); + $profile = $this->getCurrentProfile(); $cmdEcho = $this->getMock($profile->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)); } @@ -185,7 +283,7 @@ abstract class PredisConnectionTestCase extends PredisTestCase */ public function testIsAbleToWriteMultipleCommandsAndReadThemBackForPipelining() { - $profile = $this->getProfile(); + $profile = $this->getCurrentProfile(); $cmdPing = $this->getMock($profile->getCommandClass('ping'), array('parseResponse')); $cmdPing->expects($this->never()) @@ -196,7 +294,7 @@ abstract class PredisConnectionTestCase extends PredisTestCase $cmdEcho->expects($this->never()) ->method('parseResponse'); - $connection = $this->getConnection(); + $connection = $this->createConnection(); $connection->writeRequest($cmdPing); $connection->writeRequest($cmdEcho); @@ -210,8 +308,7 @@ abstract class PredisConnectionTestCase extends PredisTestCase */ public function testSendsInitializationCommandsOnConnection() { - $profile = $this->getProfile(); - $connection = $this->getConnection($profile, true); + $profile = $this->getCurrentProfile(); $cmdPing = $this->getMock($profile->getCommandClass('ping'), array('getArguments')); $cmdPing->expects($this->once()) @@ -223,6 +320,7 @@ abstract class PredisConnectionTestCase extends PredisTestCase ->method('getArguments') ->will($this->returnValue(array('ECHOED'))); + $connection = $this->createConnection(); $connection->addConnectCommand($cmdPing); $connection->addConnectCommand($cmdEcho); @@ -234,8 +332,8 @@ abstract class PredisConnectionTestCase extends PredisTestCase */ public function testReadsStatusResponses() { - $profile = $this->getProfile(); - $connection = $this->getConnection($profile, true); + $profile = $this->getCurrentProfile(); + $connection = $this->createConnection(true); $connection->writeRequest($profile->createCommand('set', array('foo', 'bar'))); $this->assertInstanceOf('Predis\Response\Status', $connection->read()); @@ -254,8 +352,8 @@ abstract class PredisConnectionTestCase extends PredisTestCase */ public function testReadsBulkResponses() { - $profile = $this->getProfile(); - $connection = $this->getConnection($profile, true); + $profile = $this->getCurrentProfile(); + $connection = $this->createConnection(true); $connection->executeCommand($profile->createCommand('set', array('foo', 'bar'))); @@ -271,8 +369,8 @@ abstract class PredisConnectionTestCase extends PredisTestCase */ public function testReadsIntegerResponses() { - $profile = $this->getProfile(); - $connection = $this->getConnection($profile, true); + $profile = $this->getCurrentProfile(); + $connection = $this->createConnection(true); $connection->executeCommand($profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol'))); $connection->writeRequest($profile->createCommand('llen', array('metavars'))); @@ -285,8 +383,8 @@ abstract class PredisConnectionTestCase extends PredisTestCase */ public function testReadsErrorResponsesAsResponseErrorObjects() { - $profile = $this->getProfile(); - $connection = $this->getConnection($profile, true); + $profile = $this->getCurrentProfile(); + $connection = $this->createConnection(true); $connection->executeCommand($profile->createCommand('set', array('foo', 'bar'))); $connection->writeRequest($profile->createCommand('rpush', array('foo', 'baz'))); @@ -300,8 +398,8 @@ abstract class PredisConnectionTestCase extends PredisTestCase */ public function testReadsMultibulkResponsesAsArrays() { - $profile = $this->getProfile(); - $connection = $this->getConnection($profile, true); + $profile = $this->getCurrentProfile(); + $connection = $this->createConnection(true); $connection->executeCommand($profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol'))); $connection->writeRequest($profile->createCommand('lrange', array('metavars', 0, -1))); @@ -316,7 +414,10 @@ abstract class PredisConnectionTestCase extends PredisTestCase */ public function testThrowsExceptionOnConnectionTimeout() { - $connection = $this->getConnection($_, false, array('host' => '169.254.10.10', 'timeout' => 0.5)); + $connection = $this->createConnectionWithParams(array( + 'host' => '169.254.10.10', + 'timeout' => 0.5, + ), false); $connection->connect(); } @@ -328,12 +429,31 @@ abstract class PredisConnectionTestCase extends PredisTestCase */ public function testThrowsExceptionOnReadWriteTimeout() { - $profile = $this->getProfile(); - $connection = $this->getConnection($profile, true, array('read_write_timeout' => 0.5)); + $profile = $this->getCurrentProfile(); + + $connection = $this->createConnectionWithParams(array( + 'read_write_timeout' => 0.5, + ), true); $connection->executeCommand($profile->createCommand('brpop', array('foo', 3))); } + /** + * @medium + * @group connected + * @expectedException \Predis\Protocol\ProtocolException + */ + public function testThrowsExceptionOnProtocolDesynchronizationErrors() + { + $connection = $this->createConnection(); + $stream = $connection->getResource(); + + $connection->writeRequest($this->getCurrentProfile()->createCommand('ping')); + fread($stream, 1); + + $connection->read(); + } + // ******************************************************************** // // ---- HELPER METHODS ------------------------------------------------ // // ******************************************************************** // @@ -381,20 +501,33 @@ abstract class PredisConnectionTestCase extends PredisTestCase } /** - * Returns a new instance of a connection instance. + * Creates a new connection instance. * - * @param Profile\ProfileInterface $profile Reference to the server profile instance. - * @param bool $initialize Push default initialization commands (SELECT and FLUSHDB). - * @param array $parameters Additional connection parameters. + * @param bool $initialize Push default initialization commands (SELECT and FLUSHDB). * - * @return StreamConnection + * @return NodeConnectionInterface */ - protected function getConnection(&$profile = null, $initialize = false, array $parameters = array()) + 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; + $profile = $this->getCurrentProfile(); - $parameters = $this->getParameters($parameters); - $profile = $this->getProfile(); + if (!$parameters instanceof ParametersInterface) { + $parameters = $this->getParameters($parameters); + } $connection = new $class($parameters); diff --git a/tests/PHPUnit/PredisTestCase.php b/tests/PHPUnit/PredisTestCase.php index 014c25e2..a12ce792 100644 --- a/tests/PHPUnit/PredisTestCase.php +++ b/tests/PHPUnit/PredisTestCase.php @@ -156,6 +156,20 @@ abstract class PredisTestCase extends \PHPUnit_Framework_TestCase return Profile\Factory::get($version ?: REDIS_SERVER_VERSION); } + /** + * Returns the current server profile in use by the test suite. + * + * @return Profile\ProfileInterface + */ + protected function getCurrentProfile() + { + static $profile; + + $profile = $this->getProfile(); + + return $profile; + } + /** * Returns a new client instance. * diff --git a/tests/Predis/Connection/CompositeStreamConnectionTest.php b/tests/Predis/Connection/CompositeStreamConnectionTest.php index 33c92c33..748f0bef 100644 --- a/tests/Predis/Connection/CompositeStreamConnectionTest.php +++ b/tests/Predis/Connection/CompositeStreamConnectionTest.php @@ -18,84 +18,6 @@ class CompositeStreamConnectionTest extends PredisConnectionTestCase { const CONNECTION_CLASS = 'Predis\Connection\CompositeStreamConnection'; - /** - * @group disconnected - */ - public function testConstructorDoesNotOpenConnection() - { - $connection = new CompositeStreamConnection($this->getParameters()); - - $this->assertFalse($connection->isConnected()); - } - - /** - * @group disconnected - */ - public function testSupportsSchemeTCP() - { - $parameters = $this->getParameters(array('scheme' => 'tcp')); - $connection = new StreamConnection($parameters); - - $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection); - } - - /** - * @group disconnected - */ - public function testSupportsSchemeRedis() - { - $parameters = $this->getParameters(array('scheme' => 'redis')); - $connection = new StreamConnection($parameters); - - $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection); - } - - /** - * @group disconnected - */ - public function testSupportsSchemeUnix() - { - $parameters = $this->getParameters(array('scheme' => 'unix')); - $connection = new StreamConnection($parameters); - - $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection); - } - - /** - * @group disconnected - * @expectedException \InvalidArgumentException - * @expectedExceptionMessage Invalid scheme: 'udp'. - */ - public function testThrowsExceptionOnInvalidScheme() - { - $parameters = $this->getParameters(array('scheme' => 'udp')); - new CompositeStreamConnection($parameters); - } - - /** - * @group disconnected - */ - public function testExposesParameters() - { - $parameters = $this->getParameters(); - $connection = new CompositeStreamConnection($parameters); - - $this->assertSame($parameters, $connection->getParameters()); - } - - /** - * @group disconnected - */ - public function testCanBeSerialized() - { - $parameters = $this->getParameters(array('alias' => 'redis', 'read_write_timeout' => 10)); - $connection = new CompositeStreamConnection($parameters); - - $unserialized = unserialize(serialize($connection)); - - $this->assertEquals($connection, $unserialized); - } - // ******************************************************************** // // ---- INTEGRATION TESTS --------------------------------------------- // // ******************************************************************** // @@ -105,7 +27,9 @@ class CompositeStreamConnectionTest extends PredisConnectionTestCase */ public function testReadsMultibulkResponsesAsIterators() { - $connection = $this->getConnection($profile, true); + $connection = $this->createConnection(true); + $profile = $this->getCurrentProfile(); + $connection->getProtocol()->useIterableMultibulk(true); $connection->executeCommand($profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol'))); @@ -124,16 +48,16 @@ class CompositeStreamConnectionTest extends PredisConnectionTestCase $this->markTestSkipped('This test does not currently work on HHVM.'); } - $connection1 = new CompositeStreamConnection($this->getParameters(array('persistent' => 0))); + $connection1 = $this->createConnectionWithParams(array('persistent' => 0)); $this->assertNonPersistentConnection($connection1); - $connection2 = new CompositeStreamConnection($this->getParameters(array('persistent' => false))); + $connection2 = $this->createConnectionWithParams(array('persistent' => false)); $this->assertNonPersistentConnection($connection2); - $connection3 = new CompositeStreamConnection($this->getParameters(array('persistent' => '0'))); + $connection3 = $this->createConnectionWithParams(array('persistent' => '0')); $this->assertNonPersistentConnection($connection3); - $connection4 = new CompositeStreamConnection($this->getParameters(array('persistent' => 'false'))); + $connection4 = $this->createConnectionWithParams(array('persistent' => 'false')); $this->assertNonPersistentConnection($connection4); } @@ -146,16 +70,16 @@ class CompositeStreamConnectionTest extends PredisConnectionTestCase $this->markTestSkipped('This test does not currently work on HHVM.'); } - $connection1 = new CompositeStreamConnection($this->getParameters(array('persistent' => 1))); + $connection1 = $this->createConnectionWithParams(array('persistent' => 1)); $this->assertPersistentConnection($connection1); - $connection2 = new CompositeStreamConnection($this->getParameters(array('persistent' => true))); + $connection2 = $this->createConnectionWithParams(array('persistent' => true)); $this->assertPersistentConnection($connection2); - $connection3 = new CompositeStreamConnection($this->getParameters(array('persistent' => '1'))); + $connection3 = $this->createConnectionWithParams(array('persistent' => '1')); $this->assertPersistentConnection($connection3); - $connection4 = new CompositeStreamConnection($this->getParameters(array('persistent' => 'true'))); + $connection4 = $this->createConnectionWithParams(array('persistent' => 'true')); $this->assertPersistentConnection($connection4); $connection1->disconnect(); @@ -170,8 +94,8 @@ class CompositeStreamConnectionTest extends PredisConnectionTestCase $this->markTestSkipped('This test does not currently work on HHVM.'); } - $connection1 = new CompositeStreamConnection($this->getParameters(array('persistent' => true))); - $connection2 = new CompositeStreamConnection($this->getParameters(array('persistent' => true))); + $connection1 = $this->createConnectionWithParams(array('persistent' => true)); + $connection2 = $this->createConnectionWithParams(array('persistent' => true)); $this->assertPersistentConnection($connection1); $this->assertPersistentConnection($connection2); @@ -190,28 +114,12 @@ class CompositeStreamConnectionTest extends PredisConnectionTestCase $this->markTestSkipped('This test does not currently work on HHVM.'); } - $connection1 = new CompositeStreamConnection($this->getParameters(array('persistent' => 'conn1'))); - $connection2 = new CompositeStreamConnection($this->getParameters(array('persistent' => 'conn2'))); + $connection1 = $this->createConnectionWithParams(array('persistent' => 'conn1')); + $connection2 = $this->createConnectionWithParams(array('persistent' => 'conn2')); $this->assertPersistentConnection($connection1); $this->assertPersistentConnection($connection2); $this->assertNotSame($connection1->getResource(), $connection2->getResource()); } - - /** - * @group connected - * @expectedException \Predis\Protocol\ProtocolException - * @expectedExceptionMessage Unknown response prefix: 'P'. - */ - public function testThrowsExceptionOnProtocolDesynchronizationErrors() - { - $connection = $this->getConnection($profile); - $stream = $connection->getResource(); - - $connection->writeRequest($profile->createCommand('ping')); - fread($stream, 1); - - $connection->read(); - } } diff --git a/tests/Predis/Connection/PhpiredisSocketConnectionTest.php b/tests/Predis/Connection/PhpiredisSocketConnectionTest.php index 71cd3269..3fb16d9f 100644 --- a/tests/Predis/Connection/PhpiredisSocketConnectionTest.php +++ b/tests/Predis/Connection/PhpiredisSocketConnectionTest.php @@ -19,85 +19,6 @@ class PhpiredisSocketConnectionTest extends PredisConnectionTestCase { const CONNECTION_CLASS = 'Predis\Connection\PhpiredisSocketConnection'; - /** - * @group disconnected - */ - public function testConstructorDoesNotOpenConnection() - { - $connection = new PhpiredisSocketConnection($this->getParameters()); - - $this->assertFalse($connection->isConnected()); - } - - /** - * @group disconnected - */ - public function testSupportsSchemeTCP() - { - $parameters = $this->getParameters(array('scheme' => 'tcp')); - $connection = new PhpiredisSocketConnection($parameters); - - $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection); - } - - /** - * @group disconnected - */ - public function testSupportsSchemeRedis() - { - $parameters = $this->getParameters(array('scheme' => 'redis')); - $connection = new PhpiredisSocketConnection($parameters); - - $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection); - } - - /** - * @group disconnected - */ - public function testSupportsSchemeUnix() - { - $parameters = $this->getParameters(array('scheme' => 'unix')); - $connection = new PhpiredisSocketConnection($parameters); - - $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection); - } - - /** - * @group disconnected - * @expectedException \InvalidArgumentException - * @expectedExceptionMessage Invalid scheme: 'udp'. - */ - public function testThrowsExceptionOnInvalidScheme() - { - $parameters = $this->getParameters(array('scheme' => 'udp')); - new PhpiredisSocketConnection($parameters); - } - - /** - * @group disconnected - */ - public function testExposesParameters() - { - $parameters = $this->getParameters(); - $connection = new PhpiredisSocketConnection($parameters); - - $this->assertSame($parameters, $connection->getParameters()); - } - - /** - * @group disconnected - */ - public function testCanBeSerialized() - { - $parameters = $this->getParameters(array('alias' => 'redis', 'read_write_timeout' => 10)); - $connection = new PhpiredisSocketConnection($parameters); - - $unserialized = unserialize(serialize($connection)); - - $this->assertInstanceOf('Predis\Connection\PhpiredisSocketConnection', $unserialized); - $this->assertEquals($parameters, $unserialized->getParameters()); - } - // ******************************************************************** // // ---- INTEGRATION TESTS --------------------------------------------- // // ******************************************************************** // @@ -109,22 +30,21 @@ class PhpiredisSocketConnectionTest extends PredisConnectionTestCase */ public function testThrowsExceptionOnUnresolvableHostname() { - $parameters = $this->getParameters(array('host' => 'bogus.tld')); - $connection = new PhpiredisSocketConnection($parameters); + $connection = $this->createConnectionWithParams(array('host' => 'bogus.tld')); $connection->connect(); } /** + * @medium * @group connected * @expectedException \Predis\Protocol\ProtocolException - * @expectedExceptionMessage Protocol error, got "P" as reply type byte */ public function testThrowsExceptionOnProtocolDesynchronizationErrors() { - $connection = $this->getConnection($profile); + $connection = $this->createConnection(); $socket = $connection->getResource(); - $connection->writeRequest($profile->createCommand('ping')); + $connection->writeRequest($this->getCurrentProfile()->createCommand('ping')); socket_read($socket, 1); $connection->read(); diff --git a/tests/Predis/Connection/PhpiredisStreamConnectionTest.php b/tests/Predis/Connection/PhpiredisStreamConnectionTest.php index 2a2c8629..6815a1e9 100644 --- a/tests/Predis/Connection/PhpiredisStreamConnectionTest.php +++ b/tests/Predis/Connection/PhpiredisStreamConnectionTest.php @@ -19,102 +19,41 @@ class PhpiredisStreamConnectionTest extends PredisConnectionTestCase { const CONNECTION_CLASS = 'Predis\Connection\PhpiredisStreamConnection'; - /** - * @group disconnected - */ - public function testConstructorDoesNotOpenConnection() - { - $connection = new PhpiredisStreamConnection($this->getParameters()); - - $this->assertFalse($connection->isConnected()); - } - - /** - * @group disconnected - */ - public function testSupportsSchemeTCP() - { - $parameters = $this->getParameters(array('scheme' => 'tcp')); - $connection = new PhpiredisStreamConnection($parameters); - - $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection); - } - - /** - * @group disconnected - */ - public function testSupportsSchemeRedis() - { - $parameters = $this->getParameters(array('scheme' => 'redis')); - $connection = new PhpiredisStreamConnection($parameters); - - $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection); - } - - /** - * @group disconnected - */ - public function testSupportsSchemeUnix() - { - $parameters = $this->getParameters(array('scheme' => 'unix')); - $connection = new PhpiredisStreamConnection($parameters); - - $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection); - } - - /** - * @group disconnected - * @expectedException \InvalidArgumentException - * @expectedExceptionMessage Invalid scheme: 'udp'. - */ - public function testThrowsExceptionOnInvalidScheme() - { - $parameters = $this->getParameters(array('scheme' => 'udp')); - new PhpiredisStreamConnection($parameters); - } - - /** - * @group disconnected - */ - public function testExposesParameters() - { - $parameters = $this->getParameters(); - $connection = new PhpiredisStreamConnection($parameters); - - $this->assertSame($parameters, $connection->getParameters()); - } - - /** - * @group disconnected - */ - public function testCanBeSerialized() - { - $parameters = $this->getParameters(array('alias' => 'redis', 'read_write_timeout' => 10)); - $connection = new PhpiredisStreamConnection($parameters); - - $unserialized = unserialize(serialize($connection)); - - $this->assertInstanceOf('Predis\Connection\PhpiredisStreamConnection', $unserialized); - $this->assertEquals($parameters, $unserialized->getParameters()); - } - // ******************************************************************** // // ---- INTEGRATION TESTS --------------------------------------------- // // ******************************************************************** // /** * @group connected + * @group slow * @requires PHP 5.4 + * @expectedException \Predis\Connection\ConnectionException */ - public function testAcceptsTcpNodelayParameter() + public function testThrowsExceptionOnReadWriteTimeout() { - $connection = new PhpiredisStreamConnection($this->getParameters(array('tcp_nodelay' => false))); - $connection->connect(); - $this->assertTrue($connection->isConnected()); + $profile = $this->getCurrentProfile(); - $connection = new PhpiredisStreamConnection($this->getParameters(array('tcp_nodelay' => true))); - $connection->connect(); - $this->assertTrue($connection->isConnected()); + $connection = $this->createConnectionWithParams(array( + 'read_write_timeout' => 0.5, + ), true); + + $connection->executeCommand($profile->createCommand('brpop', array('foo', 3))); + } + + /** + * @medium + * @group connected + * @expectedException \Predis\Protocol\ProtocolException + */ + public function testThrowsExceptionOnProtocolDesynchronizationErrors() + { + $connection = $this->createConnection(); + $stream = $connection->getResource(); + + $connection->writeRequest($this->getCurrentProfile()->createCommand('ping')); + stream_socket_recvfrom($stream, 1); + + $connection->read(); } /** @@ -126,16 +65,16 @@ class PhpiredisStreamConnectionTest extends PredisConnectionTestCase $this->markTestSkipped('This test does not currently work on HHVM.'); } - $connection1 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => 0))); + $connection1 = $this->createConnectionWithParams(array('persistent' => 0)); $this->assertNonPersistentConnection($connection1); - $connection2 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => false))); + $connection2 = $this->createConnectionWithParams(array('persistent' => false)); $this->assertNonPersistentConnection($connection2); - $connection3 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => '0'))); + $connection3 = $this->createConnectionWithParams(array('persistent' => '0')); $this->assertNonPersistentConnection($connection3); - $connection4 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => 'false'))); + $connection4 = $this->createConnectionWithParams(array('persistent' => 'false')); $this->assertNonPersistentConnection($connection4); } @@ -148,16 +87,16 @@ class PhpiredisStreamConnectionTest extends PredisConnectionTestCase $this->markTestSkipped('This test does not currently work on HHVM.'); } - $connection1 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => 1))); + $connection1 = $this->createConnectionWithParams(array('persistent' => 1)); $this->assertPersistentConnection($connection1); - $connection2 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => true))); + $connection2 = $this->createConnectionWithParams(array('persistent' => true)); $this->assertPersistentConnection($connection2); - $connection3 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => '1'))); + $connection3 = $this->createConnectionWithParams(array('persistent' => '1')); $this->assertPersistentConnection($connection3); - $connection4 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => 'true'))); + $connection4 = $this->createConnectionWithParams(array('persistent' => 'true')); $this->assertPersistentConnection($connection4); $connection1->disconnect(); @@ -172,8 +111,8 @@ class PhpiredisStreamConnectionTest extends PredisConnectionTestCase $this->markTestSkipped('This test does not currently work on HHVM.'); } - $connection1 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => true))); - $connection2 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => true))); + $connection1 = $this->createConnectionWithParams(array('persistent' => true)); + $connection2 = $this->createConnectionWithParams(array('persistent' => true)); $this->assertPersistentConnection($connection1); $this->assertPersistentConnection($connection2); @@ -192,29 +131,12 @@ class PhpiredisStreamConnectionTest extends PredisConnectionTestCase $this->markTestSkipped('This test does not currently work on HHVM.'); } - $connection1 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => 'conn1'))); - $connection2 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => 'conn2'))); + $connection1 = $this->createConnectionWithParams(array('persistent' => 'conn1')); + $connection2 = $this->createConnectionWithParams(array('persistent' => 'conn2')); $this->assertPersistentConnection($connection1); $this->assertPersistentConnection($connection2); $this->assertNotSame($connection1->getResource(), $connection2->getResource()); } - - /** - * @medium - * @group connected - * @expectedException \Predis\Protocol\ProtocolException - * @expectedExceptionMessage Protocol error, got "P" as reply type byte - */ - public function testThrowsExceptionOnProtocolDesynchronizationErrors() - { - $connection = $this->getConnection($profile); - $socket = $connection->getResource(); - - $connection->writeRequest($profile->createCommand('ping')); - fread($socket, 1); - - $connection->read(); - } } diff --git a/tests/Predis/Connection/StreamConnectionTest.php b/tests/Predis/Connection/StreamConnectionTest.php index 7de62615..1ac77f6f 100644 --- a/tests/Predis/Connection/StreamConnectionTest.php +++ b/tests/Predis/Connection/StreamConnectionTest.php @@ -18,103 +18,10 @@ class StreamConnectionTest extends PredisConnectionTestCase { const CONNECTION_CLASS = 'Predis\Connection\StreamConnection'; - /** - * @group disconnected - */ - public function testConstructorDoesNotOpenConnection() - { - $connection = new StreamConnection($this->getParameters()); - - $this->assertFalse($connection->isConnected()); - } - - /** - * @group disconnected - */ - public function testSupportsSchemeTCP() - { - $parameters = $this->getParameters(array('scheme' => 'tcp')); - $connection = new StreamConnection($parameters); - - $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection); - } - - /** - * @group disconnected - */ - public function testSupportsSchemeRedis() - { - $parameters = $this->getParameters(array('scheme' => 'redis')); - $connection = new StreamConnection($parameters); - - $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection); - } - - /** - * @group disconnected - */ - public function testSupportsSchemeUnix() - { - $parameters = $this->getParameters(array('scheme' => 'unix')); - $connection = new StreamConnection($parameters); - - $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection); - } - - /** - * @group disconnected - * @expectedException \InvalidArgumentException - * @expectedExceptionMessage Invalid scheme: 'udp'. - */ - public function testThrowsExceptionOnInvalidScheme() - { - $parameters = $this->getParameters(array('scheme' => 'udp')); - new StreamConnection($parameters); - } - - /** - * @group disconnected - */ - public function testExposesParameters() - { - $parameters = $this->getParameters(); - $connection = new StreamConnection($parameters); - - $this->assertSame($parameters, $connection->getParameters()); - } - - /** - * @group disconnected - */ - public function testCanBeSerialized() - { - $parameters = $this->getParameters(array('alias' => 'redis', 'read_write_timeout' => 10)); - $connection = new StreamConnection($parameters); - - $unserialized = unserialize(serialize($connection)); - - $this->assertEquals($connection, $unserialized); - } - // ******************************************************************** // // ---- INTEGRATION TESTS --------------------------------------------- // // ******************************************************************** // - /** - * @group connected - * @requires PHP 5.4 - */ - public function testAcceptsTcpNodelayParameter() - { - $connection = new StreamConnection($this->getParameters(array('tcp_nodelay' => false))); - $connection->connect(); - $this->assertTrue($connection->isConnected()); - - $connection = new StreamConnection($this->getParameters(array('tcp_nodelay' => true))); - $connection->connect(); - $this->assertTrue($connection->isConnected()); - } - /** * @group connected */ @@ -124,16 +31,16 @@ class StreamConnectionTest extends PredisConnectionTestCase $this->markTestSkipped('This test does not currently work on HHVM.'); } - $connection1 = new StreamConnection($this->getParameters(array('persistent' => 0))); + $connection1 = $this->createConnectionWithParams(array('persistent' => 0)); $this->assertNonPersistentConnection($connection1); - $connection2 = new StreamConnection($this->getParameters(array('persistent' => false))); + $connection2 = $this->createConnectionWithParams(array('persistent' => false)); $this->assertNonPersistentConnection($connection2); - $connection3 = new StreamConnection($this->getParameters(array('persistent' => '0'))); + $connection3 = $this->createConnectionWithParams(array('persistent' => '0')); $this->assertNonPersistentConnection($connection3); - $connection4 = new StreamConnection($this->getParameters(array('persistent' => 'false'))); + $connection4 = $this->createConnectionWithParams(array('persistent' => 'false')); $this->assertNonPersistentConnection($connection4); } @@ -146,16 +53,16 @@ class StreamConnectionTest extends PredisConnectionTestCase $this->markTestSkipped('This test does not currently work on HHVM.'); } - $connection1 = new StreamConnection($this->getParameters(array('persistent' => 1))); + $connection1 = $this->createConnectionWithParams(array('persistent' => 1)); $this->assertPersistentConnection($connection1); - $connection2 = new StreamConnection($this->getParameters(array('persistent' => true))); + $connection2 = $this->createConnectionWithParams(array('persistent' => true)); $this->assertPersistentConnection($connection2); - $connection3 = new StreamConnection($this->getParameters(array('persistent' => '1'))); + $connection3 = $this->createConnectionWithParams(array('persistent' => '1')); $this->assertPersistentConnection($connection3); - $connection4 = new StreamConnection($this->getParameters(array('persistent' => 'true'))); + $connection4 = $this->createConnectionWithParams(array('persistent' => 'true')); $this->assertPersistentConnection($connection4); $connection1->disconnect(); @@ -170,8 +77,8 @@ class StreamConnectionTest extends PredisConnectionTestCase $this->markTestSkipped('This test does not currently work on HHVM.'); } - $connection1 = new StreamConnection($this->getParameters(array('persistent' => true))); - $connection2 = new StreamConnection($this->getParameters(array('persistent' => true))); + $connection1 = $this->createConnectionWithParams(array('persistent' => true)); + $connection2 = $this->createConnectionWithParams(array('persistent' => true)); $this->assertPersistentConnection($connection1); $this->assertPersistentConnection($connection2); @@ -190,28 +97,12 @@ class StreamConnectionTest extends PredisConnectionTestCase $this->markTestSkipped('This test does not currently work on HHVM.'); } - $connection1 = new StreamConnection($this->getParameters(array('persistent' => 'conn1'))); - $connection2 = new StreamConnection($this->getParameters(array('persistent' => 'conn2'))); + $connection1 = $this->createConnectionWithParams(array('persistent' => 'conn1')); + $connection2 = $this->createConnectionWithParams(array('persistent' => 'conn2')); $this->assertPersistentConnection($connection1); $this->assertPersistentConnection($connection2); $this->assertNotSame($connection1->getResource(), $connection2->getResource()); } - - /** - * @group connected - * @expectedException \Predis\Protocol\ProtocolException - * @expectedExceptionMessage Unknown response prefix: 'P'. - */ - public function testThrowsExceptionOnProtocolDesynchronizationErrors() - { - $connection = $this->getConnection($profile); - $stream = $connection->getResource(); - - $connection->writeRequest($profile->createCommand('ping')); - fread($stream, 1); - - $connection->read(); - } } diff --git a/tests/Predis/Connection/WebdisConnectionTest.php b/tests/Predis/Connection/WebdisConnectionTest.php index 4bc0332f..79439316 100644 --- a/tests/Predis/Connection/WebdisConnectionTest.php +++ b/tests/Predis/Connection/WebdisConnectionTest.php @@ -23,14 +23,12 @@ use PredisTestCase; */ class WebdisConnectionTest extends PredisTestCase { - const CONNECTION_CLASS = 'Predis\Connection\WebdisConnection'; - /** * @group disconnected */ public function testIsConnectedAlwaysReturnsTrue() { - $connection = new WebdisConnection($this->getParameters()); + $connection = $this->createConnection(); $this->assertTrue($connection->isConnected()); } @@ -40,8 +38,7 @@ class WebdisConnectionTest extends PredisTestCase */ public function testSupportsSchemeUnix() { - $parameters = $this->getParameters(array('scheme' => 'http')); - $connection = new WebdisConnection($parameters); + $connection = $this->createConnectionWithParams(array('scheme' => 'http')); $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection); } @@ -53,8 +50,7 @@ class WebdisConnectionTest extends PredisTestCase */ public function testThrowsExceptionOnInvalidScheme() { - $parameters = $this->getParameters(array('scheme' => 'tcp')); - new WebdisConnection($parameters); + $connection = $this->createConnectionWithParams(array('scheme' => 'tcp')); } /** @@ -64,8 +60,8 @@ class WebdisConnectionTest extends PredisTestCase */ public function testWritingCommandsIsNotSupported() { - $connection = new WebdisConnection($this->getParameters()); - $connection->writeRequest($this->getProfile()->createCommand('ping')); + $connection = $this->createConnection(); + $connection->writeRequest($this->getCurrentProfile()->createCommand('ping')); } /** @@ -75,8 +71,8 @@ class WebdisConnectionTest extends PredisTestCase */ public function testReadingResponsesIsNotSupported() { - $connection = new WebdisConnection($this->getParameters()); - $connection->readResponse($this->getProfile()->createCommand('ping')); + $connection = $this->createConnection(); + $connection->readResponse($this->getCurrentProfile()->createCommand('ping')); } /** @@ -86,7 +82,7 @@ class WebdisConnectionTest extends PredisTestCase */ public function testReadingFromConnectionIsNotSupported() { - $connection = new WebdisConnection($this->getParameters()); + $connection = $this->createConnection(); $connection->read(); } @@ -97,8 +93,8 @@ class WebdisConnectionTest extends PredisTestCase */ public function testAddingConnectCommandsIsNotSupported() { - $connection = new WebdisConnection($this->getParameters()); - $connection->addConnectCommand($this->getProfile()->createCommand('ping')); + $connection = $this->createConnection(); + $connection->addConnectCommand($this->getCurrentProfile()->createCommand('ping')); } /** @@ -108,8 +104,8 @@ class WebdisConnectionTest extends PredisTestCase */ public function testRejectCommandSelect() { - $connection = new WebdisConnection($this->getParameters()); - $connection->executeCommand($this->getProfile()->createCommand('select', array(0))); + $connection = $this->createConnection(); + $connection->executeCommand($this->getCurrentProfile()->createCommand('select', array(0))); } /** @@ -119,8 +115,8 @@ class WebdisConnectionTest extends PredisTestCase */ public function testRejectCommandAuth() { - $connection = new WebdisConnection($this->getParameters()); - $connection->executeCommand($this->getProfile()->createCommand('auth', array('foobar'))); + $connection = $this->createConnection(); + $connection->executeCommand($this->getCurrentProfile()->createCommand('auth', array('foobar'))); } /** @@ -128,8 +124,12 @@ class WebdisConnectionTest extends PredisTestCase */ public function testCanBeSerialized() { - $parameters = $this->getParameters(array('alias' => 'webdis')); - $connection = new WebdisConnection($parameters); + $parameters = $this->getParameters(array( + 'alias' => 'redis', + 'read_write_timeout' => 10, + )); + + $connection = $this->createConnectionWithParams($parameters); $unserialized = unserialize(serialize($connection)); @@ -141,6 +141,28 @@ class WebdisConnectionTest extends PredisTestCase // ---- INTEGRATION TESTS --------------------------------------------- // // ******************************************************************** // + /** + * @group connected + */ + public function testExecutesMultipleCommandsOnServer() + { + $profile = $this->getCurrentProfile(); + + $cmdPing = $profile->createCommand('ping'); + $cmdEcho = $profile->createCommand('echo', array('echoed')); + $cmdGet = $profile->createCommand('get', array('foobar')); + $cmdRpush = $profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol')); + $cmdLrange = $profile->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)); + } + /** * @medium * @group disconnected @@ -149,9 +171,8 @@ class WebdisConnectionTest extends PredisTestCase */ public function testThrowExceptionWhenUnableToConnect() { - $parameters = $this->getParameters(array('host' => '169.254.10.10')); - $connection = new WebdisConnection($parameters); - $connection->executeCommand($this->getProfile()->createCommand('ping')); + $connection = $this->createConnectionWithParams(array('host' => '169.254.10.10')); + $connection->executeCommand($this->getCurrentProfile()->createCommand('ping')); } // ******************************************************************** // @@ -175,14 +196,23 @@ class WebdisConnectionTest extends PredisTestCase /** * {@inheritdoc} */ - protected function getConnection(&$profile = null, array $parameters = array()) + protected function createConnection() { - $class = static::CONNECTION_CLASS; + return $this->createConnectionWithParams(array()); + } - $parameters = $this->getParameters($parameters); - $profile = $this->getProfile(); + /** + * {@inheritdoc} + */ + protected function createConnectionWithParams($parameters) + { + $profile = $this->getCurrentProfile(); - $connection = new $class($parameters); + if (!$parameters instanceof ParametersInterface) { + $parameters = $this->getParameters($parameters); + } + + $connection = new WebdisConnection($parameters); $connection->executeCommand($profile->createCommand('flushdb')); return $connection;