ClientTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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 \PHPUnit_Framework_TestCase as StandardTestCase;
  12. use Predis\Profiles\ServerProfile;
  13. use Predis\Network\PredisCluster;
  14. use Predis\Network\MasterSlaveReplication;
  15. /**
  16. *
  17. */
  18. class ClientTest extends StandardTestCase
  19. {
  20. /**
  21. * @group disconnected
  22. */
  23. public function testConstructorWithoutArguments()
  24. {
  25. $client = new Client();
  26. $connection = $client->getConnection();
  27. $this->assertInstanceOf('Predis\Network\IConnectionSingle', $connection);
  28. $parameters = $connection->getParameters();
  29. $this->assertSame($parameters->host, '127.0.0.1');
  30. $this->assertSame($parameters->port, 6379);
  31. $options = $client->getOptions();
  32. $this->assertSame($options->profile->getVersion(), ServerProfile::getDefault()->getVersion());
  33. $this->assertFalse($client->isConnected());
  34. }
  35. /**
  36. * @group disconnected
  37. */
  38. public function testConstructorWithNullArgument()
  39. {
  40. $client = new Client(null);
  41. $connection = $client->getConnection();
  42. $this->assertInstanceOf('Predis\Network\IConnectionSingle', $connection);
  43. $parameters = $connection->getParameters();
  44. $this->assertSame($parameters->host, '127.0.0.1');
  45. $this->assertSame($parameters->port, 6379);
  46. $options = $client->getOptions();
  47. $this->assertSame($options->profile->getVersion(), ServerProfile::getDefault()->getVersion());
  48. $this->assertFalse($client->isConnected());
  49. }
  50. /**
  51. * @group disconnected
  52. */
  53. public function testConstructorWithNullAndNullArguments()
  54. {
  55. $client = new Client(null, null);
  56. $connection = $client->getConnection();
  57. $this->assertInstanceOf('Predis\Network\IConnectionSingle', $connection);
  58. $parameters = $connection->getParameters();
  59. $this->assertSame($parameters->host, '127.0.0.1');
  60. $this->assertSame($parameters->port, 6379);
  61. $options = $client->getOptions();
  62. $this->assertSame($options->profile->getVersion(), ServerProfile::getDefault()->getVersion());
  63. $this->assertFalse($client->isConnected());
  64. }
  65. /**
  66. * @group disconnected
  67. */
  68. public function testConstructorWithArrayArgument()
  69. {
  70. $client = new Client($arg1 = array('host' => 'localhost', 'port' => 7000));
  71. $parameters = $client->getConnection()->getParameters();
  72. $this->assertSame($parameters->host, $arg1['host']);
  73. $this->assertSame($parameters->port, $arg1['port']);
  74. }
  75. /**
  76. * @group disconnected
  77. */
  78. public function testConstructorWithArrayOfArrayArgument()
  79. {
  80. $arg1 = array(
  81. array('host' => 'localhost', 'port' => 7000),
  82. array('host' => 'localhost', 'port' => 7001),
  83. );
  84. $client = new Client($arg1);
  85. $this->assertInstanceOf('Predis\Network\IConnectionCluster', $client->getConnection());
  86. }
  87. /**
  88. * @group disconnected
  89. */
  90. public function testConstructorWithStringArgument()
  91. {
  92. $client = new Client('tcp://localhost:7000');
  93. $parameters = $client->getConnection()->getParameters();
  94. $this->assertSame($parameters->host, 'localhost');
  95. $this->assertSame($parameters->port, 7000);
  96. }
  97. /**
  98. * @group disconnected
  99. */
  100. public function testConstructorWithArrayOfStringArgument()
  101. {
  102. $client = new Client($arg1 = array('tcp://localhost:7000', 'tcp://localhost:7001'));
  103. $this->assertInstanceOf('Predis\Network\IConnectionCluster', $client->getConnection());
  104. }
  105. /**
  106. * @group disconnected
  107. */
  108. public function testConstructorWithArrayOfConnectionsArgument()
  109. {
  110. $connection1 = $this->getMock('Predis\Network\IConnectionSingle');
  111. $connection2 = $this->getMock('Predis\Network\IConnectionSingle');
  112. $client = new Client(array($connection1, $connection2));
  113. $this->assertInstanceOf('Predis\Network\IConnectionCluster', $cluster = $client->getConnection());
  114. $this->assertSame($connection1, $cluster->getConnectionById(0));
  115. $this->assertSame($connection2, $cluster->getConnectionById(1));
  116. }
  117. /**
  118. * @group disconnected
  119. */
  120. public function testConstructorWithConnectionArgument()
  121. {
  122. $factory = new ConnectionFactory();
  123. $connection = $factory->create('tcp://localhost:7000');
  124. $client = new Client($connection);
  125. $this->assertInstanceOf('Predis\Network\IConnectionSingle', $client->getConnection());
  126. $this->assertSame($connection, $client->getConnection());
  127. $parameters = $client->getConnection()->getParameters();
  128. $this->assertSame($parameters->host, 'localhost');
  129. $this->assertSame($parameters->port, 7000);
  130. }
  131. /**
  132. * @group disconnected
  133. */
  134. public function testConstructorWithClusterArgument()
  135. {
  136. $cluster = new PredisCluster();
  137. $factory = new ConnectionFactory();
  138. $factory->createCluster($cluster, array('tcp://localhost:7000', 'tcp://localhost:7001'));
  139. $client = new Client($cluster);
  140. $this->assertInstanceOf('Predis\Network\IConnectionCluster', $client->getConnection());
  141. $this->assertSame($cluster, $client->getConnection());
  142. }
  143. /**
  144. * @group disconnected
  145. */
  146. public function testConstructorWithReplicationArgument()
  147. {
  148. $replication = new MasterSlaveReplication();
  149. $factory = new ConnectionFactory();
  150. $factory->createReplication($replication, array('tcp://host1?alias=master', 'tcp://host2?alias=slave'));
  151. $client = new Client($replication);
  152. $this->assertInstanceOf('Predis\Network\IConnectionReplication', $client->getConnection());
  153. $this->assertSame($replication, $client->getConnection());
  154. }
  155. /**
  156. * @group disconnected
  157. */
  158. public function testConstructorWithNullAndStringArgument()
  159. {
  160. $client = new Client(null, '2.0');
  161. $this->assertSame($client->getProfile()->getVersion(), ServerProfile::get('2.0')->getVersion());
  162. }
  163. /**
  164. * @group disconnected
  165. */
  166. public function testConstructorWithNullAndProfileArgument()
  167. {
  168. $client = new Client(null, $arg2 = ServerProfile::get('2.0'));
  169. $this->assertSame($client->getProfile(), $arg2);
  170. }
  171. /**
  172. * @group disconnected
  173. */
  174. public function testConstructorWithNullAndArrayArgument()
  175. {
  176. $factory = $this->getMock('Predis\IConnectionFactory');
  177. $arg2 = array('profile' => '2.0', 'prefix' => 'prefix:', 'connections' => $factory);
  178. $client = new Client(null, $arg2);
  179. $profile = $client->getProfile();
  180. $this->assertSame($profile->getVersion(), ServerProfile::get('2.0')->getVersion());
  181. $this->assertInstanceOf('Predis\Commands\Processors\KeyPrefixProcessor', $profile->getProcessor());
  182. $this->assertSame('prefix:', $profile->getProcessor()->getPrefix());
  183. $this->assertSame($factory, $client->getConnectionFactory());
  184. }
  185. /**
  186. * @group disconnected
  187. */
  188. public function testConstructorWithArrayAndOptionReplicationArgument()
  189. {
  190. $arg1 = array('tcp://host1?alias=master', 'tcp://host2?alias=slave');
  191. $arg2 = array('replication' => true);
  192. $client = new Client($arg1, $arg2);
  193. $this->assertInstanceOf('Predis\Network\IConnectionReplication', $connection = $client->getConnection());
  194. $this->assertSame('host1', $connection->getConnectionById('master')->getParameters()->host);
  195. $this->assertSame('host2', $connection->getConnectionById('slave')->getParameters()->host);
  196. }
  197. /**
  198. * @group disconnected
  199. */
  200. public function testConnectAndDisconnect()
  201. {
  202. $connection = $this->getMock('Predis\Network\IConnection');
  203. $connection->expects($this->once())->method('connect');
  204. $connection->expects($this->once())->method('disconnect');
  205. $client = new Client($connection);
  206. $client->connect();
  207. $client->disconnect();
  208. }
  209. /**
  210. * @group disconnected
  211. */
  212. public function testIsConnectedChecksConnectionState()
  213. {
  214. $connection = $this->getMock('Predis\Network\IConnection');
  215. $connection->expects($this->once())->method('isConnected');
  216. $client = new Client($connection);
  217. $client->isConnected();
  218. }
  219. /**
  220. * @group disconnected
  221. */
  222. public function testQuitIsAliasForDisconnect()
  223. {
  224. $connection = $this->getMock('Predis\Network\IConnection');
  225. $connection->expects($this->once())->method('disconnect');
  226. $client = new Client($connection);
  227. $client->quit();
  228. }
  229. /**
  230. * @group disconnected
  231. */
  232. public function testCreatesNewCommandUsingSpecifiedProfile()
  233. {
  234. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  235. $profile = $this->getMock('Predis\Profiles\IServerProfile');
  236. $profile->expects($this->once())
  237. ->method('createCommand')
  238. ->with('ping', array())
  239. ->will($this->returnValue($ping));
  240. $client = new Client(null, $profile);
  241. $this->assertSame($ping, $client->createCommand('ping', array()));
  242. }
  243. /**
  244. * @group disconnected
  245. */
  246. public function testExecuteCommand()
  247. {
  248. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  249. $connection= $this->getMock('Predis\Network\IConnection');
  250. $connection->expects($this->once())
  251. ->method('executeCommand')
  252. ->with($ping)
  253. ->will($this->returnValue(true));
  254. $client = new Client($connection);
  255. $this->assertTrue($client->executeCommand($ping));
  256. }
  257. /**
  258. * @group disconnected
  259. */
  260. public function testExecuteCommandOnEachNode()
  261. {
  262. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  263. $connection1 = $this->getMock('Predis\Network\IConnectionSingle');
  264. $connection1->expects($this->once())
  265. ->method('executeCommand')
  266. ->with($ping)
  267. ->will($this->returnValue(true));
  268. $connection2 = $this->getMock('Predis\Network\IConnectionSingle');
  269. $connection2->expects($this->once())
  270. ->method('executeCommand')
  271. ->with($ping)
  272. ->will($this->returnValue(false));
  273. $client = new Client(array($connection1, $connection2));
  274. $this->assertSame(array(true, false), $client->executeCommandOnShards($ping));
  275. }
  276. /**
  277. * @group disconnected
  278. */
  279. public function testExecuteCommandOnEachNodeButConnectionSingle()
  280. {
  281. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  282. $connection = $this->getMock('Predis\Network\IConnectionSingle');
  283. $connection->expects($this->once())
  284. ->method('executeCommand')
  285. ->with($ping)
  286. ->will($this->returnValue(true));
  287. $client = new Client($connection);
  288. $this->assertSame(array(true), $client->executeCommandOnShards($ping));
  289. }
  290. /**
  291. * @group disconnected
  292. */
  293. public function testCallingRedisCommandExecutesInstanceOfCommand()
  294. {
  295. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  296. $connection = $this->getMock('Predis\Network\IConnection');
  297. $connection->expects($this->once())
  298. ->method('executeCommand')
  299. ->with($this->isInstanceOf('Predis\Commands\ConnectionPing'))
  300. ->will($this->returnValue(true));
  301. $profile = $this->getMock('Predis\Profiles\IServerProfile');
  302. $profile->expects($this->once())
  303. ->method('createCommand')
  304. ->with('ping', array())
  305. ->will($this->returnValue($ping));
  306. $client = $this->getMock('Predis\Client', array('createCommand'), array($connection, $profile));
  307. $this->assertTrue($client->ping());
  308. }
  309. /**
  310. * @group disconnected
  311. * @expectedException Predis\ClientException
  312. * @expectedExceptionMessage 'invalidcommand' is not a registered Redis command
  313. */
  314. public function testThrowsExceptionOnNonRegisteredRedisCommand()
  315. {
  316. $client = new Client();
  317. $client->invalidCommand();
  318. }
  319. /**
  320. * @group disconnected
  321. */
  322. public function testGetConnectionFromClusterWithAlias()
  323. {
  324. $client = new Client(array('tcp://host1?alias=node01', 'tcp://host2?alias=node02'));
  325. $this->assertInstanceOf('Predis\Network\IConnectionCluster', $cluster = $client->getConnection());
  326. $this->assertInstanceOf('Predis\Network\IConnectionSingle', $node01 = $client->getConnection('node01'));
  327. $this->assertInstanceOf('Predis\Network\IConnectionSingle', $node02 = $client->getConnection('node02'));
  328. $this->assertSame('host1', $node01->getParameters()->host);
  329. $this->assertSame('host2', $node02->getParameters()->host);
  330. }
  331. /**
  332. * @group disconnected
  333. * @expectedException Predis\NotSupportedException
  334. * @expectedExceptionMessage Retrieving connections by alias is supported only with aggregated connections (cluster or replication)
  335. */
  336. public function testGetConnectionWithAliasWorksOnlyWithCluster()
  337. {
  338. $client = new Client();
  339. $client->getConnection('node01');
  340. }
  341. /**
  342. * @group disconnected
  343. */
  344. public function testPipelineWithoutArgumentsReturnsPipelineContext()
  345. {
  346. $client = new Client();
  347. $this->assertInstanceOf('Predis\Pipeline\PipelineContext', $pipeline = $client->pipeline());
  348. }
  349. /**
  350. * @group disconnected
  351. */
  352. public function testPipelineWithArrayReturnsPipelineContextWithOptions()
  353. {
  354. $client = new Client();
  355. $executor = $this->getMock('Predis\Pipeline\IPipelineExecutor');
  356. $options = array('executor' => $executor);
  357. $this->assertInstanceOf('Predis\Pipeline\PipelineContext', $pipeline = $client->pipeline($options));
  358. $reflection = new \ReflectionProperty($pipeline, 'executor');
  359. $reflection->setAccessible(true);
  360. $this->assertSame($executor, $reflection->getValue($pipeline));
  361. }
  362. /**
  363. * @group disconnected
  364. */
  365. public function testPipelineWithCallableExecutesPipeline()
  366. {
  367. $callable = $this->getMock('stdClass', array('__invoke'));
  368. $callable->expects($this->once())
  369. ->method('__invoke')
  370. ->with($this->isInstanceOf('Predis\Pipeline\PipelineContext'));
  371. $client = new Client();
  372. $client->pipeline($callable);
  373. }
  374. /**
  375. * @group disconnected
  376. */
  377. public function testPipelineWithArrayAndCallableExecutesPipelineWithOptions()
  378. {
  379. $executor = $this->getMock('Predis\Pipeline\IPipelineExecutor');
  380. $options = array('executor' => $executor);
  381. $test = $this;
  382. $mockCallback = function($pipeline) use($executor, $test) {
  383. $reflection = new \ReflectionProperty($pipeline, 'executor');
  384. $reflection->setAccessible(true);
  385. $test->assertSame($executor, $reflection->getValue($pipeline));
  386. };
  387. $callable = $this->getMock('stdClass', array('__invoke'));
  388. $callable->expects($this->once())
  389. ->method('__invoke')
  390. ->with($this->isInstanceOf('Predis\Pipeline\PipelineContext'))
  391. ->will($this->returnCallback($mockCallback));
  392. $client = new Client();
  393. $client->pipeline($options, $callable);
  394. }
  395. /**
  396. * @group disconnected
  397. */
  398. public function testPubSubWithoutArgumentsReturnsPubSubContext()
  399. {
  400. $client = new Client();
  401. $this->assertInstanceOf('Predis\PubSub\PubSubContext', $pubsub = $client->pubSub());
  402. }
  403. /**
  404. * @group disconnected
  405. */
  406. public function testPubSubWithArrayReturnsPubSubContextWithOptions()
  407. {
  408. $connection = $this->getMock('Predis\Network\IConnectionSingle');
  409. $options = array('subscribe' => 'channel');
  410. $client = new Client($connection);
  411. $this->assertInstanceOf('Predis\PubSub\PubSubContext', $pubsub = $client->pubSub($options));
  412. $reflection = new \ReflectionProperty($pubsub, 'options');
  413. $reflection->setAccessible(true);
  414. $this->assertSame($options, $reflection->getValue($pubsub));
  415. }
  416. /**
  417. * @group disconnected
  418. */
  419. public function testPubSubWithArrayAndCallableExecutesPubSub()
  420. {
  421. // NOTE: we use a subscribe count of 0 in the fake message to trick
  422. // the context and to make it think that it can be closed
  423. // since there are no more subscriptions active.
  424. $message = array('subscribe', 'channel', 0);
  425. $options = array('subscribe' => 'channel');
  426. $connection = $this->getMock('Predis\Network\IConnectionSingle');
  427. $connection->expects($this->once())
  428. ->method('read')
  429. ->will($this->returnValue($message));
  430. $callable = $this->getMock('stdClass', array('__invoke'));
  431. $callable->expects($this->once())
  432. ->method('__invoke');
  433. $client = new Client($connection);
  434. $client->pubSub($options, $callable);
  435. }
  436. /**
  437. * @group disconnected
  438. */
  439. public function testMultiExecWithoutArgumentsReturnsMultiExecContext()
  440. {
  441. $client = new Client();
  442. $this->assertInstanceOf('Predis\Transaction\MultiExecContext', $pubsub = $client->multiExec());
  443. }
  444. /**
  445. * @group disconnected
  446. */
  447. public function testMultiExecWithArrayReturnsMultiExecContextWithOptions()
  448. {
  449. $options = array('cas' => true, 'retry' => 3);
  450. $client = new Client();
  451. $this->assertInstanceOf('Predis\Transaction\MultiExecContext', $tx = $client->multiExec($options));
  452. $reflection = new \ReflectionProperty($tx, 'options');
  453. $reflection->setAccessible(true);
  454. $this->assertSame($options, $reflection->getValue($tx));
  455. }
  456. /**
  457. * @group disconnected
  458. */
  459. public function testMultiExecWithArrayAndCallableExecutesMultiExec()
  460. {
  461. // NOTE: we use CAS since testing the actual MULTI/EXEC context
  462. // here is not the point.
  463. $options = array('cas' => true, 'retry' => 3);
  464. $connection = $this->getMock('Predis\Network\IConnectionSingle');
  465. $connection->expects($this->once())
  466. ->method('executeCommand')
  467. ->will($this->returnValue(new ResponseQueued()));
  468. $txCallback = function($tx) { $tx->ping(); };
  469. $callable = $this->getMock('stdClass', array('__invoke'));
  470. $callable->expects($this->once())
  471. ->method('__invoke')
  472. ->will($this->returnCallback($txCallback));
  473. $client = new Client($connection);
  474. $client->multiExec($options, $callable);
  475. }
  476. /**
  477. * @group disconnected
  478. */
  479. public function testMonitorReturnsMonitorContext()
  480. {
  481. $connection = $this->getMock('Predis\Network\IConnectionSingle');
  482. $client = new Client($connection);
  483. $this->assertInstanceOf('Predis\MonitorContext', $monitor = $client->monitor());
  484. }
  485. // ******************************************************************** //
  486. // ---- HELPER METHODS ------------------------------------------------ //
  487. // ******************************************************************** //
  488. /**
  489. * Returns a named array with the default connection parameters and their values.
  490. *
  491. * @return Array Default connection parameters.
  492. */
  493. protected function getDefaultParametersArray()
  494. {
  495. return array(
  496. 'scheme' => 'tcp',
  497. 'host' => REDIS_SERVER_HOST,
  498. 'port' => REDIS_SERVER_PORT,
  499. 'database' => REDIS_SERVER_DBNUM,
  500. );
  501. }
  502. /**
  503. * Returns a named array with the default client options and their values.
  504. *
  505. * @return Array Default connection parameters.
  506. */
  507. protected function getDefaultOptionsArray()
  508. {
  509. return array(
  510. 'profile' => REDIS_SERVER_VERSION,
  511. );
  512. }
  513. /**
  514. * Returns a named array with the default connection parameters merged with
  515. * the specified additional parameters.
  516. *
  517. * @param Array $additional Additional connection parameters.
  518. * @return Array Connection parameters.
  519. */
  520. protected function getParametersArray(Array $additional)
  521. {
  522. return array_merge($this->getDefaultParametersArray(), $additional);
  523. }
  524. /**
  525. * Returns an URI string representation of the specified connection parameters.
  526. *
  527. * @param Array $parameters Array of connection parameters.
  528. * @return String URI string.
  529. */
  530. protected function getParametersString(Array $parameters)
  531. {
  532. $defaults = $this->getDefaultParametersArray();
  533. $scheme = isset($parameters['scheme']) ? $parameters['scheme'] : $defaults['scheme'];
  534. $host = isset($parameters['host']) ? $parameters['host'] : $defaults['host'];
  535. $port = isset($parameters['port']) ? $parameters['port'] : $defaults['port'];
  536. unset($parameters['scheme'], $parameters['host'], $parameters['port']);
  537. $uriString = "$scheme://$host:$port/?";
  538. foreach ($parameters as $k => $v) {
  539. $uriString .= "$k=$v&";
  540. }
  541. return $uriString;
  542. }
  543. }