ClientTest.php 29 KB

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