From f02b5494c7794708cb191a03756587535cb7402e Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Wed, 10 Mar 2010 11:47:41 +0100 Subject: [PATCH 01/12] New set of tests for client-specific features. --- test/PredisClientFeatures.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 test/PredisClientFeatures.php diff --git a/test/PredisClientFeatures.php b/test/PredisClientFeatures.php new file mode 100644 index 00000000..4c254649 --- /dev/null +++ b/test/PredisClientFeatures.php @@ -0,0 +1,24 @@ +redis = RC::getConnection(); + $this->redis->flushDatabase(); + } + + protected function tearDown() { + } + + protected function onNotSuccessfulTest($exception) { + // drops and reconnect to a redis server on uncaught exceptions + RC::resetConnection(); + parent::onNotSuccessfulTest($exception); + } +} +?> \ No newline at end of file From 95664719a66097bb9b0ba24ceb765e852f18fae9 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Wed, 10 Mar 2010 12:04:17 +0100 Subject: [PATCH 02/12] Tests for Predis\ConnectionParameters. --- test/PredisClientFeatures.php | 42 +++++++++++++++++++++++++++++++++++ test/PredisShared.php | 16 +++++++++++++ 2 files changed, 58 insertions(+) diff --git a/test/PredisClientFeatures.php b/test/PredisClientFeatures.php index 4c254649..96c1f020 100644 --- a/test/PredisClientFeatures.php +++ b/test/PredisClientFeatures.php @@ -20,5 +20,47 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { RC::resetConnection(); parent::onNotSuccessfulTest($exception); } + + + /* ConnectionParameters */ + + function testConnectionParametersDefaultValues() { + $params = new Predis\ConnectionParameters(); + + $this->assertEquals(Predis\ConnectionParameters::DEFAULT_HOST, $params->host); + $this->assertEquals(Predis\ConnectionParameters::DEFAULT_PORT, $params->port); + $this->assertEquals(Predis\ConnectionParameters::DEFAULT_TIMEOUT, $params->connection_timeout); + $this->assertNull($params->read_write_timeout); + $this->assertNull($params->database); + $this->assertNull($params->password); + $this->assertNull($params->alias); + } + + function testConnectionParametersSetupValuesArray() { + $paramsArray = RC::getConnectionParametersArgumentsArray(); + $params = new Predis\ConnectionParameters($paramsArray); + + $this->assertEquals($paramsArray['host'], $params->host); + $this->assertEquals($paramsArray['port'], $params->port); + $this->assertEquals($paramsArray['connection_timeout'], $params->connection_timeout); + $this->assertEquals($paramsArray['read_write_timeout'], $params->read_write_timeout); + $this->assertEquals($paramsArray['database'], $params->database); + $this->assertEquals($paramsArray['password'], $params->password); + $this->assertEquals($paramsArray['alias'], $params->alias); + } + + function testConnectionParametersSetupValuesString() { + $paramsArray = RC::getConnectionParametersArgumentsArray(); + $paramsString = RC::getConnectionParametersArgumentsString($paramsArray); + $params = new Predis\ConnectionParameters($paramsArray); + + $this->assertEquals($paramsArray['host'], $params->host); + $this->assertEquals($paramsArray['port'], $params->port); + $this->assertEquals($paramsArray['connection_timeout'], $params->connection_timeout); + $this->assertEquals($paramsArray['read_write_timeout'], $params->read_write_timeout); + $this->assertEquals($paramsArray['database'], $params->database); + $this->assertEquals($paramsArray['password'], $params->password); + $this->assertEquals($paramsArray['alias'], $params->alias); + } } ?> \ No newline at end of file diff --git a/test/PredisShared.php b/test/PredisShared.php index 2eb2bbe9..621d1f6c 100644 --- a/test/PredisShared.php +++ b/test/PredisShared.php @@ -138,5 +138,21 @@ class RC { } return $values; } + + public static function getConnectionParametersArgumentsArray() { + return array( + 'host' => '10.0.0.1', 'port' => 6380, 'connection_timeout' => 10, 'read_write_timeout' => 30, + 'database' => 5, 'password' => 'dbpassword', 'alias' => 'connection_alias' + ); + } + + public static function getConnectionParametersArgumentsString($arguments = null) { + // TODO: must be improved + $args = $arguments ?: RC::getConnectionParametersArgumentsArray(); + $paramsString = "redis://{$args['host']}:{$args['port']}/"; + $paramsString .= "?connection_timeout={$args['connection_timeout']}&read_write_timeout={$args['read_write_timeout']}"; + $paramsString .= "&database={$args['database']}&password={$args['password']}&alias={$args['alias']}"; + return $paramsString; + } } ?> From ea9f83d4c047f7d8f9832984a2a338cd56e4f0af Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Wed, 10 Mar 2010 14:04:43 +0100 Subject: [PATCH 03/12] Tests for Predis\RedisServerProfile. --- test/PredisClientFeatures.php | 59 +++++++++++++++++++++++++++++++++++ test/PredisShared.php | 12 +++++++ 2 files changed, 71 insertions(+) diff --git a/test/PredisClientFeatures.php b/test/PredisClientFeatures.php index 96c1f020..c2c181e2 100644 --- a/test/PredisClientFeatures.php +++ b/test/PredisClientFeatures.php @@ -62,5 +62,64 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { $this->assertEquals($paramsArray['password'], $params->password); $this->assertEquals($paramsArray['alias'], $params->alias); } + + + /* RedisServerProfile and derivates */ + + function testRedisServerProfile_GetSpecificVersions() { + $this->assertType('\Predis\RedisServer_v1_0', \Predis\RedisServerProfile::get('1.0')); + $this->assertType('\Predis\RedisServer_v1_2', \Predis\RedisServerProfile::get('1.2')); + $this->assertType('\Predis\RedisServer_vNext', \Predis\RedisServerProfile::get('dev')); + $this->assertType('\Predis\RedisServerProfile', \Predis\RedisServerProfile::get('default')); + $this->assertEquals(\Predis\RedisServerProfile::get('default'), \Predis\RedisServerProfile::getDefault()); + } + + function testRedisServerProfile_SupportedCommands() { + $profile_10 = \Predis\RedisServerProfile::get('1.0'); + $profile_12 = \Predis\RedisServerProfile::get('1.2'); + + $this->assertTrue($profile_10->supportsCommand('info')); + $this->assertTrue($profile_12->supportsCommand('info')); + + $this->assertFalse($profile_10->supportsCommand('mset')); + $this->assertTrue($profile_12->supportsCommand('mset')); + + $this->assertFalse($profile_10->supportsCommand('multi')); + $this->assertFalse($profile_12->supportsCommand('multi')); + } + + function testRedisServerProfile_CommandsCreation() { + $profile = \Predis\RedisServerProfile::get('1.0'); + + $cmdNoArgs = $profile->createCommand('info'); + $this->assertType('\Predis\Commands\Info', $cmdNoArgs); + $this->assertNull($cmdNoArgs->getArgument()); + + $args = array('key1', 'key2'); + $cmdWithArgs = $profile->createCommand('mget', $args); + $this->assertType('\Predis\Commands\GetMultiple', $cmdWithArgs); + $this->assertEquals($args[0], $cmdWithArgs->getArgument()); // TODO: why? + $this->assertEquals($args[0], $cmdWithArgs->getArgument(0)); + $this->assertEquals($args[1], $cmdWithArgs->getArgument(1)); + + $bogusCommand = 'not_existing_command'; + $expectedMessage = "'$bogusCommand' is not a registered Redis command"; + RC::testForClientException($this, $expectedMessage, function($test) + use($profile, $bogusCommand) { + + $profile->createCommand($bogusCommand); + }); + } + + function testRedisServerProfile_CommandsRegistration() { + $profile = \Predis\RedisServerProfile::get('1.0'); + $cmdId = 'mset'; + $cmdClass = '\Predis\Commands\SetMultiple'; + + $this->assertFalse($profile->supportsCommand($cmdId)); + $profile->registerCommand(new $cmdClass(), $cmdId); + $this->assertTrue($profile->supportsCommand($cmdId)); + $this->assertType($cmdClass, $profile->createCommand($cmdId)); + } } ?> \ No newline at end of file diff --git a/test/PredisShared.php b/test/PredisShared.php index 621d1f6c..caa783d6 100644 --- a/test/PredisShared.php +++ b/test/PredisShared.php @@ -108,6 +108,18 @@ class RC { $testcaseInstance->assertEquals($expectedMessage, $thrownException->getMessage()); } + public static function testForClientException($testcaseInstance, $expectedMessage, $wrapFunction) { + $thrownException = null; + try { + $wrapFunction($testcaseInstance); + } + catch (Predis\ClientException $exception) { + $thrownException = $exception; + } + $testcaseInstance->assertType('Predis\ClientException', $thrownException); + $testcaseInstance->assertEquals($expectedMessage, $thrownException->getMessage()); + } + public static function pushTailAndReturn(Predis\Client $client, $keyName, Array $values, $wipeOut = 0) { if ($wipeOut == true) { $client->delete($keyName); From 83ab54e9fc100dbbf32e0ea5ab6c497cd3a218a0 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Wed, 10 Mar 2010 14:18:15 +0100 Subject: [PATCH 04/12] Tests for Predis\ResponseQueued. --- test/PredisClientFeatures.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/PredisClientFeatures.php b/test/PredisClientFeatures.php index c2c181e2..a5de66bc 100644 --- a/test/PredisClientFeatures.php +++ b/test/PredisClientFeatures.php @@ -121,5 +121,14 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { $this->assertTrue($profile->supportsCommand($cmdId)); $this->assertType($cmdClass, $profile->createCommand($cmdId)); } + + + /* ResponseQueued */ + + function testResponseQueued() { + $response = new \Predis\ResponseQueued(); + $this->assertTrue($response->queued); + $this->assertEquals(\Predis\ResponseReader::QUEUED, (string)$response); + } } ?> \ No newline at end of file From 90078b95c7895b6e4479f05cfadffbd1473e1949 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Wed, 10 Mar 2010 14:18:28 +0100 Subject: [PATCH 05/12] Tests for Predis\ResponseError. --- test/PredisClientFeatures.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/PredisClientFeatures.php b/test/PredisClientFeatures.php index a5de66bc..8f572790 100644 --- a/test/PredisClientFeatures.php +++ b/test/PredisClientFeatures.php @@ -130,5 +130,17 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { $this->assertTrue($response->queued); $this->assertEquals(\Predis\ResponseReader::QUEUED, (string)$response); } + + + /* ResponseError */ + + function testResponseError() { + $errorMessage = 'ERROR MESSAGE'; + $response = new \Predis\ResponseError($errorMessage); + + $this->assertTrue($response->error); + $this->assertEquals($errorMessage, $response->message); + $this->assertEquals($errorMessage, (string)$response); + } } ?> \ No newline at end of file From 5b039c8ee840a6332d451a1233e3071bbce9de4c Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Wed, 10 Mar 2010 14:59:34 +0100 Subject: [PATCH 06/12] Tests for Predis\Command. --- test/PredisClientFeatures.php | 87 +++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/test/PredisClientFeatures.php b/test/PredisClientFeatures.php index 8f572790..42f79c5d 100644 --- a/test/PredisClientFeatures.php +++ b/test/PredisClientFeatures.php @@ -64,6 +64,93 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { } + /* Command and derivates */ + + function testCommand_TestArguments() { + $cmdArgs = array('key1', 'key2', 'key3'); + + $cmd = new \Predis\Commands\GetMultiple(); + $cmd->setArgumentsArray($cmdArgs); + $this->assertEquals($cmdArgs[0], $cmd->getArgument(0)); + $this->assertEquals($cmdArgs[1], $cmd->getArgument(1)); + $this->assertEquals($cmdArgs[2], $cmd->getArgument(2)); + + $cmd = new \Predis\Commands\GetMultiple(); + $cmd->setArguments('key1', 'key2', 'key3'); + $this->assertEquals($cmdArgs[0], $cmd->getArgument(0)); + $this->assertEquals($cmdArgs[1], $cmd->getArgument(1)); + $this->assertEquals($cmdArgs[2], $cmd->getArgument(2)); + + $cmd = new \Predis\Commands\Ping(); + $this->assertNull($cmd->getArgument(0)); + } + + function testCommand_InlineWithNoArguments() { + $cmd = new \Predis\Commands\Ping(); + + $this->assertType('\Predis\InlineCommand', $cmd); + $this->assertEquals('PING', $cmd->getCommandId()); + $this->assertFalse($cmd->closesConnection()); + $this->assertFalse($cmd->canBeHashed()); + $this->assertNull($cmd->getHash()); + $this->assertEquals("PING\r\n", $cmd()); + } + + function testCommand_InlineWithArguments() { + $cmd = new \Predis\Commands\Get(); + $cmd->setArgumentsArray(array('key')); + + $this->assertType('\Predis\InlineCommand', $cmd); + $this->assertEquals('GET', $cmd->getCommandId()); + $this->assertFalse($cmd->closesConnection()); + $this->assertTrue($cmd->canBeHashed()); + $this->assertNotNull($cmd->getHash()); + $this->assertEquals("GET key\r\n", $cmd()); + } + + function testCommand_BulkWithArguments() { + $cmd = new \Predis\Commands\Set(); + $cmd->setArgumentsArray(array('key', 'value')); + + $this->assertType('\Predis\BulkCommand', $cmd); + $this->assertEquals('SET', $cmd->getCommandId()); + $this->assertFalse($cmd->closesConnection()); + $this->assertTrue($cmd->canBeHashed()); + $this->assertNotNull($cmd->getHash()); + $this->assertEquals("SET key 5\r\nvalue\r\n", $cmd()); + } + + function testCommand_MultiBulkWithArguments() { + $cmd = new \Predis\Commands\SetMultiple(); + $cmd->setArgumentsArray(array('key1', 'value1', 'key2', 'value2')); + + $this->assertType('\Predis\MultiBulkCommand', $cmd); + $this->assertEquals('MSET', $cmd->getCommandId()); + $this->assertFalse($cmd->closesConnection()); + $this->assertFalse($cmd->canBeHashed()); + $this->assertNull($cmd->getHash()); + $this->assertEquals("*5\r\n$4\r\nMSET\r\n$4\r\nkey1\r\n$6\r\nvalue1\r\n$4\r\nkey2\r\n$6\r\nvalue2\r\n", $cmd()); + } + + function testCommand_ParseResponse() { + // default parser + $cmd = new \Predis\Commands\Get(); + $this->assertEquals('test', $cmd->parseResponse('test')); + + // overridden parser (boolean) + $cmd = new \Predis\Commands\Exists(); + $this->assertTrue($cmd->parseResponse('1')); + $this->assertFalse($cmd->parseResponse('0')); + + // overridden parser (boolean) + $cmd = new \Predis\Commands\Ping(); + $this->assertTrue($cmd->parseResponse('PONG')); + + // overridden parser (complex) + // TODO: emulate a respons to INFO + } + + /* RedisServerProfile and derivates */ function testRedisServerProfile_GetSpecificVersions() { From 1394df5625fab91a8e42a05a44af9a25f37ae359 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Wed, 10 Mar 2010 16:24:09 +0100 Subject: [PATCH 07/12] Tests for Predis\Connection. --- test/PredisClientFeatures.php | 98 +++++++++++++++++++++++++++++++++++ test/PredisShared.php | 18 +++++-- 2 files changed, 113 insertions(+), 3 deletions(-) diff --git a/test/PredisClientFeatures.php b/test/PredisClientFeatures.php index 42f79c5d..246c06b3 100644 --- a/test/PredisClientFeatures.php +++ b/test/PredisClientFeatures.php @@ -229,5 +229,103 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { $this->assertEquals($errorMessage, $response->message); $this->assertEquals($errorMessage, (string)$response); } + + + /* Connection */ + + function testConnection_StringCastReturnsIPAndPort() { + $connection = new \Predis\Connection(RC::getConnectionParameters()); + $this->assertEquals(RC::SERVER_HOST . ':' . RC::SERVER_PORT, (string) $connection); + } + + function testConnection_ConnectDisconnect() { + $connection = new \Predis\Connection(RC::getConnectionParameters()); + + $this->assertFalse($connection->isConnected()); + $connection->connect(); + $this->assertTrue($connection->isConnected()); + $connection->disconnect(); + $this->assertFalse($connection->isConnected()); + } + + function testConnection_WriteAndReadCommand() { + $cmd = \Predis\RedisServerProfile::getDefault()->createCommand('ping'); + $connection = new \Predis\Connection(RC::getConnectionParameters()); + $connection->connect(); + + $connection->writeCommand($cmd); + $this->assertTrue($connection->readResponse($cmd)); + } + + function testConnection_WriteCommandAndCloseConnection() { + $cmd = \Predis\RedisServerProfile::getDefault()->createCommand('quit'); + $connection = new \Predis\Connection(RC::getConnectionParameters()); + $connection->connect(); + + $this->assertTrue($connection->isConnected()); + $connection->writeCommand($cmd); + $exceptionMessage = 'An error has occurred while reading from the network stream'; + RC::testForClientException($this, $exceptionMessage, function($test) use($connection, $cmd) { + $connection->readResponse($cmd); + }); + //$this->assertFalse($connection->isConnected()); + } + + function testConnection_GetSocketOpensConnection() { + $connection = new \Predis\Connection(RC::getConnectionParameters()); + + $this->assertFalse($connection->isConnected()); + $this->assertType('resource', $connection->getSocket()); + $this->assertTrue($connection->isConnected()); + } + + function testConnection_LazyConnect() { + $cmd = \Predis\RedisServerProfile::getDefault()->createCommand('ping'); + $connection = new \Predis\Connection(RC::getConnectionParameters()); + + $this->assertFalse($connection->isConnected()); + $connection->writeCommand($cmd); + $this->assertTrue($connection->isConnected()); + $this->assertTrue($connection->readResponse($cmd)); + } + + function testConnection_RawCommand() { + $connection = new \Predis\Connection(RC::getConnectionParameters()); + $this->assertEquals('PONG', $connection->rawCommand("PING\r\n")); + } + + function testConnection_Alias() { + $connection1 = new \Predis\Connection(RC::getConnectionParameters()); + $this->assertNull($connection1->getAlias()); + + $args = array_merge(RC::getConnectionArguments(), array('alias' => 'servername')); + $connection2 = new \Predis\Connection(new \Predis\ConnectionParameters($args)); + $this->assertEquals('servername', $connection2->getAlias()); + } + + function testConnection_ConnectionTimeout() { + $timeout = 3; + $args = array('host' => '1.0.0.1', 'connection_timeout' => $timeout); + $connection = new \Predis\Connection(new \Predis\ConnectionParameters($args)); + + $start = time(); + RC::testForClientException($this, null, function($test) use($connection) { + $connection->connect(); + }); + $this->assertEquals((float)(time() - $start), $timeout, '', 1); + } + + function testConnection_ReadTimeout() { + $timeout = 1; + $args = array_merge(RC::getConnectionArguments(), array('read_write_timeout' => $timeout)); + $cmdFake = \Predis\RedisServerProfile::getDefault()->createCommand('ping'); + $connection = new \Predis\Connection(new \Predis\ConnectionParameters($args)); + + $start = time(); + RC::testForClientException($this, null, function($test) use($connection, $cmdFake) { + $connection->readResponse($cmdFake); + }); + $this->assertEquals((float)(time() - $start), $timeout, '', 1); + } } ?> \ No newline at end of file diff --git a/test/PredisShared.php b/test/PredisShared.php index caa783d6..3617c026 100644 --- a/test/PredisShared.php +++ b/test/PredisShared.php @@ -25,9 +25,17 @@ class RC { private static $_connection; + public static function getConnectionArguments() { + return array('host' => RC::SERVER_HOST, 'port' => RC::SERVER_PORT); + } + + public static function getConnectionParameters() { + return new Predis\ConnectionParameters(array('host' => RC::SERVER_HOST, 'port' => RC::SERVER_PORT)); + } + private static function createConnection() { $serverProfile = Predis\RedisServerProfile::get('dev'); - $connection = new Predis\Client(array('host' => RC::SERVER_HOST, 'port' => RC::SERVER_PORT), $serverProfile); + $connection = new Predis\Client(RC::getConnectionArguments(), $serverProfile); $connection->connect(); $connection->selectDatabase(RC::DEFAULT_DATABASE); return $connection; @@ -105,7 +113,9 @@ class RC { $thrownException = $exception; } $testcaseInstance->assertType('Predis\ServerException', $thrownException); - $testcaseInstance->assertEquals($expectedMessage, $thrownException->getMessage()); + if (isset($expectedMessage)) { + $testcaseInstance->assertEquals($expectedMessage, $thrownException->getMessage()); + } } public static function testForClientException($testcaseInstance, $expectedMessage, $wrapFunction) { @@ -117,7 +127,9 @@ class RC { $thrownException = $exception; } $testcaseInstance->assertType('Predis\ClientException', $thrownException); - $testcaseInstance->assertEquals($expectedMessage, $thrownException->getMessage()); + if (isset($expectedMessage)) { + $testcaseInstance->assertEquals($expectedMessage, $thrownException->getMessage()); + } } public static function pushTailAndReturn(Predis\Client $client, $keyName, Array $values, $wipeOut = 0) { From e9e2b8ca5ac35650374126e073219cd150129412 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Wed, 10 Mar 2010 17:46:35 +0100 Subject: [PATCH 08/12] Tests for Predis\ResponseReader. --- test/PredisClientFeatures.php | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/test/PredisClientFeatures.php b/test/PredisClientFeatures.php index 246c06b3..4dca2669 100644 --- a/test/PredisClientFeatures.php +++ b/test/PredisClientFeatures.php @@ -327,5 +327,42 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { }); $this->assertEquals((float)(time() - $start), $timeout, '', 1); } + + + /* ResponseReader */ + + function testResponseReader_OptionIterableMultiBulkReplies() { + $connection = new \Predis\Connection(RC::getConnectionParameters()); + $responseReader = $connection->getResponseReader(); + + $responseReader->setOption('iterable_multibulk_replies', false); + $this->assertFalse($responseReader->getOption('iterable_multibulk_replies')); + $this->assertType('array', $connection->rawCommand("KEYS *\r\n")); + + $responseReader->setOption('iterable_multibulk_replies', true); + $this->assertTrue($responseReader->getOption('iterable_multibulk_replies')); + $this->assertType('\Iterator', $connection->rawCommand("KEYS *\r\n")); + } + + function testResponseReader_OptionExceptionOnError() { + $connection = new \Predis\Connection(RC::getConnectionParameters()); + $responseReader = $connection->getResponseReader(); + $connection->rawCommand("SET key 5\r\nvalue\r\n"); + $rawCmdUnexpected = "LPUSH key 5\r\nvalue\r\n"; + + $responseReader->setOption('error_throw_exception', false); + $this->assertFalse($responseReader->getOption('error_throw_exception')); + $errorReply = $connection->rawCommand($rawCmdUnexpected); + $this->assertType('\Predis\ResponseError', $errorReply); + $this->assertEquals(RC::EXCEPTION_WRONG_TYPE, $errorReply->message); + + $responseReader->setOption('error_throw_exception', true); + $this->assertTrue($responseReader->getOption('error_throw_exception')); + RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function() + use ($connection, $rawCmdUnexpected) { + + $connection->rawCommand($rawCmdUnexpected); + }); + } } ?> \ No newline at end of file From 4dc9affda2f8d4a1224f2e8694f2aaaefccffff1 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Wed, 10 Mar 2010 17:48:28 +0100 Subject: [PATCH 09/12] Minor adjustments in a few tests. --- test/PredisClientFeatures.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/PredisClientFeatures.php b/test/PredisClientFeatures.php index 4dca2669..bbc1c686 100644 --- a/test/PredisClientFeatures.php +++ b/test/PredisClientFeatures.php @@ -25,11 +25,11 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { /* ConnectionParameters */ function testConnectionParametersDefaultValues() { - $params = new Predis\ConnectionParameters(); + $params = new \Predis\ConnectionParameters(); - $this->assertEquals(Predis\ConnectionParameters::DEFAULT_HOST, $params->host); - $this->assertEquals(Predis\ConnectionParameters::DEFAULT_PORT, $params->port); - $this->assertEquals(Predis\ConnectionParameters::DEFAULT_TIMEOUT, $params->connection_timeout); + $this->assertEquals(\Predis\ConnectionParameters::DEFAULT_HOST, $params->host); + $this->assertEquals(\Predis\ConnectionParameters::DEFAULT_PORT, $params->port); + $this->assertEquals(\Predis\ConnectionParameters::DEFAULT_TIMEOUT, $params->connection_timeout); $this->assertNull($params->read_write_timeout); $this->assertNull($params->database); $this->assertNull($params->password); @@ -38,7 +38,7 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { function testConnectionParametersSetupValuesArray() { $paramsArray = RC::getConnectionParametersArgumentsArray(); - $params = new Predis\ConnectionParameters($paramsArray); + $params = new \Predis\ConnectionParameters($paramsArray); $this->assertEquals($paramsArray['host'], $params->host); $this->assertEquals($paramsArray['port'], $params->port); @@ -52,7 +52,7 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { function testConnectionParametersSetupValuesString() { $paramsArray = RC::getConnectionParametersArgumentsArray(); $paramsString = RC::getConnectionParametersArgumentsString($paramsArray); - $params = new Predis\ConnectionParameters($paramsArray); + $params = new \Predis\ConnectionParameters($paramsArray); $this->assertEquals($paramsArray['host'], $params->host); $this->assertEquals($paramsArray['port'], $params->port); @@ -191,7 +191,7 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { $bogusCommand = 'not_existing_command'; $expectedMessage = "'$bogusCommand' is not a registered Redis command"; - RC::testForClientException($this, $expectedMessage, function($test) + RC::testForClientException($this, $expectedMessage, function() use($profile, $bogusCommand) { $profile->createCommand($bogusCommand); @@ -265,7 +265,7 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { $this->assertTrue($connection->isConnected()); $connection->writeCommand($cmd); $exceptionMessage = 'An error has occurred while reading from the network stream'; - RC::testForClientException($this, $exceptionMessage, function($test) use($connection, $cmd) { + RC::testForClientException($this, $exceptionMessage, function() use($connection, $cmd) { $connection->readResponse($cmd); }); //$this->assertFalse($connection->isConnected()); @@ -309,7 +309,7 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { $connection = new \Predis\Connection(new \Predis\ConnectionParameters($args)); $start = time(); - RC::testForClientException($this, null, function($test) use($connection) { + RC::testForClientException($this, null, function() use($connection) { $connection->connect(); }); $this->assertEquals((float)(time() - $start), $timeout, '', 1); @@ -322,7 +322,7 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { $connection = new \Predis\Connection(new \Predis\ConnectionParameters($args)); $start = time(); - RC::testForClientException($this, null, function($test) use($connection, $cmdFake) { + RC::testForClientException($this, null, function() use($connection, $cmdFake) { $connection->readResponse($cmdFake); }); $this->assertEquals((float)(time() - $start), $timeout, '', 1); From ff103b385d8e9a31c757843fedc2a1f6664ccff4 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Wed, 10 Mar 2010 18:46:45 +0100 Subject: [PATCH 10/12] Tests for Predis\CommandPipeline. --- test/PredisClientFeatures.php | 95 +++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/test/PredisClientFeatures.php b/test/PredisClientFeatures.php index bbc1c686..06936cbf 100644 --- a/test/PredisClientFeatures.php +++ b/test/PredisClientFeatures.php @@ -364,5 +364,100 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { $connection->rawCommand($rawCmdUnexpected); }); } + + + /* Client + CommandPipeline */ + + function testCommandPipeline_Simple() { + $client = RC::getConnection(); + $client->flushdb(); + + $pipe = $client->pipeline(); + + $this->assertType('\Predis\CommandPipeline', $pipe); + $this->assertType('\Predis\CommandPipeline', $pipe->set('foo', 'bar')); + $this->assertType('\Predis\CommandPipeline', $pipe->set('hoge', 'piyo')); + $this->assertType('\Predis\CommandPipeline', $pipe->mset(array( + 'foofoo' => 'barbar', 'hogehoge' => 'piyopiyo' + ))); + $this->assertType('\Predis\CommandPipeline', $pipe->mget(array( + 'foo', 'hoge', 'foofoo', 'hogehoge' + ))); + + $replies = $pipe->execute(); + $this->assertType('array', $replies); + $this->assertEquals(4, count($replies)); + $this->assertEquals(4, count($replies[3])); + $this->assertEquals('barbar', $replies[3][2]); + } + + function testCommandPipeline_FluentInterface() { + $client = RC::getConnection(); + $client->flushdb(); + + $replies = $client->pipeline()->ping()->set('foo', 'bar')->get('foo')->execute(); + $this->assertType('array', $replies); + $this->assertEquals('bar', $replies[2]); + } + + function testCommandPipeline_CallableAnonymousBlock() { + $client = RC::getConnection(); + $client->flushdb(); + + $replies = $client->pipeline(function($pipe) { + $pipe->ping(); + $pipe->set('foo', 'bar'); + $pipe->get('foo'); + }); + + $this->assertType('array', $replies); + $this->assertEquals('bar', $replies[2]); + } + + function testCommandPipeline_ClientExceptionInCallableBlock() { + $client = RC::getConnection(); + $client->flushdb(); + + RC::testForClientException($this, 'TEST', function() use($client) { + $client->pipeline(function($pipe) { + $pipe->ping(); + $pipe->set('foo', 'bar'); + throw new \Predis\ClientException("TEST"); + }); + }); + $this->assertFalse($client->exists('foo')); + } + + function testCommandPipeline_ServerExceptionInCallableBlock() { + $client = RC::getConnection(); + $client->flushdb(); + + $replies = $client->pipeline(function($pipe) { + $pipe->set('foo', 'bar'); + $pipe->lpush('foo', 'piyo'); // LIST operation on STRING type returns an ERROR + $pipe->set('hoge', 'piyo'); + }); + + $this->assertType('array', $replies); + $this->assertType('\Predis\ResponseError', $replies[1]); + $this->assertTrue($client->exists('foo')); + $this->assertTrue($client->exists('hoge')); + } + + function testCommandPipeline_Flush() { + $client = RC::getConnection(); + $client->flushdb(); + + $pipe = $client->pipeline(); + $pipe->set('foo', 'bar')->set('hoge', 'piyo'); + $pipe->flushPipeline(); + $pipe->ping()->mget(array('foo', 'hoge')); + $replies = $pipe->execute(); + + $this->assertType('array', $replies); + $this->assertEquals(4, count($replies)); + $this->assertEquals('bar', $replies[3][0]); + $this->assertEquals('piyo', $replies[3][1]); + } } ?> \ No newline at end of file From 471102253a55eefbe1c580c21047e035bb9fa66f Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Sat, 17 Apr 2010 17:04:06 +0200 Subject: [PATCH 11/12] Updated the tests to reflect the changes that has been made in the internal design of the library in the latest weeks. --- test/PredisClientFeatures.php | 48 +++++++++++++++++++++-------------- test/PredisShared.php | 14 ++++++++++ 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/test/PredisClientFeatures.php b/test/PredisClientFeatures.php index 06936cbf..b7154592 100644 --- a/test/PredisClientFeatures.php +++ b/test/PredisClientFeatures.php @@ -92,7 +92,7 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { $this->assertEquals('PING', $cmd->getCommandId()); $this->assertFalse($cmd->closesConnection()); $this->assertFalse($cmd->canBeHashed()); - $this->assertNull($cmd->getHash()); + $this->assertNull($cmd->getHash(new \Predis\Utilities\HashRing())); $this->assertEquals("PING\r\n", $cmd()); } @@ -104,7 +104,7 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { $this->assertEquals('GET', $cmd->getCommandId()); $this->assertFalse($cmd->closesConnection()); $this->assertTrue($cmd->canBeHashed()); - $this->assertNotNull($cmd->getHash()); + $this->assertNotNull($cmd->getHash(new \Predis\Utilities\HashRing())); $this->assertEquals("GET key\r\n", $cmd()); } @@ -116,7 +116,7 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { $this->assertEquals('SET', $cmd->getCommandId()); $this->assertFalse($cmd->closesConnection()); $this->assertTrue($cmd->canBeHashed()); - $this->assertNotNull($cmd->getHash()); + $this->assertNotNull($cmd->getHash(new \Predis\Utilities\HashRing())); $this->assertEquals("SET key 5\r\nvalue\r\n", $cmd()); } @@ -128,7 +128,7 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { $this->assertEquals('MSET', $cmd->getCommandId()); $this->assertFalse($cmd->closesConnection()); $this->assertFalse($cmd->canBeHashed()); - $this->assertNull($cmd->getHash()); + $this->assertNull($cmd->getHash(new \Predis\Utilities\HashRing())); $this->assertEquals("*5\r\n$4\r\nMSET\r\n$4\r\nkey1\r\n$6\r\nvalue1\r\n$4\r\nkey2\r\n$6\r\nvalue2\r\n", $cmd()); } @@ -264,8 +264,8 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { $this->assertTrue($connection->isConnected()); $connection->writeCommand($cmd); - $exceptionMessage = 'An error has occurred while reading from the network stream'; - RC::testForClientException($this, $exceptionMessage, function() use($connection, $cmd) { + $exceptionMessage = 'Error while reading line from the server'; + RC::testForCommunicationException($this, $exceptionMessage, function() use($connection, $cmd) { $connection->readResponse($cmd); }); //$this->assertFalse($connection->isConnected()); @@ -296,11 +296,11 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { function testConnection_Alias() { $connection1 = new \Predis\Connection(RC::getConnectionParameters()); - $this->assertNull($connection1->getAlias()); + $this->assertNull($connection1->getParameters()->alias); $args = array_merge(RC::getConnectionArguments(), array('alias' => 'servername')); $connection2 = new \Predis\Connection(new \Predis\ConnectionParameters($args)); - $this->assertEquals('servername', $connection2->getAlias()); + $this->assertEquals('servername', $connection2->getParameters()->alias); } function testConnection_ConnectionTimeout() { @@ -309,7 +309,7 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { $connection = new \Predis\Connection(new \Predis\ConnectionParameters($args)); $start = time(); - RC::testForClientException($this, null, function() use($connection) { + RC::testForCommunicationException($this, null, function() use($connection) { $connection->connect(); }); $this->assertEquals((float)(time() - $start), $timeout, '', 1); @@ -321,8 +321,9 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { $cmdFake = \Predis\RedisServerProfile::getDefault()->createCommand('ping'); $connection = new \Predis\Connection(new \Predis\ConnectionParameters($args)); + $expectedMessage = 'Error while reading line from the server'; $start = time(); - RC::testForClientException($this, null, function() use($connection, $cmdFake) { + RC::testForCommunicationException($this, $expectedMessage, function() use($connection, $cmdFake) { $connection->readResponse($cmdFake); }); $this->assertEquals((float)(time() - $start), $timeout, '', 1); @@ -335,12 +336,16 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { $connection = new \Predis\Connection(RC::getConnectionParameters()); $responseReader = $connection->getResponseReader(); - $responseReader->setOption('iterable_multibulk_replies', false); - $this->assertFalse($responseReader->getOption('iterable_multibulk_replies')); + $responseReader->setHandler( + \Predis\ResponseReader::PREFIX_MULTI_BULK, + new \Predis\ResponseMultiBulkHandler() + ); $this->assertType('array', $connection->rawCommand("KEYS *\r\n")); - $responseReader->setOption('iterable_multibulk_replies', true); - $this->assertTrue($responseReader->getOption('iterable_multibulk_replies')); + $responseReader->setHandler( + \Predis\ResponseReader::PREFIX_MULTI_BULK, + new \Predis\ResponseMultiBulkStreamHandler() + ); $this->assertType('\Iterator', $connection->rawCommand("KEYS *\r\n")); } @@ -350,14 +355,18 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { $connection->rawCommand("SET key 5\r\nvalue\r\n"); $rawCmdUnexpected = "LPUSH key 5\r\nvalue\r\n"; - $responseReader->setOption('error_throw_exception', false); - $this->assertFalse($responseReader->getOption('error_throw_exception')); + $responseReader->setHandler( + \Predis\ResponseReader::PREFIX_ERROR, + new \Predis\ResponseErrorSilentHandler() + ); $errorReply = $connection->rawCommand($rawCmdUnexpected); $this->assertType('\Predis\ResponseError', $errorReply); $this->assertEquals(RC::EXCEPTION_WRONG_TYPE, $errorReply->message); - $responseReader->setOption('error_throw_exception', true); - $this->assertTrue($responseReader->getOption('error_throw_exception')); + $responseReader->setHandler( + \Predis\ResponseReader::PREFIX_ERROR, + new \Predis\ResponseErrorHandler() + ); RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function() use ($connection, $rawCmdUnexpected) { @@ -431,6 +440,7 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { function testCommandPipeline_ServerExceptionInCallableBlock() { $client = RC::getConnection(); $client->flushdb(); + $client->getResponseReader()->setHandler('-', new \Predis\ResponseErrorSilentHandler()); $replies = $client->pipeline(function($pipe) { $pipe->set('foo', 'bar'); @@ -460,4 +470,4 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { $this->assertEquals('piyo', $replies[3][1]); } } -?> \ No newline at end of file +?> diff --git a/test/PredisShared.php b/test/PredisShared.php index 3617c026..9cc916d8 100644 --- a/test/PredisShared.php +++ b/test/PredisShared.php @@ -132,6 +132,20 @@ class RC { } } + public static function testForCommunicationException($testcaseInstance, $expectedMessage, $wrapFunction) { + $thrownException = null; + try { + $wrapFunction($testcaseInstance); + } + catch (Predis\CommunicationException $exception) { + $thrownException = $exception; + } + $testcaseInstance->assertType('Predis\CommunicationException', $thrownException); + if (isset($expectedMessage)) { + $testcaseInstance->assertEquals($expectedMessage, $thrownException->getMessage()); + } + } + public static function pushTailAndReturn(Predis\Client $client, $keyName, Array $values, $wipeOut = 0) { if ($wipeOut == true) { $client->delete($keyName); From 1a9454a4f250697726b95cfd0257ee9ad2d84391 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Thu, 13 May 2010 18:26:56 +0200 Subject: [PATCH 12/12] Updated the tests to reflect the changes that has been made in the internal design of the library in the latest weeks (preparing to merge into master). --- test/PredisClientFeatures.php | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/test/PredisClientFeatures.php b/test/PredisClientFeatures.php index b7154592..0c9fb151 100644 --- a/test/PredisClientFeatures.php +++ b/test/PredisClientFeatures.php @@ -3,6 +3,7 @@ define('I_AM_AWARE_OF_THE_DESTRUCTIVE_POWER_OF_THIS_TEST_SUITE', false); require_once 'PHPUnit/Framework.php'; require_once 'PredisShared.php'; +require_once '../lib/Predis_Compatibility.php'; class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { public $redis; @@ -86,7 +87,7 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { } function testCommand_InlineWithNoArguments() { - $cmd = new \Predis\Commands\Ping(); + $cmd = new \Predis\Compatibility\v1_0\Commands\Ping(); $this->assertType('\Predis\InlineCommand', $cmd); $this->assertEquals('PING', $cmd->getCommandId()); @@ -97,7 +98,7 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { } function testCommand_InlineWithArguments() { - $cmd = new \Predis\Commands\Get(); + $cmd = new \Predis\Compatibility\v1_0\Commands\Get(); $cmd->setArgumentsArray(array('key')); $this->assertType('\Predis\InlineCommand', $cmd); @@ -109,7 +110,7 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { } function testCommand_BulkWithArguments() { - $cmd = new \Predis\Commands\Set(); + $cmd = new \Predis\Compatibility\v1_0\Commands\Set(); $cmd->setArgumentsArray(array('key', 'value')); $this->assertType('\Predis\BulkCommand', $cmd); @@ -179,12 +180,12 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { $profile = \Predis\RedisServerProfile::get('1.0'); $cmdNoArgs = $profile->createCommand('info'); - $this->assertType('\Predis\Commands\Info', $cmdNoArgs); + $this->assertType('\Predis\Compatibility\v1_0\Commands\Info', $cmdNoArgs); $this->assertNull($cmdNoArgs->getArgument()); $args = array('key1', 'key2'); $cmdWithArgs = $profile->createCommand('mget', $args); - $this->assertType('\Predis\Commands\GetMultiple', $cmdWithArgs); + $this->assertType('\Predis\Compatibility\v1_0\Commands\GetMultiple', $cmdWithArgs); $this->assertEquals($args[0], $cmdWithArgs->getArgument()); // TODO: why? $this->assertEquals($args[0], $cmdWithArgs->getArgument(0)); $this->assertEquals($args[1], $cmdWithArgs->getArgument(1)); @@ -215,7 +216,7 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { function testResponseQueued() { $response = new \Predis\ResponseQueued(); $this->assertTrue($response->queued); - $this->assertEquals(\Predis\ResponseReader::QUEUED, (string)$response); + $this->assertEquals(\Predis\Protocol::QUEUED, (string)$response); } @@ -337,13 +338,13 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { $responseReader = $connection->getResponseReader(); $responseReader->setHandler( - \Predis\ResponseReader::PREFIX_MULTI_BULK, + \Predis\Protocol::PREFIX_MULTI_BULK, new \Predis\ResponseMultiBulkHandler() ); $this->assertType('array', $connection->rawCommand("KEYS *\r\n")); $responseReader->setHandler( - \Predis\ResponseReader::PREFIX_MULTI_BULK, + \Predis\Protocol::PREFIX_MULTI_BULK, new \Predis\ResponseMultiBulkStreamHandler() ); $this->assertType('\Iterator', $connection->rawCommand("KEYS *\r\n")); @@ -356,7 +357,7 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { $rawCmdUnexpected = "LPUSH key 5\r\nvalue\r\n"; $responseReader->setHandler( - \Predis\ResponseReader::PREFIX_ERROR, + \Predis\Protocol::PREFIX_ERROR, new \Predis\ResponseErrorSilentHandler() ); $errorReply = $connection->rawCommand($rawCmdUnexpected); @@ -364,7 +365,7 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { $this->assertEquals(RC::EXCEPTION_WRONG_TYPE, $errorReply->message); $responseReader->setHandler( - \Predis\ResponseReader::PREFIX_ERROR, + \Predis\Protocol::PREFIX_ERROR, new \Predis\ResponseErrorHandler() ); RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function()