ClientTest.php 29 KB

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