ClientTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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\Connection\ConnectionFactory;
  13. use Predis\Connection\MasterSlaveReplication;
  14. use Predis\Connection\PredisCluster;
  15. use Predis\Profile\ServerProfile;
  16. use Predis\Response\ResponseError;
  17. use Predis\Response\ResponseQueued;
  18. /**
  19. *
  20. */
  21. class ClientTest extends StandardTestCase
  22. {
  23. /**
  24. * @group disconnected
  25. */
  26. public function testConstructorWithoutArguments()
  27. {
  28. $client = new Client();
  29. $connection = $client->getConnection();
  30. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $connection);
  31. $parameters = $connection->getParameters();
  32. $this->assertSame($parameters->host, '127.0.0.1');
  33. $this->assertSame($parameters->port, 6379);
  34. $options = $client->getOptions();
  35. $this->assertSame($options->profile->getVersion(), ServerProfile::getDefault()->getVersion());
  36. $this->assertFalse($client->isConnected());
  37. }
  38. /**
  39. * @group disconnected
  40. */
  41. public function testConstructorWithNullArgument()
  42. {
  43. $client = new Client(null);
  44. $connection = $client->getConnection();
  45. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $connection);
  46. $parameters = $connection->getParameters();
  47. $this->assertSame($parameters->host, '127.0.0.1');
  48. $this->assertSame($parameters->port, 6379);
  49. $options = $client->getOptions();
  50. $this->assertSame($options->profile->getVersion(), ServerProfile::getDefault()->getVersion());
  51. $this->assertFalse($client->isConnected());
  52. }
  53. /**
  54. * @group disconnected
  55. */
  56. public function testConstructorWithNullAndNullArguments()
  57. {
  58. $client = new Client(null, null);
  59. $connection = $client->getConnection();
  60. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $connection);
  61. $parameters = $connection->getParameters();
  62. $this->assertSame($parameters->host, '127.0.0.1');
  63. $this->assertSame($parameters->port, 6379);
  64. $options = $client->getOptions();
  65. $this->assertSame($options->profile->getVersion(), ServerProfile::getDefault()->getVersion());
  66. $this->assertFalse($client->isConnected());
  67. }
  68. /**
  69. * @group disconnected
  70. */
  71. public function testConstructorWithArrayArgument()
  72. {
  73. $client = new Client($arg1 = array('host' => 'localhost', 'port' => 7000));
  74. $parameters = $client->getConnection()->getParameters();
  75. $this->assertSame($parameters->host, $arg1['host']);
  76. $this->assertSame($parameters->port, $arg1['port']);
  77. }
  78. /**
  79. * @group disconnected
  80. */
  81. public function testConstructorWithArrayOfArrayArgument()
  82. {
  83. $arg1 = array(
  84. array('host' => 'localhost', 'port' => 7000),
  85. array('host' => 'localhost', 'port' => 7001),
  86. );
  87. $client = new Client($arg1);
  88. $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $client->getConnection());
  89. }
  90. /**
  91. * @group disconnected
  92. */
  93. public function testConstructorWithStringArgument()
  94. {
  95. $client = new Client('tcp://localhost:7000');
  96. $parameters = $client->getConnection()->getParameters();
  97. $this->assertSame($parameters->host, 'localhost');
  98. $this->assertSame($parameters->port, 7000);
  99. }
  100. /**
  101. * @group disconnected
  102. */
  103. public function testConstructorWithArrayOfStringArgument()
  104. {
  105. $client = new Client($arg1 = array('tcp://localhost:7000', 'tcp://localhost:7001'));
  106. $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $client->getConnection());
  107. }
  108. /**
  109. * @group disconnected
  110. */
  111. public function testConstructorWithArrayOfConnectionsArgument()
  112. {
  113. $connection1 = $this->getMock('Predis\Connection\SingleConnectionInterface');
  114. $connection2 = $this->getMock('Predis\Connection\SingleConnectionInterface');
  115. $client = new Client(array($connection1, $connection2));
  116. $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $cluster = $client->getConnection());
  117. $this->assertSame($connection1, $cluster->getConnectionById(0));
  118. $this->assertSame($connection2, $cluster->getConnectionById(1));
  119. }
  120. /**
  121. * @group disconnected
  122. */
  123. public function testConstructorWithConnectionArgument()
  124. {
  125. $factory = new ConnectionFactory();
  126. $connection = $factory->create('tcp://localhost:7000');
  127. $client = new Client($connection);
  128. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $client->getConnection());
  129. $this->assertSame($connection, $client->getConnection());
  130. $parameters = $client->getConnection()->getParameters();
  131. $this->assertSame($parameters->host, 'localhost');
  132. $this->assertSame($parameters->port, 7000);
  133. }
  134. /**
  135. * @group disconnected
  136. */
  137. public function testConstructorWithClusterArgument()
  138. {
  139. $cluster = new PredisCluster();
  140. $factory = new ConnectionFactory();
  141. $factory->aggregate($cluster, array('tcp://localhost:7000', 'tcp://localhost:7001'));
  142. $client = new Client($cluster);
  143. $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $client->getConnection());
  144. $this->assertSame($cluster, $client->getConnection());
  145. }
  146. /**
  147. * @group disconnected
  148. */
  149. public function testConstructorWithReplicationArgument()
  150. {
  151. $replication = new MasterSlaveReplication();
  152. $factory = new ConnectionFactory();
  153. $factory->aggregate($replication, array('tcp://host1?alias=master', 'tcp://host2?alias=slave'));
  154. $client = new Client($replication);
  155. $this->assertInstanceOf('Predis\Connection\ReplicationConnectionInterface', $client->getConnection());
  156. $this->assertSame($replication, $client->getConnection());
  157. }
  158. /**
  159. * @group disconnected
  160. */
  161. public function testConstructorWithCallableArgument()
  162. {
  163. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  164. $callable = $this->getMock('stdClass', array('__invoke'));
  165. $callable->expects($this->once())
  166. ->method('__invoke')
  167. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'))
  168. ->will($this->returnValue($connection));
  169. $client = new Client($callable);
  170. $this->assertSame($connection, $client->getConnection());
  171. }
  172. /**
  173. * @group disconnected
  174. * @expectedException UnexpectedValueException
  175. * @expectedExceptionMessage The callable connection initializer returned an invalid type
  176. */
  177. public function testConstructorWithCallableConnectionInitializerThrowsExceptionOnInvalidReturnType()
  178. {
  179. $wrongType = $this->getMock('stdClass');
  180. $callable = $this->getMock('stdClass', array('__invoke'));
  181. $callable->expects($this->once())
  182. ->method('__invoke')
  183. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'))
  184. ->will($this->returnValue($wrongType));
  185. $client = new Client($callable);
  186. }
  187. /**
  188. * @group disconnected
  189. */
  190. public function testConstructorWithNullAndArrayArgument()
  191. {
  192. $factory = $this->getMock('Predis\Connection\ConnectionFactoryInterface');
  193. $arg2 = array('profile' => '2.0', 'prefix' => 'prefix:', 'connections' => $factory);
  194. $client = new Client(null, $arg2);
  195. $profile = $client->getProfile();
  196. $this->assertSame($profile->getVersion(), ServerProfile::get('2.0')->getVersion());
  197. $this->assertInstanceOf('Predis\Command\Processor\KeyPrefixProcessor', $profile->getProcessor());
  198. $this->assertSame('prefix:', $profile->getProcessor()->getPrefix());
  199. }
  200. /**
  201. * @group disconnected
  202. */
  203. public function testConstructorWithArrayAndOptionReplication()
  204. {
  205. $arg1 = array('tcp://host1?alias=master', 'tcp://host2?alias=slave');
  206. $arg2 = array('replication' => true);
  207. $client = new Client($arg1, $arg2);
  208. $this->assertInstanceOf('Predis\Connection\ReplicationConnectionInterface', $connection = $client->getConnection());
  209. $this->assertSame('host1', $connection->getConnectionById('master')->getParameters()->host);
  210. $this->assertSame('host2', $connection->getConnectionById('slave')->getParameters()->host);
  211. }
  212. /**
  213. * @group disconnected
  214. */
  215. public function testConstructorWithArrayAndOptionAggregate()
  216. {
  217. $arg1 = array('tcp://host1', 'tcp://host2');
  218. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  219. $fnaggregate = $this->getMock('stdClass', array('__invoke'));
  220. $fnaggregate->expects($this->once())
  221. ->method('__invoke')
  222. ->with($arg1)
  223. ->will($this->returnValue($connection));
  224. $fncluster = $this->getMock('stdClass', array('__invoke'));
  225. $fncluster->expects($this->never())->method('__invoke');
  226. $fnreplication = $this->getMock('stdClass', array('__invoke'));
  227. $fnreplication->expects($this->never())->method('__invoke');
  228. $arg2 = array(
  229. 'aggregate' => function () use ($fnaggregate) { return $fnaggregate; },
  230. 'cluster' => function () use ($fncluster) { return $fncluster; },
  231. 'replication' => function () use ($fnreplication) { return $fncluster; },
  232. );
  233. $client = new Client($arg1, $arg2);
  234. $this->assertSame($connection, $client->getConnection());
  235. }
  236. /**
  237. * @group disconnected
  238. * @expectedException UnexpectedValueException
  239. * @expectedExceptionMessage The callable connection initializer returned an invalid type
  240. */
  241. public function testConstructorWithArrayAndOptionAggregateThrowsExceptionOnInvalidReturnType()
  242. {
  243. $arg1 = array('tcp://host1', 'tcp://host2');
  244. $fnaggregate = $this->getMock('stdClass', array('__invoke'));
  245. $fnaggregate->expects($this->once())
  246. ->method('__invoke')
  247. ->with($arg1)
  248. ->will($this->returnValue(false));
  249. $arg2 = array('aggregate' => function () use ($fnaggregate) { return $fnaggregate; });
  250. $client = new Client($arg1, $arg2);
  251. }
  252. /**
  253. * @group disconnected
  254. */
  255. public function testConnectAndDisconnect()
  256. {
  257. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  258. $connection->expects($this->once())->method('connect');
  259. $connection->expects($this->once())->method('disconnect');
  260. $client = new Client($connection);
  261. $client->connect();
  262. $client->disconnect();
  263. }
  264. /**
  265. * @group disconnected
  266. */
  267. public function testIsConnectedChecksConnectionState()
  268. {
  269. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  270. $connection->expects($this->once())->method('isConnected');
  271. $client = new Client($connection);
  272. $client->isConnected();
  273. }
  274. /**
  275. * @group disconnected
  276. */
  277. public function testQuitIsAliasForDisconnect()
  278. {
  279. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  280. $connection->expects($this->once())->method('disconnect');
  281. $client = new Client($connection);
  282. $client->quit();
  283. }
  284. /**
  285. * @group disconnected
  286. */
  287. public function testCreatesNewCommandUsingSpecifiedProfile()
  288. {
  289. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  290. $profile = $this->getMock('Predis\Profile\ServerProfileInterface');
  291. $profile->expects($this->once())
  292. ->method('createCommand')
  293. ->with('ping', array())
  294. ->will($this->returnValue($ping));
  295. $client = new Client(null, array('profile' => $profile));
  296. $this->assertSame($ping, $client->createCommand('ping', array()));
  297. }
  298. /**
  299. * @group disconnected
  300. */
  301. public function testExecuteCommandReturnsParsedReplies()
  302. {
  303. $profile = ServerProfile::getDefault();
  304. $ping = $profile->createCommand('ping', array());
  305. $hgetall = $profile->createCommand('hgetall', array('metavars', 'foo', 'hoge'));
  306. $connection= $this->getMock('Predis\Connection\ConnectionInterface');
  307. $connection->expects($this->at(0))
  308. ->method('executeCommand')
  309. ->with($ping)
  310. ->will($this->returnValue('PONG'));
  311. $connection->expects($this->at(1))
  312. ->method('executeCommand')
  313. ->with($hgetall)
  314. ->will($this->returnValue(array('foo', 'bar', 'hoge', 'piyo')));
  315. $client = new Client($connection);
  316. $this->assertTrue($client->executeCommand($ping));
  317. $this->assertSame(array('foo' => 'bar', 'hoge' => 'piyo'), $client->executeCommand($hgetall));
  318. }
  319. /**
  320. * @group disconnected
  321. * @expectedException Predis\Response\ServerException
  322. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  323. */
  324. public function testExecuteCommandThrowsExceptionOnRedisError()
  325. {
  326. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  327. $expectedResponse = new ResponseError('ERR Operation against a key holding the wrong kind of value');
  328. $connection= $this->getMock('Predis\Connection\ConnectionInterface');
  329. $connection->expects($this->once())
  330. ->method('executeCommand')
  331. ->will($this->returnValue($expectedResponse));
  332. $client = new Client($connection);
  333. $client->executeCommand($ping);
  334. }
  335. /**
  336. * @group disconnected
  337. */
  338. public function testExecuteCommandReturnsErrorResponseOnRedisError()
  339. {
  340. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  341. $expectedResponse = new ResponseError('ERR Operation against a key holding the wrong kind of value');
  342. $connection= $this->getMock('Predis\Connection\ConnectionInterface');
  343. $connection->expects($this->once())
  344. ->method('executeCommand')
  345. ->will($this->returnValue($expectedResponse));
  346. $client = new Client($connection, array('exceptions' => false));
  347. $response = $client->executeCommand($ping);
  348. $this->assertSame($response, $expectedResponse);
  349. }
  350. /**
  351. * @group disconnected
  352. */
  353. public function testCallingRedisCommandExecutesInstanceOfCommand()
  354. {
  355. $ping = ServerProfile::getDefault()->createCommand('ping', array());
  356. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  357. $connection->expects($this->once())
  358. ->method('executeCommand')
  359. ->with($this->isInstanceOf('Predis\Command\ConnectionPing'))
  360. ->will($this->returnValue('PONG'));
  361. $profile = $this->getMock('Predis\Profile\ServerProfileInterface');
  362. $profile->expects($this->once())
  363. ->method('createCommand')
  364. ->with('ping', array())
  365. ->will($this->returnValue($ping));
  366. $options = array('profile' => $profile);
  367. $client = $this->getMock('Predis\Client', null, array($connection, $options));
  368. $this->assertTrue($client->ping());
  369. }
  370. /**
  371. * @group disconnected
  372. * @expectedException Predis\Response\ServerException
  373. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  374. */
  375. public function testCallingRedisCommandThrowsExceptionOnServerError()
  376. {
  377. $expectedResponse = new ResponseError('ERR Operation against a key holding the wrong kind of value');
  378. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  379. $connection->expects($this->once())
  380. ->method('executeCommand')
  381. ->with($this->isInstanceOf('Predis\Command\ConnectionPing'))
  382. ->will($this->returnValue($expectedResponse));
  383. $client = new Client($connection);
  384. $client->ping();
  385. }
  386. /**
  387. * @group disconnected
  388. */
  389. public function testCallingRedisCommandReturnsErrorResponseOnRedisError()
  390. {
  391. $expectedResponse = new ResponseError('ERR Operation against a key holding the wrong kind of value');
  392. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  393. $connection->expects($this->once())
  394. ->method('executeCommand')
  395. ->with($this->isInstanceOf('Predis\Command\ConnectionPing'))
  396. ->will($this->returnValue($expectedResponse));
  397. $client = new Client($connection, array('exceptions' => false));
  398. $response = $client->ping();
  399. $this->assertSame($response, $expectedResponse);
  400. }
  401. /**
  402. * @group disconnected
  403. * @expectedException Predis\ClientException
  404. * @expectedExceptionMessage 'invalidcommand' is not a registered Redis command
  405. */
  406. public function testThrowsExceptionOnNonRegisteredRedisCommand()
  407. {
  408. $client = new Client();
  409. $client->invalidCommand();
  410. }
  411. /**
  412. * @group disconnected
  413. */
  414. public function testGetConnectionFromAggregatedConnectionWithAlias()
  415. {
  416. $client = new Client(array('tcp://host1?alias=node01', 'tcp://host2?alias=node02'));
  417. $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $cluster = $client->getConnection());
  418. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node01 = $client->getConnectionById('node01'));
  419. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node02 = $client->getConnectionById('node02'));
  420. $this->assertSame('host1', $node01->getParameters()->host);
  421. $this->assertSame('host2', $node02->getParameters()->host);
  422. }
  423. /**
  424. * @group disconnected
  425. * @expectedException Predis\NotSupportedException
  426. * @expectedExceptionMessage Retrieving connections by ID is supported only when using aggregated connections
  427. */
  428. public function testGetConnectionByIdWorksOnlyWithAggregatedConnections()
  429. {
  430. $client = new Client();
  431. $client->getConnectionById('node01');
  432. }
  433. /**
  434. * @group disconnected
  435. */
  436. public function testCreateClientWithConnectionFromAggregatedConnection()
  437. {
  438. $client = new Client(array('tcp://host1?alias=node01', 'tcp://host2?alias=node02'), array('prefix' => 'pfx:'));
  439. $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $cluster = $client->getConnection());
  440. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node01 = $client->getConnectionById('node01'));
  441. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node02 = $client->getConnectionById('node02'));
  442. $clientNode02 = $client->getClientFor('node02');
  443. $this->assertInstanceOf('Predis\Client', $clientNode02);
  444. $this->assertSame($node02, $clientNode02->getConnection());
  445. $this->assertSame($client->getOptions(), $clientNode02->getOptions());
  446. }
  447. /**
  448. * @group disconnected
  449. */
  450. public function testGetClientForReturnsInstanceOfSubclass()
  451. {
  452. $nodes = array('tcp://host1?alias=node01', 'tcp://host2?alias=node02');
  453. $client = $this->getMock('Predis\Client', array('dummy'), array($nodes), 'SubclassedClient');
  454. $this->assertInstanceOf('SubclassedClient', $client->getClientFor('node02'));
  455. }
  456. /**
  457. * @group disconnected
  458. */
  459. public function testPipelineWithoutArgumentsReturnsPipelineContext()
  460. {
  461. $client = new Client();
  462. $this->assertInstanceOf('Predis\Pipeline\PipelineContext', $client->pipeline());
  463. }
  464. /**
  465. * @group disconnected
  466. */
  467. public function testPipelineWithArrayReturnsPipelineContextWithOptions()
  468. {
  469. $client = new Client();
  470. $executor = $this->getMock('Predis\Pipeline\PipelineExecutorInterface');
  471. $options = array('executor' => $executor);
  472. $this->assertInstanceOf('Predis\Pipeline\PipelineContext', $pipeline = $client->pipeline($options));
  473. $this->assertSame($executor, $pipeline->getExecutor());
  474. $options = array('executor' => function ($client, $options) use ($executor) { return $executor; });
  475. $this->assertInstanceOf('Predis\Pipeline\PipelineContext', $pipeline = $client->pipeline($options));
  476. $this->assertSame($executor, $pipeline->getExecutor());
  477. }
  478. /**
  479. * @group disconnected
  480. */
  481. public function testPipelineWithCallableExecutesPipeline()
  482. {
  483. $callable = $this->getMock('stdClass', array('__invoke'));
  484. $callable->expects($this->once())
  485. ->method('__invoke')
  486. ->with($this->isInstanceOf('Predis\Pipeline\PipelineContext'));
  487. $client = new Client();
  488. $client->pipeline($callable);
  489. }
  490. /**
  491. * @group disconnected
  492. */
  493. public function testPipelineWithArrayAndCallableExecutesPipelineWithOptions()
  494. {
  495. $executor = $this->getMock('Predis\Pipeline\PipelineExecutorInterface');
  496. $options = array('executor' => $executor);
  497. $test = $this;
  498. $mockCallback = function ($pipeline) use ($executor, $test) {
  499. $reflection = new \ReflectionProperty($pipeline, 'executor');
  500. $reflection->setAccessible(true);
  501. $test->assertSame($executor, $reflection->getValue($pipeline));
  502. };
  503. $callable = $this->getMock('stdClass', array('__invoke'));
  504. $callable->expects($this->once())
  505. ->method('__invoke')
  506. ->with($this->isInstanceOf('Predis\Pipeline\PipelineContext'))
  507. ->will($this->returnCallback($mockCallback));
  508. $client = new Client();
  509. $client->pipeline($options, $callable);
  510. }
  511. /**
  512. * @group disconnected
  513. */
  514. public function testPubSubLoopWithoutArgumentsReturnsPubSubContext()
  515. {
  516. $client = new Client();
  517. $this->assertInstanceOf('Predis\PubSub\PubSubContext', $client->pubSubLoop());
  518. }
  519. /**
  520. * @group disconnected
  521. */
  522. public function testPubSubLoopWithArrayReturnsPubSubContextWithOptions()
  523. {
  524. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  525. $options = array('subscribe' => 'channel');
  526. $client = new Client($connection);
  527. $this->assertInstanceOf('Predis\PubSub\PubSubContext', $pubsub = $client->pubSubLoop($options));
  528. $reflection = new \ReflectionProperty($pubsub, 'options');
  529. $reflection->setAccessible(true);
  530. $this->assertSame($options, $reflection->getValue($pubsub));
  531. }
  532. /**
  533. * @group disconnected
  534. */
  535. public function testPubSubLoopWithArrayAndCallableExecutesPubSub()
  536. {
  537. // NOTE: we use a subscribe count of 0 in the fake message to trick
  538. // the context and to make it think that it can be closed
  539. // since there are no more subscriptions active.
  540. $message = array('subscribe', 'channel', 0);
  541. $options = array('subscribe' => 'channel');
  542. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  543. $connection->expects($this->once())
  544. ->method('read')
  545. ->will($this->returnValue($message));
  546. $callable = $this->getMock('stdClass', array('__invoke'));
  547. $callable->expects($this->once())
  548. ->method('__invoke');
  549. $client = new Client($connection);
  550. $client->pubSubLoop($options, $callable);
  551. }
  552. /**
  553. * @group disconnected
  554. */
  555. public function testTransactionWithoutArgumentsReturnsMultiExecContext()
  556. {
  557. $client = new Client();
  558. $this->assertInstanceOf('Predis\Transaction\MultiExecContext', $client->transaction());
  559. }
  560. /**
  561. * @group disconnected
  562. */
  563. public function testTransactionWithArrayReturnsMultiExecContextWithOptions()
  564. {
  565. $options = array('cas' => true, 'retry' => 3);
  566. $client = new Client();
  567. $this->assertInstanceOf('Predis\Transaction\MultiExecContext', $tx = $client->transaction($options));
  568. $reflection = new \ReflectionProperty($tx, 'options');
  569. $reflection->setAccessible(true);
  570. $this->assertSame($options, $reflection->getValue($tx));
  571. }
  572. /**
  573. * @group disconnected
  574. */
  575. public function testTransactionWithArrayAndCallableExecutesMultiExec()
  576. {
  577. // NOTE: we use CAS since testing the actual MULTI/EXEC context
  578. // here is not the point.
  579. $options = array('cas' => true, 'retry' => 3);
  580. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  581. $connection->expects($this->once())
  582. ->method('executeCommand')
  583. ->will($this->returnValue(new ResponseQueued()));
  584. $txCallback = function ($tx) {
  585. $tx->ping();
  586. };
  587. $callable = $this->getMock('stdClass', array('__invoke'));
  588. $callable->expects($this->once())
  589. ->method('__invoke')
  590. ->will($this->returnCallback($txCallback));
  591. $client = new Client($connection);
  592. $client->transaction($options, $callable);
  593. }
  594. /**
  595. * @group disconnected
  596. */
  597. public function testMonitorReturnsMonitorContext()
  598. {
  599. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  600. $client = new Client($connection);
  601. $this->assertInstanceOf('Predis\Monitor\MonitorContext', $monitor = $client->monitor());
  602. }
  603. /**
  604. * @group disconnected
  605. */
  606. public function testClientResendScriptedCommandUsingEvalOnNoScriptErrors()
  607. {
  608. $command = $this->getMockForAbstractClass('Predis\Command\ScriptedCommand', array(), '', true, true, true, array('parseResponse'));
  609. $command->expects($this->once())
  610. ->method('getScript')
  611. ->will($this->returnValue('return redis.call(\'exists\', KEYS[1])'));
  612. $command->expects($this->once())
  613. ->method('parseResponse')
  614. ->with('OK')
  615. ->will($this->returnValue(true));
  616. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  617. $connection->expects($this->at(0))
  618. ->method('executeCommand')
  619. ->with($command)
  620. ->will($this->returnValue(new ResponseError('NOSCRIPT')));
  621. $connection->expects($this->at(1))
  622. ->method('executeCommand')
  623. ->with($this->isInstanceOf('Predis\Command\ServerEval'))
  624. ->will($this->returnValue('OK'));
  625. $client = new Client($connection);
  626. $this->assertTrue($client->executeCommand($command));
  627. }
  628. // ******************************************************************** //
  629. // ---- HELPER METHODS ------------------------------------------------ //
  630. // ******************************************************************** //
  631. /**
  632. * Returns a named array with the default connection parameters and their values.
  633. *
  634. * @return Array Default connection parameters.
  635. */
  636. protected function getDefaultParametersArray()
  637. {
  638. return array(
  639. 'scheme' => 'tcp',
  640. 'host' => REDIS_SERVER_HOST,
  641. 'port' => REDIS_SERVER_PORT,
  642. 'database' => REDIS_SERVER_DBNUM,
  643. );
  644. }
  645. /**
  646. * Returns a named array with the default client options and their values.
  647. *
  648. * @return Array Default connection parameters.
  649. */
  650. protected function getDefaultOptionsArray()
  651. {
  652. return array(
  653. 'profile' => REDIS_SERVER_VERSION,
  654. );
  655. }
  656. /**
  657. * Returns a named array with the default connection parameters merged with
  658. * the specified additional parameters.
  659. *
  660. * @param Array $additional Additional connection parameters.
  661. * @return Array Connection parameters.
  662. */
  663. protected function getParametersArray(Array $additional)
  664. {
  665. return array_merge($this->getDefaultParametersArray(), $additional);
  666. }
  667. /**
  668. * Returns an URI string representation of the specified connection parameters.
  669. *
  670. * @param Array $parameters Array of connection parameters.
  671. * @return String URI string.
  672. */
  673. protected function getParametersString(Array $parameters)
  674. {
  675. $defaults = $this->getDefaultParametersArray();
  676. $scheme = isset($parameters['scheme']) ? $parameters['scheme'] : $defaults['scheme'];
  677. $host = isset($parameters['host']) ? $parameters['host'] : $defaults['host'];
  678. $port = isset($parameters['port']) ? $parameters['port'] : $defaults['port'];
  679. unset($parameters['scheme'], $parameters['host'], $parameters['port']);
  680. $uriString = "$scheme://$host:$port/?";
  681. foreach ($parameters as $k => $v) {
  682. $uriString .= "$k=$v&";
  683. }
  684. return $uriString;
  685. }
  686. }