MultiExecTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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\Transaction;
  11. use PHPUnit_Framework_TestCase as StandardTestCase;
  12. use Predis\Client;
  13. use Predis\Command\CommandInterface;
  14. use Predis\Response;
  15. /**
  16. * @group realm-transaction
  17. */
  18. class MultiExecTest extends StandardTestCase
  19. {
  20. /**
  21. * @group disconnected
  22. * @expectedException Predis\NotSupportedException
  23. * @expectedExceptionMessage The current profile does not support MULTI, EXEC and DISCARD
  24. */
  25. public function testThrowsExceptionOnUnsupportedMultiExecInProfile()
  26. {
  27. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  28. $client = new Client($connection, array('profile' => '1.2'));
  29. $tx = new MultiExec($client);
  30. }
  31. /**
  32. * @group disconnected
  33. * @expectedException Predis\NotSupportedException
  34. * @expectedExceptionMessage WATCH is not supported by the current profile
  35. */
  36. public function testThrowsExceptionOnUnsupportedWatchInProfile()
  37. {
  38. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  39. $client = new Client($connection, array('profile' => '2.0'));
  40. $tx = new MultiExec($client, array('options' => 'cas'));
  41. $tx->watch('foo');
  42. }
  43. /**
  44. * @group disconnected
  45. * @expectedException Predis\NotSupportedException
  46. * @expectedExceptionMessage UNWATCH is not supported by the current profile
  47. */
  48. public function testThrowsExceptionOnUnsupportedUnwatchInProfile()
  49. {
  50. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  51. $client = new Client($connection, array('profile' => '2.0'));
  52. $tx = new MultiExec($client, array('options' => 'cas'));
  53. $tx->unwatch('foo');
  54. }
  55. /**
  56. * @group disconnected
  57. */
  58. public function testExecutionWithFluentInterface()
  59. {
  60. $commands = array();
  61. $expected = array('one', 'two', 'three');
  62. $callback = $this->getExecuteCallback($expected, $commands);
  63. $tx = $this->getMockedTransaction($callback);
  64. $this->assertSame($expected, $tx->echo('one')->echo('two')->echo('three')->execute());
  65. $this->assertSame(array('MULTI', 'ECHO', 'ECHO', 'ECHO', 'EXEC'), self::commandsToIDs($commands));
  66. }
  67. /**
  68. * @group disconnected
  69. */
  70. public function testExecutionWithCallable()
  71. {
  72. $commands = array();
  73. $expected = array('one', 'two', 'three');
  74. $callback = $this->getExecuteCallback($expected, $commands);
  75. $tx = $this->getMockedTransaction($callback);
  76. $replies = $tx->execute(function ($tx) {
  77. $tx->echo('one');
  78. $tx->echo('two');
  79. $tx->echo('three');
  80. });
  81. $this->assertSame($expected, $replies);
  82. $this->assertSame(array('MULTI', 'ECHO', 'ECHO', 'ECHO', 'EXEC'), self::commandsToIDs($commands));
  83. }
  84. /**
  85. * @group disconnected
  86. */
  87. public function testCannotMixExecutionWithFluentInterfaceAndCallable()
  88. {
  89. $commands = array();
  90. $callback = $this->getExecuteCallback(null, $commands);
  91. $tx = $this->getMockedTransaction($callback);
  92. $exception = null;
  93. try {
  94. $tx->echo('foo')->execute(function ($tx) {
  95. $tx->echo('bar');
  96. });
  97. } catch (\Exception $ex) {
  98. $exception = $ex;
  99. }
  100. $this->assertInstanceOf('Predis\ClientException', $exception);
  101. $this->assertSame(array('MULTI', 'ECHO', 'DISCARD'), self::commandsToIDs($commands));
  102. }
  103. /**
  104. * @group disconnected
  105. */
  106. public function testEmptyTransactionDoesNotSendMultiExecCommands()
  107. {
  108. $commands = array();
  109. $callback = $this->getExecuteCallback(null, $commands);
  110. $tx = $this->getMockedTransaction($callback);
  111. $replies = $tx->execute(function ($tx) {
  112. // NOOP
  113. });
  114. $this->assertNull($replies);
  115. $this->assertSame(array(), self::commandsToIDs($commands));
  116. }
  117. /**
  118. * @group disconnected
  119. * @expectedException Predis\ClientException
  120. * @expectedExceptionMessage Cannot invoke "execute" or "exec" inside an active transaction context
  121. */
  122. public function testThrowsExceptionOnExecInsideTransactionBlock()
  123. {
  124. $commands = array();
  125. $callback = $this->getExecuteCallback(null, $commands);
  126. $tx = $this->getMockedTransaction($callback);
  127. $replies = $tx->execute(function ($tx) {
  128. $tx->exec();
  129. });
  130. $this->assertNull($replies);
  131. $this->assertSame(array(), self::commandsToIDs($commands));
  132. }
  133. /**
  134. * @group disconnected
  135. */
  136. public function testEmptyTransactionIgnoresDiscard()
  137. {
  138. $commands = array();
  139. $callback = $this->getExecuteCallback(null, $commands);
  140. $tx = $this->getMockedTransaction($callback);
  141. $replies = $tx->execute(function ($tx) {
  142. $tx->discard();
  143. });
  144. $this->assertNull($replies);
  145. $this->assertSame(array(), self::commandsToIDs($commands));
  146. }
  147. /**
  148. * @group disconnected
  149. */
  150. public function testTransactionWithCommandsSendsDiscard()
  151. {
  152. $commands = array();
  153. $callback = $this->getExecuteCallback(null, $commands);
  154. $tx = $this->getMockedTransaction($callback);
  155. $replies = $tx->execute(function ($tx) {
  156. $tx->set('foo', 'bar');
  157. $tx->get('foo');
  158. $tx->discard();
  159. });
  160. $this->assertNull($replies);
  161. $this->assertSame(array('MULTI', 'SET', 'GET', 'DISCARD'), self::commandsToIDs($commands));
  162. }
  163. /**
  164. * @group disconnected
  165. */
  166. public function testSendMultiOnCommandsFollowingDiscard()
  167. {
  168. $commands = array();
  169. $expected = array('after DISCARD');
  170. $callback = $this->getExecuteCallback($expected, $commands);
  171. $tx = $this->getMockedTransaction($callback);
  172. $replies = $tx->execute(function ($tx) {
  173. $tx->echo('before DISCARD');
  174. $tx->discard();
  175. $tx->echo('after DISCARD');
  176. });
  177. $this->assertSame($replies, $expected);
  178. $this->assertSame(array('MULTI', 'ECHO', 'DISCARD', 'MULTI', 'ECHO', 'EXEC'), self::commandsToIDs($commands));
  179. }
  180. /**
  181. * @group disconnected
  182. * @expectedException Predis\ClientException
  183. */
  184. public function testThrowsExceptionOnWatchInsideMulti()
  185. {
  186. $callback = $this->getExecuteCallback();
  187. $tx = $this->getMockedTransaction($callback);
  188. $tx->echo('foobar')->watch('foo')->execute();
  189. }
  190. /**
  191. * @group disconnected
  192. */
  193. public function testUnwatchInsideMulti()
  194. {
  195. $commands = array();
  196. $expected = array('foobar', true);
  197. $callback = $this->getExecuteCallback($expected, $commands);
  198. $tx = $this->getMockedTransaction($callback);
  199. $replies = $tx->echo('foobar')->unwatch('foo')->execute();
  200. $this->assertSame($replies, $expected);
  201. $this->assertSame(array('MULTI', 'ECHO', 'UNWATCH', 'EXEC'), self::commandsToIDs($commands));
  202. }
  203. /**
  204. * @group disconnected
  205. */
  206. public function testAutomaticWatchInOptions()
  207. {
  208. $txCommands = $casCommands = array();
  209. $expected = array('bar', 'piyo');
  210. $options = array('watch' => array('foo', 'hoge'));
  211. $callback = $this->getExecuteCallback($expected, $txCommands, $casCommands);
  212. $tx = $this->getMockedTransaction($callback, $options);
  213. $replies = $tx->execute(function ($tx) {
  214. $tx->get('foo');
  215. $tx->get('hoge');
  216. });
  217. $this->assertSame($replies, $expected);
  218. $this->assertSame(array('WATCH'), self::commandsToIDs($casCommands));
  219. $this->assertSame(array('foo', 'hoge'), $casCommands[0]->getArguments());
  220. $this->assertSame(array('MULTI', 'GET', 'GET', 'EXEC'), self::commandsToIDs($txCommands));
  221. }
  222. /**
  223. * @group disconnected
  224. */
  225. public function testCheckAndSetWithFluentInterface()
  226. {
  227. $txCommands = $casCommands = array();
  228. $expected = array('bar', 'piyo');
  229. $options = array('cas' => true, 'watch' => array('foo', 'hoge'));
  230. $callback = $this->getExecuteCallback($expected, $txCommands, $casCommands);
  231. $tx = $this->getMockedTransaction($callback, $options);
  232. $tx->watch('foobar');
  233. $this->assertSame('DUMMY_REPLY', $tx->get('foo'));
  234. $this->assertSame('DUMMY_REPLY', $tx->get('hoge'));
  235. $replies = $tx->multi()
  236. ->get('foo')
  237. ->get('hoge')
  238. ->execute();
  239. $this->assertSame($replies, $expected);
  240. $this->assertSame(array('WATCH', 'WATCH', 'GET', 'GET'), self::commandsToIDs($casCommands));
  241. $this->assertSame(array('MULTI', 'GET', 'GET', 'EXEC'), self::commandsToIDs($txCommands));
  242. }
  243. /**
  244. * @group disconnected
  245. */
  246. public function testCheckAndSetWithBlock()
  247. {
  248. $txCommands = $casCommands = array();
  249. $expected = array('bar', 'piyo');
  250. $options = array('cas' => true, 'watch' => array('foo', 'hoge'));
  251. $callback = $this->getExecuteCallback($expected, $txCommands, $casCommands);
  252. $tx = $this->getMockedTransaction($callback, $options);
  253. $test = $this;
  254. $replies = $tx->execute(function ($tx) use ($test) {
  255. $tx->watch('foobar');
  256. $reply1 = $tx->get('foo');
  257. $reply2 = $tx->get('hoge');
  258. $test->assertSame('DUMMY_REPLY', $reply1);
  259. $test->assertSame('DUMMY_REPLY', $reply2);
  260. $tx->multi();
  261. $tx->get('foo');
  262. $tx->get('hoge');
  263. });
  264. $this->assertSame($replies, $expected);
  265. $this->assertSame(array('WATCH', 'WATCH', 'GET', 'GET'), self::commandsToIDs($casCommands));
  266. $this->assertSame(array('MULTI', 'GET', 'GET', 'EXEC'), self::commandsToIDs($txCommands));
  267. }
  268. /**
  269. * @group disconnected
  270. */
  271. public function testCheckAndSetWithEmptyBlock()
  272. {
  273. $txCommands = $casCommands = array();
  274. $options = array('cas' => true);
  275. $callback = $this->getExecuteCallback(array(), $txCommands, $casCommands);
  276. $tx = $this->getMockedTransaction($callback, $options);
  277. $tx->execute(function ($tx) {
  278. $tx->multi();
  279. });
  280. $this->assertSame(array(), self::commandsToIDs($casCommands));
  281. $this->assertSame(array(), self::commandsToIDs($txCommands));
  282. }
  283. /**
  284. * @group disconnected
  285. */
  286. public function testCheckAndSetWithoutExec()
  287. {
  288. $txCommands = $casCommands = array();
  289. $options = array('cas' => true);
  290. $callback = $this->getExecuteCallback(array(), $txCommands, $casCommands);
  291. $tx = $this->getMockedTransaction($callback, $options);
  292. $tx->execute(function ($tx) {
  293. $bar = $tx->get('foo');
  294. $tx->set('hoge', 'piyo');
  295. });
  296. $this->assertSame(array('GET', 'SET'), self::commandsToIDs($casCommands));
  297. $this->assertSame(array(), self::commandsToIDs($txCommands));
  298. }
  299. /**
  300. * @group disconnected
  301. * @expectedException InvalidArgumentException
  302. * @expectedExceptionMessage Automatic retries can be used only when a callable block is provided
  303. */
  304. public function testThrowsExceptionOnAutomaticRetriesWithFluentInterface()
  305. {
  306. $options = array('retry' => 1);
  307. $callback = $this->getExecuteCallback();
  308. $tx = $this->getMockedTransaction($callback, $options);
  309. $tx->echo('message')->execute();
  310. }
  311. /**
  312. * @group disconnected
  313. */
  314. public function testAutomaticRetryOnServerSideTransactionAbort()
  315. {
  316. $casCommands = $txCommands = array();
  317. $expected = array('bar');
  318. $options = array('watch' => array('foo', 'bar'), 'retry' => ($attempts = 2) + 1);
  319. $sentinel = $this->getMock('stdClass', array('signal'));
  320. $sentinel->expects($this->exactly($attempts))->method('signal');
  321. $callback = $this->getExecuteCallback($expected, $txCommands, $casCommands);
  322. $tx = $this->getMockedTransaction($callback, $options);
  323. $replies = $tx->execute(function ($tx) use ($sentinel, &$attempts) {
  324. $tx->get('foo');
  325. if ($attempts > 0) {
  326. $attempts -= 1;
  327. $sentinel->signal();
  328. $tx->echo('!!ABORT!!');
  329. }
  330. });
  331. $this->assertSame($replies, $expected);
  332. $this->assertSame(array('WATCH'), self::commandsToIDs($casCommands));
  333. $this->assertSame(array('foo', 'bar'), $casCommands[0]->getArguments());
  334. $this->assertSame(array('MULTI', 'GET', 'EXEC'), self::commandsToIDs($txCommands));
  335. }
  336. /**
  337. * @group disconnected
  338. * @expectedException Predis\Transaction\AbortedMultiExecException
  339. */
  340. public function testThrowsExceptionOnServerSideTransactionAbort()
  341. {
  342. $callback = $this->getExecuteCallback();
  343. $tx = $this->getMockedTransaction($callback);
  344. $replies = $tx->execute(function ($tx) {
  345. $tx->echo('!!ABORT!!');
  346. });
  347. }
  348. /**
  349. * @group disconnected
  350. */
  351. public function testHandlesStandardExceptionsInBlock()
  352. {
  353. $commands = array();
  354. $expected = array('foobar', true);
  355. $callback = $this->getExecuteCallback($expected, $commands);
  356. $tx = $this->getMockedTransaction($callback);
  357. $replies = null;
  358. try {
  359. $replies = $tx->execute(function ($tx) {
  360. $tx->set('foo', 'bar');
  361. $tx->get('foo');
  362. throw new \RuntimeException('TEST');
  363. });
  364. } catch (\Exception $ex) {
  365. // NOOP
  366. }
  367. $this->assertNull($replies, $expected);
  368. $this->assertSame(array('MULTI', 'SET', 'GET', 'DISCARD'), self::commandsToIDs($commands));
  369. }
  370. /**
  371. * @group disconnected
  372. */
  373. public function testHandlesServerExceptionsInBlock()
  374. {
  375. $commands = array();
  376. $expected = array('foobar', true);
  377. $callback = $this->getExecuteCallback($expected, $commands);
  378. $tx = $this->getMockedTransaction($callback);
  379. $replies = null;
  380. try {
  381. $replies = $tx->execute(function ($tx) {
  382. $tx->set('foo', 'bar');
  383. $tx->echo('ERR Invalid operation');
  384. $tx->get('foo');
  385. });
  386. } catch (Response\ServerException $ex) {
  387. $tx->discard();
  388. }
  389. $this->assertNull($replies);
  390. $this->assertSame(array('MULTI', 'SET', 'ECHO', 'DISCARD'), self::commandsToIDs($commands));
  391. }
  392. // ******************************************************************** //
  393. // ---- INTEGRATION TESTS --------------------------------------------- //
  394. // ******************************************************************** //
  395. /**
  396. * @group connected
  397. */
  398. public function testIntegrationHandlesStandardExceptionsInBlock()
  399. {
  400. $client = $this->getClient();
  401. $exception = null;
  402. try {
  403. $client->transaction(function ($tx) {
  404. $tx->set('foo', 'bar');
  405. throw new \RuntimeException("TEST");
  406. });
  407. } catch (\Exception $ex) {
  408. $exception = $ex;
  409. }
  410. $this->assertInstanceOf('RuntimeException', $exception);
  411. $this->assertFalse($client->exists('foo'));
  412. }
  413. /**
  414. * @group connected
  415. */
  416. public function testIntegrationThrowsExceptionOnRedisErrorInBlock()
  417. {
  418. $client = $this->getClient();
  419. $exception = null;
  420. $value = (string) rand();
  421. try {
  422. $client->transaction(function ($tx) use ($value) {
  423. $tx->set('foo', 'bar');
  424. $tx->lpush('foo', 'bar');
  425. $tx->set('foo', $value);
  426. });
  427. } catch (Response\ServerException $ex) {
  428. $exception = $ex;
  429. }
  430. $this->assertInstanceOf('Predis\Response\ErrorInterface', $exception);
  431. $this->assertSame($value, $client->get('foo'));
  432. }
  433. /**
  434. * @group connected
  435. */
  436. public function testIntegrationReturnsErrorObjectOnRedisErrorInBlock()
  437. {
  438. $client = $this->getClient(array(), array('exceptions' => false));
  439. $replies = $client->transaction(function ($tx) {
  440. $tx->set('foo', 'bar');
  441. $tx->lpush('foo', 'bar');
  442. $tx->echo('foobar');
  443. });
  444. $this->assertTrue($replies[0]);
  445. $this->assertInstanceOf('Predis\Response\ErrorInterface', $replies[1]);
  446. $this->assertSame('foobar', $replies[2]);
  447. }
  448. /**
  449. * @group connected
  450. */
  451. public function testIntegrationSendMultiOnCommandsAfterDiscard()
  452. {
  453. $client = $this->getClient();
  454. $replies = $client->transaction(function ($tx) {
  455. $tx->set('foo', 'bar');
  456. $tx->discard();
  457. $tx->set('hoge', 'piyo');
  458. });
  459. $this->assertSame(1, count($replies));
  460. $this->assertFalse($client->exists('foo'));
  461. $this->assertTrue($client->exists('hoge'));
  462. }
  463. /**
  464. * @group connected
  465. */
  466. public function testIntegrationWritesOnWatchedKeysAbortTransaction()
  467. {
  468. $exception = null;
  469. $client1 = $this->getClient();
  470. $client2 = $this->getClient();
  471. try {
  472. $client1->transaction(array('watch' => 'sentinel'), function ($tx) use ($client2) {
  473. $tx->set('sentinel', 'client1');
  474. $tx->get('sentinel');
  475. $client2->set('sentinel', 'client2');
  476. });
  477. } catch (AbortedMultiExecException $ex) {
  478. $exception = $ex;
  479. }
  480. $this->assertInstanceOf('Predis\Transaction\AbortedMultiExecException', $exception);
  481. $this->assertSame('client2', $client1->get('sentinel'));
  482. }
  483. /**
  484. * @group connected
  485. */
  486. public function testIntegrationCheckAndSetWithDiscardAndRetry()
  487. {
  488. $client = $this->getClient();
  489. $client->set('foo', 'bar');
  490. $options = array('watch' => 'foo', 'cas' => true);
  491. $replies = $client->transaction($options, function ($tx) {
  492. $tx->watch('foobar');
  493. $foo = $tx->get('foo');
  494. $tx->multi();
  495. $tx->set('foobar', $foo);
  496. $tx->discard();
  497. $tx->mget('foo', 'foobar');
  498. });
  499. $this->assertInternalType('array', $replies);
  500. $this->assertSame(array(array('bar', null)), $replies);
  501. $hijack = true;
  502. $client2 = $this->getClient();
  503. $client->set('foo', 'bar');
  504. $options = array('watch' => 'foo', 'cas' => true, 'retry' => 1);
  505. $replies = $client->transaction($options, function ($tx) use ($client2, &$hijack) {
  506. $foo = $tx->get('foo');
  507. $tx->multi();
  508. $tx->set('foobar', $foo);
  509. $tx->discard();
  510. if ($hijack) {
  511. $hijack = false;
  512. $client2->set('foo', 'hijacked!');
  513. }
  514. $tx->mget('foo', 'foobar');
  515. });
  516. $this->assertInternalType('array', $replies);
  517. $this->assertSame(array(array('hijacked!', null)), $replies);
  518. }
  519. // ******************************************************************** //
  520. // ---- HELPER METHODS ------------------------------------------------ //
  521. // ******************************************************************** //
  522. /**
  523. * Returns a mocked instance of Predis\Connection\SingleConnectionInterface
  524. * using the specified callback to return values from executeCommand().
  525. *
  526. * @param \Closure $executeCallback
  527. * @return \Predis\Connection\SingleConnectionInterface
  528. */
  529. protected function getMockedConnection($executeCallback)
  530. {
  531. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  532. $connection->expects($this->any())
  533. ->method('executeCommand')
  534. ->will($this->returnCallback($executeCallback));
  535. return $connection;
  536. }
  537. /**
  538. * Returns a mocked instance of Predis\Transaction\MultiExec using
  539. * the specified callback to return values from the executeCommand method
  540. * of the underlying connection.
  541. *
  542. * @param \Closure $executeCallback
  543. * @return MultiExec
  544. */
  545. protected function getMockedTransaction($executeCallback, $options = array())
  546. {
  547. $connection = $this->getMockedConnection($executeCallback);
  548. $client = new Client($connection);
  549. $transaction = new MultiExec($client, $options);
  550. return $transaction;
  551. }
  552. /**
  553. * Returns a callback that emulates a server-side MULTI/EXEC transaction context.
  554. *
  555. * @param array $expected Expected replies.
  556. * @param array $commands Reference to an array that stores the whole flow of commands.
  557. * @return \Closure
  558. */
  559. protected function getExecuteCallback($expected = array(), &$commands = array(), &$cas = array())
  560. {
  561. $multi = $watch = $abort = false;
  562. return function (CommandInterface $command) use (&$expected, &$commands, &$cas, &$multi, &$watch, &$abort) {
  563. $cmd = $command->getId();
  564. if ($multi || $cmd === 'MULTI') {
  565. $commands[] = $command;
  566. } else {
  567. $cas[] = $command;
  568. }
  569. switch ($cmd) {
  570. case 'WATCH':
  571. if ($multi) {
  572. throw new Response\ServerException("ERR $cmd inside MULTI is not allowed");
  573. }
  574. return $watch = true;
  575. case 'MULTI':
  576. if ($multi) {
  577. throw new Response\ServerException("ERR MULTI calls can not be nested");
  578. }
  579. return $multi = true;
  580. case 'EXEC':
  581. if (!$multi) {
  582. throw new Response\ServerException("ERR $cmd without MULTI");
  583. }
  584. $watch = $multi = false;
  585. if ($abort) {
  586. $commands = $cas = array();
  587. $abort = false;
  588. return null;
  589. }
  590. return $expected;
  591. case 'DISCARD':
  592. if (!$multi) {
  593. throw new Response\ServerException("ERR $cmd without MULTI");
  594. }
  595. $watch = $multi = false;
  596. return true;
  597. case 'ECHO':
  598. @list($trigger) = $command->getArguments();
  599. if (strpos($trigger, 'ERR ') === 0) {
  600. throw new Response\ServerException($trigger);
  601. }
  602. if ($trigger === '!!ABORT!!' && $multi) {
  603. $abort = true;
  604. }
  605. return new Response\StatusQueued();
  606. case 'UNWATCH':
  607. $watch = false;
  608. default:
  609. return $multi ? new Response\StatusQueued() : 'DUMMY_REPLY';
  610. }
  611. };
  612. }
  613. /**
  614. * Converts an array of instances of Predis\Command\CommandInterface and
  615. * returns an array containing their IDs.
  616. *
  617. * @param array $commands List of commands instances.
  618. * @return array
  619. */
  620. protected static function commandsToIDs($commands) {
  621. return array_map(function($cmd) { return $cmd->getId(); }, $commands);
  622. }
  623. /**
  624. * Returns a client instance connected to the specified Redis
  625. * server instance to perform integration tests.
  626. *
  627. * @param array Additional connection parameters.
  628. * @param array Additional client options.
  629. * @return Client client instance.
  630. */
  631. protected function getClient(array $parameters = array(), array $options = array())
  632. {
  633. $parameters = array_merge(array(
  634. 'scheme' => 'tcp',
  635. 'host' => REDIS_SERVER_HOST,
  636. 'port' => REDIS_SERVER_PORT,
  637. 'database' => REDIS_SERVER_DBNUM,
  638. ), $parameters);
  639. $options = array_merge(array(
  640. 'profile' => REDIS_SERVER_VERSION
  641. ), $options);
  642. $client = new Client($parameters, $options);
  643. $client->connect();
  644. $client->flushdb();
  645. return $client;
  646. }
  647. }