ClientTest.php 31 KB

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