ClientTest.php 29 KB

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