ClientTest.php 29 KB

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