ClientTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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 PredisTestCase;
  12. use Predis\Connection\ConnectionFactory;
  13. use Predis\Connection\MasterSlaveReplication;
  14. use Predis\Connection\PredisCluster;
  15. use Predis\Profile;
  16. use Predis\Response;
  17. /**
  18. *
  19. */
  20. class ClientTest extends PredisTestCase
  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(), Profile\Factory::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(), Profile\Factory::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(), Profile\Factory::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(), Profile\Factory::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 = Profile\Factory::getDefault()->createCommand('ping', array());
  289. $profile = $this->getMock('Predis\Profile\ProfileInterface');
  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 = Profile\Factory::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 = Profile\Factory::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 = Profile\Factory::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 = Profile\Factory::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\ProfileInterface');
  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. */
  403. public function testRawCommand()
  404. {
  405. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  406. $connection->expects($this->at(0))
  407. ->method('executeCommand')
  408. ->with($this->isRedisCommand('SET', array('foo', 'bar')))
  409. ->will($this->returnValue(true));
  410. $connection->expects($this->at(1))
  411. ->method('executeCommand')
  412. ->with($this->isRedisCommand('GET', array('foo')))
  413. ->will($this->returnValue('bar'));
  414. $connection->expects($this->at(2))
  415. ->method('executeCommand')
  416. ->with($this->isRedisCommand('PING'))
  417. ->will($this->returnValue('PONG'));
  418. $client = new Client($connection);
  419. $this->assertSame('OK', $client->raw(array('SET', 'foo', 'bar')));
  420. $this->assertSame('bar', $client->raw(array('GET', 'foo')));
  421. $error = true; // $error is always populated by reference.
  422. $this->assertSame('PONG', $client->raw(array('PING'), $error));
  423. $this->assertFalse($error);
  424. }
  425. /**
  426. * @group disconnected
  427. */
  428. public function testRawCommandNeverAppliesPrefix()
  429. {
  430. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  431. $connection->expects($this->at(0))
  432. ->method('executeCommand')
  433. ->with($this->isRedisCommand('SET', array('foo', 'bar')))
  434. ->will($this->returnValue(true));
  435. $connection->expects($this->at(1))
  436. ->method('executeCommand')
  437. ->with($this->isRedisCommand('GET', array('foo')))
  438. ->will($this->returnValue('bar'));
  439. $client = new Client($connection, array('prefix' => 'predis:'));
  440. $this->assertSame('OK', $client->raw(array('SET', 'foo', 'bar')));
  441. $this->assertSame('bar', $client->raw(array('GET', 'foo')));
  442. }
  443. /**
  444. * @group disconnected
  445. */
  446. public function testRawCommandNeverThrowsExceptions()
  447. {
  448. $message = 'ERR Mock error response';
  449. $response = new Response\Error($message);
  450. $connection = $this->getMock('Predis\Connection\ConnectionInterface');
  451. $connection->expects($this->once())
  452. ->method('executeCommand')
  453. ->with($this->isRedisCommand('PING'))
  454. ->will($this->returnValue($response));
  455. $client = new Client($connection, array('exceptions' => true));
  456. $this->assertSame($message, $client->raw(array('PING'), $error));
  457. $this->assertTrue($error);
  458. }
  459. /**
  460. * @group disconnected
  461. * @expectedException Predis\ClientException
  462. * @expectedExceptionMessage 'INVALIDCOMMAND' is not a registered Redis command
  463. */
  464. public function testThrowsExceptionOnNonRegisteredRedisCommand()
  465. {
  466. $client = new Client();
  467. $client->invalidCommand();
  468. }
  469. /**
  470. * @group disconnected
  471. */
  472. public function testGetConnectionFromAggregatedConnectionWithAlias()
  473. {
  474. $client = new Client(array('tcp://host1?alias=node01', 'tcp://host2?alias=node02'));
  475. $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $cluster = $client->getConnection());
  476. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node01 = $client->getConnectionById('node01'));
  477. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node02 = $client->getConnectionById('node02'));
  478. $this->assertSame('host1', $node01->getParameters()->host);
  479. $this->assertSame('host2', $node02->getParameters()->host);
  480. }
  481. /**
  482. * @group disconnected
  483. * @expectedException Predis\NotSupportedException
  484. * @expectedExceptionMessage Retrieving connections by ID is supported only when using aggregated connections
  485. */
  486. public function testGetConnectionByIdWorksOnlyWithAggregatedConnections()
  487. {
  488. $client = new Client();
  489. $client->getConnectionById('node01');
  490. }
  491. /**
  492. * @group disconnected
  493. */
  494. public function testCreateClientWithConnectionFromAggregatedConnection()
  495. {
  496. $client = new Client(array('tcp://host1?alias=node01', 'tcp://host2?alias=node02'), array('prefix' => 'pfx:'));
  497. $this->assertInstanceOf('Predis\Connection\ClusterConnectionInterface', $cluster = $client->getConnection());
  498. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node01 = $client->getConnectionById('node01'));
  499. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $node02 = $client->getConnectionById('node02'));
  500. $clientNode02 = $client->getClientFor('node02');
  501. $this->assertInstanceOf('Predis\Client', $clientNode02);
  502. $this->assertSame($node02, $clientNode02->getConnection());
  503. $this->assertSame($client->getOptions(), $clientNode02->getOptions());
  504. }
  505. /**
  506. * @group disconnected
  507. */
  508. public function testGetClientForReturnsInstanceOfSubclass()
  509. {
  510. $nodes = array('tcp://host1?alias=node01', 'tcp://host2?alias=node02');
  511. $client = $this->getMock('Predis\Client', array('dummy'), array($nodes), 'SubclassedClient');
  512. $this->assertInstanceOf('SubclassedClient', $client->getClientFor('node02'));
  513. }
  514. /**
  515. * @group disconnected
  516. */
  517. public function testPipelineWithoutArgumentsReturnsPipeline()
  518. {
  519. $client = new Client();
  520. $this->assertInstanceOf('Predis\Pipeline\Pipeline', $client->pipeline());
  521. }
  522. /**
  523. * @group disconnected
  524. */
  525. public function testPipelineWithArrayReturnsPipeline()
  526. {
  527. $client = new Client();
  528. $this->assertInstanceOf('Predis\Pipeline\Pipeline', $client->pipeline(array()));
  529. $this->assertInstanceOf('Predis\Pipeline\Atomic', $client->pipeline(array('atomic' => true)));
  530. $this->assertInstanceOf('Predis\Pipeline\FireAndForget', $client->pipeline(array('fire-and-forget' => true)));
  531. }
  532. /**
  533. * @group disconnected
  534. */
  535. public function testPipelineWithCallableExecutesPipeline()
  536. {
  537. $callable = $this->getMock('stdClass', array('__invoke'));
  538. $callable->expects($this->once())
  539. ->method('__invoke')
  540. ->with($this->isInstanceOf('Predis\Pipeline\Pipeline'));
  541. $client = new Client();
  542. $client->pipeline($callable);
  543. }
  544. /**
  545. * @group disconnected
  546. */
  547. public function testPubSubLoopWithoutArgumentsReturnsPubSubConsumer()
  548. {
  549. $client = new Client();
  550. $this->assertInstanceOf('Predis\PubSub\Consumer', $client->pubSubLoop());
  551. }
  552. /**
  553. * @group disconnected
  554. */
  555. public function testPubSubLoopWithArrayReturnsPubSubConsumerWithOptions()
  556. {
  557. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  558. $options = array('subscribe' => 'channel');
  559. $client = new Client($connection);
  560. $this->assertInstanceOf('Predis\PubSub\Consumer', $pubsub = $client->pubSubLoop($options));
  561. $reflection = new \ReflectionProperty($pubsub, 'options');
  562. $reflection->setAccessible(true);
  563. $this->assertSame($options, $reflection->getValue($pubsub));
  564. }
  565. /**
  566. * @group disconnected
  567. */
  568. public function testPubSubLoopWithArrayAndCallableExecutesPubSub()
  569. {
  570. // NOTE: we use a subscribe count of 0 in the fake message to trick
  571. // the context and to make it think that it can be closed
  572. // since there are no more subscriptions active.
  573. $message = array('subscribe', 'channel', 0);
  574. $options = array('subscribe' => 'channel');
  575. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  576. $connection->expects($this->once())
  577. ->method('read')
  578. ->will($this->returnValue($message));
  579. $callable = $this->getMock('stdClass', array('__invoke'));
  580. $callable->expects($this->once())
  581. ->method('__invoke');
  582. $client = new Client($connection);
  583. $client->pubSubLoop($options, $callable);
  584. }
  585. /**
  586. * @group disconnected
  587. */
  588. public function testTransactionWithoutArgumentsReturnsMultiExec()
  589. {
  590. $client = new Client();
  591. $this->assertInstanceOf('Predis\Transaction\MultiExec', $client->transaction());
  592. }
  593. /**
  594. * @group disconnected
  595. * @todo I hate this test but reflection is the easiest way in this case.
  596. */
  597. public function testTransactionWithArrayReturnsTransactionMultiExecWithOptions()
  598. {
  599. $options = array('cas' => true, 'retry' => 3);
  600. $client = new Client();
  601. $this->assertInstanceOf('Predis\Transaction\MultiExec', $tx = $client->transaction($options));
  602. $property = new \ReflectionProperty($tx, 'modeCAS');
  603. $property->setAccessible(true);
  604. $this->assertSame($options['cas'], $property->getValue($tx));
  605. $property = new \ReflectionProperty($tx, 'attempts');
  606. $property->setAccessible(true);
  607. $this->assertSame($options['retry'], $property->getValue($tx));
  608. }
  609. /**
  610. * @group disconnected
  611. */
  612. public function testTransactionWithArrayAndCallableExecutesMultiExec()
  613. {
  614. // NOTE: we use CAS since testing the actual MULTI/EXEC context
  615. // here is not the point.
  616. $options = array('cas' => true, 'retry' => 3);
  617. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  618. $connection->expects($this->once())
  619. ->method('executeCommand')
  620. ->will($this->returnValue(new Response\StatusQueued()));
  621. $txCallback = function ($tx) {
  622. $tx->ping();
  623. };
  624. $callable = $this->getMock('stdClass', array('__invoke'));
  625. $callable->expects($this->once())
  626. ->method('__invoke')
  627. ->will($this->returnCallback($txCallback));
  628. $client = new Client($connection);
  629. $client->transaction($options, $callable);
  630. }
  631. /**
  632. * @group disconnected
  633. */
  634. public function testMonitorReturnsMonitorConsumer()
  635. {
  636. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  637. $client = new Client($connection);
  638. $this->assertInstanceOf('Predis\Monitor\Consumer', $monitor = $client->monitor());
  639. }
  640. /**
  641. * @group disconnected
  642. */
  643. public function testClientResendScriptCommandUsingEvalOnNoScriptErrors()
  644. {
  645. $command = $this->getMockForAbstractClass('Predis\Command\ScriptCommand', array(), '', true, true, true, array('parseResponse'));
  646. $command->expects($this->once())
  647. ->method('getScript')
  648. ->will($this->returnValue('return redis.call(\'exists\', KEYS[1])'));
  649. $command->expects($this->once())
  650. ->method('parseResponse')
  651. ->with('OK')
  652. ->will($this->returnValue(true));
  653. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  654. $connection->expects($this->at(0))
  655. ->method('executeCommand')
  656. ->with($command)
  657. ->will($this->returnValue(new Response\Error('NOSCRIPT')));
  658. $connection->expects($this->at(1))
  659. ->method('executeCommand')
  660. ->with($this->isInstanceOf('Predis\Command\ServerEval'))
  661. ->will($this->returnValue('OK'));
  662. $client = new Client($connection);
  663. $this->assertTrue($client->executeCommand($command));
  664. }
  665. // ******************************************************************** //
  666. // ---- HELPER METHODS ------------------------------------------------ //
  667. // ******************************************************************** //
  668. /**
  669. * Returns an URI string representation of the specified connection parameters.
  670. *
  671. * @param array $parameters Array of connection parameters.
  672. * @return String URI string.
  673. */
  674. protected function getParametersString(array $parameters)
  675. {
  676. $defaults = $this->getDefaultParametersArray();
  677. $scheme = isset($parameters['scheme']) ? $parameters['scheme'] : $defaults['scheme'];
  678. $host = isset($parameters['host']) ? $parameters['host'] : $defaults['host'];
  679. $port = isset($parameters['port']) ? $parameters['port'] : $defaults['port'];
  680. unset($parameters['scheme'], $parameters['host'], $parameters['port']);
  681. $uriString = "$scheme://$host:$port/?";
  682. foreach ($parameters as $k => $v) {
  683. $uriString .= "$k=$v&";
  684. }
  685. return $uriString;
  686. }
  687. }