ClientTest.php 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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. * @expectedException \InvalidArgumentException
  75. * @expectedExceptionMessage Array of connection parameters requires `cluster`, `replication` or `aggregate` client option
  76. */
  77. public function testConstructorThrowsExceptionWithArrayOfParametersArgumentAndMissingOption()
  78. {
  79. $arg1 = array(
  80. array('host' => 'localhost', 'port' => 7000),
  81. array('host' => 'localhost', 'port' => 7001),
  82. );
  83. $client = new Client($arg1);
  84. }
  85. /**
  86. * @group disconnected
  87. */
  88. public function testConstructorWithArrayOfArrayArgumentAndClusterOption()
  89. {
  90. $arg1 = array(
  91. array('host' => 'localhost', 'port' => 7000),
  92. array('host' => 'localhost', 'port' => 7001),
  93. );
  94. $client = new Client($arg1, array(
  95. 'aggregate' => $this->getAggregateInitializer($arg1),
  96. ));
  97. $this->assertInstanceOf('Predis\Connection\AggregateConnectionInterface', $client->getConnection());
  98. }
  99. /**
  100. * @group disconnected
  101. */
  102. public function testConstructorWithStringArgument()
  103. {
  104. $client = new Client('tcp://localhost:7000');
  105. $parameters = $client->getConnection()->getParameters();
  106. $this->assertSame($parameters->host, 'localhost');
  107. $this->assertSame($parameters->port, 7000);
  108. }
  109. /**
  110. * @group disconnected
  111. */
  112. public function testConstructorWithArrayOfStringArgument()
  113. {
  114. $arg1 = array('tcp://localhost:7000', 'tcp://localhost:7001');
  115. $client = new Client($arg1, array(
  116. 'aggregate' => $this->getAggregateInitializer($arg1),
  117. ));
  118. $this->assertInstanceOf('Predis\Connection\AggregateConnectionInterface', $client->getConnection());
  119. }
  120. /**
  121. * @group disconnected
  122. */
  123. public function testConstructorWithArrayOfConnectionsArgument()
  124. {
  125. $arg1 = array(
  126. $this->getMock('Predis\Connection\NodeConnectionInterface'),
  127. $this->getMock('Predis\Connection\NodeConnectionInterface'),
  128. );
  129. $client = new Client($arg1, array(
  130. 'aggregate' => $this->getAggregateInitializer($arg1),
  131. ));
  132. $this->assertInstanceOf('Predis\Connection\AggregateConnectionInterface', $client->getConnection());
  133. }
  134. /**
  135. * @group disconnected
  136. */
  137. public function testConstructorWithConnectionArgument()
  138. {
  139. $factory = new Connection\Factory();
  140. $connection = $factory->create('tcp://localhost:7000');
  141. $client = new Client($connection);
  142. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $client->getConnection());
  143. $this->assertSame($connection, $client->getConnection());
  144. $parameters = $client->getConnection()->getParameters();
  145. $this->assertSame($parameters->host, 'localhost');
  146. $this->assertSame($parameters->port, 7000);
  147. }
  148. /**
  149. * @group disconnected
  150. */
  151. public function testConstructorWithClusterArgument()
  152. {
  153. $cluster = new Connection\Cluster\PredisCluster();
  154. $factory = new Connection\Factory();
  155. $factory->aggregate($cluster, array('tcp://localhost:7000', 'tcp://localhost:7001'));
  156. $client = new Client($cluster);
  157. $this->assertInstanceOf('Predis\Connection\Cluster\ClusterInterface', $client->getConnection());
  158. $this->assertSame($cluster, $client->getConnection());
  159. }
  160. /**
  161. * @group disconnected
  162. */
  163. public function testConstructorWithReplicationArgument()
  164. {
  165. $replication = new Connection\Replication\MasterSlaveReplication();
  166. $factory = new Connection\Factory();
  167. $factory->aggregate($replication, array('tcp://host1?alias=master', 'tcp://host2?alias=slave'));
  168. $client = new Client($replication);
  169. $this->assertInstanceOf('Predis\Connection\Replication\ReplicationInterface', $client->getConnection());
  170. $this->assertSame($replication, $client->getConnection());
  171. }
  172. /**
  173. * @group disconnected
  174. */
  175. public function testConstructorWithCallableArgument()
  176. {
  177. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  178. $callable = $this->getMock('stdClass', array('__invoke'));
  179. $callable
  180. ->expects($this->once())
  181. ->method('__invoke')
  182. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'))
  183. ->will($this->returnValue($connection));
  184. $client = new Client($callable);
  185. $this->assertSame($connection, $client->getConnection());
  186. }
  187. /**
  188. * @group disconnected
  189. * @expectedException \InvalidArgumentException
  190. * @expectedExceptionMessage Callable parameters must return a valid connection
  191. */
  192. public function testConstructorWithCallableConnectionInitializerThrowsExceptionOnInvalidReturnType()
  193. {
  194. $wrongType = $this->getMock('stdClass');
  195. $callable = $this->getMock('stdClass', array('__invoke'));
  196. $callable
  197. ->expects($this->once())
  198. ->method('__invoke')
  199. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'))
  200. ->will($this->returnValue($wrongType));
  201. new Client($callable);
  202. }
  203. /**
  204. * @group disconnected
  205. */
  206. public function testConstructorWithNullAndArrayArgument()
  207. {
  208. $connections = $this->getMock('Predis\Connection\FactoryInterface');
  209. $arg2 = array('prefix' => 'prefix:', 'connections' => $connections);
  210. $client = new Client(null, $arg2);
  211. $this->assertInstanceOf('Predis\Command\FactoryInterface', $commands = $client->getCommandFactory());
  212. $this->assertInstanceOf('Predis\Command\Processor\KeyPrefixProcessor', $commands->getProcessor());
  213. $this->assertSame('prefix:', $commands->getProcessor()->getPrefix());
  214. }
  215. /**
  216. * @group disconnected
  217. */
  218. public function testConstructorWithArrayAndOptionReplication()
  219. {
  220. $arg1 = array('tcp://host1?alias=master', 'tcp://host2?alias=slave');
  221. $arg2 = array('replication' => 'predis');
  222. $client = new Client($arg1, $arg2);
  223. $this->assertInstanceOf('Predis\Connection\Replication\ReplicationInterface', $connection = $client->getConnection());
  224. $this->assertSame('host1', $connection->getConnectionById('master')->getParameters()->host);
  225. $this->assertSame('host2', $connection->getConnectionById('slave')->getParameters()->host);
  226. }
  227. /**
  228. * @group disconnected
  229. */
  230. public function testClusterOptionHasPrecedenceOverReplicationOptionAndAggregateOption()
  231. {
  232. $arg1 = array('tcp://host1', 'tcp://host2');
  233. $connection = $this->getMock('Predis\Connection\AggregateConnectionInterface');
  234. $fncluster = $this->getMock('stdClass', array('__invoke'));
  235. $fncluster
  236. ->expects($this->once())
  237. ->method('__invoke')
  238. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'), $arg1)
  239. ->will($this->returnValue($connection));
  240. $fnreplication = $this->getMock('stdClass', array('__invoke'));
  241. $fnreplication
  242. ->expects($this->never())
  243. ->method('__invoke');
  244. $fnaggregate = $this->getMock('stdClass', array('__invoke'));
  245. $fnaggregate
  246. ->expects($this->never())
  247. ->method('__invoke');
  248. $arg2 = array(
  249. 'cluster' => $fncluster,
  250. 'replication' => $fnreplication,
  251. 'aggregate' => $fnaggregate,
  252. );
  253. $client = new Client($arg1, $arg2);
  254. $this->assertSame($connection, $client->getConnection());
  255. }
  256. /**
  257. * @group disconnected
  258. */
  259. public function testReplicationOptionHasPrecedenceOverAggregateOption()
  260. {
  261. $arg1 = array('tcp://host1', 'tcp://host2');
  262. $connection = $this->getMock('Predis\Connection\AggregateConnectionInterface');
  263. $fnreplication = $this->getMock('stdClass', array('__invoke'));
  264. $fnreplication
  265. ->expects($this->once())
  266. ->method('__invoke')
  267. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'), $arg1)
  268. ->will($this->returnValue($connection));
  269. $fnaggregate = $this->getMock('stdClass', array('__invoke'));
  270. $fnaggregate->expects($this->never())->method('__invoke');
  271. $arg2 = array(
  272. 'replication' => $fnreplication,
  273. 'aggregate' => $fnaggregate,
  274. );
  275. $client = new Client($arg1, $arg2);
  276. $this->assertSame($connection, $client->getConnection());
  277. }
  278. /**
  279. * @group disconnected
  280. */
  281. public function testAggregateOptionDoesNotTriggerAggregationInClient()
  282. {
  283. $arg1 = array('tcp://host1', 'tcp://host2');
  284. $connection = $this->getMock('Predis\Connection\AggregateConnectionInterface');
  285. $fnaggregate = $this->getMock('stdClass', array('__invoke'));
  286. $fnaggregate
  287. ->expects($this->once())
  288. ->method('__invoke')
  289. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'), $arg1)
  290. ->will($this->returnValue($connection));
  291. $connections = $this->getMock('Predis\Connection\FactoryInterface');
  292. $connections
  293. ->expects($this->never())
  294. ->method('aggregate');
  295. $arg2 = array('aggregate' => $fnaggregate, 'connections' => $connections);
  296. $client = new Client($arg1, $arg2);
  297. $this->assertSame($connection, $client->getConnection());
  298. }
  299. /**
  300. * @group disconnected
  301. * @expectedException \InvalidArgumentException
  302. * @expectedExceptionMessage Invalid type for connection parameters
  303. */
  304. public function testConstructorWithInvalidArgumentType()
  305. {
  306. $client = new Client(new \stdClass());
  307. }
  308. /**
  309. * @group disconnected
  310. * @expectedException \InvalidArgumentException
  311. * @expectedExceptionMessage Invalid type for client options
  312. */
  313. public function testConstructorWithInvalidOptionType()
  314. {
  315. $client = new Client('tcp://host1', new \stdClass());
  316. }
  317. /**
  318. * @group disconnected
  319. */
  320. public function testConnectAndDisconnect()
  321. {
  322. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  323. $connection
  324. ->expects($this->once())
  325. ->method('connect');
  326. $connection
  327. ->expects($this->once())
  328. ->method('disconnect');
  329. $client = new Client($connection);
  330. $client->connect();
  331. $client->disconnect();
  332. }
  333. /**
  334. * @group disconnected
  335. */
  336. public function testIsConnectedChecksConnectionState()
  337. {
  338. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  339. $connection
  340. ->expects($this->once())
  341. ->method('isConnected');
  342. $client = new Client($connection);
  343. $client->isConnected();
  344. }
  345. /**
  346. * @group disconnected
  347. */
  348. public function testQuitIsAliasForDisconnect()
  349. {
  350. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  351. $connection
  352. ->expects($this->once())
  353. ->method('disconnect');
  354. $client = new Client($connection);
  355. $client->quit();
  356. }
  357. /**
  358. * @group disconnected
  359. */
  360. public function testCreatesNewCommandUsingSpecifiedCommandFactory()
  361. {
  362. $ping = $this->getCommandFactory()->createCommand('ping', array());
  363. $commands = $this->getMock('Predis\Command\FactoryInterface');
  364. $commands
  365. ->expects($this->once())
  366. ->method('createCommand')
  367. ->with('ping', array())
  368. ->will($this->returnValue($ping));
  369. $client = new Client(null, array('commands' => $commands));
  370. $this->assertSame($ping, $client->createCommand('ping', array()));
  371. }
  372. /**
  373. * @group disconnected
  374. */
  375. public function testExecuteCommandReturnsParsedResponses()
  376. {
  377. $commands = $this->getCommandFactory();
  378. $ping = $commands->createCommand('ping', array());
  379. $hgetall = $commands->createCommand('hgetall', array('metavars', 'foo', 'hoge'));
  380. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  381. $connection
  382. ->expects($this->at(0))
  383. ->method('executeCommand')
  384. ->with($ping)
  385. ->will($this->returnValue(new Response\Status('PONG')));
  386. $connection
  387. ->expects($this->at(1))
  388. ->method('executeCommand')
  389. ->with($hgetall)
  390. ->will($this->returnValue(array('foo', 'bar', 'hoge', 'piyo')));
  391. $client = new Client($connection);
  392. $this->assertEquals('PONG', $client->executeCommand($ping));
  393. $this->assertSame(array('foo' => 'bar', 'hoge' => 'piyo'), $client->executeCommand($hgetall));
  394. }
  395. /**
  396. * @group disconnected
  397. * @expectedException \Predis\Response\ServerException
  398. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  399. */
  400. public function testExecuteCommandThrowsExceptionOnRedisError()
  401. {
  402. $ping = $this->getCommandFactory()->createCommand('ping', array());
  403. $expectedResponse = new Response\Error('ERR Operation against a key holding the wrong kind of value');
  404. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  405. $connection
  406. ->expects($this->once())
  407. ->method('executeCommand')
  408. ->will($this->returnValue($expectedResponse));
  409. $client = new Client($connection);
  410. $client->executeCommand($ping);
  411. }
  412. /**
  413. * @group disconnected
  414. */
  415. public function testExecuteCommandReturnsErrorResponseOnRedisError()
  416. {
  417. $ping = $this->getCommandFactory()->createCommand('ping', array());
  418. $expectedResponse = new Response\Error('ERR Operation against a key holding the wrong kind of value');
  419. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  420. $connection
  421. ->expects($this->once())
  422. ->method('executeCommand')
  423. ->will($this->returnValue($expectedResponse));
  424. $client = new Client($connection, array('exceptions' => false));
  425. $response = $client->executeCommand($ping);
  426. $this->assertSame($response, $expectedResponse);
  427. }
  428. /**
  429. * @group disconnected
  430. */
  431. public function testCallingRedisCommandExecutesInstanceOfCommand()
  432. {
  433. $ping = $this->getCommandFactory()->createCommand('ping', array());
  434. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  435. $connection
  436. ->expects($this->once())
  437. ->method('executeCommand')
  438. ->with($this->isInstanceOf('Predis\Command\Redis\PING'))
  439. ->will($this->returnValue('PONG'));
  440. $commands = $this->getMock('Predis\Command\FactoryInterface');
  441. $commands
  442. ->expects($this->once())
  443. ->method('createCommand')
  444. ->with('ping', array())
  445. ->will($this->returnValue($ping));
  446. $options = array('commands' => $commands);
  447. $client = $this->getMock('Predis\Client', null, array($connection, $options));
  448. $this->assertEquals('PONG', $client->ping());
  449. }
  450. /**
  451. * @group disconnected
  452. * @expectedException \Predis\Response\ServerException
  453. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  454. */
  455. public function testCallingRedisCommandThrowsExceptionOnServerError()
  456. {
  457. $expectedResponse = new Response\Error('ERR Operation against a key holding the wrong kind of value');
  458. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  459. $connection
  460. ->expects($this->once())
  461. ->method('executeCommand')
  462. ->with($this->isRedisCommand('PING'))
  463. ->will($this->returnValue($expectedResponse));
  464. $client = new Client($connection);
  465. $client->ping();
  466. }
  467. /**
  468. * @group disconnected
  469. */
  470. public function testCallingRedisCommandReturnsErrorResponseOnRedisError()
  471. {
  472. $expectedResponse = new Response\Error('ERR Operation against a key holding the wrong kind of value');
  473. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  474. $connection
  475. ->expects($this->once())
  476. ->method('executeCommand')
  477. ->with($this->isRedisCommand('PING'))
  478. ->will($this->returnValue($expectedResponse));
  479. $client = new Client($connection, array('exceptions' => false));
  480. $response = $client->ping();
  481. $this->assertSame($response, $expectedResponse);
  482. }
  483. /**
  484. * @group disconnected
  485. */
  486. public function testRawCommand()
  487. {
  488. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  489. $connection
  490. ->expects($this->at(0))
  491. ->method('executeCommand')
  492. ->with($this->isRedisCommand('SET', array('foo', 'bar')))
  493. ->will($this->returnValue(new Response\Status('OK')));
  494. $connection
  495. ->expects($this->at(1))
  496. ->method('executeCommand')
  497. ->with($this->isRedisCommand('GET', array('foo')))
  498. ->will($this->returnValue('bar'));
  499. $connection
  500. ->expects($this->at(2))
  501. ->method('executeCommand')
  502. ->with($this->isRedisCommand('PING'))
  503. ->will($this->returnValue('PONG'));
  504. $client = new Client($connection);
  505. $this->assertSame('OK', $client->executeRaw(array('SET', 'foo', 'bar')));
  506. $this->assertSame('bar', $client->executeRaw(array('GET', 'foo')));
  507. $error = true; // $error is always populated by reference.
  508. $this->assertSame('PONG', $client->executeRaw(array('PING'), $error));
  509. $this->assertFalse($error);
  510. }
  511. /**
  512. * @group disconnected
  513. */
  514. public function testRawCommandNeverAppliesPrefix()
  515. {
  516. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  517. $connection
  518. ->expects($this->at(0))
  519. ->method('executeCommand')
  520. ->with($this->isRedisCommand('SET', array('foo', 'bar')))
  521. ->will($this->returnValue(new Response\Status('OK')));
  522. $connection
  523. ->expects($this->at(1))
  524. ->method('executeCommand')
  525. ->with($this->isRedisCommand('GET', array('foo')))
  526. ->will($this->returnValue('bar'));
  527. $client = new Client($connection, array('prefix' => 'predis:'));
  528. $this->assertSame('OK', $client->executeRaw(array('SET', 'foo', 'bar')));
  529. $this->assertSame('bar', $client->executeRaw(array('GET', 'foo')));
  530. }
  531. /**
  532. * @group disconnected
  533. */
  534. public function testRawCommandNeverThrowsExceptions()
  535. {
  536. $message = 'ERR Mock error response';
  537. $response = new Response\Error($message);
  538. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  539. $connection
  540. ->expects($this->once())
  541. ->method('executeCommand')
  542. ->with($this->isRedisCommand('PING'))
  543. ->will($this->returnValue($response));
  544. $client = new Client($connection, array('exceptions' => true));
  545. $this->assertSame($message, $client->executeRaw(array('PING'), $error));
  546. $this->assertTrue($error);
  547. }
  548. /**
  549. * @group disconnected
  550. * @expectedException \Predis\ClientException
  551. * @expectedExceptionMessage Command 'INVALIDCOMMAND' is not a registered Redis command.
  552. */
  553. public function testThrowsExceptionOnNonRegisteredRedisCommand()
  554. {
  555. $client = new Client();
  556. $client->invalidCommand();
  557. }
  558. /**
  559. * @group disconnected
  560. */
  561. public function testGetClientByMethodReturnsInstanceOfSubclass()
  562. {
  563. $nodes = array('tcp://host1?alias=node01', 'tcp://host2?alias=node02');
  564. $client = $this->getMock('Predis\Client', array('dummy'), array($nodes, array('cluster' => 'predis')), 'SubclassedClient');
  565. $this->assertInstanceOf('SubclassedClient', $client->getClientBy('id', 'node02'));
  566. }
  567. /**
  568. * @group disconnected
  569. */
  570. public function testGetClientByMethodInvokesCallableInSecondArgumentAndReturnsItsReturnValue()
  571. {
  572. $test = $this;
  573. $client = new Client(array('tcp://host1?alias=node01', 'tcp://host2?alias=node02'), array('cluster' => 'predis'));
  574. $callable = $this->getMock('stdClass', array('__invoke'));
  575. $callable
  576. ->expects($this->once())
  577. ->method('__invoke')
  578. ->with($this->callback(function ($clientNode) use ($test, $client) {
  579. $test->isInstanceOf('Predis\ClientInterface', $clientNode);
  580. $test->assertNotSame($client, $clientNode);
  581. $test->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection = $clientNode->getConnection());
  582. $test->assertSame('node02', $connection->getParameters()->alias);
  583. return true;
  584. }))
  585. ->will($this->returnValue('value'));
  586. $this->assertSame('value', $client->getClientBy('id', 'node02', $callable));
  587. }
  588. /**
  589. * @group disconnected
  590. */
  591. public function testGetClientByMethodSupportsSelectingConnectionById()
  592. {
  593. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  594. $aggregate = $this->getMockBuilder('Predis\Connection\AggregateConnectionInterface')
  595. ->setMethods(array('getConnectionById'))
  596. ->getMockForAbstractClass();
  597. $aggregate
  598. ->expects($this->once())
  599. ->method('getConnectionById')
  600. ->with('nodeXX')
  601. ->will($this->returnValue($connection));
  602. $client = new Client($aggregate);
  603. $nodeClient = $client->getClientBy('id', 'nodeXX');
  604. $this->assertSame($connection, $nodeClient->getConnection());
  605. $this->assertSame($client->getOptions(), $nodeClient->getOptions());
  606. }
  607. /**
  608. * @group disconnected
  609. * @expectedException \InvalidArgumentException
  610. * @expectedExceptionMessage Cannot find a connection by id matching `nodeXX`
  611. */
  612. public function testGetClientByMethodThrowsExceptionSelectingConnectionByUnknownId()
  613. {
  614. $aggregate = $this->getMock('Predis\Connection\AggregateConnectionInterface');
  615. $aggregate
  616. ->expects($this->once())
  617. ->method('getConnectionById')
  618. ->with('nodeXX')
  619. ->will($this->returnValue(null));
  620. $client = new Client($aggregate);
  621. $client->getClientBy('id', 'nodeXX');
  622. }
  623. /**
  624. * @group disconnected
  625. */
  626. public function testGetClientByMethodSupportsSelectingConnectionByKey()
  627. {
  628. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  629. $aggregate = $this->getMockBuilder('Predis\Connection\AggregateConnectionInterface')
  630. ->setMethods(array('getConnectionByKey'))
  631. ->getMockForAbstractClass();
  632. $aggregate
  633. ->expects($this->once())
  634. ->method('getConnectionByKey')
  635. ->with('key:1')
  636. ->will($this->returnValue($connection));
  637. $client = new Client($aggregate);
  638. $nodeClient = $client->getClientBy('key', 'key:1');
  639. $this->assertSame($connection, $nodeClient->getConnection());
  640. $this->assertSame($client->getOptions(), $nodeClient->getOptions());
  641. }
  642. /**
  643. * @group disconnected
  644. */
  645. public function testGetClientByMethodSupportsSelectingConnectionBySlot()
  646. {
  647. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  648. $aggregate = $this->getMockBuilder('Predis\Connection\AggregateConnectionInterface')
  649. ->setMethods(array('getConnectionBySlot'))
  650. ->getMockForAbstractClass();
  651. $aggregate
  652. ->expects($this->once())
  653. ->method('getConnectionBySlot')
  654. ->with(5460)
  655. ->will($this->returnValue($connection));
  656. $client = new Client($aggregate);
  657. $nodeClient = $client->getClientBy('slot', 5460);
  658. $this->assertSame($connection, $nodeClient->getConnection());
  659. $this->assertSame($client->getOptions(), $nodeClient->getOptions());
  660. }
  661. /**
  662. * @group disconnected
  663. */
  664. public function testGetClientByMethodSupportsSelectingConnectionByCommand()
  665. {
  666. $command = \Predis\Command\RawCommand::create('GET', 'key');
  667. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  668. $aggregate = $this->getMockBuilder('Predis\Connection\AggregateConnectionInterface')
  669. ->setMethods(array('getConnectionByCommand'))
  670. ->getMockForAbstractClass();
  671. $aggregate
  672. ->expects($this->once())
  673. ->method('getConnectionByCommand')
  674. ->with($command)
  675. ->will($this->returnValue($connection));
  676. $client = new Client($aggregate);
  677. $nodeClient = $client->getClientBy('command', $command);
  678. $this->assertSame($connection, $nodeClient->getConnection());
  679. $this->assertSame($client->getOptions(), $nodeClient->getOptions());
  680. }
  681. /**
  682. * @group disconnected
  683. * @expectedException \InvalidArgumentException
  684. * @expectedExceptionMessage Invalid selector type: `unknown`
  685. */
  686. public function testGetClientByMethodThrowsExceptionWhenSelectingConnectionByUnknownType()
  687. {
  688. $client = new Client('tcp://127.0.0.1?alias=node01');
  689. $client->getClientBy('unknown', 'test');
  690. }
  691. /**
  692. * @group disconnected
  693. * @expectedException \InvalidArgumentException
  694. * @expectedExceptionMessage Selecting connection by id is not supported by Predis\Connection\StreamConnection
  695. */
  696. public function testGetClientByMethodThrowsExceptionWhenConnectionDoesNotSupportSelectorType()
  697. {
  698. $client = new Client('tcp://127.0.0.1?alias=node01');
  699. $client->getClientBy('id', 'node01');
  700. }
  701. /**
  702. * @group disconnected
  703. */
  704. public function testPipelineWithoutArgumentsReturnsPipeline()
  705. {
  706. $client = new Client();
  707. $this->assertInstanceOf('Predis\Pipeline\Pipeline', $client->pipeline());
  708. }
  709. /**
  710. * @group disconnected
  711. */
  712. public function testPipelineWithArrayReturnsPipeline()
  713. {
  714. $client = new Client();
  715. $this->assertInstanceOf('Predis\Pipeline\Pipeline', $client->pipeline(array()));
  716. $this->assertInstanceOf('Predis\Pipeline\Atomic', $client->pipeline(array('atomic' => true)));
  717. $this->assertInstanceOf('Predis\Pipeline\FireAndForget', $client->pipeline(array('fire-and-forget' => true)));
  718. }
  719. /**
  720. * @group disconnected
  721. */
  722. public function testPipelineWithCallableExecutesPipeline()
  723. {
  724. $callable = $this->getMock('stdClass', array('__invoke'));
  725. $callable
  726. ->expects($this->once())
  727. ->method('__invoke')
  728. ->with($this->isInstanceOf('Predis\Pipeline\Pipeline'));
  729. $client = new Client();
  730. $client->pipeline($callable);
  731. }
  732. /**
  733. * @group disconnected
  734. */
  735. public function testPubSubLoopWithoutArgumentsReturnsPubSubConsumer()
  736. {
  737. $client = new Client();
  738. $this->assertInstanceOf('Predis\PubSub\Consumer', $client->pubSubLoop());
  739. }
  740. /**
  741. * @group disconnected
  742. */
  743. public function testPubSubLoopWithArrayReturnsPubSubConsumerWithOptions()
  744. {
  745. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  746. $options = array('subscribe' => 'channel');
  747. $client = new Client($connection);
  748. $this->assertInstanceOf('Predis\PubSub\Consumer', $pubsub = $client->pubSubLoop($options));
  749. $reflection = new \ReflectionProperty($pubsub, 'options');
  750. $reflection->setAccessible(true);
  751. $this->assertSame($options, $reflection->getValue($pubsub));
  752. }
  753. /**
  754. * @group disconnected
  755. */
  756. public function testPubSubLoopWithArrayAndCallableExecutesPubSub()
  757. {
  758. // NOTE: we use a subscribe count of 0 in the message payload to trick
  759. // the context and forcing it to be closed since there are no more
  760. // active subscriptions.
  761. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  762. $connection
  763. ->expects($this->once())
  764. ->method('read')
  765. ->will($this->returnValue(array('subscribe', 'channel', 0)));
  766. $callable = $this->getMock('stdClass', array('__invoke'));
  767. $callable
  768. ->expects($this->once())
  769. ->method('__invoke');
  770. $client = new Client($connection);
  771. $this->assertNull($client->pubSubLoop(array('subscribe' => 'channel'), $callable));
  772. }
  773. /**
  774. * @group disconnected
  775. */
  776. public function testPubSubLoopWithCallableReturningFalseStopsPubSubConsumer()
  777. {
  778. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  779. $connection
  780. ->expects($this->at(1))
  781. ->method('read')
  782. ->will($this->returnValue(array('subscribe', 'channel', 1)));
  783. $connection
  784. ->expects($this->at(2))
  785. ->method('writeRequest')
  786. ->with($this->isRedisCommand('UNSUBSCRIBE'));
  787. $connection
  788. ->expects($this->at(3))
  789. ->method('read')
  790. ->will($this->returnValue(array('unsubscribe', 'channel', 0)));
  791. $callable = $this->getMock('stdClass', array('__invoke'));
  792. $callable
  793. ->expects($this->at(0))
  794. ->method('__invoke')
  795. ->will($this->returnValue(false));
  796. $client = new Client($connection);
  797. $this->assertNull($client->pubSubLoop(array('subscribe' => 'channel'), $callable));
  798. }
  799. /**
  800. * @group disconnected
  801. */
  802. public function testTransactionWithoutArgumentsReturnsMultiExec()
  803. {
  804. $client = new Client();
  805. $this->assertInstanceOf('Predis\Transaction\MultiExec', $client->transaction());
  806. }
  807. /**
  808. * @group disconnected
  809. */
  810. public function testTransactionWithArrayReturnsMultiExecTransactionWithOptions()
  811. {
  812. $options = array('cas' => true, 'retry' => 3);
  813. $client = new Client();
  814. $this->assertInstanceOf('Predis\Transaction\MultiExec', $tx = $client->transaction($options));
  815. // I hate this part but reflection is the easiest way in this case.
  816. $property = new \ReflectionProperty($tx, 'modeCAS');
  817. $property->setAccessible(true);
  818. $this->assertSame($options['cas'], $property->getValue($tx));
  819. $property = new \ReflectionProperty($tx, 'attempts');
  820. $property->setAccessible(true);
  821. $this->assertSame($options['retry'], $property->getValue($tx));
  822. }
  823. /**
  824. * @group disconnected
  825. */
  826. public function testTransactionWithArrayAndCallableExecutesMultiExec()
  827. {
  828. // We use CAS here as we don't care about the actual MULTI/EXEC context.
  829. $options = array('cas' => true, 'retry' => 3);
  830. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  831. $connection
  832. ->expects($this->once())
  833. ->method('executeCommand')
  834. ->will($this->returnValue(new Response\Status('QUEUED')));
  835. $callable = $this->getMock('stdClass', array('__invoke'));
  836. $callable
  837. ->expects($this->once())
  838. ->method('__invoke')
  839. ->will($this->returnCallback(function ($tx) { $tx->ping(); }));
  840. $client = new Client($connection);
  841. $client->transaction($options, $callable);
  842. }
  843. /**
  844. * @group disconnected
  845. */
  846. public function testMonitorReturnsMonitorConsumer()
  847. {
  848. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  849. $client = new Client($connection);
  850. $this->assertInstanceOf('Predis\Monitor\Consumer', $monitor = $client->monitor());
  851. }
  852. /**
  853. * @group disconnected
  854. */
  855. public function testClientResendScriptCommandUsingEvalOnNoScriptErrors()
  856. {
  857. $command = $this->getMockForAbstractClass('Predis\Command\ScriptCommand', array(), '', true, true, true, array('parseResponse'));
  858. $command
  859. ->expects($this->once())
  860. ->method('getScript')
  861. ->will($this->returnValue('return redis.call(\'exists\', KEYS[1])'));
  862. $command
  863. ->expects($this->once())
  864. ->method('parseResponse')
  865. ->with('OK')
  866. ->will($this->returnValue(true));
  867. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  868. $connection
  869. ->expects($this->at(0))
  870. ->method('executeCommand')
  871. ->with($command)
  872. ->will($this->returnValue(new Response\Error('NOSCRIPT')));
  873. $connection
  874. ->expects($this->at(1))
  875. ->method('executeCommand')
  876. ->with($this->isRedisCommand('EVAL'))
  877. ->will($this->returnValue('OK'));
  878. $client = new Client($connection);
  879. $this->assertTrue($client->executeCommand($command));
  880. }
  881. /**
  882. * @group disconnected
  883. */
  884. public function testGetIteratorWithTraversableConnections()
  885. {
  886. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6381');
  887. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6382');
  888. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6383');
  889. $aggregate = new \Predis\Connection\Cluster\PredisCluster();
  890. $aggregate->add($connection1);
  891. $aggregate->add($connection2);
  892. $aggregate->add($connection3);
  893. $client = new Client($aggregate);
  894. $iterator = $client->getIterator();
  895. $this->assertInstanceOf('\Predis\Client', $nodeClient = $iterator->current());
  896. $this->assertSame($connection1, $nodeClient->getConnection());
  897. $this->assertSame('127.0.0.1:6381', $iterator->key());
  898. $iterator->next();
  899. $this->assertInstanceOf('\Predis\Client', $nodeClient = $iterator->current());
  900. $this->assertSame($connection2, $nodeClient->getConnection());
  901. $this->assertSame('127.0.0.1:6382', $iterator->key());
  902. $iterator->next();
  903. $this->assertInstanceOf('\Predis\Client', $nodeClient = $iterator->current());
  904. $this->assertSame($connection3, $nodeClient->getConnection());
  905. $this->assertSame('127.0.0.1:6383', $iterator->key());
  906. }
  907. /**
  908. * @group disconnected
  909. * @expectedException \Predis\ClientException
  910. * @expectedExceptionMessage The underlying connection is not traversable
  911. */
  912. public function testGetIteratorWithNonTraversableConnectionThrowsException()
  913. {
  914. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  915. $client = new Client($connection);
  916. $client->getIterator();
  917. }
  918. // ******************************************************************** //
  919. // ---- HELPER METHODS ------------------------------------------------ //
  920. // ******************************************************************** //
  921. /**
  922. * Returns an URI string representation of the specified connection parameters.
  923. *
  924. * @param array $parameters Array of connection parameters.
  925. *
  926. * @return string URI string.
  927. */
  928. protected function getParametersString(array $parameters)
  929. {
  930. $defaults = $this->getDefaultParametersArray();
  931. $scheme = isset($parameters['scheme']) ? $parameters['scheme'] : $defaults['scheme'];
  932. $host = isset($parameters['host']) ? $parameters['host'] : $defaults['host'];
  933. $port = isset($parameters['port']) ? $parameters['port'] : $defaults['port'];
  934. unset($parameters['scheme'], $parameters['host'], $parameters['port']);
  935. $uriString = "$scheme://$host:$port/?";
  936. foreach ($parameters as $k => $v) {
  937. $uriString .= "$k=$v&";
  938. }
  939. return $uriString;
  940. }
  941. /**
  942. * Returns a mock callable simulating an aggregate connection initializer.
  943. *
  944. * @param mixed $parameters Expected connection parameters
  945. *
  946. * @return callable
  947. */
  948. protected function getAggregateInitializer($parameters)
  949. {
  950. $connection = $this->getMock('Predis\Connection\AggregateConnectionInterface');
  951. $callable = $this->getMock('stdClass', array('__invoke'));
  952. $callable
  953. ->expects($this->once())
  954. ->method('__invoke')
  955. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'), $parameters)
  956. ->will($this->returnValue($connection));
  957. return $callable;
  958. }
  959. }