ClientTest.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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 testGetConnectionFromAggregateConnectionWithAlias()
  562. {
  563. $client = new Client(array('tcp://host1?alias=node01', 'tcp://host2?alias=node02'), array('cluster' => 'predis'));
  564. $this->assertInstanceOf('Predis\Connection\Cluster\ClusterInterface', $cluster = $client->getConnection());
  565. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $node01 = $client->getConnectionById('node01'));
  566. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $node02 = $client->getConnectionById('node02'));
  567. $this->assertSame('host1', $node01->getParameters()->host);
  568. $this->assertSame('host2', $node02->getParameters()->host);
  569. }
  570. /**
  571. * @group disconnected
  572. * @expectedException \Predis\NotSupportedException
  573. * @expectedExceptionMessage Retrieving connections by ID is supported only by aggregate connections.
  574. */
  575. public function testGetConnectionByIdWorksOnlyWithAggregateConnections()
  576. {
  577. $client = new Client();
  578. $client->getConnectionById('node01');
  579. }
  580. /**
  581. * @group disconnected
  582. */
  583. public function testGetClientByMethodCreatesClientWithConnectionFromAggregateConnection()
  584. {
  585. $client = new Client(array('tcp://host1?alias=node01', 'tcp://host2?alias=node02'), array('prefix' => 'pfx:', 'cluster' => 'predis'));
  586. $this->assertInstanceOf('Predis\Connection\Cluster\ClusterInterface', $cluster = $client->getConnection());
  587. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $node01 = $client->getConnectionById('node01'));
  588. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $node02 = $client->getConnectionById('node02'));
  589. $clientNode02 = $client->getClientBy('id', 'node02');
  590. $this->assertInstanceOf('Predis\Client', $clientNode02);
  591. $this->assertSame($node02, $clientNode02->getConnection());
  592. $this->assertSame($client->getOptions(), $clientNode02->getOptions());
  593. }
  594. /**
  595. * @group disconnected
  596. */
  597. public function testGetClientByMethodReturnsInstanceOfSubclass()
  598. {
  599. $nodes = array('tcp://host1?alias=node01', 'tcp://host2?alias=node02');
  600. $client = $this->getMock('Predis\Client', array('dummy'), array($nodes, array('cluster' => 'predis')), 'SubclassedClient');
  601. $this->assertInstanceOf('SubclassedClient', $client->getClientBy('id', 'node02'));
  602. }
  603. /**
  604. * @group disconnected
  605. */
  606. public function testGetClientByMethodInvokesCallableInSecondArgumentAndReturnsItsReturnValue()
  607. {
  608. $test = $this;
  609. $client = new Client(array('tcp://host1?alias=node01', 'tcp://host2?alias=node02'), array('cluster' => 'predis'));
  610. $callable = $this->getMock('stdClass', array('__invoke'));
  611. $callable
  612. ->expects($this->once())
  613. ->method('__invoke')
  614. ->with($this->callback(function ($clientNode) use ($test, $client) {
  615. $test->isInstanceOf('Predis\ClientInterface', $clientNode);
  616. $test->assertNotSame($client, $clientNode);
  617. $test->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection = $clientNode->getConnection());
  618. $test->assertSame('node02', $connection->getParameters()->alias);
  619. return true;
  620. }))
  621. ->will($this->returnValue('value'));
  622. $this->assertSame('value', $client->getClientBy('id', 'node02', $callable));
  623. }
  624. /**
  625. * @group disconnected
  626. */
  627. public function testGetClientByMethodSupportsSelectingConnectionById()
  628. {
  629. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  630. $aggregate = $this->getMockBuilder('Predis\Connection\AggregateConnectionInterface')
  631. ->setMethods(array('getConnectionById'))
  632. ->getMockForAbstractClass();
  633. $aggregate
  634. ->expects($this->once())
  635. ->method('getConnectionById')
  636. ->with('nodeXX')
  637. ->will($this->returnValue($connection));
  638. $client = new Client($aggregate);
  639. $nodeClient = $client->getClientBy('id', 'nodeXX');
  640. $this->assertSame($connection, $nodeClient->getConnection());
  641. $this->assertSame($client->getOptions(), $nodeClient->getOptions());
  642. }
  643. /**
  644. * @group disconnected
  645. * @expectedException \InvalidArgumentException
  646. * @expectedExceptionMessage Cannot find a connection by id matching `nodeXX`
  647. */
  648. public function testGetClientByMethodThrowsExceptionSelectingConnectionByUnknownId()
  649. {
  650. $aggregate = $this->getMock('Predis\Connection\AggregateConnectionInterface');
  651. $aggregate
  652. ->expects($this->once())
  653. ->method('getConnectionById')
  654. ->with('nodeXX')
  655. ->will($this->returnValue(null));
  656. $client = new Client($aggregate);
  657. $client->getClientBy('id', 'nodeXX');
  658. }
  659. /**
  660. * @group disconnected
  661. */
  662. public function testGetClientByMethodSupportsSelectingConnectionByKey()
  663. {
  664. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  665. $aggregate = $this->getMockBuilder('Predis\Connection\AggregateConnectionInterface')
  666. ->setMethods(array('getConnectionByKey'))
  667. ->getMockForAbstractClass();
  668. $aggregate
  669. ->expects($this->once())
  670. ->method('getConnectionByKey')
  671. ->with('key:1')
  672. ->will($this->returnValue($connection));
  673. $client = new Client($aggregate);
  674. $nodeClient = $client->getClientBy('key', 'key:1');
  675. $this->assertSame($connection, $nodeClient->getConnection());
  676. $this->assertSame($client->getOptions(), $nodeClient->getOptions());
  677. }
  678. /**
  679. * @group disconnected
  680. */
  681. public function testGetClientByMethodSupportsSelectingConnectionBySlot()
  682. {
  683. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  684. $aggregate = $this->getMockBuilder('Predis\Connection\AggregateConnectionInterface')
  685. ->setMethods(array('getConnectionBySlot'))
  686. ->getMockForAbstractClass();
  687. $aggregate
  688. ->expects($this->once())
  689. ->method('getConnectionBySlot')
  690. ->with(5460)
  691. ->will($this->returnValue($connection));
  692. $client = new Client($aggregate);
  693. $nodeClient = $client->getClientBy('slot', 5460);
  694. $this->assertSame($connection, $nodeClient->getConnection());
  695. $this->assertSame($client->getOptions(), $nodeClient->getOptions());
  696. }
  697. /**
  698. * @group disconnected
  699. */
  700. public function testGetClientByMethodSupportsSelectingConnectionByCommand()
  701. {
  702. $command = \Predis\Command\RawCommand::create('GET', 'key');
  703. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  704. $aggregate = $this->getMockBuilder('Predis\Connection\AggregateConnectionInterface')
  705. ->setMethods(array('getConnectionByCommand'))
  706. ->getMockForAbstractClass();
  707. $aggregate
  708. ->expects($this->once())
  709. ->method('getConnectionByCommand')
  710. ->with($command)
  711. ->will($this->returnValue($connection));
  712. $client = new Client($aggregate);
  713. $nodeClient = $client->getClientBy('command', $command);
  714. $this->assertSame($connection, $nodeClient->getConnection());
  715. $this->assertSame($client->getOptions(), $nodeClient->getOptions());
  716. }
  717. /**
  718. * @group disconnected
  719. * @expectedException \InvalidArgumentException
  720. * @expectedExceptionMessage Invalid selector type: `unknown`
  721. */
  722. public function testGetClientByMethodThrowsExceptionWhenSelectingConnectionByUnknownType()
  723. {
  724. $client = new Client('tcp://127.0.0.1?alias=node01');
  725. $client->getClientBy('unknown', 'test');
  726. }
  727. /**
  728. * @group disconnected
  729. * @expectedException \InvalidArgumentException
  730. * @expectedExceptionMessage Selecting connection by id is not supported by Predis\Connection\StreamConnection
  731. */
  732. public function testGetClientByMethodThrowsExceptionWhenConnectionDoesNotSupportSelectorType()
  733. {
  734. $client = new Client('tcp://127.0.0.1?alias=node01');
  735. $client->getClientBy('id', 'node01');
  736. }
  737. /**
  738. * @group disconnected
  739. */
  740. public function testPipelineWithoutArgumentsReturnsPipeline()
  741. {
  742. $client = new Client();
  743. $this->assertInstanceOf('Predis\Pipeline\Pipeline', $client->pipeline());
  744. }
  745. /**
  746. * @group disconnected
  747. */
  748. public function testPipelineWithArrayReturnsPipeline()
  749. {
  750. $client = new Client();
  751. $this->assertInstanceOf('Predis\Pipeline\Pipeline', $client->pipeline(array()));
  752. $this->assertInstanceOf('Predis\Pipeline\Atomic', $client->pipeline(array('atomic' => true)));
  753. $this->assertInstanceOf('Predis\Pipeline\FireAndForget', $client->pipeline(array('fire-and-forget' => true)));
  754. }
  755. /**
  756. * @group disconnected
  757. */
  758. public function testPipelineWithCallableExecutesPipeline()
  759. {
  760. $callable = $this->getMock('stdClass', array('__invoke'));
  761. $callable
  762. ->expects($this->once())
  763. ->method('__invoke')
  764. ->with($this->isInstanceOf('Predis\Pipeline\Pipeline'));
  765. $client = new Client();
  766. $client->pipeline($callable);
  767. }
  768. /**
  769. * @group disconnected
  770. */
  771. public function testPubSubLoopWithoutArgumentsReturnsPubSubConsumer()
  772. {
  773. $client = new Client();
  774. $this->assertInstanceOf('Predis\PubSub\Consumer', $client->pubSubLoop());
  775. }
  776. /**
  777. * @group disconnected
  778. */
  779. public function testPubSubLoopWithArrayReturnsPubSubConsumerWithOptions()
  780. {
  781. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  782. $options = array('subscribe' => 'channel');
  783. $client = new Client($connection);
  784. $this->assertInstanceOf('Predis\PubSub\Consumer', $pubsub = $client->pubSubLoop($options));
  785. $reflection = new \ReflectionProperty($pubsub, 'options');
  786. $reflection->setAccessible(true);
  787. $this->assertSame($options, $reflection->getValue($pubsub));
  788. }
  789. /**
  790. * @group disconnected
  791. */
  792. public function testPubSubLoopWithArrayAndCallableExecutesPubSub()
  793. {
  794. // NOTE: we use a subscribe count of 0 in the message payload to trick
  795. // the context and forcing it to be closed since there are no more
  796. // active subscriptions.
  797. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  798. $connection
  799. ->expects($this->once())
  800. ->method('read')
  801. ->will($this->returnValue(array('subscribe', 'channel', 0)));
  802. $callable = $this->getMock('stdClass', array('__invoke'));
  803. $callable
  804. ->expects($this->once())
  805. ->method('__invoke');
  806. $client = new Client($connection);
  807. $this->assertNull($client->pubSubLoop(array('subscribe' => 'channel'), $callable));
  808. }
  809. /**
  810. * @group disconnected
  811. */
  812. public function testPubSubLoopWithCallableReturningFalseStopsPubSubConsumer()
  813. {
  814. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  815. $connection
  816. ->expects($this->at(1))
  817. ->method('read')
  818. ->will($this->returnValue(array('subscribe', 'channel', 1)));
  819. $connection
  820. ->expects($this->at(2))
  821. ->method('writeRequest')
  822. ->with($this->isRedisCommand('UNSUBSCRIBE'));
  823. $connection
  824. ->expects($this->at(3))
  825. ->method('read')
  826. ->will($this->returnValue(array('unsubscribe', 'channel', 0)));
  827. $callable = $this->getMock('stdClass', array('__invoke'));
  828. $callable
  829. ->expects($this->at(0))
  830. ->method('__invoke')
  831. ->will($this->returnValue(false));
  832. $client = new Client($connection);
  833. $this->assertNull($client->pubSubLoop(array('subscribe' => 'channel'), $callable));
  834. }
  835. /**
  836. * @group disconnected
  837. */
  838. public function testTransactionWithoutArgumentsReturnsMultiExec()
  839. {
  840. $client = new Client();
  841. $this->assertInstanceOf('Predis\Transaction\MultiExec', $client->transaction());
  842. }
  843. /**
  844. * @group disconnected
  845. */
  846. public function testTransactionWithArrayReturnsMultiExecTransactionWithOptions()
  847. {
  848. $options = array('cas' => true, 'retry' => 3);
  849. $client = new Client();
  850. $this->assertInstanceOf('Predis\Transaction\MultiExec', $tx = $client->transaction($options));
  851. // I hate this part but reflection is the easiest way in this case.
  852. $property = new \ReflectionProperty($tx, 'modeCAS');
  853. $property->setAccessible(true);
  854. $this->assertSame($options['cas'], $property->getValue($tx));
  855. $property = new \ReflectionProperty($tx, 'attempts');
  856. $property->setAccessible(true);
  857. $this->assertSame($options['retry'], $property->getValue($tx));
  858. }
  859. /**
  860. * @group disconnected
  861. */
  862. public function testTransactionWithArrayAndCallableExecutesMultiExec()
  863. {
  864. // We use CAS here as we don't care about the actual MULTI/EXEC context.
  865. $options = array('cas' => true, 'retry' => 3);
  866. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  867. $connection
  868. ->expects($this->once())
  869. ->method('executeCommand')
  870. ->will($this->returnValue(new Response\Status('QUEUED')));
  871. $callable = $this->getMock('stdClass', array('__invoke'));
  872. $callable
  873. ->expects($this->once())
  874. ->method('__invoke')
  875. ->will($this->returnCallback(function ($tx) { $tx->ping(); }));
  876. $client = new Client($connection);
  877. $client->transaction($options, $callable);
  878. }
  879. /**
  880. * @group disconnected
  881. */
  882. public function testMonitorReturnsMonitorConsumer()
  883. {
  884. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  885. $client = new Client($connection);
  886. $this->assertInstanceOf('Predis\Monitor\Consumer', $monitor = $client->monitor());
  887. }
  888. /**
  889. * @group disconnected
  890. */
  891. public function testClientResendScriptCommandUsingEvalOnNoScriptErrors()
  892. {
  893. $command = $this->getMockForAbstractClass('Predis\Command\ScriptCommand', array(), '', true, true, true, array('parseResponse'));
  894. $command
  895. ->expects($this->once())
  896. ->method('getScript')
  897. ->will($this->returnValue('return redis.call(\'exists\', KEYS[1])'));
  898. $command
  899. ->expects($this->once())
  900. ->method('parseResponse')
  901. ->with('OK')
  902. ->will($this->returnValue(true));
  903. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  904. $connection
  905. ->expects($this->at(0))
  906. ->method('executeCommand')
  907. ->with($command)
  908. ->will($this->returnValue(new Response\Error('NOSCRIPT')));
  909. $connection
  910. ->expects($this->at(1))
  911. ->method('executeCommand')
  912. ->with($this->isRedisCommand('EVAL'))
  913. ->will($this->returnValue('OK'));
  914. $client = new Client($connection);
  915. $this->assertTrue($client->executeCommand($command));
  916. }
  917. /**
  918. * @group disconnected
  919. */
  920. public function testGetIteratorWithTraversableConnections()
  921. {
  922. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6381');
  923. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6382');
  924. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6383');
  925. $aggregate = new \Predis\Connection\Cluster\PredisCluster();
  926. $aggregate->add($connection1);
  927. $aggregate->add($connection2);
  928. $aggregate->add($connection3);
  929. $client = new Client($aggregate);
  930. $iterator = $client->getIterator();
  931. $this->assertInstanceOf('\Predis\Client', $nodeClient = $iterator->current());
  932. $this->assertSame($connection1, $nodeClient->getConnection());
  933. $this->assertSame('127.0.0.1:6381', $iterator->key());
  934. $iterator->next();
  935. $this->assertInstanceOf('\Predis\Client', $nodeClient = $iterator->current());
  936. $this->assertSame($connection2, $nodeClient->getConnection());
  937. $this->assertSame('127.0.0.1:6382', $iterator->key());
  938. $iterator->next();
  939. $this->assertInstanceOf('\Predis\Client', $nodeClient = $iterator->current());
  940. $this->assertSame($connection3, $nodeClient->getConnection());
  941. $this->assertSame('127.0.0.1:6383', $iterator->key());
  942. }
  943. /**
  944. * @group disconnected
  945. * @expectedException \Predis\ClientException
  946. * @expectedExceptionMessage The underlying connection is not traversable
  947. */
  948. public function testGetIteratorWithNonTraversableConnectionThrowsException()
  949. {
  950. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  951. $client = new Client($connection);
  952. $client->getIterator();
  953. }
  954. // ******************************************************************** //
  955. // ---- HELPER METHODS ------------------------------------------------ //
  956. // ******************************************************************** //
  957. /**
  958. * Returns an URI string representation of the specified connection parameters.
  959. *
  960. * @param array $parameters Array of connection parameters.
  961. *
  962. * @return string URI string.
  963. */
  964. protected function getParametersString(array $parameters)
  965. {
  966. $defaults = $this->getDefaultParametersArray();
  967. $scheme = isset($parameters['scheme']) ? $parameters['scheme'] : $defaults['scheme'];
  968. $host = isset($parameters['host']) ? $parameters['host'] : $defaults['host'];
  969. $port = isset($parameters['port']) ? $parameters['port'] : $defaults['port'];
  970. unset($parameters['scheme'], $parameters['host'], $parameters['port']);
  971. $uriString = "$scheme://$host:$port/?";
  972. foreach ($parameters as $k => $v) {
  973. $uriString .= "$k=$v&";
  974. }
  975. return $uriString;
  976. }
  977. /**
  978. * Returns a mock callable simulating an aggregate connection initializer.
  979. *
  980. * @param mixed $parameters Expected connection parameters
  981. *
  982. * @return callable
  983. */
  984. protected function getAggregateInitializer($parameters)
  985. {
  986. $connection = $this->getMock('Predis\Connection\AggregateConnectionInterface');
  987. $callable = $this->getMock('stdClass', array('__invoke'));
  988. $callable
  989. ->expects($this->once())
  990. ->method('__invoke')
  991. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'), $parameters)
  992. ->will($this->returnValue($connection));
  993. return $callable;
  994. }
  995. }