ClientTest.php 27 KB

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