PredisClientFeatures.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. define('I_AM_AWARE_OF_THE_DESTRUCTIVE_POWER_OF_THIS_TEST_SUITE', false);
  3. require_once 'PHPUnit/Framework.php';
  4. require_once 'PredisShared.php';
  5. class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
  6. public $redis;
  7. protected function setUp() {
  8. $this->redis = RC::getConnection();
  9. $this->redis->flushDatabase();
  10. }
  11. protected function tearDown() {
  12. }
  13. protected function onNotSuccessfulTest($exception) {
  14. // drops and reconnect to a redis server on uncaught exceptions
  15. RC::resetConnection();
  16. parent::onNotSuccessfulTest($exception);
  17. }
  18. /* ConnectionParameters */
  19. function testConnectionParametersDefaultValues() {
  20. $params = new Predis\ConnectionParameters();
  21. $this->assertEquals(Predis\ConnectionParameters::DEFAULT_HOST, $params->host);
  22. $this->assertEquals(Predis\ConnectionParameters::DEFAULT_PORT, $params->port);
  23. $this->assertEquals(Predis\ConnectionParameters::DEFAULT_TIMEOUT, $params->connection_timeout);
  24. $this->assertNull($params->read_write_timeout);
  25. $this->assertNull($params->database);
  26. $this->assertNull($params->password);
  27. $this->assertNull($params->alias);
  28. }
  29. function testConnectionParametersSetupValuesArray() {
  30. $paramsArray = RC::getConnectionParametersArgumentsArray();
  31. $params = new Predis\ConnectionParameters($paramsArray);
  32. $this->assertEquals($paramsArray['host'], $params->host);
  33. $this->assertEquals($paramsArray['port'], $params->port);
  34. $this->assertEquals($paramsArray['connection_timeout'], $params->connection_timeout);
  35. $this->assertEquals($paramsArray['read_write_timeout'], $params->read_write_timeout);
  36. $this->assertEquals($paramsArray['database'], $params->database);
  37. $this->assertEquals($paramsArray['password'], $params->password);
  38. $this->assertEquals($paramsArray['alias'], $params->alias);
  39. }
  40. function testConnectionParametersSetupValuesString() {
  41. $paramsArray = RC::getConnectionParametersArgumentsArray();
  42. $paramsString = RC::getConnectionParametersArgumentsString($paramsArray);
  43. $params = new Predis\ConnectionParameters($paramsArray);
  44. $this->assertEquals($paramsArray['host'], $params->host);
  45. $this->assertEquals($paramsArray['port'], $params->port);
  46. $this->assertEquals($paramsArray['connection_timeout'], $params->connection_timeout);
  47. $this->assertEquals($paramsArray['read_write_timeout'], $params->read_write_timeout);
  48. $this->assertEquals($paramsArray['database'], $params->database);
  49. $this->assertEquals($paramsArray['password'], $params->password);
  50. $this->assertEquals($paramsArray['alias'], $params->alias);
  51. }
  52. /* RedisServerProfile and derivates */
  53. function testRedisServerProfile_GetSpecificVersions() {
  54. $this->assertType('\Predis\RedisServer_v1_0', \Predis\RedisServerProfile::get('1.0'));
  55. $this->assertType('\Predis\RedisServer_v1_2', \Predis\RedisServerProfile::get('1.2'));
  56. $this->assertType('\Predis\RedisServer_vNext', \Predis\RedisServerProfile::get('dev'));
  57. $this->assertType('\Predis\RedisServerProfile', \Predis\RedisServerProfile::get('default'));
  58. $this->assertEquals(\Predis\RedisServerProfile::get('default'), \Predis\RedisServerProfile::getDefault());
  59. }
  60. function testRedisServerProfile_SupportedCommands() {
  61. $profile_10 = \Predis\RedisServerProfile::get('1.0');
  62. $profile_12 = \Predis\RedisServerProfile::get('1.2');
  63. $this->assertTrue($profile_10->supportsCommand('info'));
  64. $this->assertTrue($profile_12->supportsCommand('info'));
  65. $this->assertFalse($profile_10->supportsCommand('mset'));
  66. $this->assertTrue($profile_12->supportsCommand('mset'));
  67. $this->assertFalse($profile_10->supportsCommand('multi'));
  68. $this->assertFalse($profile_12->supportsCommand('multi'));
  69. }
  70. function testRedisServerProfile_CommandsCreation() {
  71. $profile = \Predis\RedisServerProfile::get('1.0');
  72. $cmdNoArgs = $profile->createCommand('info');
  73. $this->assertType('\Predis\Commands\Info', $cmdNoArgs);
  74. $this->assertNull($cmdNoArgs->getArgument());
  75. $args = array('key1', 'key2');
  76. $cmdWithArgs = $profile->createCommand('mget', $args);
  77. $this->assertType('\Predis\Commands\GetMultiple', $cmdWithArgs);
  78. $this->assertEquals($args[0], $cmdWithArgs->getArgument()); // TODO: why?
  79. $this->assertEquals($args[0], $cmdWithArgs->getArgument(0));
  80. $this->assertEquals($args[1], $cmdWithArgs->getArgument(1));
  81. $bogusCommand = 'not_existing_command';
  82. $expectedMessage = "'$bogusCommand' is not a registered Redis command";
  83. RC::testForClientException($this, $expectedMessage, function($test)
  84. use($profile, $bogusCommand) {
  85. $profile->createCommand($bogusCommand);
  86. });
  87. }
  88. function testRedisServerProfile_CommandsRegistration() {
  89. $profile = \Predis\RedisServerProfile::get('1.0');
  90. $cmdId = 'mset';
  91. $cmdClass = '\Predis\Commands\SetMultiple';
  92. $this->assertFalse($profile->supportsCommand($cmdId));
  93. $profile->registerCommand(new $cmdClass(), $cmdId);
  94. $this->assertTrue($profile->supportsCommand($cmdId));
  95. $this->assertType($cmdClass, $profile->createCommand($cmdId));
  96. }
  97. }
  98. ?>