ClientTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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\Profile\ServerProfile;
  13. use Predis\Connection\PredisCluster;
  14. use Predis\Connection\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\Connection\SingleConnectionInterface', $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\Connection\SingleConnectionInterface', $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\Connection\SingleConnectionInterface', $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\Connection\ClusterConnectionInterface', $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\Connection\ClusterConnectionInterface', $client->getConnection());
  104. }
  105. /**
  106. * @group disconnected
  107. */
  108. public function testConstructorWithArrayOfConnectionsArgument()
  109. {
  110. $connection1 = $this->getMock('Predis\Connection\SingleConnectionInterface');
  111. $connection2 = $this->getMock('Predis\Connection\SingleConnectionInterface');
  112. $client = new Client(array($connection1, $connection2));
  113. $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $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\Connection\SingleConnectionInterface', $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->createAggregated($cluster, array('tcp://localhost:7000', 'tcp://localhost:7001'));
  139. $client = new Client($cluster);
  140. $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $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->createAggregated($replication, array('tcp://host1?alias=master', 'tcp://host2?alias=slave'));
  151. $client = new Client($replication);
  152. $this->assertInstanceOf('Predis\Connection\ReplicationConnectionInterface', $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\ConnectionFactoryInterface');
  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\Command\Processor\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\Connection\ReplicationConnectionInterface', $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\Connection\ConnectionInterface');
  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\Connection\ConnectionInterface');
  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\Connection\ConnectionInterface');
  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\Profile\ServerProfileInterface');
  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\Connection\ConnectionInterface');
  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 testCallingRedisCommandExecutesInstanceOfCommand()
  261. {
  262. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  263. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  264. $connection->expects($this->once())
  265. ->method('executeCommand')
  266. ->with($this->isInstanceOf('Predis\Command\ConnectionPing'))
  267. ->will($this->returnValue(true));
  268. $profile = $this->getMock('Predis\Profile\ServerProfileInterface');
  269. $profile->expects($this->once())
  270. ->method('createCommand')
  271. ->with('ping', array())
  272. ->will($this->returnValue($ping));
  273. $client = $this->getMock('Predis\Client', array('createCommand'), array($connection, $profile));
  274. $this->assertTrue($client->ping());
  275. }
  276. /**
  277. * @group disconnected
  278. * @expectedException Predis\ClientException
  279. * @expectedExceptionMessage 'invalidcommand' is not a registered Redis command
  280. */
  281. public function testThrowsExceptionOnNonRegisteredRedisCommand()
  282. {
  283. $client = new Client();
  284. $client->invalidCommand();
  285. }
  286. /**
  287. * @group disconnected
  288. */
  289. public function testGetConnectionFromClusterWithAlias()
  290. {
  291. $client = new Client(array('tcp://host1?alias=node01', 'tcp://host2?alias=node02'));
  292. $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $cluster = $client->getConnection());
  293. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node01 = $client->getConnection('node01'));
  294. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node02 = $client->getConnection('node02'));
  295. $this->assertSame('host1', $node01->getParameters()->host);
  296. $this->assertSame('host2', $node02->getParameters()->host);
  297. }
  298. /**
  299. * @group disconnected
  300. * @expectedException Predis\NotSupportedException
  301. * @expectedExceptionMessage Retrieving connections by alias is supported only with aggregated connections (cluster or replication)
  302. */
  303. public function testGetConnectionWithAliasWorksOnlyWithCluster()
  304. {
  305. $client = new Client();
  306. $client->getConnection('node01');
  307. }
  308. /**
  309. * @group disconnected
  310. */
  311. public function testPipelineWithoutArgumentsReturnsPipelineContext()
  312. {
  313. $client = new Client();
  314. $this->assertInstanceOf('Predis\Pipeline\PipelineContext', $pipeline = $client->pipeline());
  315. }
  316. /**
  317. * @group disconnected
  318. */
  319. public function testPipelineWithArrayReturnsPipelineContextWithOptions()
  320. {
  321. $client = new Client();
  322. $executor = $this->getMock('Predis\Pipeline\PipelineExecutorInterface');
  323. $options = array('executor' => $executor);
  324. $this->assertInstanceOf('Predis\Pipeline\PipelineContext', $pipeline = $client->pipeline($options));
  325. $reflection = new \ReflectionProperty($pipeline, 'executor');
  326. $reflection->setAccessible(true);
  327. $this->assertSame($executor, $reflection->getValue($pipeline));
  328. }
  329. /**
  330. * @group disconnected
  331. */
  332. public function testPipelineWithCallableExecutesPipeline()
  333. {
  334. $callable = $this->getMock('stdClass', array('__invoke'));
  335. $callable->expects($this->once())
  336. ->method('__invoke')
  337. ->with($this->isInstanceOf('Predis\Pipeline\PipelineContext'));
  338. $client = new Client();
  339. $client->pipeline($callable);
  340. }
  341. /**
  342. * @group disconnected
  343. */
  344. public function testPipelineWithArrayAndCallableExecutesPipelineWithOptions()
  345. {
  346. $executor = $this->getMock('Predis\Pipeline\PipelineExecutorInterface');
  347. $options = array('executor' => $executor);
  348. $test = $this;
  349. $mockCallback = function($pipeline) use($executor, $test) {
  350. $reflection = new \ReflectionProperty($pipeline, 'executor');
  351. $reflection->setAccessible(true);
  352. $test->assertSame($executor, $reflection->getValue($pipeline));
  353. };
  354. $callable = $this->getMock('stdClass', array('__invoke'));
  355. $callable->expects($this->once())
  356. ->method('__invoke')
  357. ->with($this->isInstanceOf('Predis\Pipeline\PipelineContext'))
  358. ->will($this->returnCallback($mockCallback));
  359. $client = new Client();
  360. $client->pipeline($options, $callable);
  361. }
  362. /**
  363. * @group disconnected
  364. */
  365. public function testPubSubWithoutArgumentsReturnsPubSubContext()
  366. {
  367. $client = new Client();
  368. $this->assertInstanceOf('Predis\PubSub\PubSubContext', $pubsub = $client->pubSub());
  369. }
  370. /**
  371. * @group disconnected
  372. */
  373. public function testPubSubWithArrayReturnsPubSubContextWithOptions()
  374. {
  375. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  376. $options = array('subscribe' => 'channel');
  377. $client = new Client($connection);
  378. $this->assertInstanceOf('Predis\PubSub\PubSubContext', $pubsub = $client->pubSub($options));
  379. $reflection = new \ReflectionProperty($pubsub, 'options');
  380. $reflection->setAccessible(true);
  381. $this->assertSame($options, $reflection->getValue($pubsub));
  382. }
  383. /**
  384. * @group disconnected
  385. */
  386. public function testPubSubWithArrayAndCallableExecutesPubSub()
  387. {
  388. // NOTE: we use a subscribe count of 0 in the fake message to trick
  389. // the context and to make it think that it can be closed
  390. // since there are no more subscriptions active.
  391. $message = array('subscribe', 'channel', 0);
  392. $options = array('subscribe' => 'channel');
  393. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  394. $connection->expects($this->once())
  395. ->method('read')
  396. ->will($this->returnValue($message));
  397. $callable = $this->getMock('stdClass', array('__invoke'));
  398. $callable->expects($this->once())
  399. ->method('__invoke');
  400. $client = new Client($connection);
  401. $client->pubSub($options, $callable);
  402. }
  403. /**
  404. * @group disconnected
  405. */
  406. public function testMultiExecWithoutArgumentsReturnsMultiExecContext()
  407. {
  408. $client = new Client();
  409. $this->assertInstanceOf('Predis\Transaction\MultiExecContext', $pubsub = $client->multiExec());
  410. }
  411. /**
  412. * @group disconnected
  413. */
  414. public function testMultiExecWithArrayReturnsMultiExecContextWithOptions()
  415. {
  416. $options = array('cas' => true, 'retry' => 3);
  417. $client = new Client();
  418. $this->assertInstanceOf('Predis\Transaction\MultiExecContext', $tx = $client->multiExec($options));
  419. $reflection = new \ReflectionProperty($tx, 'options');
  420. $reflection->setAccessible(true);
  421. $this->assertSame($options, $reflection->getValue($tx));
  422. }
  423. /**
  424. * @group disconnected
  425. */
  426. public function testMultiExecWithArrayAndCallableExecutesMultiExec()
  427. {
  428. // NOTE: we use CAS since testing the actual MULTI/EXEC context
  429. // here is not the point.
  430. $options = array('cas' => true, 'retry' => 3);
  431. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  432. $connection->expects($this->once())
  433. ->method('executeCommand')
  434. ->will($this->returnValue(new ResponseQueued()));
  435. $txCallback = function($tx) { $tx->ping(); };
  436. $callable = $this->getMock('stdClass', array('__invoke'));
  437. $callable->expects($this->once())
  438. ->method('__invoke')
  439. ->will($this->returnCallback($txCallback));
  440. $client = new Client($connection);
  441. $client->multiExec($options, $callable);
  442. }
  443. /**
  444. * @group disconnected
  445. */
  446. public function testMonitorReturnsMonitorContext()
  447. {
  448. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  449. $client = new Client($connection);
  450. $this->assertInstanceOf('Predis\Monitor\MonitorContext', $monitor = $client->monitor());
  451. }
  452. // ******************************************************************** //
  453. // ---- HELPER METHODS ------------------------------------------------ //
  454. // ******************************************************************** //
  455. /**
  456. * Returns a named array with the default connection parameters and their values.
  457. *
  458. * @return Array Default connection parameters.
  459. */
  460. protected function getDefaultParametersArray()
  461. {
  462. return array(
  463. 'scheme' => 'tcp',
  464. 'host' => REDIS_SERVER_HOST,
  465. 'port' => REDIS_SERVER_PORT,
  466. 'database' => REDIS_SERVER_DBNUM,
  467. );
  468. }
  469. /**
  470. * Returns a named array with the default client options and their values.
  471. *
  472. * @return Array Default connection parameters.
  473. */
  474. protected function getDefaultOptionsArray()
  475. {
  476. return array(
  477. 'profile' => REDIS_SERVER_VERSION,
  478. );
  479. }
  480. /**
  481. * Returns a named array with the default connection parameters merged with
  482. * the specified additional parameters.
  483. *
  484. * @param Array $additional Additional connection parameters.
  485. * @return Array Connection parameters.
  486. */
  487. protected function getParametersArray(Array $additional)
  488. {
  489. return array_merge($this->getDefaultParametersArray(), $additional);
  490. }
  491. /**
  492. * Returns an URI string representation of the specified connection parameters.
  493. *
  494. * @param Array $parameters Array of connection parameters.
  495. * @return String URI string.
  496. */
  497. protected function getParametersString(Array $parameters)
  498. {
  499. $defaults = $this->getDefaultParametersArray();
  500. $scheme = isset($parameters['scheme']) ? $parameters['scheme'] : $defaults['scheme'];
  501. $host = isset($parameters['host']) ? $parameters['host'] : $defaults['host'];
  502. $port = isset($parameters['port']) ? $parameters['port'] : $defaults['port'];
  503. unset($parameters['scheme'], $parameters['host'], $parameters['port']);
  504. $uriString = "$scheme://$host:$port/?";
  505. foreach ($parameters as $k => $v) {
  506. $uriString .= "$k=$v&";
  507. }
  508. return $uriString;
  509. }
  510. }