From ea9f83d4c047f7d8f9832984a2a338cd56e4f0af Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Wed, 10 Mar 2010 14:04:43 +0100 Subject: [PATCH] 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);