ClientTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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. use Predis\Response;
  17. /**
  18. *
  19. */
  20. class ClientTest extends StandardTestCase
  21. {
  22. /**
  23. * @group disconnected
  24. */
  25. public function testConstructorWithoutArguments()
  26. {
  27. $client = new Client();
  28. $connection = $client->getConnection();
  29. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $connection);
  30. $parameters = $connection->getParameters();
  31. $this->assertSame($parameters->host, '127.0.0.1');
  32. $this->assertSame($parameters->port, 6379);
  33. $options = $client->getOptions();
  34. $this->assertSame($options->profile->getVersion(), ServerProfile::getDefault()->getVersion());
  35. $this->assertFalse($client->isConnected());
  36. }
  37. /**
  38. * @group disconnected
  39. */
  40. public function testConstructorWithNullArgument()
  41. {
  42. $client = new Client(null);
  43. $connection = $client->getConnection();
  44. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $connection);
  45. $parameters = $connection->getParameters();
  46. $this->assertSame($parameters->host, '127.0.0.1');
  47. $this->assertSame($parameters->port, 6379);
  48. $options = $client->getOptions();
  49. $this->assertSame($options->profile->getVersion(), ServerProfile::getDefault()->getVersion());
  50. $this->assertFalse($client->isConnected());
  51. }
  52. /**
  53. * @group disconnected
  54. */
  55. public function testConstructorWithNullAndNullArguments()
  56. {
  57. $client = new Client(null, null);
  58. $connection = $client->getConnection();
  59. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $connection);
  60. $parameters = $connection->getParameters();
  61. $this->assertSame($parameters->host, '127.0.0.1');
  62. $this->assertSame($parameters->port, 6379);
  63. $options = $client->getOptions();
  64. $this->assertSame($options->profile->getVersion(), ServerProfile::getDefault()->getVersion());
  65. $this->assertFalse($client->isConnected());
  66. }
  67. /**
  68. * @group disconnected
  69. */
  70. public function testConstructorWithArrayArgument()
  71. {
  72. $client = new Client($arg1 = array('host' => 'localhost', 'port' => 7000));
  73. $parameters = $client->getConnection()->getParameters();
  74. $this->assertSame($parameters->host, $arg1['host']);
  75. $this->assertSame($parameters->port, $arg1['port']);
  76. }
  77. /**
  78. * @group disconnected
  79. */
  80. public function testConstructorWithArrayOfArrayArgument()
  81. {
  82. $arg1 = array(
  83. array('host' => 'localhost', 'port' => 7000),
  84. array('host' => 'localhost', 'port' => 7001),
  85. );
  86. $client = new Client($arg1);
  87. $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $client->getConnection());
  88. }
  89. /**
  90. * @group disconnected
  91. */
  92. public function testConstructorWithStringArgument()
  93. {
  94. $client = new Client('tcp://localhost:7000');
  95. $parameters = $client->getConnection()->getParameters();
  96. $this->assertSame($parameters->host, 'localhost');
  97. $this->assertSame($parameters->port, 7000);
  98. }
  99. /**
  100. * @group disconnected
  101. */
  102. public function testConstructorWithArrayOfStringArgument()
  103. {
  104. $client = new Client($arg1 = array('tcp://localhost:7000', 'tcp://localhost:7001'));
  105. $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $client->getConnection());
  106. }
  107. /**
  108. * @group disconnected
  109. */
  110. public function testConstructorWithArrayOfConnectionsArgument()
  111. {
  112. $connection1 = $this->getMock('Predis\Connection\SingleConnectionInterface');
  113. $connection2 = $this->getMock('Predis\Connection\SingleConnectionInterface');
  114. $client = new Client(array($connection1, $connection2));
  115. $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $cluster = $client->getConnection());
  116. $this->assertSame($connection1, $cluster->getConnectionById(0));
  117. $this->assertSame($connection2, $cluster->getConnectionById(1));
  118. }
  119. /**
  120. * @group disconnected
  121. */
  122. public function testConstructorWithConnectionArgument()
  123. {
  124. $factory = new ConnectionFactory();
  125. $connection = $factory->create('tcp://localhost:7000');
  126. $client = new Client($connection);
  127. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $client->getConnection());
  128. $this->assertSame($connection, $client->getConnection());
  129. $parameters = $client->getConnection()->getParameters();
  130. $this->assertSame($parameters->host, 'localhost');
  131. $this->assertSame($parameters->port, 7000);
  132. }
  133. /**
  134. * @group disconnected
  135. */
  136. public function testConstructorWithClusterArgument()
  137. {
  138. $cluster = new PredisCluster();
  139. $factory = new ConnectionFactory();
  140. $factory->aggregate($cluster, array('tcp://localhost:7000', 'tcp://localhost:7001'));
  141. $client = new Client($cluster);
  142. $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $client->getConnection());
  143. $this->assertSame($cluster, $client->getConnection());
  144. }
  145. /**
  146. * @group disconnected
  147. */
  148. public function testConstructorWithReplicationArgument()
  149. {
  150. $replication = new MasterSlaveReplication();
  151. $factory = new ConnectionFactory();
  152. $factory->aggregate($replication, array('tcp://host1?alias=master', 'tcp://host2?alias=slave'));
  153. $client = new Client($replication);
  154. $this->assertInstanceOf('Predis\Connection\ReplicationConnectionInterface', $client->getConnection());
  155. $this->assertSame($replication, $client->getConnection());
  156. }
  157. /**
  158. * @group disconnected
  159. */
  160. public function testConstructorWithCallableArgument()
  161. {
  162. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  163. $callable = $this->getMock('stdClass', array('__invoke'));
  164. $callable->expects($this->once())
  165. ->method('__invoke')
  166. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'))
  167. ->will($this->returnValue($connection));
  168. $client = new Client($callable);
  169. $this->assertSame($connection, $client->getConnection());
  170. }
  171. /**
  172. * @group disconnected
  173. * @expectedException UnexpectedValueException
  174. * @expectedExceptionMessage The callable connection initializer returned an invalid type
  175. */
  176. public function testConstructorWithCallableConnectionInitializerThrowsExceptionOnInvalidReturnType()
  177. {
  178. $wrongType = $this->getMock('stdClass');
  179. $callable = $this->getMock('stdClass', array('__invoke'));
  180. $callable->expects($this->once())
  181. ->method('__invoke')
  182. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'))
  183. ->will($this->returnValue($wrongType));
  184. $client = new Client($callable);
  185. }
  186. /**
  187. * @group disconnected
  188. */
  189. public function testConstructorWithNullAndArrayArgument()
  190. {
  191. $factory = $this->getMock('Predis\Connection\ConnectionFactoryInterface');
  192. $arg2 = array('profile' => '2.0', 'prefix' => 'prefix:', 'connections' => $factory);
  193. $client = new Client(null, $arg2);
  194. $profile = $client->getProfile();
  195. $this->assertSame($profile->getVersion(), ServerProfile::get('2.0')->getVersion());
  196. $this->assertInstanceOf('Predis\Command\Processor\KeyPrefixProcessor', $profile->getProcessor());
  197. $this->assertSame('prefix:', $profile->getProcessor()->getPrefix());
  198. }
  199. /**
  200. * @group disconnected
  201. */
  202. public function testConstructorWithArrayAndOptionReplication()
  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 testConstructorWithArrayAndOptionAggregate()
  215. {
  216. $arg1 = array('tcp://host1', 'tcp://host2');
  217. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  218. $fnaggregate = $this->getMock('stdClass', array('__invoke'));
  219. $fnaggregate->expects($this->once())
  220. ->method('__invoke')
  221. ->with($arg1)
  222. ->will($this->returnValue($connection));
  223. $fncluster = $this->getMock('stdClass', array('__invoke'));
  224. $fncluster->expects($this->never())->method('__invoke');
  225. $fnreplication = $this->getMock('stdClass', array('__invoke'));
  226. $fnreplication->expects($this->never())->method('__invoke');
  227. $arg2 = array(
  228. 'aggregate' => function () use ($fnaggregate) { return $fnaggregate; },
  229. 'cluster' => function () use ($fncluster) { return $fncluster; },
  230. 'replication' => function () use ($fnreplication) { return $fncluster; },
  231. );
  232. $client = new Client($arg1, $arg2);
  233. $this->assertSame($connection, $client->getConnection());
  234. }
  235. /**
  236. * @group disconnected
  237. * @expectedException UnexpectedValueException
  238. * @expectedExceptionMessage The callable connection initializer returned an invalid type
  239. */
  240. public function testConstructorWithArrayAndOptionAggregateThrowsExceptionOnInvalidReturnType()
  241. {
  242. $arg1 = array('tcp://host1', 'tcp://host2');
  243. $fnaggregate = $this->getMock('stdClass', array('__invoke'));
  244. $fnaggregate->expects($this->once())
  245. ->method('__invoke')
  246. ->with($arg1)
  247. ->will($this->returnValue(false));
  248. $arg2 = array('aggregate' => function () use ($fnaggregate) { return $fnaggregate; });
  249. $client = new Client($arg1, $arg2);
  250. }
  251. /**
  252. * @group disconnected
  253. */
  254. public function testConnectAndDisconnect()
  255. {
  256. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  257. $connection->expects($this->once())->method('connect');
  258. $connection->expects($this->once())->method('disconnect');
  259. $client = new Client($connection);
  260. $client->connect();
  261. $client->disconnect();
  262. }
  263. /**
  264. * @group disconnected
  265. */
  266. public function testIsConnectedChecksConnectionState()
  267. {
  268. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  269. $connection->expects($this->once())->method('isConnected');
  270. $client = new Client($connection);
  271. $client->isConnected();
  272. }
  273. /**
  274. * @group disconnected
  275. */
  276. public function testQuitIsAliasForDisconnect()
  277. {
  278. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  279. $connection->expects($this->once())->method('disconnect');
  280. $client = new Client($connection);
  281. $client->quit();
  282. }
  283. /**
  284. * @group disconnected
  285. */
  286. public function testCreatesNewCommandUsingSpecifiedProfile()
  287. {
  288. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  289. $profile = $this->getMock('Predis\Profile\ServerProfileInterface');
  290. $profile->expects($this->once())
  291. ->method('createCommand')
  292. ->with('ping', array())
  293. ->will($this->returnValue($ping));
  294. $client = new Client(null, array('profile' => $profile));
  295. $this->assertSame($ping, $client->createCommand('ping', array()));
  296. }
  297. /**
  298. * @group disconnected
  299. */
  300. public function testExecuteCommandReturnsParsedReplies()
  301. {
  302. $profile = ServerProfile::getDefault();
  303. $ping = $profile->createCommand('ping', array());
  304. $hgetall = $profile->createCommand('hgetall', array('metavars', 'foo', 'hoge'));
  305. $connection= $this->getMock('Predis\Connection\ConnectionInterface');
  306. $connection->expects($this->at(0))
  307. ->method('executeCommand')
  308. ->with($ping)
  309. ->will($this->returnValue('PONG'));
  310. $connection->expects($this->at(1))
  311. ->method('executeCommand')
  312. ->with($hgetall)
  313. ->will($this->returnValue(array('foo', 'bar', 'hoge', 'piyo')));
  314. $client = new Client($connection);
  315. $this->assertTrue($client->executeCommand($ping));
  316. $this->assertSame(array('foo' => 'bar', 'hoge' => 'piyo'), $client->executeCommand($hgetall));
  317. }
  318. /**
  319. * @group disconnected
  320. * @expectedException Predis\Response\ServerException
  321. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  322. */
  323. public function testExecuteCommandThrowsExceptionOnRedisError()
  324. {
  325. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  326. $expectedResponse = new Response\Error('ERR Operation against a key holding the wrong kind of value');
  327. $connection= $this->getMock('Predis\Connection\ConnectionInterface');
  328. $connection->expects($this->once())
  329. ->method('executeCommand')
  330. ->will($this->returnValue($expectedResponse));
  331. $client = new Client($connection);
  332. $client->executeCommand($ping);
  333. }
  334. /**
  335. * @group disconnected
  336. */
  337. public function testExecuteCommandReturnsErrorResponseOnRedisError()
  338. {
  339. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  340. $expectedResponse = new Response\Error('ERR Operation against a key holding the wrong kind of value');
  341. $connection= $this->getMock('Predis\Connection\ConnectionInterface');
  342. $connection->expects($this->once())
  343. ->method('executeCommand')
  344. ->will($this->returnValue($expectedResponse));
  345. $client = new Client($connection, array('exceptions' => false));
  346. $response = $client->executeCommand($ping);
  347. $this->assertSame($response, $expectedResponse);
  348. }
  349. /**
  350. * @group disconnected
  351. */
  352. public function testCallingRedisCommandExecutesInstanceOfCommand()
  353. {
  354. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  355. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  356. $connection->expects($this->once())
  357. ->method('executeCommand')
  358. ->with($this->isInstanceOf('Predis\Command\ConnectionPing'))
  359. ->will($this->returnValue('PONG'));
  360. $profile = $this->getMock('Predis\Profile\ServerProfileInterface');
  361. $profile->expects($this->once())
  362. ->method('createCommand')
  363. ->with('ping', array())
  364. ->will($this->returnValue($ping));
  365. $options = array('profile' => $profile);
  366. $client = $this->getMock('Predis\Client', null, array($connection, $options));
  367. $this->assertTrue($client->ping());
  368. }
  369. /**
  370. * @group disconnected
  371. * @expectedException Predis\Response\ServerException
  372. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  373. */
  374. public function testCallingRedisCommandThrowsExceptionOnServerError()
  375. {
  376. $expectedResponse = new Response\Error('ERR Operation against a key holding the wrong kind of value');
  377. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  378. $connection->expects($this->once())
  379. ->method('executeCommand')
  380. ->with($this->isInstanceOf('Predis\Command\ConnectionPing'))
  381. ->will($this->returnValue($expectedResponse));
  382. $client = new Client($connection);
  383. $client->ping();
  384. }
  385. /**
  386. * @group disconnected
  387. */
  388. public function testCallingRedisCommandReturnsErrorResponseOnRedisError()
  389. {
  390. $expectedResponse = new Response\Error('ERR Operation against a key holding the wrong kind of value');
  391. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  392. $connection->expects($this->once())
  393. ->method('executeCommand')
  394. ->with($this->isInstanceOf('Predis\Command\ConnectionPing'))
  395. ->will($this->returnValue($expectedResponse));
  396. $client = new Client($connection, array('exceptions' => false));
  397. $response = $client->ping();
  398. $this->assertSame($response, $expectedResponse);
  399. }
  400. /**
  401. * @group disconnected
  402. * @expectedException Predis\ClientException
  403. * @expectedExceptionMessage 'invalidcommand' is not a registered Redis command
  404. */
  405. public function testThrowsExceptionOnNonRegisteredRedisCommand()
  406. {
  407. $client = new Client();
  408. $client->invalidCommand();
  409. }
  410. /**
  411. * @group disconnected
  412. */
  413. public function testGetConnectionFromAggregatedConnectionWithAlias()
  414. {
  415. $client = new Client(array('tcp://host1?alias=node01', 'tcp://host2?alias=node02'));
  416. $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $cluster = $client->getConnection());
  417. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node01 = $client->getConnectionById('node01'));
  418. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node02 = $client->getConnectionById('node02'));
  419. $this->assertSame('host1', $node01->getParameters()->host);
  420. $this->assertSame('host2', $node02->getParameters()->host);
  421. }
  422. /**
  423. * @group disconnected
  424. * @expectedException Predis\NotSupportedException
  425. * @expectedExceptionMessage Retrieving connections by ID is supported only when using aggregated connections
  426. */
  427. public function testGetConnectionByIdWorksOnlyWithAggregatedConnections()
  428. {
  429. $client = new Client();
  430. $client->getConnectionById('node01');
  431. }
  432. /**
  433. * @group disconnected
  434. */
  435. public function testCreateClientWithConnectionFromAggregatedConnection()
  436. {
  437. $client = new Client(array('tcp://host1?alias=node01', 'tcp://host2?alias=node02'), array('prefix' => 'pfx:'));
  438. $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $cluster = $client->getConnection());
  439. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node01 = $client->getConnectionById('node01'));
  440. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node02 = $client->getConnectionById('node02'));
  441. $clientNode02 = $client->getClientFor('node02');
  442. $this->assertInstanceOf('Predis\Client', $clientNode02);
  443. $this->assertSame($node02, $clientNode02->getConnection());
  444. $this->assertSame($client->getOptions(), $clientNode02->getOptions());
  445. }
  446. /**
  447. * @group disconnected
  448. */
  449. public function testGetClientForReturnsInstanceOfSubclass()
  450. {
  451. $nodes = array('tcp://host1?alias=node01', 'tcp://host2?alias=node02');
  452. $client = $this->getMock('Predis\Client', array('dummy'), array($nodes), 'SubclassedClient');
  453. $this->assertInstanceOf('SubclassedClient', $client->getClientFor('node02'));
  454. }
  455. /**
  456. * @group disconnected
  457. */
  458. public function testPipelineWithoutArgumentsReturnsPipeline()
  459. {
  460. $client = new Client();
  461. $this->assertInstanceOf('Predis\Pipeline\Pipeline', $client->pipeline());
  462. }
  463. /**
  464. * @group disconnected
  465. */
  466. public function testPipelineWithArrayReturnsPipeline()
  467. {
  468. $client = new Client();
  469. $this->assertInstanceOf('Predis\Pipeline\Pipeline', $client->pipeline(array()));
  470. $this->assertInstanceOf('Predis\Pipeline\Atomic', $client->pipeline(array('atomic' => true)));
  471. $this->assertInstanceOf('Predis\Pipeline\FireAndForget', $client->pipeline(array('fire-and-forget' => true)));
  472. }
  473. /**
  474. * @group disconnected
  475. */
  476. public function testPipelineWithCallableExecutesPipeline()
  477. {
  478. $callable = $this->getMock('stdClass', array('__invoke'));
  479. $callable->expects($this->once())
  480. ->method('__invoke')
  481. ->with($this->isInstanceOf('Predis\Pipeline\Pipeline'));
  482. $client = new Client();
  483. $client->pipeline($callable);
  484. }
  485. /**
  486. * @group disconnected
  487. */
  488. public function testPubSubLoopWithoutArgumentsReturnsPubSubConsumer()
  489. {
  490. $client = new Client();
  491. $this->assertInstanceOf('Predis\PubSub\Consumer', $client->pubSubLoop());
  492. }
  493. /**
  494. * @group disconnected
  495. */
  496. public function testPubSubLoopWithArrayReturnsPubSubConsumerWithOptions()
  497. {
  498. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  499. $options = array('subscribe' => 'channel');
  500. $client = new Client($connection);
  501. $this->assertInstanceOf('Predis\PubSub\Consumer', $pubsub = $client->pubSubLoop($options));
  502. $reflection = new \ReflectionProperty($pubsub, 'options');
  503. $reflection->setAccessible(true);
  504. $this->assertSame($options, $reflection->getValue($pubsub));
  505. }
  506. /**
  507. * @group disconnected
  508. */
  509. public function testPubSubLoopWithArrayAndCallableExecutesPubSub()
  510. {
  511. // NOTE: we use a subscribe count of 0 in the fake message to trick
  512. // the context and to make it think that it can be closed
  513. // since there are no more subscriptions active.
  514. $message = array('subscribe', 'channel', 0);
  515. $options = array('subscribe' => 'channel');
  516. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  517. $connection->expects($this->once())
  518. ->method('read')
  519. ->will($this->returnValue($message));
  520. $callable = $this->getMock('stdClass', array('__invoke'));
  521. $callable->expects($this->once())
  522. ->method('__invoke');
  523. $client = new Client($connection);
  524. $client->pubSubLoop($options, $callable);
  525. }
  526. /**
  527. * @group disconnected
  528. */
  529. public function testTransactionWithoutArgumentsReturnsMultiExec()
  530. {
  531. $client = new Client();
  532. $this->assertInstanceOf('Predis\Transaction\MultiExec', $client->transaction());
  533. }
  534. /**
  535. * @group disconnected
  536. * @todo I hate this test but reflection is the easiest way in this case.
  537. */
  538. public function testTransactionWithArrayReturnsTransactionMultiExecWithOptions()
  539. {
  540. $options = array('cas' => true, 'retry' => 3);
  541. $client = new Client();
  542. $this->assertInstanceOf('Predis\Transaction\MultiExec', $tx = $client->transaction($options));
  543. $property = new \ReflectionProperty($tx, 'modeCAS');
  544. $property->setAccessible(true);
  545. $this->assertSame($options['cas'], $property->getValue($tx));
  546. $property = new \ReflectionProperty($tx, 'attempts');
  547. $property->setAccessible(true);
  548. $this->assertSame($options['retry'], $property->getValue($tx));
  549. }
  550. /**
  551. * @group disconnected
  552. */
  553. public function testTransactionWithArrayAndCallableExecutesMultiExec()
  554. {
  555. // NOTE: we use CAS since testing the actual MULTI/EXEC context
  556. // here is not the point.
  557. $options = array('cas' => true, 'retry' => 3);
  558. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  559. $connection->expects($this->once())
  560. ->method('executeCommand')
  561. ->will($this->returnValue(new Response\StatusQueued()));
  562. $txCallback = function ($tx) {
  563. $tx->ping();
  564. };
  565. $callable = $this->getMock('stdClass', array('__invoke'));
  566. $callable->expects($this->once())
  567. ->method('__invoke')
  568. ->will($this->returnCallback($txCallback));
  569. $client = new Client($connection);
  570. $client->transaction($options, $callable);
  571. }
  572. /**
  573. * @group disconnected
  574. */
  575. public function testMonitorReturnsMonitorConsumer()
  576. {
  577. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  578. $client = new Client($connection);
  579. $this->assertInstanceOf('Predis\Monitor\Consumer', $monitor = $client->monitor());
  580. }
  581. /**
  582. * @group disconnected
  583. */
  584. public function testClientResendScriptedCommandUsingEvalOnNoScriptErrors()
  585. {
  586. $command = $this->getMockForAbstractClass('Predis\Command\ScriptedCommand', array(), '', true, true, true, array('parseResponse'));
  587. $command->expects($this->once())
  588. ->method('getScript')
  589. ->will($this->returnValue('return redis.call(\'exists\', KEYS[1])'));
  590. $command->expects($this->once())
  591. ->method('parseResponse')
  592. ->with('OK')
  593. ->will($this->returnValue(true));
  594. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  595. $connection->expects($this->at(0))
  596. ->method('executeCommand')
  597. ->with($command)
  598. ->will($this->returnValue(new Response\Error('NOSCRIPT')));
  599. $connection->expects($this->at(1))
  600. ->method('executeCommand')
  601. ->with($this->isInstanceOf('Predis\Command\ServerEval'))
  602. ->will($this->returnValue('OK'));
  603. $client = new Client($connection);
  604. $this->assertTrue($client->executeCommand($command));
  605. }
  606. // ******************************************************************** //
  607. // ---- HELPER METHODS ------------------------------------------------ //
  608. // ******************************************************************** //
  609. /**
  610. * Returns a named array with the default connection parameters and their values.
  611. *
  612. * @return array Default connection parameters.
  613. */
  614. protected function getDefaultParametersArray()
  615. {
  616. return array(
  617. 'scheme' => 'tcp',
  618. 'host' => REDIS_SERVER_HOST,
  619. 'port' => REDIS_SERVER_PORT,
  620. 'database' => REDIS_SERVER_DBNUM,
  621. );
  622. }
  623. /**
  624. * Returns a named array with the default client options and their values.
  625. *
  626. * @return array Default connection parameters.
  627. */
  628. protected function getDefaultOptionsArray()
  629. {
  630. return array(
  631. 'profile' => REDIS_SERVER_VERSION,
  632. );
  633. }
  634. /**
  635. * Returns a named array with the default connection parameters merged with
  636. * the specified additional parameters.
  637. *
  638. * @param array $additional Additional connection parameters.
  639. * @return array Connection parameters.
  640. */
  641. protected function getParametersArray(array $additional)
  642. {
  643. return array_merge($this->getDefaultParametersArray(), $additional);
  644. }
  645. /**
  646. * Returns an URI string representation of the specified connection parameters.
  647. *
  648. * @param array $parameters Array of connection parameters.
  649. * @return String URI string.
  650. */
  651. protected function getParametersString(array $parameters)
  652. {
  653. $defaults = $this->getDefaultParametersArray();
  654. $scheme = isset($parameters['scheme']) ? $parameters['scheme'] : $defaults['scheme'];
  655. $host = isset($parameters['host']) ? $parameters['host'] : $defaults['host'];
  656. $port = isset($parameters['port']) ? $parameters['port'] : $defaults['port'];
  657. unset($parameters['scheme'], $parameters['host'], $parameters['port']);
  658. $uriString = "$scheme://$host:$port/?";
  659. foreach ($parameters as $k => $v) {
  660. $uriString .= "$k=$v&";
  661. }
  662. return $uriString;
  663. }
  664. }