ClientTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis;
  11. use \PHPUnit_Framework_TestCase as StandardTestCase;
  12. use Predis\Profile\ServerProfile;
  13. use Predis\Connection\PredisCluster;
  14. use Predis\Connection\MasterSlaveReplication;
  15. /**
  16. *
  17. */
  18. class ClientTest extends StandardTestCase
  19. {
  20. /**
  21. * @group disconnected
  22. */
  23. public function testConstructorWithoutArguments()
  24. {
  25. $client = new Client();
  26. $connection = $client->getConnection();
  27. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $connection);
  28. $parameters = $connection->getParameters();
  29. $this->assertSame($parameters->host, '127.0.0.1');
  30. $this->assertSame($parameters->port, 6379);
  31. $options = $client->getOptions();
  32. $this->assertSame($options->profile->getVersion(), ServerProfile::getDefault()->getVersion());
  33. $this->assertFalse($client->isConnected());
  34. }
  35. /**
  36. * @group disconnected
  37. */
  38. public function testConstructorWithNullArgument()
  39. {
  40. $client = new Client(null);
  41. $connection = $client->getConnection();
  42. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $connection);
  43. $parameters = $connection->getParameters();
  44. $this->assertSame($parameters->host, '127.0.0.1');
  45. $this->assertSame($parameters->port, 6379);
  46. $options = $client->getOptions();
  47. $this->assertSame($options->profile->getVersion(), ServerProfile::getDefault()->getVersion());
  48. $this->assertFalse($client->isConnected());
  49. }
  50. /**
  51. * @group disconnected
  52. */
  53. public function testConstructorWithNullAndNullArguments()
  54. {
  55. $client = new Client(null, null);
  56. $connection = $client->getConnection();
  57. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $connection);
  58. $parameters = $connection->getParameters();
  59. $this->assertSame($parameters->host, '127.0.0.1');
  60. $this->assertSame($parameters->port, 6379);
  61. $options = $client->getOptions();
  62. $this->assertSame($options->profile->getVersion(), ServerProfile::getDefault()->getVersion());
  63. $this->assertFalse($client->isConnected());
  64. }
  65. /**
  66. * @group disconnected
  67. */
  68. public function testConstructorWithArrayArgument()
  69. {
  70. $client = new Client($arg1 = array('host' => 'localhost', 'port' => 7000));
  71. $parameters = $client->getConnection()->getParameters();
  72. $this->assertSame($parameters->host, $arg1['host']);
  73. $this->assertSame($parameters->port, $arg1['port']);
  74. }
  75. /**
  76. * @group disconnected
  77. */
  78. public function testConstructorWithArrayOfArrayArgument()
  79. {
  80. $arg1 = array(
  81. array('host' => 'localhost', 'port' => 7000),
  82. array('host' => 'localhost', 'port' => 7001),
  83. );
  84. $client = new Client($arg1);
  85. $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $client->getConnection());
  86. }
  87. /**
  88. * @group disconnected
  89. */
  90. public function testConstructorWithStringArgument()
  91. {
  92. $client = new Client('tcp://localhost:7000');
  93. $parameters = $client->getConnection()->getParameters();
  94. $this->assertSame($parameters->host, 'localhost');
  95. $this->assertSame($parameters->port, 7000);
  96. }
  97. /**
  98. * @group disconnected
  99. */
  100. public function testConstructorWithArrayOfStringArgument()
  101. {
  102. $client = new Client($arg1 = array('tcp://localhost:7000', 'tcp://localhost:7001'));
  103. $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $client->getConnection());
  104. }
  105. /**
  106. * @group disconnected
  107. */
  108. public function testConstructorWithArrayOfConnectionsArgument()
  109. {
  110. $connection1 = $this->getMock('Predis\Connection\SingleConnectionInterface');
  111. $connection2 = $this->getMock('Predis\Connection\SingleConnectionInterface');
  112. $client = new Client(array($connection1, $connection2));
  113. $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $cluster = $client->getConnection());
  114. $this->assertSame($connection1, $cluster->getConnectionById(0));
  115. $this->assertSame($connection2, $cluster->getConnectionById(1));
  116. }
  117. /**
  118. * @group disconnected
  119. */
  120. public function testConstructorWithConnectionArgument()
  121. {
  122. $factory = new ConnectionFactory();
  123. $connection = $factory->create('tcp://localhost:7000');
  124. $client = new Client($connection);
  125. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $client->getConnection());
  126. $this->assertSame($connection, $client->getConnection());
  127. $parameters = $client->getConnection()->getParameters();
  128. $this->assertSame($parameters->host, 'localhost');
  129. $this->assertSame($parameters->port, 7000);
  130. }
  131. /**
  132. * @group disconnected
  133. */
  134. public function testConstructorWithClusterArgument()
  135. {
  136. $cluster = new PredisCluster();
  137. $factory = new ConnectionFactory();
  138. $factory->createAggregated($cluster, array('tcp://localhost:7000', 'tcp://localhost:7001'));
  139. $client = new Client($cluster);
  140. $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $client->getConnection());
  141. $this->assertSame($cluster, $client->getConnection());
  142. }
  143. /**
  144. * @group disconnected
  145. */
  146. public function testConstructorWithReplicationArgument()
  147. {
  148. $replication = new MasterSlaveReplication();
  149. $factory = new ConnectionFactory();
  150. $factory->createAggregated($replication, array('tcp://host1?alias=master', 'tcp://host2?alias=slave'));
  151. $client = new Client($replication);
  152. $this->assertInstanceOf('Predis\Connection\ReplicationConnectionInterface', $client->getConnection());
  153. $this->assertSame($replication, $client->getConnection());
  154. }
  155. /**
  156. * @group disconnected
  157. */
  158. public function testConstructorWithNullAndStringArgument()
  159. {
  160. $client = new Client(null, '2.0');
  161. $this->assertSame($client->getProfile()->getVersion(), ServerProfile::get('2.0')->getVersion());
  162. }
  163. /**
  164. * @group disconnected
  165. */
  166. public function testConstructorWithNullAndProfileArgument()
  167. {
  168. $client = new Client(null, $arg2 = ServerProfile::get('2.0'));
  169. $this->assertSame($client->getProfile(), $arg2);
  170. }
  171. /**
  172. * @group disconnected
  173. */
  174. public function testConstructorWithNullAndArrayArgument()
  175. {
  176. $factory = $this->getMock('Predis\ConnectionFactoryInterface');
  177. $arg2 = array('profile' => '2.0', 'prefix' => 'prefix:', 'connections' => $factory);
  178. $client = new Client(null, $arg2);
  179. $profile = $client->getProfile();
  180. $this->assertSame($profile->getVersion(), ServerProfile::get('2.0')->getVersion());
  181. $this->assertInstanceOf('Predis\Command\Processor\KeyPrefixProcessor', $profile->getProcessor());
  182. $this->assertSame('prefix:', $profile->getProcessor()->getPrefix());
  183. $this->assertSame($factory, $client->getConnectionFactory());
  184. }
  185. /**
  186. * @group disconnected
  187. */
  188. public function testConstructorWithArrayAndOptionReplicationArgument()
  189. {
  190. $arg1 = array('tcp://host1?alias=master', 'tcp://host2?alias=slave');
  191. $arg2 = array('replication' => true);
  192. $client = new Client($arg1, $arg2);
  193. $this->assertInstanceOf('Predis\Connection\ReplicationConnectionInterface', $connection = $client->getConnection());
  194. $this->assertSame('host1', $connection->getConnectionById('master')->getParameters()->host);
  195. $this->assertSame('host2', $connection->getConnectionById('slave')->getParameters()->host);
  196. }
  197. /**
  198. * @group disconnected
  199. */
  200. public function testConnectAndDisconnect()
  201. {
  202. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  203. $connection->expects($this->once())->method('connect');
  204. $connection->expects($this->once())->method('disconnect');
  205. $client = new Client($connection);
  206. $client->connect();
  207. $client->disconnect();
  208. }
  209. /**
  210. * @group disconnected
  211. */
  212. public function testIsConnectedChecksConnectionState()
  213. {
  214. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  215. $connection->expects($this->once())->method('isConnected');
  216. $client = new Client($connection);
  217. $client->isConnected();
  218. }
  219. /**
  220. * @group disconnected
  221. */
  222. public function testQuitIsAliasForDisconnect()
  223. {
  224. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  225. $connection->expects($this->once())->method('disconnect');
  226. $client = new Client($connection);
  227. $client->quit();
  228. }
  229. /**
  230. * @group disconnected
  231. */
  232. public function testCreatesNewCommandUsingSpecifiedProfile()
  233. {
  234. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  235. $profile = $this->getMock('Predis\Profile\ServerProfileInterface');
  236. $profile->expects($this->once())
  237. ->method('createCommand')
  238. ->with('ping', array())
  239. ->will($this->returnValue($ping));
  240. $client = new Client(null, $profile);
  241. $this->assertSame($ping, $client->createCommand('ping', array()));
  242. }
  243. /**
  244. * @group disconnected
  245. */
  246. public function testExecuteCommand()
  247. {
  248. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  249. $connection= $this->getMock('Predis\Connection\ConnectionInterface');
  250. $connection->expects($this->once())
  251. ->method('executeCommand')
  252. ->with($ping)
  253. ->will($this->returnValue(true));
  254. $client = new Client($connection);
  255. $this->assertTrue($client->executeCommand($ping));
  256. }
  257. /**
  258. * @group disconnected
  259. * @expectedException Predis\ServerException
  260. * @expectedExceptionMessage ERR Operation against a key holding the wrong kind of value
  261. */
  262. public function testExecuteCommandThrowsExceptionOnRedisError()
  263. {
  264. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  265. $expectedResponse = new ResponseError('ERR Operation against a key holding the wrong kind of value');
  266. $connection= $this->getMock('Predis\Connection\ConnectionInterface');
  267. $connection->expects($this->once())
  268. ->method('executeCommand')
  269. ->will($this->returnValue($expectedResponse));
  270. $client = new Client($connection);
  271. $client->executeCommand($ping);
  272. }
  273. /**
  274. * @group disconnected
  275. */
  276. public function testExecuteCommandReturnsErrorResponseOnRedisError()
  277. {
  278. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  279. $expectedResponse = new ResponseError('ERR Operation against a key holding the wrong kind of value');
  280. $connection= $this->getMock('Predis\Connection\ConnectionInterface');
  281. $connection->expects($this->once())
  282. ->method('executeCommand')
  283. ->will($this->returnValue($expectedResponse));
  284. $client = new Client($connection, array('exceptions' => false));
  285. $response = $client->executeCommand($ping);
  286. $this->assertSame($response, $expectedResponse);
  287. }
  288. /**
  289. * @group disconnected
  290. */
  291. public function testCallingRedisCommandExecutesInstanceOfCommand()
  292. {
  293. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  294. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  295. $connection->expects($this->once())
  296. ->method('executeCommand')
  297. ->with($this->isInstanceOf('Predis\Command\ConnectionPing'))
  298. ->will($this->returnValue(true));
  299. $profile = $this->getMock('Predis\Profile\ServerProfileInterface');
  300. $profile->expects($this->once())
  301. ->method('createCommand')
  302. ->with('ping', array())
  303. ->will($this->returnValue($ping));
  304. $client = $this->getMock('Predis\Client', array('createCommand'), array($connection, $profile));
  305. $this->assertTrue($client->ping());
  306. }
  307. /**
  308. * @group disconnected
  309. * @expectedException Predis\ServerException
  310. * @expectedExceptionMessage ERR Operation against a key holding the wrong kind of value
  311. */
  312. public function testCallingRedisCommandThrowsExceptionOnServerError()
  313. {
  314. $expectedResponse = new ResponseError('ERR Operation against a key holding the wrong kind of value');
  315. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  316. $connection->expects($this->once())
  317. ->method('executeCommand')
  318. ->with($this->isInstanceOf('Predis\Command\ConnectionPing'))
  319. ->will($this->returnValue($expectedResponse));
  320. $client = new Client($connection);
  321. $client->ping();
  322. }
  323. /**
  324. * @group disconnected
  325. */
  326. public function testCallingRedisCommandReturnsErrorResponseOnRedisError()
  327. {
  328. $expectedResponse = new ResponseError('ERR Operation against a key holding the wrong kind of value');
  329. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  330. $connection->expects($this->once())
  331. ->method('executeCommand')
  332. ->with($this->isInstanceOf('Predis\Command\ConnectionPing'))
  333. ->will($this->returnValue($expectedResponse));
  334. $client = new Client($connection, array('exceptions' => false));
  335. $response = $client->ping();
  336. $this->assertSame($response, $expectedResponse);
  337. }
  338. /**
  339. * @group disconnected
  340. * @expectedException Predis\ClientException
  341. * @expectedExceptionMessage 'invalidcommand' is not a registered Redis command
  342. */
  343. public function testThrowsExceptionOnNonRegisteredRedisCommand()
  344. {
  345. $client = new Client();
  346. $client->invalidCommand();
  347. }
  348. /**
  349. * @group disconnected
  350. */
  351. public function testGetConnectionFromClusterWithAlias()
  352. {
  353. $client = new Client(array('tcp://host1?alias=node01', 'tcp://host2?alias=node02'));
  354. $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $cluster = $client->getConnection());
  355. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node01 = $client->getConnection('node01'));
  356. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node02 = $client->getConnection('node02'));
  357. $this->assertSame('host1', $node01->getParameters()->host);
  358. $this->assertSame('host2', $node02->getParameters()->host);
  359. }
  360. /**
  361. * @group disconnected
  362. * @expectedException Predis\NotSupportedException
  363. * @expectedExceptionMessage Retrieving connections by alias is supported only with aggregated connections (cluster or replication)
  364. */
  365. public function testGetConnectionWithAliasWorksOnlyWithCluster()
  366. {
  367. $client = new Client();
  368. $client->getConnection('node01');
  369. }
  370. /**
  371. * @group disconnected
  372. */
  373. public function testPipelineWithoutArgumentsReturnsPipelineContext()
  374. {
  375. $client = new Client();
  376. $this->assertInstanceOf('Predis\Pipeline\PipelineContext', $pipeline = $client->pipeline());
  377. }
  378. /**
  379. * @group disconnected
  380. */
  381. public function testPipelineWithArrayReturnsPipelineContextWithOptions()
  382. {
  383. $client = new Client();
  384. $executor = $this->getMock('Predis\Pipeline\PipelineExecutorInterface');
  385. $options = array('executor' => $executor);
  386. $this->assertInstanceOf('Predis\Pipeline\PipelineContext', $pipeline = $client->pipeline($options));
  387. $this->assertSame($executor, $pipeline->getExecutor());
  388. $options = array('executor' => function ($client, $options) use ($executor) { return $executor; });
  389. $this->assertInstanceOf('Predis\Pipeline\PipelineContext', $pipeline = $client->pipeline($options));
  390. $this->assertSame($executor, $pipeline->getExecutor());
  391. }
  392. /**
  393. * @group disconnected
  394. */
  395. public function testPipelineWithCallableExecutesPipeline()
  396. {
  397. $callable = $this->getMock('stdClass', array('__invoke'));
  398. $callable->expects($this->once())
  399. ->method('__invoke')
  400. ->with($this->isInstanceOf('Predis\Pipeline\PipelineContext'));
  401. $client = new Client();
  402. $client->pipeline($callable);
  403. }
  404. /**
  405. * @group disconnected
  406. */
  407. public function testPipelineWithArrayAndCallableExecutesPipelineWithOptions()
  408. {
  409. $executor = $this->getMock('Predis\Pipeline\PipelineExecutorInterface');
  410. $options = array('executor' => $executor);
  411. $test = $this;
  412. $mockCallback = function($pipeline) use($executor, $test) {
  413. $reflection = new \ReflectionProperty($pipeline, 'executor');
  414. $reflection->setAccessible(true);
  415. $test->assertSame($executor, $reflection->getValue($pipeline));
  416. };
  417. $callable = $this->getMock('stdClass', array('__invoke'));
  418. $callable->expects($this->once())
  419. ->method('__invoke')
  420. ->with($this->isInstanceOf('Predis\Pipeline\PipelineContext'))
  421. ->will($this->returnCallback($mockCallback));
  422. $client = new Client();
  423. $client->pipeline($options, $callable);
  424. }
  425. /**
  426. * @group disconnected
  427. */
  428. public function testPubSubWithoutArgumentsReturnsPubSubContext()
  429. {
  430. $client = new Client();
  431. $this->assertInstanceOf('Predis\PubSub\PubSubContext', $pubsub = $client->pubSub());
  432. }
  433. /**
  434. * @group disconnected
  435. */
  436. public function testPubSubWithArrayReturnsPubSubContextWithOptions()
  437. {
  438. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  439. $options = array('subscribe' => 'channel');
  440. $client = new Client($connection);
  441. $this->assertInstanceOf('Predis\PubSub\PubSubContext', $pubsub = $client->pubSub($options));
  442. $reflection = new \ReflectionProperty($pubsub, 'options');
  443. $reflection->setAccessible(true);
  444. $this->assertSame($options, $reflection->getValue($pubsub));
  445. }
  446. /**
  447. * @group disconnected
  448. */
  449. public function testPubSubWithArrayAndCallableExecutesPubSub()
  450. {
  451. // NOTE: we use a subscribe count of 0 in the fake message to trick
  452. // the context and to make it think that it can be closed
  453. // since there are no more subscriptions active.
  454. $message = array('subscribe', 'channel', 0);
  455. $options = array('subscribe' => 'channel');
  456. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  457. $connection->expects($this->once())
  458. ->method('read')
  459. ->will($this->returnValue($message));
  460. $callable = $this->getMock('stdClass', array('__invoke'));
  461. $callable->expects($this->once())
  462. ->method('__invoke');
  463. $client = new Client($connection);
  464. $client->pubSub($options, $callable);
  465. }
  466. /**
  467. * @group disconnected
  468. */
  469. public function testMultiExecWithoutArgumentsReturnsMultiExecContext()
  470. {
  471. $client = new Client();
  472. $this->assertInstanceOf('Predis\Transaction\MultiExecContext', $pubsub = $client->multiExec());
  473. }
  474. /**
  475. * @group disconnected
  476. */
  477. public function testMultiExecWithArrayReturnsMultiExecContextWithOptions()
  478. {
  479. $options = array('cas' => true, 'retry' => 3);
  480. $client = new Client();
  481. $this->assertInstanceOf('Predis\Transaction\MultiExecContext', $tx = $client->multiExec($options));
  482. $reflection = new \ReflectionProperty($tx, 'options');
  483. $reflection->setAccessible(true);
  484. $this->assertSame($options, $reflection->getValue($tx));
  485. }
  486. /**
  487. * @group disconnected
  488. */
  489. public function testMultiExecWithArrayAndCallableExecutesMultiExec()
  490. {
  491. // NOTE: we use CAS since testing the actual MULTI/EXEC context
  492. // here is not the point.
  493. $options = array('cas' => true, 'retry' => 3);
  494. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  495. $connection->expects($this->once())
  496. ->method('executeCommand')
  497. ->will($this->returnValue(new ResponseQueued()));
  498. $txCallback = function($tx) { $tx->ping(); };
  499. $callable = $this->getMock('stdClass', array('__invoke'));
  500. $callable->expects($this->once())
  501. ->method('__invoke')
  502. ->will($this->returnCallback($txCallback));
  503. $client = new Client($connection);
  504. $client->multiExec($options, $callable);
  505. }
  506. /**
  507. * @group disconnected
  508. */
  509. public function testMonitorReturnsMonitorContext()
  510. {
  511. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  512. $client = new Client($connection);
  513. $this->assertInstanceOf('Predis\Monitor\MonitorContext', $monitor = $client->monitor());
  514. }
  515. /**
  516. * @group disconnected
  517. */
  518. public function testClientResendScriptedCommandUsingEvalOnNoScriptErrors()
  519. {
  520. $command = $this->getMockForAbstractClass('Predis\Command\ScriptedCommand');
  521. $command->expects($this->once())
  522. ->method('getScript')
  523. ->will($this->returnValue('return redis.call(\'exists\', KEYS[1])'));
  524. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  525. $connection->expects($this->at(0))
  526. ->method('executeCommand')
  527. ->with($command)
  528. ->will($this->returnValue(new ResponseError('NOSCRIPT')));
  529. $connection->expects($this->at(1))
  530. ->method('executeCommand')
  531. ->with($this->isInstanceOf('Predis\Command\ServerEval'))
  532. ->will($this->returnValue(true));
  533. $client = new Client($connection);
  534. $this->assertTrue($client->executeCommand($command));
  535. }
  536. // ******************************************************************** //
  537. // ---- HELPER METHODS ------------------------------------------------ //
  538. // ******************************************************************** //
  539. /**
  540. * Returns a named array with the default connection parameters and their values.
  541. *
  542. * @return Array Default connection parameters.
  543. */
  544. protected function getDefaultParametersArray()
  545. {
  546. return array(
  547. 'scheme' => 'tcp',
  548. 'host' => REDIS_SERVER_HOST,
  549. 'port' => REDIS_SERVER_PORT,
  550. 'database' => REDIS_SERVER_DBNUM,
  551. );
  552. }
  553. /**
  554. * Returns a named array with the default client options and their values.
  555. *
  556. * @return Array Default connection parameters.
  557. */
  558. protected function getDefaultOptionsArray()
  559. {
  560. return array(
  561. 'profile' => REDIS_SERVER_VERSION,
  562. );
  563. }
  564. /**
  565. * Returns a named array with the default connection parameters merged with
  566. * the specified additional parameters.
  567. *
  568. * @param Array $additional Additional connection parameters.
  569. * @return Array Connection parameters.
  570. */
  571. protected function getParametersArray(Array $additional)
  572. {
  573. return array_merge($this->getDefaultParametersArray(), $additional);
  574. }
  575. /**
  576. * Returns an URI string representation of the specified connection parameters.
  577. *
  578. * @param Array $parameters Array of connection parameters.
  579. * @return String URI string.
  580. */
  581. protected function getParametersString(Array $parameters)
  582. {
  583. $defaults = $this->getDefaultParametersArray();
  584. $scheme = isset($parameters['scheme']) ? $parameters['scheme'] : $defaults['scheme'];
  585. $host = isset($parameters['host']) ? $parameters['host'] : $defaults['host'];
  586. $port = isset($parameters['port']) ? $parameters['port'] : $defaults['port'];
  587. unset($parameters['scheme'], $parameters['host'], $parameters['port']);
  588. $uriString = "$scheme://$host:$port/?";
  589. foreach ($parameters as $k => $v) {
  590. $uriString .= "$k=$v&";
  591. }
  592. return $uriString;
  593. }
  594. }