PredisClientFeatures.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. require_once '../lib/Predis_Compatibility.php';
  6. class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
  7. public $redis;
  8. protected function setUp() {
  9. $this->redis = RC::getConnection();
  10. $this->redis->flushDatabase();
  11. }
  12. protected function tearDown() {
  13. }
  14. protected function onNotSuccessfulTest($exception) {
  15. // drops and reconnect to a redis server on uncaught exceptions
  16. RC::resetConnection();
  17. parent::onNotSuccessfulTest($exception);
  18. }
  19. /* ConnectionParameters */
  20. function testConnectionParametersDefaultValues() {
  21. $params = new \Predis\ConnectionParameters();
  22. $this->assertEquals(\Predis\ConnectionParameters::DEFAULT_HOST, $params->host);
  23. $this->assertEquals(\Predis\ConnectionParameters::DEFAULT_PORT, $params->port);
  24. $this->assertEquals(\Predis\ConnectionParameters::DEFAULT_TIMEOUT, $params->connection_timeout);
  25. $this->assertNull($params->read_write_timeout);
  26. $this->assertNull($params->database);
  27. $this->assertNull($params->password);
  28. $this->assertNull($params->alias);
  29. }
  30. function testConnectionParametersSetupValuesArray() {
  31. $paramsArray = RC::getConnectionParametersArgumentsArray();
  32. $params = new \Predis\ConnectionParameters($paramsArray);
  33. $this->assertEquals($paramsArray['host'], $params->host);
  34. $this->assertEquals($paramsArray['port'], $params->port);
  35. $this->assertEquals($paramsArray['connection_timeout'], $params->connection_timeout);
  36. $this->assertEquals($paramsArray['read_write_timeout'], $params->read_write_timeout);
  37. $this->assertEquals($paramsArray['database'], $params->database);
  38. $this->assertEquals($paramsArray['password'], $params->password);
  39. $this->assertEquals($paramsArray['alias'], $params->alias);
  40. }
  41. function testConnectionParametersSetupValuesString() {
  42. $paramsArray = RC::getConnectionParametersArgumentsArray();
  43. $paramsString = RC::getConnectionParametersArgumentsString($paramsArray);
  44. $params = new \Predis\ConnectionParameters($paramsArray);
  45. $this->assertEquals($paramsArray['host'], $params->host);
  46. $this->assertEquals($paramsArray['port'], $params->port);
  47. $this->assertEquals($paramsArray['connection_timeout'], $params->connection_timeout);
  48. $this->assertEquals($paramsArray['read_write_timeout'], $params->read_write_timeout);
  49. $this->assertEquals($paramsArray['database'], $params->database);
  50. $this->assertEquals($paramsArray['password'], $params->password);
  51. $this->assertEquals($paramsArray['alias'], $params->alias);
  52. }
  53. /* Command and derivates */
  54. function testCommand_TestArguments() {
  55. $cmdArgs = array('key1', 'key2', 'key3');
  56. $cmd = new \Predis\Commands\GetMultiple();
  57. $cmd->setArgumentsArray($cmdArgs);
  58. $this->assertEquals($cmdArgs[0], $cmd->getArgument(0));
  59. $this->assertEquals($cmdArgs[1], $cmd->getArgument(1));
  60. $this->assertEquals($cmdArgs[2], $cmd->getArgument(2));
  61. $cmd = new \Predis\Commands\GetMultiple();
  62. $cmd->setArguments('key1', 'key2', 'key3');
  63. $this->assertEquals($cmdArgs[0], $cmd->getArgument(0));
  64. $this->assertEquals($cmdArgs[1], $cmd->getArgument(1));
  65. $this->assertEquals($cmdArgs[2], $cmd->getArgument(2));
  66. $cmd = new \Predis\Commands\Ping();
  67. $this->assertNull($cmd->getArgument(0));
  68. }
  69. function testCommand_InlineWithNoArguments() {
  70. $cmd = new \Predis\Compatibility\v1_0\Commands\Ping();
  71. $this->assertType('\Predis\InlineCommand', $cmd);
  72. $this->assertEquals('PING', $cmd->getCommandId());
  73. $this->assertFalse($cmd->closesConnection());
  74. $this->assertFalse($cmd->canBeHashed());
  75. $this->assertNull($cmd->getHash(new \Predis\Utilities\HashRing()));
  76. $this->assertEquals("PING\r\n", $cmd());
  77. }
  78. function testCommand_InlineWithArguments() {
  79. $cmd = new \Predis\Compatibility\v1_0\Commands\Get();
  80. $cmd->setArgumentsArray(array('key'));
  81. $this->assertType('\Predis\InlineCommand', $cmd);
  82. $this->assertEquals('GET', $cmd->getCommandId());
  83. $this->assertFalse($cmd->closesConnection());
  84. $this->assertTrue($cmd->canBeHashed());
  85. $this->assertNotNull($cmd->getHash(new \Predis\Utilities\HashRing()));
  86. $this->assertEquals("GET key\r\n", $cmd());
  87. }
  88. function testCommand_BulkWithArguments() {
  89. $cmd = new \Predis\Compatibility\v1_0\Commands\Set();
  90. $cmd->setArgumentsArray(array('key', 'value'));
  91. $this->assertType('\Predis\BulkCommand', $cmd);
  92. $this->assertEquals('SET', $cmd->getCommandId());
  93. $this->assertFalse($cmd->closesConnection());
  94. $this->assertTrue($cmd->canBeHashed());
  95. $this->assertNotNull($cmd->getHash(new \Predis\Utilities\HashRing()));
  96. $this->assertEquals("SET key 5\r\nvalue\r\n", $cmd());
  97. }
  98. function testCommand_MultiBulkWithArguments() {
  99. $cmd = new \Predis\Commands\SetMultiple();
  100. $cmd->setArgumentsArray(array('key1', 'value1', 'key2', 'value2'));
  101. $this->assertType('\Predis\MultiBulkCommand', $cmd);
  102. $this->assertEquals('MSET', $cmd->getCommandId());
  103. $this->assertFalse($cmd->closesConnection());
  104. $this->assertFalse($cmd->canBeHashed());
  105. $this->assertNull($cmd->getHash(new \Predis\Utilities\HashRing()));
  106. $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());
  107. }
  108. function testCommand_ParseResponse() {
  109. // default parser
  110. $cmd = new \Predis\Commands\Get();
  111. $this->assertEquals('test', $cmd->parseResponse('test'));
  112. // overridden parser (boolean)
  113. $cmd = new \Predis\Commands\Exists();
  114. $this->assertTrue($cmd->parseResponse('1'));
  115. $this->assertFalse($cmd->parseResponse('0'));
  116. // overridden parser (boolean)
  117. $cmd = new \Predis\Commands\Ping();
  118. $this->assertTrue($cmd->parseResponse('PONG'));
  119. // overridden parser (complex)
  120. // TODO: emulate a respons to INFO
  121. }
  122. /* RedisServerProfile and derivates */
  123. function testRedisServerProfile_GetSpecificVersions() {
  124. $this->assertType('\Predis\RedisServer_v1_0', \Predis\RedisServerProfile::get('1.0'));
  125. $this->assertType('\Predis\RedisServer_v1_2', \Predis\RedisServerProfile::get('1.2'));
  126. $this->assertType('\Predis\RedisServer_vNext', \Predis\RedisServerProfile::get('dev'));
  127. $this->assertType('\Predis\RedisServerProfile', \Predis\RedisServerProfile::get('default'));
  128. $this->assertEquals(\Predis\RedisServerProfile::get('default'), \Predis\RedisServerProfile::getDefault());
  129. }
  130. function testRedisServerProfile_SupportedCommands() {
  131. $profile_10 = \Predis\RedisServerProfile::get('1.0');
  132. $profile_12 = \Predis\RedisServerProfile::get('1.2');
  133. $this->assertTrue($profile_10->supportsCommand('info'));
  134. $this->assertTrue($profile_12->supportsCommand('info'));
  135. $this->assertFalse($profile_10->supportsCommand('mset'));
  136. $this->assertTrue($profile_12->supportsCommand('mset'));
  137. $this->assertFalse($profile_10->supportsCommand('multi'));
  138. $this->assertFalse($profile_12->supportsCommand('multi'));
  139. }
  140. function testRedisServerProfile_CommandsCreation() {
  141. $profile = \Predis\RedisServerProfile::get('1.0');
  142. $cmdNoArgs = $profile->createCommand('info');
  143. $this->assertType('\Predis\Compatibility\v1_0\Commands\Info', $cmdNoArgs);
  144. $this->assertNull($cmdNoArgs->getArgument());
  145. $args = array('key1', 'key2');
  146. $cmdWithArgs = $profile->createCommand('mget', $args);
  147. $this->assertType('\Predis\Compatibility\v1_0\Commands\GetMultiple', $cmdWithArgs);
  148. $this->assertEquals($args[0], $cmdWithArgs->getArgument()); // TODO: why?
  149. $this->assertEquals($args[0], $cmdWithArgs->getArgument(0));
  150. $this->assertEquals($args[1], $cmdWithArgs->getArgument(1));
  151. $bogusCommand = 'not_existing_command';
  152. $expectedMessage = "'$bogusCommand' is not a registered Redis command";
  153. RC::testForClientException($this, $expectedMessage, function()
  154. use($profile, $bogusCommand) {
  155. $profile->createCommand($bogusCommand);
  156. });
  157. }
  158. function testRedisServerProfile_CommandsRegistration() {
  159. $profile = \Predis\RedisServerProfile::get('1.0');
  160. $cmdId = 'mset';
  161. $cmdClass = '\Predis\Commands\SetMultiple';
  162. $this->assertFalse($profile->supportsCommand($cmdId));
  163. $profile->registerCommand(new $cmdClass(), $cmdId);
  164. $this->assertTrue($profile->supportsCommand($cmdId));
  165. $this->assertType($cmdClass, $profile->createCommand($cmdId));
  166. }
  167. /* ResponseQueued */
  168. function testResponseQueued() {
  169. $response = new \Predis\ResponseQueued();
  170. $this->assertTrue($response->queued);
  171. $this->assertEquals(\Predis\Protocol::QUEUED, (string)$response);
  172. }
  173. /* ResponseError */
  174. function testResponseError() {
  175. $errorMessage = 'ERROR MESSAGE';
  176. $response = new \Predis\ResponseError($errorMessage);
  177. $this->assertTrue($response->error);
  178. $this->assertEquals($errorMessage, $response->message);
  179. $this->assertEquals($errorMessage, (string)$response);
  180. }
  181. /* Connection */
  182. function testConnection_StringCastReturnsIPAndPort() {
  183. $connection = new \Predis\Connection(RC::getConnectionParameters());
  184. $this->assertEquals(RC::SERVER_HOST . ':' . RC::SERVER_PORT, (string) $connection);
  185. }
  186. function testConnection_ConnectDisconnect() {
  187. $connection = new \Predis\Connection(RC::getConnectionParameters());
  188. $this->assertFalse($connection->isConnected());
  189. $connection->connect();
  190. $this->assertTrue($connection->isConnected());
  191. $connection->disconnect();
  192. $this->assertFalse($connection->isConnected());
  193. }
  194. function testConnection_WriteAndReadCommand() {
  195. $cmd = \Predis\RedisServerProfile::getDefault()->createCommand('ping');
  196. $connection = new \Predis\Connection(RC::getConnectionParameters());
  197. $connection->connect();
  198. $connection->writeCommand($cmd);
  199. $this->assertTrue($connection->readResponse($cmd));
  200. }
  201. function testConnection_WriteCommandAndCloseConnection() {
  202. $cmd = \Predis\RedisServerProfile::getDefault()->createCommand('quit');
  203. $connection = new \Predis\Connection(RC::getConnectionParameters());
  204. $connection->connect();
  205. $this->assertTrue($connection->isConnected());
  206. $connection->writeCommand($cmd);
  207. $exceptionMessage = 'Error while reading line from the server';
  208. RC::testForCommunicationException($this, $exceptionMessage, function() use($connection, $cmd) {
  209. $connection->readResponse($cmd);
  210. });
  211. //$this->assertFalse($connection->isConnected());
  212. }
  213. function testConnection_GetSocketOpensConnection() {
  214. $connection = new \Predis\Connection(RC::getConnectionParameters());
  215. $this->assertFalse($connection->isConnected());
  216. $this->assertType('resource', $connection->getSocket());
  217. $this->assertTrue($connection->isConnected());
  218. }
  219. function testConnection_LazyConnect() {
  220. $cmd = \Predis\RedisServerProfile::getDefault()->createCommand('ping');
  221. $connection = new \Predis\Connection(RC::getConnectionParameters());
  222. $this->assertFalse($connection->isConnected());
  223. $connection->writeCommand($cmd);
  224. $this->assertTrue($connection->isConnected());
  225. $this->assertTrue($connection->readResponse($cmd));
  226. }
  227. function testConnection_RawCommand() {
  228. $connection = new \Predis\Connection(RC::getConnectionParameters());
  229. $this->assertEquals('PONG', $connection->rawCommand("PING\r\n"));
  230. }
  231. function testConnection_Alias() {
  232. $connection1 = new \Predis\Connection(RC::getConnectionParameters());
  233. $this->assertNull($connection1->getParameters()->alias);
  234. $args = array_merge(RC::getConnectionArguments(), array('alias' => 'servername'));
  235. $connection2 = new \Predis\Connection(new \Predis\ConnectionParameters($args));
  236. $this->assertEquals('servername', $connection2->getParameters()->alias);
  237. }
  238. function testConnection_ConnectionTimeout() {
  239. $timeout = 3;
  240. $args = array('host' => '1.0.0.1', 'connection_timeout' => $timeout);
  241. $connection = new \Predis\Connection(new \Predis\ConnectionParameters($args));
  242. $start = time();
  243. RC::testForCommunicationException($this, null, function() use($connection) {
  244. $connection->connect();
  245. });
  246. $this->assertEquals((float)(time() - $start), $timeout, '', 1);
  247. }
  248. function testConnection_ReadTimeout() {
  249. $timeout = 1;
  250. $args = array_merge(RC::getConnectionArguments(), array('read_write_timeout' => $timeout));
  251. $cmdFake = \Predis\RedisServerProfile::getDefault()->createCommand('ping');
  252. $connection = new \Predis\Connection(new \Predis\ConnectionParameters($args));
  253. $expectedMessage = 'Error while reading line from the server';
  254. $start = time();
  255. RC::testForCommunicationException($this, $expectedMessage, function() use($connection, $cmdFake) {
  256. $connection->readResponse($cmdFake);
  257. });
  258. $this->assertEquals((float)(time() - $start), $timeout, '', 1);
  259. }
  260. /* ResponseReader */
  261. function testResponseReader_OptionIterableMultiBulkReplies() {
  262. $connection = new \Predis\Connection(RC::getConnectionParameters());
  263. $responseReader = $connection->getResponseReader();
  264. $responseReader->setHandler(
  265. \Predis\Protocol::PREFIX_MULTI_BULK,
  266. new \Predis\ResponseMultiBulkHandler()
  267. );
  268. $this->assertType('array', $connection->rawCommand("KEYS *\r\n"));
  269. $responseReader->setHandler(
  270. \Predis\Protocol::PREFIX_MULTI_BULK,
  271. new \Predis\ResponseMultiBulkStreamHandler()
  272. );
  273. $this->assertType('\Iterator', $connection->rawCommand("KEYS *\r\n"));
  274. }
  275. function testResponseReader_OptionExceptionOnError() {
  276. $connection = new \Predis\Connection(RC::getConnectionParameters());
  277. $responseReader = $connection->getResponseReader();
  278. $connection->rawCommand("SET key 5\r\nvalue\r\n");
  279. $rawCmdUnexpected = "LPUSH key 5\r\nvalue\r\n";
  280. $responseReader->setHandler(
  281. \Predis\Protocol::PREFIX_ERROR,
  282. new \Predis\ResponseErrorSilentHandler()
  283. );
  284. $errorReply = $connection->rawCommand($rawCmdUnexpected);
  285. $this->assertType('\Predis\ResponseError', $errorReply);
  286. $this->assertEquals(RC::EXCEPTION_WRONG_TYPE, $errorReply->message);
  287. $responseReader->setHandler(
  288. \Predis\Protocol::PREFIX_ERROR,
  289. new \Predis\ResponseErrorHandler()
  290. );
  291. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function()
  292. use ($connection, $rawCmdUnexpected) {
  293. $connection->rawCommand($rawCmdUnexpected);
  294. });
  295. }
  296. /* Client + CommandPipeline */
  297. function testCommandPipeline_Simple() {
  298. $client = RC::getConnection();
  299. $client->flushdb();
  300. $pipe = $client->pipeline();
  301. $this->assertType('\Predis\CommandPipeline', $pipe);
  302. $this->assertType('\Predis\CommandPipeline', $pipe->set('foo', 'bar'));
  303. $this->assertType('\Predis\CommandPipeline', $pipe->set('hoge', 'piyo'));
  304. $this->assertType('\Predis\CommandPipeline', $pipe->mset(array(
  305. 'foofoo' => 'barbar', 'hogehoge' => 'piyopiyo'
  306. )));
  307. $this->assertType('\Predis\CommandPipeline', $pipe->mget(array(
  308. 'foo', 'hoge', 'foofoo', 'hogehoge'
  309. )));
  310. $replies = $pipe->execute();
  311. $this->assertType('array', $replies);
  312. $this->assertEquals(4, count($replies));
  313. $this->assertEquals(4, count($replies[3]));
  314. $this->assertEquals('barbar', $replies[3][2]);
  315. }
  316. function testCommandPipeline_FluentInterface() {
  317. $client = RC::getConnection();
  318. $client->flushdb();
  319. $replies = $client->pipeline()->ping()->set('foo', 'bar')->get('foo')->execute();
  320. $this->assertType('array', $replies);
  321. $this->assertEquals('bar', $replies[2]);
  322. }
  323. function testCommandPipeline_CallableAnonymousBlock() {
  324. $client = RC::getConnection();
  325. $client->flushdb();
  326. $replies = $client->pipeline(function($pipe) {
  327. $pipe->ping();
  328. $pipe->set('foo', 'bar');
  329. $pipe->get('foo');
  330. });
  331. $this->assertType('array', $replies);
  332. $this->assertEquals('bar', $replies[2]);
  333. }
  334. function testCommandPipeline_ClientExceptionInCallableBlock() {
  335. $client = RC::getConnection();
  336. $client->flushdb();
  337. RC::testForClientException($this, 'TEST', function() use($client) {
  338. $client->pipeline(function($pipe) {
  339. $pipe->ping();
  340. $pipe->set('foo', 'bar');
  341. throw new \Predis\ClientException("TEST");
  342. });
  343. });
  344. $this->assertFalse($client->exists('foo'));
  345. }
  346. function testCommandPipeline_ServerExceptionInCallableBlock() {
  347. $client = RC::getConnection();
  348. $client->flushdb();
  349. $client->getResponseReader()->setHandler('-', new \Predis\ResponseErrorSilentHandler());
  350. $replies = $client->pipeline(function($pipe) {
  351. $pipe->set('foo', 'bar');
  352. $pipe->lpush('foo', 'piyo'); // LIST operation on STRING type returns an ERROR
  353. $pipe->set('hoge', 'piyo');
  354. });
  355. $this->assertType('array', $replies);
  356. $this->assertType('\Predis\ResponseError', $replies[1]);
  357. $this->assertTrue($client->exists('foo'));
  358. $this->assertTrue($client->exists('hoge'));
  359. }
  360. function testCommandPipeline_Flush() {
  361. $client = RC::getConnection();
  362. $client->flushdb();
  363. $pipe = $client->pipeline();
  364. $pipe->set('foo', 'bar')->set('hoge', 'piyo');
  365. $pipe->flushPipeline();
  366. $pipe->ping()->mget(array('foo', 'hoge'));
  367. $replies = $pipe->execute();
  368. $this->assertType('array', $replies);
  369. $this->assertEquals(4, count($replies));
  370. $this->assertEquals('bar', $replies[3][0]);
  371. $this->assertEquals('piyo', $replies[3][1]);
  372. }
  373. }
  374. ?>