PredisClientFeatures.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. /* Connection */
  181. function testConnection_StringCastReturnsIPAndPort() {
  182. $connection = new \Predis\Connection(RC::getConnectionParameters());
  183. $this->assertEquals(RC::SERVER_HOST . ':' . RC::SERVER_PORT, (string) $connection);
  184. }
  185. function testConnection_ConnectDisconnect() {
  186. $connection = new \Predis\Connection(RC::getConnectionParameters());
  187. $this->assertFalse($connection->isConnected());
  188. $connection->connect();
  189. $this->assertTrue($connection->isConnected());
  190. $connection->disconnect();
  191. $this->assertFalse($connection->isConnected());
  192. }
  193. function testConnection_WriteAndReadCommand() {
  194. $cmd = \Predis\RedisServerProfile::getDefault()->createCommand('ping');
  195. $connection = new \Predis\Connection(RC::getConnectionParameters());
  196. $connection->connect();
  197. $connection->writeCommand($cmd);
  198. $this->assertTrue($connection->readResponse($cmd));
  199. }
  200. function testConnection_WriteCommandAndCloseConnection() {
  201. $cmd = \Predis\RedisServerProfile::getDefault()->createCommand('quit');
  202. $connection = new \Predis\Connection(RC::getConnectionParameters());
  203. $connection->connect();
  204. $this->assertTrue($connection->isConnected());
  205. $connection->writeCommand($cmd);
  206. $exceptionMessage = 'An error has occurred while reading from the network stream';
  207. RC::testForClientException($this, $exceptionMessage, function($test) use($connection, $cmd) {
  208. $connection->readResponse($cmd);
  209. });
  210. //$this->assertFalse($connection->isConnected());
  211. }
  212. function testConnection_GetSocketOpensConnection() {
  213. $connection = new \Predis\Connection(RC::getConnectionParameters());
  214. $this->assertFalse($connection->isConnected());
  215. $this->assertType('resource', $connection->getSocket());
  216. $this->assertTrue($connection->isConnected());
  217. }
  218. function testConnection_LazyConnect() {
  219. $cmd = \Predis\RedisServerProfile::getDefault()->createCommand('ping');
  220. $connection = new \Predis\Connection(RC::getConnectionParameters());
  221. $this->assertFalse($connection->isConnected());
  222. $connection->writeCommand($cmd);
  223. $this->assertTrue($connection->isConnected());
  224. $this->assertTrue($connection->readResponse($cmd));
  225. }
  226. function testConnection_RawCommand() {
  227. $connection = new \Predis\Connection(RC::getConnectionParameters());
  228. $this->assertEquals('PONG', $connection->rawCommand("PING\r\n"));
  229. }
  230. function testConnection_Alias() {
  231. $connection1 = new \Predis\Connection(RC::getConnectionParameters());
  232. $this->assertNull($connection1->getAlias());
  233. $args = array_merge(RC::getConnectionArguments(), array('alias' => 'servername'));
  234. $connection2 = new \Predis\Connection(new \Predis\ConnectionParameters($args));
  235. $this->assertEquals('servername', $connection2->getAlias());
  236. }
  237. function testConnection_ConnectionTimeout() {
  238. $timeout = 3;
  239. $args = array('host' => '1.0.0.1', 'connection_timeout' => $timeout);
  240. $connection = new \Predis\Connection(new \Predis\ConnectionParameters($args));
  241. $start = time();
  242. RC::testForClientException($this, null, function($test) use($connection) {
  243. $connection->connect();
  244. });
  245. $this->assertEquals((float)(time() - $start), $timeout, '', 1);
  246. }
  247. function testConnection_ReadTimeout() {
  248. $timeout = 1;
  249. $args = array_merge(RC::getConnectionArguments(), array('read_write_timeout' => $timeout));
  250. $cmdFake = \Predis\RedisServerProfile::getDefault()->createCommand('ping');
  251. $connection = new \Predis\Connection(new \Predis\ConnectionParameters($args));
  252. $start = time();
  253. RC::testForClientException($this, null, function($test) use($connection, $cmdFake) {
  254. $connection->readResponse($cmdFake);
  255. });
  256. $this->assertEquals((float)(time() - $start), $timeout, '', 1);
  257. }
  258. }
  259. ?>