ClientTest.php 25 KB

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