PipelineContextTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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\Pipeline;
  11. use \PHPUnit_Framework_TestCase as StandardTestCase;
  12. use Predis\Client;
  13. use Predis\ClientException;
  14. use Predis\Profile\ServerProfile;
  15. /**
  16. *
  17. */
  18. class PipelineContextTest extends StandardTestCase
  19. {
  20. /**
  21. * @group disconnected
  22. */
  23. public function testConstructorWithoutOptions()
  24. {
  25. $client = new Client();
  26. $pipeline = new PipelineContext($client);
  27. $this->assertSame($client, $pipeline->getClient());
  28. $this->assertInstanceOf('Predis\Pipeline\StandardExecutor', $pipeline->getExecutor());
  29. }
  30. /**
  31. * @group disconnected
  32. */
  33. public function testConstructorWithExecutorArgument()
  34. {
  35. $client = new Client();
  36. $executor = $this->getMock('Predis\Pipeline\PipelineExecutorInterface');
  37. $pipeline = new PipelineContext($client, $executor);
  38. $this->assertSame($executor, $pipeline->getExecutor());
  39. }
  40. /**
  41. * @group disconnected
  42. */
  43. public function testCallDoesNotSendCommandsWithoutExecute()
  44. {
  45. $executor = $this->getMock('Predis\Pipeline\PipelineExecutorInterface');
  46. $executor->expects($this->never())->method('executor');
  47. $pipeline = new PipelineContext(new Client(), $executor);
  48. $pipeline->echo('one');
  49. $pipeline->echo('two');
  50. $pipeline->echo('three');
  51. }
  52. /**
  53. * @group disconnected
  54. */
  55. public function testCallReturnsPipelineForFluentInterface()
  56. {
  57. $executor = $this->getMock('Predis\Pipeline\PipelineExecutorInterface');
  58. $executor->expects($this->never())->method('executor');
  59. $pipeline = new PipelineContext(new Client(), $executor);
  60. $this->assertSame($pipeline, $pipeline->echo('one'));
  61. $this->assertSame($pipeline, $pipeline->echo('one')->echo('two')->echo('three'));
  62. }
  63. /**
  64. * @group disconnected
  65. */
  66. public function testExecuteCommandDoesNotSendCommandsWithoutExecute()
  67. {
  68. $profile = ServerProfile::getDefault();
  69. $executor = $this->getMock('Predis\Pipeline\PipelineExecutorInterface');
  70. $executor->expects($this->never())->method('executor');
  71. $pipeline = new PipelineContext(new Client(), $executor);
  72. $pipeline->executeCommand($profile->createCommand('echo', array('one')));
  73. $pipeline->executeCommand($profile->createCommand('echo', array('two')));
  74. $pipeline->executeCommand($profile->createCommand('echo', array('three')));
  75. }
  76. /**
  77. * @group disconnected
  78. */
  79. public function testExecuteWithEmptyBuffer()
  80. {
  81. $executor = $this->getMock('Predis\Pipeline\PipelineExecutorInterface');
  82. $executor->expects($this->never())->method('executor');
  83. $pipeline = new PipelineContext(new Client(), $executor);
  84. $this->assertSame(array(), $pipeline->execute());
  85. }
  86. /**
  87. * @group disconnected
  88. */
  89. public function testExecuteWithFilledBuffer()
  90. {
  91. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  92. $connection->expects($this->exactly(3))
  93. ->method('writeCommand');
  94. $connection->expects($this->exactly(3))
  95. ->method('readResponse')
  96. ->will($this->returnCallback($this->getReadCallback()));
  97. $pipeline = new PipelineContext(new Client($connection));
  98. $pipeline->echo('one');
  99. $pipeline->echo('two');
  100. $pipeline->echo('three');
  101. $pipeline->flushPipeline();
  102. $this->assertSame(array('one', 'two', 'three'), $pipeline->execute());
  103. }
  104. /**
  105. * @group disconnected
  106. */
  107. public function testFlushWithFalseArgumentDiscardsBuffer()
  108. {
  109. $executor = $this->getMock('Predis\Pipeline\PipelineExecutorInterface');
  110. $executor->expects($this->never())->method('executor');
  111. $pipeline = new PipelineContext(new Client(), $executor);
  112. $pipeline->echo('one');
  113. $pipeline->echo('two');
  114. $pipeline->echo('three');
  115. $pipeline->flushPipeline(false);
  116. $this->assertSame(array(), $pipeline->execute());
  117. }
  118. /**
  119. * @group disconnected
  120. */
  121. public function testFlushHandlesPartialBuffers()
  122. {
  123. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  124. $connection->expects($this->exactly(4))
  125. ->method('writeCommand');
  126. $connection->expects($this->exactly(4))
  127. ->method('readResponse')
  128. ->will($this->returnCallback($this->getReadCallback()));
  129. $pipeline = new PipelineContext(new Client($connection));
  130. $pipeline->echo('one');
  131. $pipeline->echo('two');
  132. $pipeline->flushPipeline();
  133. $pipeline->echo('three');
  134. $pipeline->echo('four');
  135. $this->assertSame(array('one', 'two', 'three', 'four'), $pipeline->execute());
  136. }
  137. /**
  138. * @group disconnected
  139. */
  140. public function testExecuteAcceptsCallableArgument()
  141. {
  142. $test = $this;
  143. $pipeline = new PipelineContext(new Client());
  144. $callable = function ($pipe) use ($test, $pipeline) {
  145. $test->assertSame($pipeline, $pipe);
  146. $pipe->flushPipeline(false);
  147. };
  148. $pipeline->execute($callable);
  149. }
  150. /**
  151. * @group disconnected
  152. * @expectedException InvalidArgumentException
  153. */
  154. public function testExecuteDoesNotAcceptNonCallableArgument()
  155. {
  156. $noncallable = new \stdClass();
  157. $pipeline = new PipelineContext(new Client());
  158. $pipeline->execute($noncallable);
  159. }
  160. /**
  161. * @group disconnected
  162. * @expectedException Predis\ClientException
  163. */
  164. public function testExecuteInsideCallableArgumentThrowsException()
  165. {
  166. $pipeline = new PipelineContext(new Client());
  167. $pipeline->execute(function ($pipe) {
  168. $pipe->execute();
  169. });
  170. }
  171. /**
  172. * @group disconnected
  173. */
  174. public function testExecuteWithCallableArgumentRunsPipelineInCallable()
  175. {
  176. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  177. $connection->expects($this->exactly(4))
  178. ->method('writeCommand');
  179. $connection->expects($this->exactly(4))
  180. ->method('readResponse')
  181. ->will($this->returnCallback($this->getReadCallback()));
  182. $pipeline = new PipelineContext(new Client($connection));
  183. $replies = $pipeline->execute(function ($pipe) {
  184. $pipe->echo('one');
  185. $pipe->echo('two');
  186. $pipe->echo('three');
  187. $pipe->echo('four');
  188. });
  189. $this->assertSame(array('one', 'two', 'three', 'four'), $replies);
  190. }
  191. /**
  192. * @group disconnected
  193. */
  194. public function testExecuteWithCallableArgumentHandlesExceptions()
  195. {
  196. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  197. $connection->expects($this->never())->method('writeCommand');
  198. $connection->expects($this->never())->method('readResponse');
  199. $pipeline = new PipelineContext(new Client($connection));
  200. $exception = null;
  201. $replies = null;
  202. try {
  203. $replies = $pipeline->execute(function ($pipe) {
  204. $pipe->echo('one');
  205. throw new ClientException('TEST');
  206. $pipe->echo('two');
  207. });
  208. } catch (\Exception $ex) {
  209. $exception = $ex;
  210. }
  211. $this->assertInstanceOf('Predis\ClientException', $exception);
  212. $this->assertSame('TEST', $exception->getMessage());
  213. $this->assertNull($replies);
  214. }
  215. // ******************************************************************** //
  216. // ---- INTEGRATION TESTS --------------------------------------------- //
  217. // ******************************************************************** //
  218. /**
  219. * @group connected
  220. */
  221. public function testIntegrationWithFluentInterface()
  222. {
  223. $pipeline = $this->getClient()->pipeline();
  224. $results = $pipeline->echo('one')
  225. ->echo('two')
  226. ->echo('three')
  227. ->execute();
  228. $this->assertSame(array('one', 'two', 'three'), $results);
  229. }
  230. /**
  231. * @group connected
  232. */
  233. public function testIntegrationWithCallableBlock()
  234. {
  235. $client = $this->getClient();
  236. $results = $client->pipeline(function ($pipe) {
  237. $pipe->set('foo', 'bar');
  238. $pipe->get('foo');
  239. });
  240. $this->assertSame(array(true, 'bar'), $results);
  241. $this->assertTrue($client->exists('foo'));
  242. }
  243. /**
  244. * @group connected
  245. */
  246. public function testOutOfBandMessagesInsidePipeline()
  247. {
  248. $oob = null;
  249. $client = $this->getClient();
  250. $results = $client->pipeline(function ($pipe) use (&$oob) {
  251. $pipe->set('foo', 'bar');
  252. $oob = $pipe->getClient()->echo('oob message');
  253. $pipe->get('foo');
  254. });
  255. $this->assertSame(array(true, 'bar'), $results);
  256. $this->assertSame('oob message', $oob);
  257. $this->assertTrue($client->exists('foo'));
  258. }
  259. /**
  260. * @group connected
  261. */
  262. public function testIntegrationWithClientExceptionInCallableBlock()
  263. {
  264. $client = $this->getClient();
  265. try {
  266. $client->pipeline(function ($pipe) {
  267. $pipe->set('foo', 'bar');
  268. throw new ClientException('TEST');
  269. });
  270. } catch (\Exception $ex) {
  271. $exception = $ex;
  272. }
  273. $this->assertInstanceOf('Predis\ClientException', $exception);
  274. $this->assertSame('TEST', $exception->getMessage());
  275. $this->assertFalse($client->exists('foo'));
  276. }
  277. /**
  278. * @group connected
  279. */
  280. public function testIntegrationWithServerExceptionInCallableBlock()
  281. {
  282. $client = $this->getClient();
  283. try {
  284. $client->pipeline(function ($pipe) {
  285. $pipe->set('foo', 'bar');
  286. // LPUSH on a string key fails, but won't stop
  287. // the pipeline to send the commands.
  288. $pipe->lpush('foo', 'bar');
  289. $pipe->set('hoge', 'piyo');
  290. });
  291. } catch (\Exception $ex) {
  292. $exception = $ex;
  293. }
  294. $this->assertInstanceOf('Predis\ServerException', $exception);
  295. $this->assertTrue($client->exists('foo'));
  296. $this->assertTrue($client->exists('hoge'));
  297. }
  298. /**
  299. * @group connected
  300. */
  301. public function testIntegrationWithServerErrorInCallableBlock()
  302. {
  303. $client = $this->getClient(array(), array('exceptions' => false));
  304. $results = $client->pipeline(function ($pipe) {
  305. $pipe->set('foo', 'bar');
  306. $pipe->lpush('foo', 'bar'); // LPUSH on a string key fails.
  307. $pipe->get('foo');
  308. });
  309. $this->assertTrue($results[0]);
  310. $this->assertInstanceOf('Predis\ResponseError', $results[1]);
  311. $this->assertSame('bar', $results[2]);
  312. }
  313. // ******************************************************************** //
  314. // ---- HELPER METHODS ------------------------------------------------ //
  315. // ******************************************************************** //
  316. /**
  317. * Returns a client instance connected to the specified Redis
  318. * server instance to perform integration tests.
  319. *
  320. * @return array Additional connection parameters.
  321. * @return array Additional client options.
  322. * @return Client New client instance.
  323. */
  324. protected function getClient(Array $parameters = array(), Array $options = array())
  325. {
  326. $parameters = array_merge(array(
  327. 'scheme' => 'tcp',
  328. 'host' => REDIS_SERVER_HOST,
  329. 'port' => REDIS_SERVER_PORT,
  330. 'database' => REDIS_SERVER_DBNUM,
  331. ), $parameters);
  332. $options = array_merge(array(
  333. 'profile' => REDIS_SERVER_VERSION,
  334. ), $options);
  335. $client = new Client($parameters, $options);
  336. $client->connect();
  337. $client->flushdb();
  338. return $client;
  339. }
  340. /**
  341. * Helper method that returns a callback used to emulate a reply
  342. * to an ECHO command.
  343. *
  344. * @return \Closure
  345. */
  346. protected function getReadCallback()
  347. {
  348. return function ($command) {
  349. if (($id = $command->getId()) !== 'ECHO') {
  350. throw new \InvalidArgumentException("Expected ECHO, got {$id}");
  351. }
  352. list($echoed) = $command->getArguments();
  353. return $echoed;
  354. };
  355. }
  356. }