ClientTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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 testGetConnectionFromClusterWithAlias()
  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->getConnection('node01'));
  342. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node02 = $client->getConnection('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 alias is supported only with aggregated connections
  350. */
  351. public function testGetConnectionWithAliasWorksOnlyWithCluster()
  352. {
  353. $client = new Client();
  354. $client->getConnection('node01');
  355. }
  356. /**
  357. * @group disconnected
  358. */
  359. public function testPipelineWithoutArgumentsReturnsPipelineContext()
  360. {
  361. $client = new Client();
  362. $this->assertInstanceOf('Predis\Pipeline\PipelineContext', $pipeline = $client->pipeline());
  363. }
  364. /**
  365. * @group disconnected
  366. */
  367. public function testPipelineWithArrayReturnsPipelineContextWithOptions()
  368. {
  369. $client = new Client();
  370. $executor = $this->getMock('Predis\Pipeline\PipelineExecutorInterface');
  371. $options = array('executor' => $executor);
  372. $this->assertInstanceOf('Predis\Pipeline\PipelineContext', $pipeline = $client->pipeline($options));
  373. $this->assertSame($executor, $pipeline->getExecutor());
  374. $options = array('executor' => function ($client, $options) use ($executor) { return $executor; });
  375. $this->assertInstanceOf('Predis\Pipeline\PipelineContext', $pipeline = $client->pipeline($options));
  376. $this->assertSame($executor, $pipeline->getExecutor());
  377. }
  378. /**
  379. * @group disconnected
  380. */
  381. public function testPipelineWithCallableExecutesPipeline()
  382. {
  383. $callable = $this->getMock('stdClass', array('__invoke'));
  384. $callable->expects($this->once())
  385. ->method('__invoke')
  386. ->with($this->isInstanceOf('Predis\Pipeline\PipelineContext'));
  387. $client = new Client();
  388. $client->pipeline($callable);
  389. }
  390. /**
  391. * @group disconnected
  392. */
  393. public function testPipelineWithArrayAndCallableExecutesPipelineWithOptions()
  394. {
  395. $executor = $this->getMock('Predis\Pipeline\PipelineExecutorInterface');
  396. $options = array('executor' => $executor);
  397. $test = $this;
  398. $mockCallback = function ($pipeline) use ($executor, $test) {
  399. $reflection = new \ReflectionProperty($pipeline, 'executor');
  400. $reflection->setAccessible(true);
  401. $test->assertSame($executor, $reflection->getValue($pipeline));
  402. };
  403. $callable = $this->getMock('stdClass', array('__invoke'));
  404. $callable->expects($this->once())
  405. ->method('__invoke')
  406. ->with($this->isInstanceOf('Predis\Pipeline\PipelineContext'))
  407. ->will($this->returnCallback($mockCallback));
  408. $client = new Client();
  409. $client->pipeline($options, $callable);
  410. }
  411. /**
  412. * @group disconnected
  413. */
  414. public function testPubSubWithoutArgumentsReturnsPubSubContext()
  415. {
  416. $client = new Client();
  417. $this->assertInstanceOf('Predis\PubSub\PubSubContext', $pubsub = $client->pubSub());
  418. }
  419. /**
  420. * @group disconnected
  421. */
  422. public function testPubSubWithArrayReturnsPubSubContextWithOptions()
  423. {
  424. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  425. $options = array('subscribe' => 'channel');
  426. $client = new Client($connection);
  427. $this->assertInstanceOf('Predis\PubSub\PubSubContext', $pubsub = $client->pubSub($options));
  428. $reflection = new \ReflectionProperty($pubsub, 'options');
  429. $reflection->setAccessible(true);
  430. $this->assertSame($options, $reflection->getValue($pubsub));
  431. }
  432. /**
  433. * @group disconnected
  434. */
  435. public function testPubSubWithArrayAndCallableExecutesPubSub()
  436. {
  437. // NOTE: we use a subscribe count of 0 in the fake message to trick
  438. // the context and to make it think that it can be closed
  439. // since there are no more subscriptions active.
  440. $message = array('subscribe', 'channel', 0);
  441. $options = array('subscribe' => 'channel');
  442. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  443. $connection->expects($this->once())
  444. ->method('read')
  445. ->will($this->returnValue($message));
  446. $callable = $this->getMock('stdClass', array('__invoke'));
  447. $callable->expects($this->once())
  448. ->method('__invoke');
  449. $client = new Client($connection);
  450. $client->pubSub($options, $callable);
  451. }
  452. /**
  453. * @group disconnected
  454. */
  455. public function testMultiExecWithoutArgumentsReturnsMultiExecContext()
  456. {
  457. $client = new Client();
  458. $this->assertInstanceOf('Predis\Transaction\MultiExecContext', $pubsub = $client->multiExec());
  459. }
  460. /**
  461. * @group disconnected
  462. */
  463. public function testMultiExecWithArrayReturnsMultiExecContextWithOptions()
  464. {
  465. $options = array('cas' => true, 'retry' => 3);
  466. $client = new Client();
  467. $this->assertInstanceOf('Predis\Transaction\MultiExecContext', $tx = $client->multiExec($options));
  468. $reflection = new \ReflectionProperty($tx, 'options');
  469. $reflection->setAccessible(true);
  470. $this->assertSame($options, $reflection->getValue($tx));
  471. }
  472. /**
  473. * @group disconnected
  474. */
  475. public function testMultiExecWithArrayAndCallableExecutesMultiExec()
  476. {
  477. // NOTE: we use CAS since testing the actual MULTI/EXEC context
  478. // here is not the point.
  479. $options = array('cas' => true, 'retry' => 3);
  480. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  481. $connection->expects($this->once())
  482. ->method('executeCommand')
  483. ->will($this->returnValue(new ResponseQueued()));
  484. $txCallback = function ($tx) {
  485. $tx->ping();
  486. };
  487. $callable = $this->getMock('stdClass', array('__invoke'));
  488. $callable->expects($this->once())
  489. ->method('__invoke')
  490. ->will($this->returnCallback($txCallback));
  491. $client = new Client($connection);
  492. $client->multiExec($options, $callable);
  493. }
  494. /**
  495. * @group disconnected
  496. */
  497. public function testMonitorReturnsMonitorContext()
  498. {
  499. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  500. $client = new Client($connection);
  501. $this->assertInstanceOf('Predis\Monitor\MonitorContext', $monitor = $client->monitor());
  502. }
  503. /**
  504. * @group disconnected
  505. */
  506. public function testClientResendScriptedCommandUsingEvalOnNoScriptErrors()
  507. {
  508. $command = $this->getMockForAbstractClass('Predis\Command\ScriptedCommand');
  509. $command->expects($this->once())
  510. ->method('getScript')
  511. ->will($this->returnValue('return redis.call(\'exists\', KEYS[1])'));
  512. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  513. $connection->expects($this->at(0))
  514. ->method('executeCommand')
  515. ->with($command)
  516. ->will($this->returnValue(new ResponseError('NOSCRIPT')));
  517. $connection->expects($this->at(1))
  518. ->method('executeCommand')
  519. ->with($this->isInstanceOf('Predis\Command\ServerEval'))
  520. ->will($this->returnValue(true));
  521. $client = new Client($connection);
  522. $this->assertTrue($client->executeCommand($command));
  523. }
  524. // ******************************************************************** //
  525. // ---- HELPER METHODS ------------------------------------------------ //
  526. // ******************************************************************** //
  527. /**
  528. * Returns a named array with the default connection parameters and their values.
  529. *
  530. * @return Array Default connection parameters.
  531. */
  532. protected function getDefaultParametersArray()
  533. {
  534. return array(
  535. 'scheme' => 'tcp',
  536. 'host' => REDIS_SERVER_HOST,
  537. 'port' => REDIS_SERVER_PORT,
  538. 'database' => REDIS_SERVER_DBNUM,
  539. );
  540. }
  541. /**
  542. * Returns a named array with the default client options and their values.
  543. *
  544. * @return Array Default connection parameters.
  545. */
  546. protected function getDefaultOptionsArray()
  547. {
  548. return array(
  549. 'profile' => REDIS_SERVER_VERSION,
  550. );
  551. }
  552. /**
  553. * Returns a named array with the default connection parameters merged with
  554. * the specified additional parameters.
  555. *
  556. * @param Array $additional Additional connection parameters.
  557. * @return Array Connection parameters.
  558. */
  559. protected function getParametersArray(Array $additional)
  560. {
  561. return array_merge($this->getDefaultParametersArray(), $additional);
  562. }
  563. /**
  564. * Returns an URI string representation of the specified connection parameters.
  565. *
  566. * @param Array $parameters Array of connection parameters.
  567. * @return String URI string.
  568. */
  569. protected function getParametersString(Array $parameters)
  570. {
  571. $defaults = $this->getDefaultParametersArray();
  572. $scheme = isset($parameters['scheme']) ? $parameters['scheme'] : $defaults['scheme'];
  573. $host = isset($parameters['host']) ? $parameters['host'] : $defaults['host'];
  574. $port = isset($parameters['port']) ? $parameters['port'] : $defaults['port'];
  575. unset($parameters['scheme'], $parameters['host'], $parameters['port']);
  576. $uriString = "$scheme://$host:$port/?";
  577. foreach ($parameters as $k => $v) {
  578. $uriString .= "$k=$v&";
  579. }
  580. return $uriString;
  581. }
  582. }