ClientTest.php 20 KB

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