RedisClusterTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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\Connection;
  11. use PredisTestCase;
  12. use Predis\ResponseError;
  13. use Predis\Profile\ServerProfile;
  14. /**
  15. *
  16. */
  17. class RedisClusterTest extends PredisTestCase
  18. {
  19. /**
  20. * @group disconnected
  21. */
  22. public function testExposesCommandHashStrategy()
  23. {
  24. $cluster = new RedisCluster();
  25. $this->assertInstanceOf('Predis\Cluster\RedisClusterHashStrategy', $cluster->getCommandHashStrategy());
  26. }
  27. /**
  28. * @group disconnected
  29. */
  30. public function testAddingConnectionsToCluster()
  31. {
  32. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  33. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  34. $cluster = new RedisCluster();
  35. $cluster->add($connection1);
  36. $cluster->add($connection2);
  37. $this->assertSame(2, count($cluster));
  38. $this->assertSame($connection1, $cluster->getConnectionById('127.0.0.1:6379'));
  39. $this->assertSame($connection2, $cluster->getConnectionById('127.0.0.1:6380'));
  40. }
  41. /**
  42. * @group disconnected
  43. */
  44. public function testRemovingConnectionsFromCluster()
  45. {
  46. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  47. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  48. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6371');
  49. $cluster = new RedisCluster();
  50. $cluster->add($connection1);
  51. $cluster->add($connection2);
  52. $this->assertTrue($cluster->remove($connection1));
  53. $this->assertFalse($cluster->remove($connection3));
  54. $this->assertSame(1, count($cluster));
  55. }
  56. /**
  57. * @group disconnected
  58. */
  59. public function testRemovingConnectionsFromClusterByAlias()
  60. {
  61. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  62. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  63. $cluster = new RedisCluster();
  64. $cluster->add($connection1);
  65. $cluster->add($connection2);
  66. $this->assertTrue($cluster->removeById('127.0.0.1:6380'));
  67. $this->assertFalse($cluster->removeById('127.0.0.1:6390'));
  68. $this->assertSame(1, count($cluster));
  69. }
  70. /**
  71. * @group disconnected
  72. */
  73. public function testCountReturnsNumberOfConnectionsInPool()
  74. {
  75. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  76. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  77. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  78. $cluster = new RedisCluster();
  79. $cluster->add($connection1);
  80. $cluster->add($connection2);
  81. $cluster->add($connection3);
  82. $this->assertSame(3, count($cluster));
  83. $cluster->remove($connection3);
  84. $this->assertSame(2, count($cluster));
  85. }
  86. /**
  87. * @group disconnected
  88. */
  89. public function testConnectPicksRandomConnection()
  90. {
  91. $connect1 = false;
  92. $connect2 = false;
  93. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  94. $connection1->expects($this->any())
  95. ->method('connect')
  96. ->will($this->returnCallback(function () use (&$connect1) {
  97. $connect1 = true;
  98. }));
  99. $connection1->expects($this->any())
  100. ->method('isConnected')
  101. ->will($this->returnCallback(function () use (&$connect1) {
  102. return $connect1;
  103. }));
  104. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  105. $connection2->expects($this->any())
  106. ->method('connect')
  107. ->will($this->returnCallback(function () use (&$connect2) {
  108. $connect2 = true;
  109. }));
  110. $connection2->expects($this->any())
  111. ->method('isConnected')
  112. ->will($this->returnCallback(function () use (&$connect2) {
  113. return $connect2;
  114. }));
  115. $cluster = new RedisCluster();
  116. $cluster->add($connection1);
  117. $cluster->add($connection2);
  118. $cluster->connect();
  119. $this->assertTrue($cluster->isConnected());
  120. if ($connect1) {
  121. $this->assertTrue($connect1);
  122. $this->assertFalse($connect2);
  123. } else {
  124. $this->assertFalse($connect1);
  125. $this->assertTrue($connect2);
  126. }
  127. }
  128. /**
  129. * @group disconnected
  130. */
  131. public function testDisconnectForcesAllConnectionsToDisconnect()
  132. {
  133. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  134. $connection1->expects($this->once())->method('disconnect');
  135. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  136. $connection2->expects($this->once())->method('disconnect');
  137. $cluster = new RedisCluster();
  138. $cluster->add($connection1);
  139. $cluster->add($connection2);
  140. $cluster->disconnect();
  141. }
  142. /**
  143. * @group disconnected
  144. */
  145. public function testIsConnectedReturnsTrueIfAtLeastOneConnectionIsOpen()
  146. {
  147. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  148. $connection1->expects($this->once())
  149. ->method('isConnected')
  150. ->will($this->returnValue(false));
  151. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  152. $connection2->expects($this->once())
  153. ->method('isConnected')
  154. ->will($this->returnValue(true));
  155. $cluster = new RedisCluster();
  156. $cluster->add($connection1);
  157. $cluster->add($connection2);
  158. $this->assertTrue($cluster->isConnected());
  159. }
  160. /**
  161. * @group disconnected
  162. */
  163. public function testIsConnectedReturnsFalseIfAllConnectionsAreClosed()
  164. {
  165. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  166. $connection1->expects($this->once())
  167. ->method('isConnected')
  168. ->will($this->returnValue(false));
  169. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  170. $connection2->expects($this->once())
  171. ->method('isConnected')
  172. ->will($this->returnValue(false));
  173. $cluster = new RedisCluster();
  174. $cluster->add($connection1);
  175. $cluster->add($connection2);
  176. $this->assertFalse($cluster->isConnected());
  177. }
  178. /**
  179. * @group disconnected
  180. */
  181. public function testCanReturnAnIteratorForConnections()
  182. {
  183. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  184. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  185. $cluster = new RedisCluster();
  186. $cluster->add($connection1);
  187. $cluster->add($connection2);
  188. $this->assertInstanceOf('Iterator', $iterator = $cluster->getIterator());
  189. $connections = iterator_to_array($iterator);
  190. $this->assertSame($connection1, $connections[0]);
  191. $this->assertSame($connection2, $connections[1]);
  192. }
  193. /**
  194. * @group disconnected
  195. */
  196. public function testCanAssignConnectionsToCustomSlots()
  197. {
  198. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  199. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  200. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  201. $cluster = new RedisCluster();
  202. $cluster->add($connection1);
  203. $cluster->add($connection2);
  204. $cluster->add($connection3);
  205. $cluster->setSlots(0, 1364, '127.0.0.1:6379');
  206. $cluster->setSlots(1365, 2729, '127.0.0.1:6380');
  207. $cluster->setSlots(2730, 4095, '127.0.0.1:6381');
  208. $expectedMap = array_merge(
  209. array_fill(0, 1365, '127.0.0.1:6379'),
  210. array_fill(1364, 1365, '127.0.0.1:6380'),
  211. array_fill(2729, 1366, '127.0.0.1:6381')
  212. );
  213. $this->assertSame($expectedMap, $cluster->getSlotsMap());
  214. }
  215. /**
  216. * @group disconnected
  217. */
  218. public function testAddingConnectionResetsSlotsMap()
  219. {
  220. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  221. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  222. $cluster = new RedisCluster();
  223. $cluster->add($connection1);
  224. $cluster->setSlots(0, 4095, '127.0.0.1:6379');
  225. $this->assertSame(array_fill(0, 4096, '127.0.0.1:6379'), $cluster->getSlotsMap());
  226. $cluster->add($connection2);
  227. $this->assertEmpty($cluster->getSlotsMap());
  228. }
  229. /**
  230. * @group disconnected
  231. */
  232. public function testRemovingConnectionResetsSlotsMap()
  233. {
  234. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  235. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  236. $cluster = new RedisCluster();
  237. $cluster->add($connection1);
  238. $cluster->add($connection2);
  239. $cluster->setSlots(0, 2047, '127.0.0.1:6379');
  240. $cluster->setSlots(2048, 4095, '127.0.0.1:6380');
  241. $expectedMap = array_merge(
  242. array_fill(0, 2048, '127.0.0.1:6379'),
  243. array_fill(2048, 2048, '127.0.0.1:6380')
  244. );
  245. $this->assertSame($expectedMap, $cluster->getSlotsMap());
  246. $cluster->remove($connection1);
  247. $this->assertEmpty($cluster->getSlotsMap());
  248. }
  249. /**
  250. * @group disconnected
  251. */
  252. public function testCanAssignConnectionsToCustomSlotsFromParameters()
  253. {
  254. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379?slots=0-5460');
  255. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380?slots=5461-10921');
  256. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=10922-16383');
  257. $cluster = new RedisCluster();
  258. $cluster->add($connection1);
  259. $cluster->add($connection2);
  260. $cluster->add($connection3);
  261. $expectedMap = array_merge(
  262. array_fill(0, 5461, '127.0.0.1:6379'),
  263. array_fill(5460, 5461, '127.0.0.1:6380'),
  264. array_fill(10921, 5462, '127.0.0.1:6381')
  265. );
  266. $cluster->buildSlotsMap();
  267. $this->assertSame($expectedMap, $cluster->getSlotsMap());
  268. }
  269. /**
  270. * @group disconnected
  271. */
  272. public function testReturnsCorrectConnectionUsingSlotID()
  273. {
  274. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  275. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  276. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  277. $cluster = new RedisCluster();
  278. $cluster->add($connection1);
  279. $cluster->add($connection2);
  280. $cluster->add($connection3);
  281. $this->assertSame($connection1, $cluster->getConnectionBySlot(0));
  282. $this->assertSame($connection2, $cluster->getConnectionBySlot(5461));
  283. $this->assertSame($connection3, $cluster->getConnectionBySlot(10922));
  284. $cluster->setSlots(5461, 7096, '127.0.0.1:6380');
  285. $this->assertSame($connection2, $cluster->getConnectionBySlot(5461));
  286. }
  287. /**
  288. * @group disconnected
  289. */
  290. public function testReturnsCorrectConnectionUsingCommandInstance()
  291. {
  292. $profile = ServerProfile::getDefault();
  293. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  294. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  295. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  296. $cluster = new RedisCluster();
  297. $cluster->add($connection1);
  298. $cluster->add($connection2);
  299. $cluster->add($connection3);
  300. $set = $profile->createCommand('set', array('node:1001', 'foobar'));
  301. $get = $profile->createCommand('get', array('node:1001'));
  302. $this->assertSame($connection1, $cluster->getConnection($set));
  303. $this->assertSame($connection1, $cluster->getConnection($get));
  304. $set = $profile->createCommand('set', array('node:1048', 'foobar'));
  305. $get = $profile->createCommand('get', array('node:1048'));
  306. $this->assertSame($connection2, $cluster->getConnection($set));
  307. $this->assertSame($connection2, $cluster->getConnection($get));
  308. $set = $profile->createCommand('set', array('node:1082', 'foobar'));
  309. $get = $profile->createCommand('get', array('node:1082'));
  310. $this->assertSame($connection3, $cluster->getConnection($set));
  311. $this->assertSame($connection3, $cluster->getConnection($get));
  312. }
  313. /**
  314. * @group disconnected
  315. */
  316. public function testWritesCommandToCorrectConnection()
  317. {
  318. $command = ServerProfile::getDefault()->createCommand('get', array('node:1001'));
  319. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  320. $connection1->expects($this->once())->method('writeCommand')->with($command);
  321. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  322. $connection2->expects($this->never())->method('writeCommand');
  323. $cluster = new RedisCluster();
  324. $cluster->add($connection1);
  325. $cluster->add($connection2);
  326. $cluster->writeCommand($command);
  327. }
  328. /**
  329. * @group disconnected
  330. */
  331. public function testReadsCommandFromCorrectConnection()
  332. {
  333. $command = ServerProfile::getDefault()->createCommand('get', array('node:1050'));
  334. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  335. $connection1->expects($this->never())->method('readResponse');
  336. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  337. $connection2->expects($this->once())->method('readResponse')->with($command);
  338. $cluster = new RedisCluster();
  339. $cluster->add($connection1);
  340. $cluster->add($connection2);
  341. $cluster->readResponse($command);
  342. }
  343. /**
  344. * @group disconnected
  345. */
  346. public function testDoesNotSupportKeyTags()
  347. {
  348. $profile = ServerProfile::getDefault();
  349. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  350. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  351. $cluster = new RedisCluster();
  352. $cluster->add($connection1);
  353. $cluster->add($connection2);
  354. $set = $profile->createCommand('set', array('{node:1001}:foo', 'foobar'));
  355. $get = $profile->createCommand('get', array('{node:1001}:foo'));
  356. $this->assertSame($connection1, $cluster->getConnection($set));
  357. $this->assertSame($connection1, $cluster->getConnection($get));
  358. $set = $profile->createCommand('set', array('{node:1001}:bar', 'foobar'));
  359. $get = $profile->createCommand('get', array('{node:1001}:bar'));
  360. $this->assertSame($connection2, $cluster->getConnection($set));
  361. $this->assertSame($connection2, $cluster->getConnection($get));
  362. }
  363. /**
  364. * @group disconnected
  365. */
  366. public function testAskResponseWithConnectionInPool()
  367. {
  368. $askResponse = new ResponseError('ASK 1970 127.0.0.1:6380');
  369. $command = ServerProfile::getDefault()->createCommand('get', array('node:1001'));
  370. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  371. $connection1->expects($this->exactly(2))
  372. ->method('executeCommand')
  373. ->with($command)
  374. ->will($this->onConsecutiveCalls($askResponse, 'foobar'));
  375. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  376. $connection2->expects($this->at(2))
  377. ->method('executeCommand')
  378. ->with($this->isRedisCommand('ASKING'));
  379. $connection2->expects($this->at(3))
  380. ->method('executeCommand')
  381. ->with($command)
  382. ->will($this->returnValue('foobar'));
  383. $factory = $this->getMock('Predis\Connection\ConnectionFactory');
  384. $factory->expects($this->never())->method('create');
  385. $cluster = new RedisCluster($factory);
  386. $cluster->add($connection1);
  387. $cluster->add($connection2);
  388. $this->assertSame('foobar', $cluster->executeCommand($command));
  389. $this->assertSame('foobar', $cluster->executeCommand($command));
  390. $this->assertSame(2, count($cluster));
  391. }
  392. /**
  393. * @group disconnected
  394. */
  395. public function testAskResponseWithConnectionNotInPool()
  396. {
  397. $askResponse = new ResponseError('ASK 1970 127.0.0.1:6381');
  398. $command = ServerProfile::getDefault()->createCommand('get', array('node:1001'));
  399. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  400. $connection1->expects($this->exactly(2))
  401. ->method('executeCommand')
  402. ->with($command)
  403. ->will($this->onConsecutiveCalls($askResponse, 'foobar'));
  404. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  405. $connection2->expects($this->never())
  406. ->method('executeCommand');
  407. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  408. $connection3->expects($this->at(0))
  409. ->method('executeCommand')
  410. ->with($this->isRedisCommand('ASKING'));
  411. $connection3->expects($this->at(1))
  412. ->method('executeCommand')
  413. ->with($command)
  414. ->will($this->returnValue('foobar'));
  415. $factory = $this->getMock('Predis\Connection\ConnectionFactory');
  416. $factory->expects($this->once())
  417. ->method('create')
  418. ->with(array('host' => '127.0.0.1', 'port' => '6381'))
  419. ->will($this->returnValue($connection3));
  420. $cluster = new RedisCluster($factory);
  421. $cluster->add($connection1);
  422. $cluster->add($connection2);
  423. $this->assertSame('foobar', $cluster->executeCommand($command));
  424. $this->assertSame('foobar', $cluster->executeCommand($command));
  425. $this->assertSame(2, count($cluster));
  426. }
  427. /**
  428. * @group disconnected
  429. */
  430. public function testMovedResponseWithConnectionInPool()
  431. {
  432. $movedResponse = new ResponseError('MOVED 1970 127.0.0.1:6380');
  433. $command = ServerProfile::getDefault()->createCommand('get', array('node:1001'));
  434. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  435. $connection1->expects($this->exactly(1))
  436. ->method('executeCommand')
  437. ->with($command)
  438. ->will($this->returnValue($movedResponse));
  439. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  440. $connection2->expects($this->exactly(2))
  441. ->method('executeCommand')
  442. ->with($command)
  443. ->will($this->onConsecutiveCalls('foobar', 'foobar'));
  444. $factory = $this->getMock('Predis\Connection\ConnectionFactory');
  445. $factory->expects($this->never())->method('create');
  446. $cluster = new RedisCluster($factory);
  447. $cluster->add($connection1);
  448. $cluster->add($connection2);
  449. $this->assertSame('foobar', $cluster->executeCommand($command));
  450. $this->assertSame('foobar', $cluster->executeCommand($command));
  451. $this->assertSame(2, count($cluster));
  452. }
  453. /**
  454. * @group disconnected
  455. */
  456. public function testMovedResponseWithConnectionNotInPool()
  457. {
  458. $movedResponse = new ResponseError('MOVED 1970 127.0.0.1:6381');
  459. $command = ServerProfile::getDefault()->createCommand('get', array('node:1001'));
  460. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  461. $connection1->expects($this->once())
  462. ->method('executeCommand')
  463. ->with($command)
  464. ->will($this->returnValue($movedResponse));
  465. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  466. $connection2->expects($this->never())
  467. ->method('executeCommand');
  468. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  469. $connection3->expects($this->exactly(2))
  470. ->method('executeCommand')
  471. ->with($command)
  472. ->will($this->onConsecutiveCalls('foobar', 'foobar'));
  473. $factory = $this->getMock('Predis\Connection\ConnectionFactory');
  474. $factory->expects($this->once())
  475. ->method('create')
  476. ->with(array('host' => '127.0.0.1', 'port' => '6381'))
  477. ->will($this->returnValue($connection3));
  478. $cluster = new RedisCluster($factory);
  479. $cluster->add($connection1);
  480. $cluster->add($connection2);
  481. $this->assertSame('foobar', $cluster->executeCommand($command));
  482. $this->assertSame('foobar', $cluster->executeCommand($command));
  483. $this->assertSame(3, count($cluster));
  484. }
  485. /**
  486. * @group disconnected
  487. * @expectedException Predis\NotSupportedException
  488. * @expectedExceptionMessage Cannot use PING with redis-cluster
  489. */
  490. public function testThrowsExceptionOnNonSupportedCommand()
  491. {
  492. $ping = ServerProfile::getDefault()->createCommand('ping');
  493. $cluster = new RedisCluster();
  494. $cluster->add($this->getMockConnection('tcp://127.0.0.1:6379'));
  495. $cluster->getConnection($ping);
  496. }
  497. /**
  498. * @group disconnected
  499. */
  500. public function testCanBeSerialized()
  501. {
  502. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379?slots=0-1364');
  503. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380?slots=1365-2729');
  504. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=2730-4095');
  505. $cluster = new RedisCluster();
  506. $cluster->add($connection1);
  507. $cluster->add($connection2);
  508. $cluster->add($connection3);
  509. $cluster->buildSlotsMap();
  510. $unserialized = unserialize(serialize($cluster));
  511. $this->assertEquals($cluster, $unserialized);
  512. }
  513. // ******************************************************************** //
  514. // ---- HELPER METHODS ------------------------------------------------ //
  515. // ******************************************************************** //
  516. /**
  517. * Returns a base mocked connection from Predis\Connection\SingleConnectionInterface.
  518. *
  519. * @param mixed $parameters Optional parameters.
  520. * @return mixed
  521. */
  522. protected function getMockConnection($parameters = null)
  523. {
  524. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  525. if ($parameters) {
  526. $parameters = new ConnectionParameters($parameters);
  527. $hash = "{$parameters->host}:{$parameters->port}";
  528. $connection->expects($this->any())
  529. ->method('getParameters')
  530. ->will($this->returnValue($parameters));
  531. $connection->expects($this->any())
  532. ->method('__toString')
  533. ->will($this->returnValue($hash));
  534. }
  535. return $connection;
  536. }
  537. }