ClientTest.php 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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 testOnMethodCreatesClientWithConnectionFromAggregateConnection()
  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->on('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 testOnMethodReturnsInstanceOfSubclass()
  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->on('node02'));
  602. }
  603. /**
  604. * @group disconnected
  605. */
  606. public function testOnMethodInvokesCallableInSecondArgumentAndReturnsItsReturnValue()
  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->on('node02', $callable));
  623. }
  624. /**
  625. * @group disconnected
  626. * @expectedException \Predis\NotSupportedException
  627. * @expectedExceptionMessage Retrieving connections by ID is supported only by aggregate connections
  628. */
  629. public function testOnMethodThrowsExceptionWithNodeConnection()
  630. {
  631. $client = new Client('tcp://127.0.0.1?alias=node01');
  632. $client->on('node01');
  633. }
  634. /**
  635. * @group disconnected
  636. * @expectedException \InvalidArgumentException
  637. * @expectedExceptionMessage Invalid connection ID: `nodeXX`
  638. */
  639. public function testOnMethodThrowsExceptionWithUnknownConnectionID()
  640. {
  641. $client = new Client(array('tcp://host1?alias=node01', 'tcp://host2?alias=node02'), array('cluster' => 'predis'));
  642. $client->on('nodeXX');
  643. }
  644. /**
  645. * @group disconnected
  646. */
  647. public function testPipelineWithoutArgumentsReturnsPipeline()
  648. {
  649. $client = new Client();
  650. $this->assertInstanceOf('Predis\Pipeline\Pipeline', $client->pipeline());
  651. }
  652. /**
  653. * @group disconnected
  654. */
  655. public function testPipelineWithArrayReturnsPipeline()
  656. {
  657. $client = new Client();
  658. $this->assertInstanceOf('Predis\Pipeline\Pipeline', $client->pipeline(array()));
  659. $this->assertInstanceOf('Predis\Pipeline\Atomic', $client->pipeline(array('atomic' => true)));
  660. $this->assertInstanceOf('Predis\Pipeline\FireAndForget', $client->pipeline(array('fire-and-forget' => true)));
  661. }
  662. /**
  663. * @group disconnected
  664. */
  665. public function testPipelineWithCallableExecutesPipeline()
  666. {
  667. $callable = $this->getMock('stdClass', array('__invoke'));
  668. $callable
  669. ->expects($this->once())
  670. ->method('__invoke')
  671. ->with($this->isInstanceOf('Predis\Pipeline\Pipeline'));
  672. $client = new Client();
  673. $client->pipeline($callable);
  674. }
  675. /**
  676. * @group disconnected
  677. */
  678. public function testPubSubLoopWithoutArgumentsReturnsPubSubConsumer()
  679. {
  680. $client = new Client();
  681. $this->assertInstanceOf('Predis\PubSub\Consumer', $client->pubSubLoop());
  682. }
  683. /**
  684. * @group disconnected
  685. */
  686. public function testPubSubLoopWithArrayReturnsPubSubConsumerWithOptions()
  687. {
  688. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  689. $options = array('subscribe' => 'channel');
  690. $client = new Client($connection);
  691. $this->assertInstanceOf('Predis\PubSub\Consumer', $pubsub = $client->pubSubLoop($options));
  692. $reflection = new \ReflectionProperty($pubsub, 'options');
  693. $reflection->setAccessible(true);
  694. $this->assertSame($options, $reflection->getValue($pubsub));
  695. }
  696. /**
  697. * @group disconnected
  698. */
  699. public function testPubSubLoopWithArrayAndCallableExecutesPubSub()
  700. {
  701. // NOTE: we use a subscribe count of 0 in the message payload to trick
  702. // the context and forcing it to be closed since there are no more
  703. // active subscriptions.
  704. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  705. $connection
  706. ->expects($this->once())
  707. ->method('read')
  708. ->will($this->returnValue(array('subscribe', 'channel', 0)));
  709. $callable = $this->getMock('stdClass', array('__invoke'));
  710. $callable
  711. ->expects($this->once())
  712. ->method('__invoke');
  713. $client = new Client($connection);
  714. $this->assertNull($client->pubSubLoop(array('subscribe' => 'channel'), $callable));
  715. }
  716. /**
  717. * @group disconnected
  718. */
  719. public function testPubSubLoopWithCallableReturningFalseStopsPubSubConsumer()
  720. {
  721. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  722. $connection
  723. ->expects($this->at(1))
  724. ->method('read')
  725. ->will($this->returnValue(array('subscribe', 'channel', 1)));
  726. $connection
  727. ->expects($this->at(2))
  728. ->method('writeRequest')
  729. ->with($this->isRedisCommand('UNSUBSCRIBE'));
  730. $connection
  731. ->expects($this->at(3))
  732. ->method('read')
  733. ->will($this->returnValue(array('unsubscribe', 'channel', 0)));
  734. $callable = $this->getMock('stdClass', array('__invoke'));
  735. $callable
  736. ->expects($this->at(0))
  737. ->method('__invoke')
  738. ->will($this->returnValue(false));
  739. $client = new Client($connection);
  740. $this->assertNull($client->pubSubLoop(array('subscribe' => 'channel'), $callable));
  741. }
  742. /**
  743. * @group disconnected
  744. */
  745. public function testTransactionWithoutArgumentsReturnsMultiExec()
  746. {
  747. $client = new Client();
  748. $this->assertInstanceOf('Predis\Transaction\MultiExec', $client->transaction());
  749. }
  750. /**
  751. * @group disconnected
  752. */
  753. public function testTransactionWithArrayReturnsMultiExecTransactionWithOptions()
  754. {
  755. $options = array('cas' => true, 'retry' => 3);
  756. $client = new Client();
  757. $this->assertInstanceOf('Predis\Transaction\MultiExec', $tx = $client->transaction($options));
  758. // I hate this part but reflection is the easiest way in this case.
  759. $property = new \ReflectionProperty($tx, 'modeCAS');
  760. $property->setAccessible(true);
  761. $this->assertSame($options['cas'], $property->getValue($tx));
  762. $property = new \ReflectionProperty($tx, 'attempts');
  763. $property->setAccessible(true);
  764. $this->assertSame($options['retry'], $property->getValue($tx));
  765. }
  766. /**
  767. * @group disconnected
  768. */
  769. public function testTransactionWithArrayAndCallableExecutesMultiExec()
  770. {
  771. // We use CAS here as we don't care about the actual MULTI/EXEC context.
  772. $options = array('cas' => true, 'retry' => 3);
  773. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  774. $connection
  775. ->expects($this->once())
  776. ->method('executeCommand')
  777. ->will($this->returnValue(new Response\Status('QUEUED')));
  778. $callable = $this->getMock('stdClass', array('__invoke'));
  779. $callable
  780. ->expects($this->once())
  781. ->method('__invoke')
  782. ->will($this->returnCallback(function ($tx) { $tx->ping(); }));
  783. $client = new Client($connection);
  784. $client->transaction($options, $callable);
  785. }
  786. /**
  787. * @group disconnected
  788. */
  789. public function testMonitorReturnsMonitorConsumer()
  790. {
  791. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  792. $client = new Client($connection);
  793. $this->assertInstanceOf('Predis\Monitor\Consumer', $monitor = $client->monitor());
  794. }
  795. /**
  796. * @group disconnected
  797. */
  798. public function testClientResendScriptCommandUsingEvalOnNoScriptErrors()
  799. {
  800. $command = $this->getMockForAbstractClass('Predis\Command\ScriptCommand', array(), '', true, true, true, array('parseResponse'));
  801. $command
  802. ->expects($this->once())
  803. ->method('getScript')
  804. ->will($this->returnValue('return redis.call(\'exists\', KEYS[1])'));
  805. $command
  806. ->expects($this->once())
  807. ->method('parseResponse')
  808. ->with('OK')
  809. ->will($this->returnValue(true));
  810. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  811. $connection
  812. ->expects($this->at(0))
  813. ->method('executeCommand')
  814. ->with($command)
  815. ->will($this->returnValue(new Response\Error('NOSCRIPT')));
  816. $connection
  817. ->expects($this->at(1))
  818. ->method('executeCommand')
  819. ->with($this->isRedisCommand('EVAL'))
  820. ->will($this->returnValue('OK'));
  821. $client = new Client($connection);
  822. $this->assertTrue($client->executeCommand($command));
  823. }
  824. /**
  825. * @group disconnected
  826. */
  827. public function testGetIteratorWithTraversableConnections()
  828. {
  829. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6381');
  830. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6382');
  831. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6383');
  832. $aggregate = new \Predis\Connection\Cluster\PredisCluster();
  833. $aggregate->add($connection1);
  834. $aggregate->add($connection2);
  835. $aggregate->add($connection3);
  836. $client = new Client($aggregate);
  837. $iterator = $client->getIterator();
  838. $this->assertInstanceOf('\Predis\Client', $nodeClient = $iterator->current());
  839. $this->assertSame($connection1, $nodeClient->getConnection());
  840. $this->assertSame('127.0.0.1:6381', $iterator->key());
  841. $iterator->next();
  842. $this->assertInstanceOf('\Predis\Client', $nodeClient = $iterator->current());
  843. $this->assertSame($connection2, $nodeClient->getConnection());
  844. $this->assertSame('127.0.0.1:6382', $iterator->key());
  845. $iterator->next();
  846. $this->assertInstanceOf('\Predis\Client', $nodeClient = $iterator->current());
  847. $this->assertSame($connection3, $nodeClient->getConnection());
  848. $this->assertSame('127.0.0.1:6383', $iterator->key());
  849. }
  850. /**
  851. * @group disconnected
  852. * @expectedException \Predis\ClientException
  853. * @expectedExceptionMessage The underlying connection is not traversable
  854. */
  855. public function testGetIteratorWithNonTraversableConnectionThrowsException()
  856. {
  857. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  858. $client = new Client($connection);
  859. $client->getIterator();
  860. }
  861. // ******************************************************************** //
  862. // ---- HELPER METHODS ------------------------------------------------ //
  863. // ******************************************************************** //
  864. /**
  865. * Returns an URI string representation of the specified connection parameters.
  866. *
  867. * @param array $parameters Array of connection parameters.
  868. *
  869. * @return string URI string.
  870. */
  871. protected function getParametersString(array $parameters)
  872. {
  873. $defaults = $this->getDefaultParametersArray();
  874. $scheme = isset($parameters['scheme']) ? $parameters['scheme'] : $defaults['scheme'];
  875. $host = isset($parameters['host']) ? $parameters['host'] : $defaults['host'];
  876. $port = isset($parameters['port']) ? $parameters['port'] : $defaults['port'];
  877. unset($parameters['scheme'], $parameters['host'], $parameters['port']);
  878. $uriString = "$scheme://$host:$port/?";
  879. foreach ($parameters as $k => $v) {
  880. $uriString .= "$k=$v&";
  881. }
  882. return $uriString;
  883. }
  884. /**
  885. * Returns a mock callable simulating an aggregate connection initializer.
  886. *
  887. * @param mixed $parameters Expected connection parameters
  888. *
  889. * @return callable
  890. */
  891. protected function getAggregateInitializer($parameters)
  892. {
  893. $connection = $this->getMock('Predis\Connection\AggregateConnectionInterface');
  894. $callable = $this->getMock('stdClass', array('__invoke'));
  895. $callable
  896. ->expects($this->once())
  897. ->method('__invoke')
  898. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'), $parameters)
  899. ->will($this->returnValue($connection));
  900. return $callable;
  901. }
  902. }