PredisClientFeatures.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. /* Command and derivates */
  53. function testCommand_TestArguments() {
  54. $cmdArgs = array('key1', 'key2', 'key3');
  55. $cmd = new \Predis\Commands\GetMultiple();
  56. $cmd->setArgumentsArray($cmdArgs);
  57. $this->assertEquals($cmdArgs[0], $cmd->getArgument(0));
  58. $this->assertEquals($cmdArgs[1], $cmd->getArgument(1));
  59. $this->assertEquals($cmdArgs[2], $cmd->getArgument(2));
  60. $cmd = new \Predis\Commands\GetMultiple();
  61. $cmd->setArguments('key1', 'key2', 'key3');
  62. $this->assertEquals($cmdArgs[0], $cmd->getArgument(0));
  63. $this->assertEquals($cmdArgs[1], $cmd->getArgument(1));
  64. $this->assertEquals($cmdArgs[2], $cmd->getArgument(2));
  65. $cmd = new \Predis\Commands\Ping();
  66. $this->assertNull($cmd->getArgument(0));
  67. }
  68. function testCommand_InlineWithNoArguments() {
  69. $cmd = new \Predis\Commands\Ping();
  70. $this->assertType('\Predis\InlineCommand', $cmd);
  71. $this->assertEquals('PING', $cmd->getCommandId());
  72. $this->assertFalse($cmd->closesConnection());
  73. $this->assertFalse($cmd->canBeHashed());
  74. $this->assertNull($cmd->getHash());
  75. $this->assertEquals("PING\r\n", $cmd());
  76. }
  77. function testCommand_InlineWithArguments() {
  78. $cmd = new \Predis\Commands\Get();
  79. $cmd->setArgumentsArray(array('key'));
  80. $this->assertType('\Predis\InlineCommand', $cmd);
  81. $this->assertEquals('GET', $cmd->getCommandId());
  82. $this->assertFalse($cmd->closesConnection());
  83. $this->assertTrue($cmd->canBeHashed());
  84. $this->assertNotNull($cmd->getHash());
  85. $this->assertEquals("GET key\r\n", $cmd());
  86. }
  87. function testCommand_BulkWithArguments() {
  88. $cmd = new \Predis\Commands\Set();
  89. $cmd->setArgumentsArray(array('key', 'value'));
  90. $this->assertType('\Predis\BulkCommand', $cmd);
  91. $this->assertEquals('SET', $cmd->getCommandId());
  92. $this->assertFalse($cmd->closesConnection());
  93. $this->assertTrue($cmd->canBeHashed());
  94. $this->assertNotNull($cmd->getHash());
  95. $this->assertEquals("SET key 5\r\nvalue\r\n", $cmd());
  96. }
  97. function testCommand_MultiBulkWithArguments() {
  98. $cmd = new \Predis\Commands\SetMultiple();
  99. $cmd->setArgumentsArray(array('key1', 'value1', 'key2', 'value2'));
  100. $this->assertType('\Predis\MultiBulkCommand', $cmd);
  101. $this->assertEquals('MSET', $cmd->getCommandId());
  102. $this->assertFalse($cmd->closesConnection());
  103. $this->assertFalse($cmd->canBeHashed());
  104. $this->assertNull($cmd->getHash());
  105. $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());
  106. }
  107. function testCommand_ParseResponse() {
  108. // default parser
  109. $cmd = new \Predis\Commands\Get();
  110. $this->assertEquals('test', $cmd->parseResponse('test'));
  111. // overridden parser (boolean)
  112. $cmd = new \Predis\Commands\Exists();
  113. $this->assertTrue($cmd->parseResponse('1'));
  114. $this->assertFalse($cmd->parseResponse('0'));
  115. // overridden parser (boolean)
  116. $cmd = new \Predis\Commands\Ping();
  117. $this->assertTrue($cmd->parseResponse('PONG'));
  118. // overridden parser (complex)
  119. // TODO: emulate a respons to INFO
  120. }
  121. /* RedisServerProfile and derivates */
  122. function testRedisServerProfile_GetSpecificVersions() {
  123. $this->assertType('\Predis\RedisServer_v1_0', \Predis\RedisServerProfile::get('1.0'));
  124. $this->assertType('\Predis\RedisServer_v1_2', \Predis\RedisServerProfile::get('1.2'));
  125. $this->assertType('\Predis\RedisServer_vNext', \Predis\RedisServerProfile::get('dev'));
  126. $this->assertType('\Predis\RedisServerProfile', \Predis\RedisServerProfile::get('default'));
  127. $this->assertEquals(\Predis\RedisServerProfile::get('default'), \Predis\RedisServerProfile::getDefault());
  128. }
  129. function testRedisServerProfile_SupportedCommands() {
  130. $profile_10 = \Predis\RedisServerProfile::get('1.0');
  131. $profile_12 = \Predis\RedisServerProfile::get('1.2');
  132. $this->assertTrue($profile_10->supportsCommand('info'));
  133. $this->assertTrue($profile_12->supportsCommand('info'));
  134. $this->assertFalse($profile_10->supportsCommand('mset'));
  135. $this->assertTrue($profile_12->supportsCommand('mset'));
  136. $this->assertFalse($profile_10->supportsCommand('multi'));
  137. $this->assertFalse($profile_12->supportsCommand('multi'));
  138. }
  139. function testRedisServerProfile_CommandsCreation() {
  140. $profile = \Predis\RedisServerProfile::get('1.0');
  141. $cmdNoArgs = $profile->createCommand('info');
  142. $this->assertType('\Predis\Commands\Info', $cmdNoArgs);
  143. $this->assertNull($cmdNoArgs->getArgument());
  144. $args = array('key1', 'key2');
  145. $cmdWithArgs = $profile->createCommand('mget', $args);
  146. $this->assertType('\Predis\Commands\GetMultiple', $cmdWithArgs);
  147. $this->assertEquals($args[0], $cmdWithArgs->getArgument()); // TODO: why?
  148. $this->assertEquals($args[0], $cmdWithArgs->getArgument(0));
  149. $this->assertEquals($args[1], $cmdWithArgs->getArgument(1));
  150. $bogusCommand = 'not_existing_command';
  151. $expectedMessage = "'$bogusCommand' is not a registered Redis command";
  152. RC::testForClientException($this, $expectedMessage, function($test)
  153. use($profile, $bogusCommand) {
  154. $profile->createCommand($bogusCommand);
  155. });
  156. }
  157. function testRedisServerProfile_CommandsRegistration() {
  158. $profile = \Predis\RedisServerProfile::get('1.0');
  159. $cmdId = 'mset';
  160. $cmdClass = '\Predis\Commands\SetMultiple';
  161. $this->assertFalse($profile->supportsCommand($cmdId));
  162. $profile->registerCommand(new $cmdClass(), $cmdId);
  163. $this->assertTrue($profile->supportsCommand($cmdId));
  164. $this->assertType($cmdClass, $profile->createCommand($cmdId));
  165. }
  166. /* ResponseQueued */
  167. function testResponseQueued() {
  168. $response = new \Predis\ResponseQueued();
  169. $this->assertTrue($response->queued);
  170. $this->assertEquals(\Predis\ResponseReader::QUEUED, (string)$response);
  171. }
  172. /* ResponseError */
  173. function testResponseError() {
  174. $errorMessage = 'ERROR MESSAGE';
  175. $response = new \Predis\ResponseError($errorMessage);
  176. $this->assertTrue($response->error);
  177. $this->assertEquals($errorMessage, $response->message);
  178. $this->assertEquals($errorMessage, (string)$response);
  179. }
  180. }
  181. ?>