ClientTest.php 26 KB

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