ClientTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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 testExecuteCommandReturnsParsedReplies()
  232. {
  233. $profile = ServerProfile::getDefault();
  234. $ping = $profile->createCommand('ping', array());
  235. $hgetall = $profile->createCommand('hgetall', array('metavars', 'foo', 'hoge'));
  236. $connection= $this->getMock('Predis\Connection\ConnectionInterface');
  237. $connection->expects($this->at(0))
  238. ->method('executeCommand')
  239. ->with($ping)
  240. ->will($this->returnValue('PONG'));
  241. $connection->expects($this->at(1))
  242. ->method('executeCommand')
  243. ->with($hgetall)
  244. ->will($this->returnValue(array('foo', 'bar', 'hoge', 'piyo')));
  245. $client = new Client($connection);
  246. $this->assertTrue($client->executeCommand($ping));
  247. $this->assertSame(array('foo' => 'bar', 'hoge' => 'piyo'), $client->executeCommand($hgetall));
  248. }
  249. /**
  250. * @group disconnected
  251. * @expectedException Predis\ServerException
  252. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  253. */
  254. public function testExecuteCommandThrowsExceptionOnRedisError()
  255. {
  256. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  257. $expectedResponse = new ResponseError('ERR Operation against a key holding the wrong kind of value');
  258. $connection= $this->getMock('Predis\Connection\ConnectionInterface');
  259. $connection->expects($this->once())
  260. ->method('executeCommand')
  261. ->will($this->returnValue($expectedResponse));
  262. $client = new Client($connection);
  263. $client->executeCommand($ping);
  264. }
  265. /**
  266. * @group disconnected
  267. */
  268. public function testExecuteCommandReturnsErrorResponseOnRedisError()
  269. {
  270. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  271. $expectedResponse = new ResponseError('ERR Operation against a key holding the wrong kind of value');
  272. $connection= $this->getMock('Predis\Connection\ConnectionInterface');
  273. $connection->expects($this->once())
  274. ->method('executeCommand')
  275. ->will($this->returnValue($expectedResponse));
  276. $client = new Client($connection, array('exceptions' => false));
  277. $response = $client->executeCommand($ping);
  278. $this->assertSame($response, $expectedResponse);
  279. }
  280. /**
  281. * @group disconnected
  282. */
  283. public function testCallingRedisCommandExecutesInstanceOfCommand()
  284. {
  285. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  286. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  287. $connection->expects($this->once())
  288. ->method('executeCommand')
  289. ->with($this->isInstanceOf('Predis\Command\ConnectionPing'))
  290. ->will($this->returnValue('PONG'));
  291. $profile = $this->getMock('Predis\Profile\ServerProfileInterface');
  292. $profile->expects($this->once())
  293. ->method('createCommand')
  294. ->with('ping', array())
  295. ->will($this->returnValue($ping));
  296. $options = array('profile' => $profile);
  297. $client = $this->getMock('Predis\Client', array('createCommand'), array($connection, $options));
  298. $this->assertTrue($client->ping());
  299. }
  300. /**
  301. * @group disconnected
  302. * @expectedException Predis\ServerException
  303. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  304. */
  305. public function testCallingRedisCommandThrowsExceptionOnServerError()
  306. {
  307. $expectedResponse = new ResponseError('ERR Operation against a key holding the wrong kind of value');
  308. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  309. $connection->expects($this->once())
  310. ->method('executeCommand')
  311. ->with($this->isInstanceOf('Predis\Command\ConnectionPing'))
  312. ->will($this->returnValue($expectedResponse));
  313. $client = new Client($connection);
  314. $client->ping();
  315. }
  316. /**
  317. * @group disconnected
  318. */
  319. public function testCallingRedisCommandReturnsErrorResponseOnRedisError()
  320. {
  321. $expectedResponse = new ResponseError('ERR Operation against a key holding the wrong kind of value');
  322. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  323. $connection->expects($this->once())
  324. ->method('executeCommand')
  325. ->with($this->isInstanceOf('Predis\Command\ConnectionPing'))
  326. ->will($this->returnValue($expectedResponse));
  327. $client = new Client($connection, array('exceptions' => false));
  328. $response = $client->ping();
  329. $this->assertSame($response, $expectedResponse);
  330. }
  331. /**
  332. * @group disconnected
  333. * @expectedException Predis\ClientException
  334. * @expectedExceptionMessage 'invalidcommand' is not a registered Redis command
  335. */
  336. public function testThrowsExceptionOnNonRegisteredRedisCommand()
  337. {
  338. $client = new Client();
  339. $client->invalidCommand();
  340. }
  341. /**
  342. * @group disconnected
  343. */
  344. public function testGetConnectionFromAggregatedConnectionWithAlias()
  345. {
  346. $client = new Client(array('tcp://host1?alias=node01', 'tcp://host2?alias=node02'));
  347. $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $cluster = $client->getConnection());
  348. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node01 = $client->getConnectionById('node01'));
  349. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node02 = $client->getConnectionById('node02'));
  350. $this->assertSame('host1', $node01->getParameters()->host);
  351. $this->assertSame('host2', $node02->getParameters()->host);
  352. }
  353. /**
  354. * @group disconnected
  355. * @expectedException Predis\NotSupportedException
  356. * @expectedExceptionMessage Retrieving connections by ID is supported only when using aggregated connections
  357. */
  358. public function testGetConnectionByIdWorksOnlyWithAggregatedConnections()
  359. {
  360. $client = new Client();
  361. $client->getConnectionById('node01');
  362. }
  363. /**
  364. * @group disconnected
  365. */
  366. public function testCreateClientWithConnectionFromAggregatedConnection()
  367. {
  368. $client = new Client(array('tcp://host1?alias=node01', 'tcp://host2?alias=node02'), array('prefix' => 'pfx:'));
  369. $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $cluster = $client->getConnection());
  370. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node01 = $client->getConnectionById('node01'));
  371. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node02 = $client->getConnectionById('node02'));
  372. $clientNode02 = $client->getClientFor('node02');
  373. $this->assertSame($node02, $clientNode02->getConnection());
  374. $this->assertSame($client->getOptions(), $clientNode02->getOptions());
  375. }
  376. /**
  377. * @group disconnected
  378. */
  379. public function testPipelineWithoutArgumentsReturnsPipelineContext()
  380. {
  381. $client = new Client();
  382. $this->assertInstanceOf('Predis\Pipeline\PipelineContext', $pipeline = $client->pipeline());
  383. }
  384. /**
  385. * @group disconnected
  386. */
  387. public function testPipelineWithArrayReturnsPipelineContextWithOptions()
  388. {
  389. $client = new Client();
  390. $executor = $this->getMock('Predis\Pipeline\PipelineExecutorInterface');
  391. $options = array('executor' => $executor);
  392. $this->assertInstanceOf('Predis\Pipeline\PipelineContext', $pipeline = $client->pipeline($options));
  393. $this->assertSame($executor, $pipeline->getExecutor());
  394. $options = array('executor' => function ($client, $options) use ($executor) { return $executor; });
  395. $this->assertInstanceOf('Predis\Pipeline\PipelineContext', $pipeline = $client->pipeline($options));
  396. $this->assertSame($executor, $pipeline->getExecutor());
  397. }
  398. /**
  399. * @group disconnected
  400. */
  401. public function testPipelineWithCallableExecutesPipeline()
  402. {
  403. $callable = $this->getMock('stdClass', array('__invoke'));
  404. $callable->expects($this->once())
  405. ->method('__invoke')
  406. ->with($this->isInstanceOf('Predis\Pipeline\PipelineContext'));
  407. $client = new Client();
  408. $client->pipeline($callable);
  409. }
  410. /**
  411. * @group disconnected
  412. */
  413. public function testPipelineWithArrayAndCallableExecutesPipelineWithOptions()
  414. {
  415. $executor = $this->getMock('Predis\Pipeline\PipelineExecutorInterface');
  416. $options = array('executor' => $executor);
  417. $test = $this;
  418. $mockCallback = function ($pipeline) use ($executor, $test) {
  419. $reflection = new \ReflectionProperty($pipeline, 'executor');
  420. $reflection->setAccessible(true);
  421. $test->assertSame($executor, $reflection->getValue($pipeline));
  422. };
  423. $callable = $this->getMock('stdClass', array('__invoke'));
  424. $callable->expects($this->once())
  425. ->method('__invoke')
  426. ->with($this->isInstanceOf('Predis\Pipeline\PipelineContext'))
  427. ->will($this->returnCallback($mockCallback));
  428. $client = new Client();
  429. $client->pipeline($options, $callable);
  430. }
  431. /**
  432. * @group disconnected
  433. */
  434. public function testPubSubWithoutArgumentsReturnsPubSubContext()
  435. {
  436. $client = new Client();
  437. $this->assertInstanceOf('Predis\PubSub\PubSubContext', $pubsub = $client->pubSub());
  438. }
  439. /**
  440. * @group disconnected
  441. */
  442. public function testPubSubWithArrayReturnsPubSubContextWithOptions()
  443. {
  444. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  445. $options = array('subscribe' => 'channel');
  446. $client = new Client($connection);
  447. $this->assertInstanceOf('Predis\PubSub\PubSubContext', $pubsub = $client->pubSub($options));
  448. $reflection = new \ReflectionProperty($pubsub, 'options');
  449. $reflection->setAccessible(true);
  450. $this->assertSame($options, $reflection->getValue($pubsub));
  451. }
  452. /**
  453. * @group disconnected
  454. */
  455. public function testPubSubWithArrayAndCallableExecutesPubSub()
  456. {
  457. // NOTE: we use a subscribe count of 0 in the fake message to trick
  458. // the context and to make it think that it can be closed
  459. // since there are no more subscriptions active.
  460. $message = array('subscribe', 'channel', 0);
  461. $options = array('subscribe' => 'channel');
  462. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  463. $connection->expects($this->once())
  464. ->method('read')
  465. ->will($this->returnValue($message));
  466. $callable = $this->getMock('stdClass', array('__invoke'));
  467. $callable->expects($this->once())
  468. ->method('__invoke');
  469. $client = new Client($connection);
  470. $client->pubSub($options, $callable);
  471. }
  472. /**
  473. * @group disconnected
  474. */
  475. public function testMultiExecWithoutArgumentsReturnsMultiExecContext()
  476. {
  477. $client = new Client();
  478. $this->assertInstanceOf('Predis\Transaction\MultiExecContext', $pubsub = $client->multiExec());
  479. }
  480. /**
  481. * @group disconnected
  482. */
  483. public function testMultiExecWithArrayReturnsMultiExecContextWithOptions()
  484. {
  485. $options = array('cas' => true, 'retry' => 3);
  486. $client = new Client();
  487. $this->assertInstanceOf('Predis\Transaction\MultiExecContext', $tx = $client->multiExec($options));
  488. $reflection = new \ReflectionProperty($tx, 'options');
  489. $reflection->setAccessible(true);
  490. $this->assertSame($options, $reflection->getValue($tx));
  491. }
  492. /**
  493. * @group disconnected
  494. */
  495. public function testMultiExecWithArrayAndCallableExecutesMultiExec()
  496. {
  497. // NOTE: we use CAS since testing the actual MULTI/EXEC context
  498. // here is not the point.
  499. $options = array('cas' => true, 'retry' => 3);
  500. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  501. $connection->expects($this->once())
  502. ->method('executeCommand')
  503. ->will($this->returnValue(new ResponseQueued()));
  504. $txCallback = function ($tx) {
  505. $tx->ping();
  506. };
  507. $callable = $this->getMock('stdClass', array('__invoke'));
  508. $callable->expects($this->once())
  509. ->method('__invoke')
  510. ->will($this->returnCallback($txCallback));
  511. $client = new Client($connection);
  512. $client->multiExec($options, $callable);
  513. }
  514. /**
  515. * @group disconnected
  516. */
  517. public function testMonitorReturnsMonitorContext()
  518. {
  519. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  520. $client = new Client($connection);
  521. $this->assertInstanceOf('Predis\Monitor\MonitorContext', $monitor = $client->monitor());
  522. }
  523. /**
  524. * @group disconnected
  525. */
  526. public function testClientResendScriptedCommandUsingEvalOnNoScriptErrors()
  527. {
  528. $command = $this->getMockForAbstractClass('Predis\Command\ScriptedCommand', array(), '', true, true, true, array('parseResponse'));
  529. $command->expects($this->once())
  530. ->method('getScript')
  531. ->will($this->returnValue('return redis.call(\'exists\', KEYS[1])'));
  532. $command->expects($this->once())
  533. ->method('parseResponse')
  534. ->with('OK')
  535. ->will($this->returnValue(true));
  536. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  537. $connection->expects($this->at(0))
  538. ->method('executeCommand')
  539. ->with($command)
  540. ->will($this->returnValue(new ResponseError('NOSCRIPT')));
  541. $connection->expects($this->at(1))
  542. ->method('executeCommand')
  543. ->with($this->isInstanceOf('Predis\Command\ServerEval'))
  544. ->will($this->returnValue('OK'));
  545. $client = new Client($connection);
  546. $this->assertTrue($client->executeCommand($command));
  547. }
  548. // ******************************************************************** //
  549. // ---- HELPER METHODS ------------------------------------------------ //
  550. // ******************************************************************** //
  551. /**
  552. * Returns a named array with the default connection parameters and their values.
  553. *
  554. * @return Array Default connection parameters.
  555. */
  556. protected function getDefaultParametersArray()
  557. {
  558. return array(
  559. 'scheme' => 'tcp',
  560. 'host' => REDIS_SERVER_HOST,
  561. 'port' => REDIS_SERVER_PORT,
  562. 'database' => REDIS_SERVER_DBNUM,
  563. );
  564. }
  565. /**
  566. * Returns a named array with the default client options and their values.
  567. *
  568. * @return Array Default connection parameters.
  569. */
  570. protected function getDefaultOptionsArray()
  571. {
  572. return array(
  573. 'profile' => REDIS_SERVER_VERSION,
  574. );
  575. }
  576. /**
  577. * Returns a named array with the default connection parameters merged with
  578. * the specified additional parameters.
  579. *
  580. * @param Array $additional Additional connection parameters.
  581. * @return Array Connection parameters.
  582. */
  583. protected function getParametersArray(Array $additional)
  584. {
  585. return array_merge($this->getDefaultParametersArray(), $additional);
  586. }
  587. /**
  588. * Returns an URI string representation of the specified connection parameters.
  589. *
  590. * @param Array $parameters Array of connection parameters.
  591. * @return String URI string.
  592. */
  593. protected function getParametersString(Array $parameters)
  594. {
  595. $defaults = $this->getDefaultParametersArray();
  596. $scheme = isset($parameters['scheme']) ? $parameters['scheme'] : $defaults['scheme'];
  597. $host = isset($parameters['host']) ? $parameters['host'] : $defaults['host'];
  598. $port = isset($parameters['port']) ? $parameters['port'] : $defaults['port'];
  599. unset($parameters['scheme'], $parameters['host'], $parameters['port']);
  600. $uriString = "$scheme://$host:$port/?";
  601. foreach ($parameters as $k => $v) {
  602. $uriString .= "$k=$v&";
  603. }
  604. return $uriString;
  605. }
  606. }