ClientTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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 testConstructorWithNullAndArrayArgument()
  159. {
  160. $factory = $this->getMock('Predis\ConnectionFactoryInterface');
  161. $arg2 = array('profile' => '2.0', 'prefix' => 'prefix:', 'connections' => $factory);
  162. $client = new Client(null, $arg2);
  163. $profile = $client->getProfile();
  164. $this->assertSame($profile->getVersion(), ServerProfile::get('2.0')->getVersion());
  165. $this->assertInstanceOf('Predis\Command\Processor\KeyPrefixProcessor', $profile->getProcessor());
  166. $this->assertSame('prefix:', $profile->getProcessor()->getPrefix());
  167. $this->assertSame($factory, $client->getConnectionFactory());
  168. }
  169. /**
  170. * @group disconnected
  171. */
  172. public function testConstructorWithArrayAndOptionReplicationArgument()
  173. {
  174. $arg1 = array('tcp://host1?alias=master', 'tcp://host2?alias=slave');
  175. $arg2 = array('replication' => true);
  176. $client = new Client($arg1, $arg2);
  177. $this->assertInstanceOf('Predis\Connection\ReplicationConnectionInterface', $connection = $client->getConnection());
  178. $this->assertSame('host1', $connection->getConnectionById('master')->getParameters()->host);
  179. $this->assertSame('host2', $connection->getConnectionById('slave')->getParameters()->host);
  180. }
  181. /**
  182. * @group disconnected
  183. */
  184. public function testConnectAndDisconnect()
  185. {
  186. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  187. $connection->expects($this->once())->method('connect');
  188. $connection->expects($this->once())->method('disconnect');
  189. $client = new Client($connection);
  190. $client->connect();
  191. $client->disconnect();
  192. }
  193. /**
  194. * @group disconnected
  195. */
  196. public function testIsConnectedChecksConnectionState()
  197. {
  198. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  199. $connection->expects($this->once())->method('isConnected');
  200. $client = new Client($connection);
  201. $client->isConnected();
  202. }
  203. /**
  204. * @group disconnected
  205. */
  206. public function testQuitIsAliasForDisconnect()
  207. {
  208. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  209. $connection->expects($this->once())->method('disconnect');
  210. $client = new Client($connection);
  211. $client->quit();
  212. }
  213. /**
  214. * @group disconnected
  215. */
  216. public function testCreatesNewCommandUsingSpecifiedProfile()
  217. {
  218. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  219. $profile = $this->getMock('Predis\Profile\ServerProfileInterface');
  220. $profile->expects($this->once())
  221. ->method('createCommand')
  222. ->with('ping', array())
  223. ->will($this->returnValue($ping));
  224. $client = new Client(null, array('profile' => $profile));
  225. $this->assertSame($ping, $client->createCommand('ping', array()));
  226. }
  227. /**
  228. * @group disconnected
  229. */
  230. public function testExecuteCommand()
  231. {
  232. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  233. $connection= $this->getMock('Predis\Connection\ConnectionInterface');
  234. $connection->expects($this->once())
  235. ->method('executeCommand')
  236. ->with($ping)
  237. ->will($this->returnValue(true));
  238. $client = new Client($connection);
  239. $this->assertTrue($client->executeCommand($ping));
  240. }
  241. /**
  242. * @group disconnected
  243. * @expectedException Predis\ServerException
  244. * @expectedExceptionMessage ERR Operation against a key holding the wrong kind of value
  245. */
  246. public function testExecuteCommandThrowsExceptionOnRedisError()
  247. {
  248. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  249. $expectedResponse = new ResponseError('ERR Operation against a key holding the wrong kind of value');
  250. $connection= $this->getMock('Predis\Connection\ConnectionInterface');
  251. $connection->expects($this->once())
  252. ->method('executeCommand')
  253. ->will($this->returnValue($expectedResponse));
  254. $client = new Client($connection);
  255. $client->executeCommand($ping);
  256. }
  257. /**
  258. * @group disconnected
  259. */
  260. public function testExecuteCommandReturnsErrorResponseOnRedisError()
  261. {
  262. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  263. $expectedResponse = new ResponseError('ERR Operation against a key holding the wrong kind of value');
  264. $connection= $this->getMock('Predis\Connection\ConnectionInterface');
  265. $connection->expects($this->once())
  266. ->method('executeCommand')
  267. ->will($this->returnValue($expectedResponse));
  268. $client = new Client($connection, array('exceptions' => false));
  269. $response = $client->executeCommand($ping);
  270. $this->assertSame($response, $expectedResponse);
  271. }
  272. /**
  273. * @group disconnected
  274. */
  275. public function testCallingRedisCommandExecutesInstanceOfCommand()
  276. {
  277. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  278. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  279. $connection->expects($this->once())
  280. ->method('executeCommand')
  281. ->with($this->isInstanceOf('Predis\Command\ConnectionPing'))
  282. ->will($this->returnValue(true));
  283. $profile = $this->getMock('Predis\Profile\ServerProfileInterface');
  284. $profile->expects($this->once())
  285. ->method('createCommand')
  286. ->with('ping', array())
  287. ->will($this->returnValue($ping));
  288. $options = array('profile' => $profile);
  289. $client = $this->getMock('Predis\Client', array('createCommand'), array($connection, $options));
  290. $this->assertTrue($client->ping());
  291. }
  292. /**
  293. * @group disconnected
  294. * @expectedException Predis\ServerException
  295. * @expectedExceptionMessage ERR Operation against a key holding the wrong kind of value
  296. */
  297. public function testCallingRedisCommandThrowsExceptionOnServerError()
  298. {
  299. $expectedResponse = new ResponseError('ERR Operation against a key holding the wrong kind of value');
  300. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  301. $connection->expects($this->once())
  302. ->method('executeCommand')
  303. ->with($this->isInstanceOf('Predis\Command\ConnectionPing'))
  304. ->will($this->returnValue($expectedResponse));
  305. $client = new Client($connection);
  306. $client->ping();
  307. }
  308. /**
  309. * @group disconnected
  310. */
  311. public function testCallingRedisCommandReturnsErrorResponseOnRedisError()
  312. {
  313. $expectedResponse = new ResponseError('ERR Operation against a key holding the wrong kind of value');
  314. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  315. $connection->expects($this->once())
  316. ->method('executeCommand')
  317. ->with($this->isInstanceOf('Predis\Command\ConnectionPing'))
  318. ->will($this->returnValue($expectedResponse));
  319. $client = new Client($connection, array('exceptions' => false));
  320. $response = $client->ping();
  321. $this->assertSame($response, $expectedResponse);
  322. }
  323. /**
  324. * @group disconnected
  325. * @expectedException Predis\ClientException
  326. * @expectedExceptionMessage 'invalidcommand' is not a registered Redis command
  327. */
  328. public function testThrowsExceptionOnNonRegisteredRedisCommand()
  329. {
  330. $client = new Client();
  331. $client->invalidCommand();
  332. }
  333. /**
  334. * @group disconnected
  335. */
  336. public function testGetConnectionFromClusterWithAlias()
  337. {
  338. $client = new Client(array('tcp://host1?alias=node01', 'tcp://host2?alias=node02'));
  339. $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $cluster = $client->getConnection());
  340. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node01 = $client->getConnection('node01'));
  341. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node02 = $client->getConnection('node02'));
  342. $this->assertSame('host1', $node01->getParameters()->host);
  343. $this->assertSame('host2', $node02->getParameters()->host);
  344. }
  345. /**
  346. * @group disconnected
  347. * @expectedException Predis\NotSupportedException
  348. * @expectedExceptionMessage Retrieving connections by alias is supported only with aggregated connections (cluster or replication)
  349. */
  350. public function testGetConnectionWithAliasWorksOnlyWithCluster()
  351. {
  352. $client = new Client();
  353. $client->getConnection('node01');
  354. }
  355. /**
  356. * @group disconnected
  357. */
  358. public function testPipelineWithoutArgumentsReturnsPipelineContext()
  359. {
  360. $client = new Client();
  361. $this->assertInstanceOf('Predis\Pipeline\PipelineContext', $pipeline = $client->pipeline());
  362. }
  363. /**
  364. * @group disconnected
  365. */
  366. public function testPipelineWithArrayReturnsPipelineContextWithOptions()
  367. {
  368. $client = new Client();
  369. $executor = $this->getMock('Predis\Pipeline\PipelineExecutorInterface');
  370. $options = array('executor' => $executor);
  371. $this->assertInstanceOf('Predis\Pipeline\PipelineContext', $pipeline = $client->pipeline($options));
  372. $this->assertSame($executor, $pipeline->getExecutor());
  373. $options = array('executor' => function ($client, $options) use ($executor) { return $executor; });
  374. $this->assertInstanceOf('Predis\Pipeline\PipelineContext', $pipeline = $client->pipeline($options));
  375. $this->assertSame($executor, $pipeline->getExecutor());
  376. }
  377. /**
  378. * @group disconnected
  379. */
  380. public function testPipelineWithCallableExecutesPipeline()
  381. {
  382. $callable = $this->getMock('stdClass', array('__invoke'));
  383. $callable->expects($this->once())
  384. ->method('__invoke')
  385. ->with($this->isInstanceOf('Predis\Pipeline\PipelineContext'));
  386. $client = new Client();
  387. $client->pipeline($callable);
  388. }
  389. /**
  390. * @group disconnected
  391. */
  392. public function testPipelineWithArrayAndCallableExecutesPipelineWithOptions()
  393. {
  394. $executor = $this->getMock('Predis\Pipeline\PipelineExecutorInterface');
  395. $options = array('executor' => $executor);
  396. $test = $this;
  397. $mockCallback = function($pipeline) use($executor, $test) {
  398. $reflection = new \ReflectionProperty($pipeline, 'executor');
  399. $reflection->setAccessible(true);
  400. $test->assertSame($executor, $reflection->getValue($pipeline));
  401. };
  402. $callable = $this->getMock('stdClass', array('__invoke'));
  403. $callable->expects($this->once())
  404. ->method('__invoke')
  405. ->with($this->isInstanceOf('Predis\Pipeline\PipelineContext'))
  406. ->will($this->returnCallback($mockCallback));
  407. $client = new Client();
  408. $client->pipeline($options, $callable);
  409. }
  410. /**
  411. * @group disconnected
  412. */
  413. public function testPubSubWithoutArgumentsReturnsPubSubContext()
  414. {
  415. $client = new Client();
  416. $this->assertInstanceOf('Predis\PubSub\PubSubContext', $pubsub = $client->pubSub());
  417. }
  418. /**
  419. * @group disconnected
  420. */
  421. public function testPubSubWithArrayReturnsPubSubContextWithOptions()
  422. {
  423. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  424. $options = array('subscribe' => 'channel');
  425. $client = new Client($connection);
  426. $this->assertInstanceOf('Predis\PubSub\PubSubContext', $pubsub = $client->pubSub($options));
  427. $reflection = new \ReflectionProperty($pubsub, 'options');
  428. $reflection->setAccessible(true);
  429. $this->assertSame($options, $reflection->getValue($pubsub));
  430. }
  431. /**
  432. * @group disconnected
  433. */
  434. public function testPubSubWithArrayAndCallableExecutesPubSub()
  435. {
  436. // NOTE: we use a subscribe count of 0 in the fake message to trick
  437. // the context and to make it think that it can be closed
  438. // since there are no more subscriptions active.
  439. $message = array('subscribe', 'channel', 0);
  440. $options = array('subscribe' => 'channel');
  441. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  442. $connection->expects($this->once())
  443. ->method('read')
  444. ->will($this->returnValue($message));
  445. $callable = $this->getMock('stdClass', array('__invoke'));
  446. $callable->expects($this->once())
  447. ->method('__invoke');
  448. $client = new Client($connection);
  449. $client->pubSub($options, $callable);
  450. }
  451. /**
  452. * @group disconnected
  453. */
  454. public function testMultiExecWithoutArgumentsReturnsMultiExecContext()
  455. {
  456. $client = new Client();
  457. $this->assertInstanceOf('Predis\Transaction\MultiExecContext', $pubsub = $client->multiExec());
  458. }
  459. /**
  460. * @group disconnected
  461. */
  462. public function testMultiExecWithArrayReturnsMultiExecContextWithOptions()
  463. {
  464. $options = array('cas' => true, 'retry' => 3);
  465. $client = new Client();
  466. $this->assertInstanceOf('Predis\Transaction\MultiExecContext', $tx = $client->multiExec($options));
  467. $reflection = new \ReflectionProperty($tx, 'options');
  468. $reflection->setAccessible(true);
  469. $this->assertSame($options, $reflection->getValue($tx));
  470. }
  471. /**
  472. * @group disconnected
  473. */
  474. public function testMultiExecWithArrayAndCallableExecutesMultiExec()
  475. {
  476. // NOTE: we use CAS since testing the actual MULTI/EXEC context
  477. // here is not the point.
  478. $options = array('cas' => true, 'retry' => 3);
  479. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  480. $connection->expects($this->once())
  481. ->method('executeCommand')
  482. ->will($this->returnValue(new ResponseQueued()));
  483. $txCallback = function($tx) { $tx->ping(); };
  484. $callable = $this->getMock('stdClass', array('__invoke'));
  485. $callable->expects($this->once())
  486. ->method('__invoke')
  487. ->will($this->returnCallback($txCallback));
  488. $client = new Client($connection);
  489. $client->multiExec($options, $callable);
  490. }
  491. /**
  492. * @group disconnected
  493. */
  494. public function testMonitorReturnsMonitorContext()
  495. {
  496. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  497. $client = new Client($connection);
  498. $this->assertInstanceOf('Predis\Monitor\MonitorContext', $monitor = $client->monitor());
  499. }
  500. /**
  501. * @group disconnected
  502. */
  503. public function testClientResendScriptedCommandUsingEvalOnNoScriptErrors()
  504. {
  505. $command = $this->getMockForAbstractClass('Predis\Command\ScriptedCommand');
  506. $command->expects($this->once())
  507. ->method('getScript')
  508. ->will($this->returnValue('return redis.call(\'exists\', KEYS[1])'));
  509. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  510. $connection->expects($this->at(0))
  511. ->method('executeCommand')
  512. ->with($command)
  513. ->will($this->returnValue(new ResponseError('NOSCRIPT')));
  514. $connection->expects($this->at(1))
  515. ->method('executeCommand')
  516. ->with($this->isInstanceOf('Predis\Command\ServerEval'))
  517. ->will($this->returnValue(true));
  518. $client = new Client($connection);
  519. $this->assertTrue($client->executeCommand($command));
  520. }
  521. // ******************************************************************** //
  522. // ---- HELPER METHODS ------------------------------------------------ //
  523. // ******************************************************************** //
  524. /**
  525. * Returns a named array with the default connection parameters and their values.
  526. *
  527. * @return Array Default connection parameters.
  528. */
  529. protected function getDefaultParametersArray()
  530. {
  531. return array(
  532. 'scheme' => 'tcp',
  533. 'host' => REDIS_SERVER_HOST,
  534. 'port' => REDIS_SERVER_PORT,
  535. 'database' => REDIS_SERVER_DBNUM,
  536. );
  537. }
  538. /**
  539. * Returns a named array with the default client options and their values.
  540. *
  541. * @return Array Default connection parameters.
  542. */
  543. protected function getDefaultOptionsArray()
  544. {
  545. return array(
  546. 'profile' => REDIS_SERVER_VERSION,
  547. );
  548. }
  549. /**
  550. * Returns a named array with the default connection parameters merged with
  551. * the specified additional parameters.
  552. *
  553. * @param Array $additional Additional connection parameters.
  554. * @return Array Connection parameters.
  555. */
  556. protected function getParametersArray(Array $additional)
  557. {
  558. return array_merge($this->getDefaultParametersArray(), $additional);
  559. }
  560. /**
  561. * Returns an URI string representation of the specified connection parameters.
  562. *
  563. * @param Array $parameters Array of connection parameters.
  564. * @return String URI string.
  565. */
  566. protected function getParametersString(Array $parameters)
  567. {
  568. $defaults = $this->getDefaultParametersArray();
  569. $scheme = isset($parameters['scheme']) ? $parameters['scheme'] : $defaults['scheme'];
  570. $host = isset($parameters['host']) ? $parameters['host'] : $defaults['host'];
  571. $port = isset($parameters['port']) ? $parameters['port'] : $defaults['port'];
  572. unset($parameters['scheme'], $parameters['host'], $parameters['port']);
  573. $uriString = "$scheme://$host:$port/?";
  574. foreach ($parameters as $k => $v) {
  575. $uriString .= "$k=$v&";
  576. }
  577. return $uriString;
  578. }
  579. }