ClientTest.php 26 KB

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