ClientTest.php 30 KB

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