MultiExecTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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 PredisTestCase;
  12. use Predis\Client;
  13. use Predis\Command\CommandInterface;
  14. use Predis\Response;
  15. /**
  16. * @group realm-transaction
  17. */
  18. class MultiExecTest extends PredisTestCase
  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_RESPONSE', $tx->get('foo'));
  234. $this->assertSame('DUMMY_RESPONSE', $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_RESPONSE', $reply1);
  259. $test->assertSame('DUMMY_RESPONSE', $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. * @group disconnected
  394. */
  395. public function testExceptionsOptionTakesPrecedenceOverClientOptionsWhenFalse()
  396. {
  397. $expected = array('before', new Response\Error('ERR simulated error'), 'after');
  398. $connection = $this->getMockedConnection(function (CommandInterface $command) use ($expected) {
  399. switch ($command->getId()) {
  400. case 'MULTI':
  401. return true;
  402. case 'EXEC':
  403. return $expected;
  404. default:
  405. return new Response\Status('QUEUED');
  406. }
  407. });
  408. $client = new Client($connection, array('exceptions' => true));
  409. $tx = new MultiExec($client, array('exceptions' => false));
  410. $result = $tx->multi()
  411. ->echo('before')
  412. ->echo('ERROR PLEASE!')
  413. ->echo('after')
  414. ->exec();
  415. $this->assertSame($expected, $result);
  416. }
  417. /**
  418. * @group disconnected
  419. * @expectedException Predis\Response\ServerException
  420. * @expectedExceptionMessage ERR simulated error
  421. */
  422. public function testExceptionsOptionTakesPrecedenceOverClientOptionsWhenTrue()
  423. {
  424. $expected = array('before', new Response\Error('ERR simulated error'), 'after');
  425. $connection = $this->getMockedConnection(function (CommandInterface $command) use ($expected) {
  426. switch ($command->getId()) {
  427. case 'MULTI':
  428. return true;
  429. case 'EXEC':
  430. return $expected;
  431. default:
  432. return new Response\Status('QUEUED');
  433. }
  434. });
  435. $client = new Client($connection, array('exceptions' => false));
  436. $tx = new MultiExec($client, array('exceptions' => true));
  437. $tx->multi()->echo('before')->echo('ERROR PLEASE!')->echo('after')->exec();
  438. }
  439. /**
  440. * @group disconnected
  441. * @expectedException Predis\Response\ServerException
  442. * @expectedExceptionMessage ERR simulated failure on EXEC
  443. */
  444. public function testExceptionsOptionDoesNotAffectTransactionControlCommands()
  445. {
  446. $connection = $this->getMockedConnection(function (CommandInterface $command) {
  447. switch ($command->getId()) {
  448. case 'MULTI':
  449. return true;
  450. case 'EXEC':
  451. return new Response\Error('ERR simulated failure on EXEC');
  452. default:
  453. return new Response\Status('QUEUED');
  454. }
  455. });
  456. $client = new Client($connection, array('exceptions' => false));
  457. $tx = new MultiExec($client);
  458. $tx->multi()->echo('test')->exec();
  459. }
  460. // ******************************************************************** //
  461. // ---- INTEGRATION TESTS --------------------------------------------- //
  462. // ******************************************************************** //
  463. /**
  464. * @group connected
  465. */
  466. public function testIntegrationHandlesStandardExceptionsInBlock()
  467. {
  468. $client = $this->getClient();
  469. $exception = null;
  470. try {
  471. $client->transaction(function ($tx) {
  472. $tx->set('foo', 'bar');
  473. throw new \RuntimeException("TEST");
  474. });
  475. } catch (\Exception $ex) {
  476. $exception = $ex;
  477. }
  478. $this->assertInstanceOf('RuntimeException', $exception);
  479. $this->assertFalse($client->exists('foo'));
  480. }
  481. /**
  482. * @group connected
  483. */
  484. public function testIntegrationThrowsExceptionOnRedisErrorInBlock()
  485. {
  486. $client = $this->getClient();
  487. $exception = null;
  488. $value = (string) rand();
  489. try {
  490. $client->transaction(function ($tx) use ($value) {
  491. $tx->set('foo', 'bar');
  492. $tx->lpush('foo', 'bar');
  493. $tx->set('foo', $value);
  494. });
  495. } catch (Response\ServerException $ex) {
  496. $exception = $ex;
  497. }
  498. $this->assertInstanceOf('Predis\Response\ErrorInterface', $exception);
  499. $this->assertSame($value, $client->get('foo'));
  500. }
  501. /**
  502. * @group connected
  503. */
  504. public function testIntegrationReturnsErrorObjectOnRedisErrorInBlock()
  505. {
  506. $client = $this->getClient(array(), array('exceptions' => false));
  507. $replies = $client->transaction(function ($tx) {
  508. $tx->set('foo', 'bar');
  509. $tx->lpush('foo', 'bar');
  510. $tx->echo('foobar');
  511. });
  512. $this->assertInstanceOf('Predis\Response\Status', $replies[0]);
  513. $this->assertInstanceOf('Predis\Response\Error', $replies[1]);
  514. $this->assertSame('foobar', $replies[2]);
  515. }
  516. /**
  517. * @group connected
  518. */
  519. public function testIntegrationSendMultiOnCommandsAfterDiscard()
  520. {
  521. $client = $this->getClient();
  522. $replies = $client->transaction(function ($tx) {
  523. $tx->set('foo', 'bar');
  524. $tx->discard();
  525. $tx->set('hoge', 'piyo');
  526. });
  527. $this->assertSame(1, count($replies));
  528. $this->assertFalse($client->exists('foo'));
  529. $this->assertTrue($client->exists('hoge'));
  530. }
  531. /**
  532. * @group connected
  533. */
  534. public function testIntegrationWritesOnWatchedKeysAbortTransaction()
  535. {
  536. $exception = null;
  537. $client1 = $this->getClient();
  538. $client2 = $this->getClient();
  539. try {
  540. $client1->transaction(array('watch' => 'sentinel'), function ($tx) use ($client2) {
  541. $tx->set('sentinel', 'client1');
  542. $tx->get('sentinel');
  543. $client2->set('sentinel', 'client2');
  544. });
  545. } catch (AbortedMultiExecException $ex) {
  546. $exception = $ex;
  547. }
  548. $this->assertInstanceOf('Predis\Transaction\AbortedMultiExecException', $exception);
  549. $this->assertSame('client2', $client1->get('sentinel'));
  550. }
  551. /**
  552. * @group connected
  553. */
  554. public function testIntegrationCheckAndSetWithDiscardAndRetry()
  555. {
  556. $client = $this->getClient();
  557. $client->set('foo', 'bar');
  558. $options = array('watch' => 'foo', 'cas' => true);
  559. $replies = $client->transaction($options, function ($tx) {
  560. $tx->watch('foobar');
  561. $foo = $tx->get('foo');
  562. $tx->multi();
  563. $tx->set('foobar', $foo);
  564. $tx->discard();
  565. $tx->mget('foo', 'foobar');
  566. });
  567. $this->assertInternalType('array', $replies);
  568. $this->assertSame(array(array('bar', null)), $replies);
  569. $hijack = true;
  570. $client2 = $this->getClient();
  571. $client->set('foo', 'bar');
  572. $options = array('watch' => 'foo', 'cas' => true, 'retry' => 1);
  573. $replies = $client->transaction($options, function ($tx) use ($client2, &$hijack) {
  574. $foo = $tx->get('foo');
  575. $tx->multi();
  576. $tx->set('foobar', $foo);
  577. $tx->discard();
  578. if ($hijack) {
  579. $hijack = false;
  580. $client2->set('foo', 'hijacked!');
  581. }
  582. $tx->mget('foo', 'foobar');
  583. });
  584. $this->assertInternalType('array', $replies);
  585. $this->assertSame(array(array('hijacked!', null)), $replies);
  586. }
  587. // ******************************************************************** //
  588. // ---- HELPER METHODS ------------------------------------------------ //
  589. // ******************************************************************** //
  590. /**
  591. * Returns a mocked instance of Predis\Connection\SingleConnectionInterface
  592. * using the specified callback to return values from executeCommand().
  593. *
  594. * @param \Closure $executeCallback
  595. * @return \Predis\Connection\SingleConnectionInterface
  596. */
  597. protected function getMockedConnection($executeCallback)
  598. {
  599. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  600. $connection->expects($this->any())
  601. ->method('executeCommand')
  602. ->will($this->returnCallback($executeCallback));
  603. return $connection;
  604. }
  605. /**
  606. * Returns a mocked instance of Predis\Transaction\MultiExec using
  607. * the specified callback to return values from the executeCommand method
  608. * of the underlying connection.
  609. *
  610. * @param \Closure $executeCallback
  611. * @return MultiExec
  612. */
  613. protected function getMockedTransaction($executeCallback, $txOpts = null, $clientOpts = null)
  614. {
  615. $connection = $this->getMockedConnection($executeCallback);
  616. $client = new Client($connection, $clientOpts ?: array());
  617. $transaction = new MultiExec($client, $txOpts ?: array());
  618. return $transaction;
  619. }
  620. /**
  621. * Returns a callback that emulates a server-side MULTI/EXEC transaction context.
  622. *
  623. * @param array $expected Expected replies.
  624. * @param array $commands Reference to an array that stores the whole flow of commands.
  625. * @return \Closure
  626. */
  627. protected function getExecuteCallback($expected = array(), &$commands = array(), &$cas = array())
  628. {
  629. $multi = $watch = $abort = false;
  630. return function (CommandInterface $command) use (&$expected, &$commands, &$cas, &$multi, &$watch, &$abort) {
  631. $cmd = $command->getId();
  632. if ($multi || $cmd === 'MULTI') {
  633. $commands[] = $command;
  634. } else {
  635. $cas[] = $command;
  636. }
  637. switch ($cmd) {
  638. case 'WATCH':
  639. if ($multi) {
  640. return new Response\Error("ERR $cmd inside MULTI is not allowed");
  641. }
  642. return $watch = true;
  643. case 'MULTI':
  644. if ($multi) {
  645. return new Response\Error("ERR MULTI calls can not be nested");
  646. }
  647. return $multi = true;
  648. case 'EXEC':
  649. if (!$multi) {
  650. return new Response\Error("ERR $cmd without MULTI");
  651. }
  652. $watch = $multi = false;
  653. if ($abort) {
  654. $commands = $cas = array();
  655. $abort = false;
  656. return null;
  657. }
  658. return $expected;
  659. case 'DISCARD':
  660. if (!$multi) {
  661. return new Response\Error("ERR $cmd without MULTI");
  662. }
  663. $watch = $multi = false;
  664. return true;
  665. case 'ECHO':
  666. @list($trigger) = $command->getArguments();
  667. if (strpos($trigger, 'ERR ') === 0) {
  668. throw new Response\ServerException($trigger);
  669. }
  670. if ($trigger === '!!ABORT!!' && $multi) {
  671. $abort = true;
  672. }
  673. return new Response\Status('QUEUED');
  674. case 'UNWATCH':
  675. $watch = false;
  676. default:
  677. return $multi ? new Response\Status('QUEUED') : 'DUMMY_RESPONSE';
  678. }
  679. };
  680. }
  681. /**
  682. * Converts an array of instances of Predis\Command\CommandInterface and
  683. * returns an array containing their IDs.
  684. *
  685. * @param array $commands List of commands instances.
  686. * @return array
  687. */
  688. protected static function commandsToIDs($commands) {
  689. return array_map(function($cmd) { return $cmd->getId(); }, $commands);
  690. }
  691. /**
  692. * Returns a client instance connected to the specified Redis
  693. * server instance to perform integration tests.
  694. *
  695. * @param array Additional connection parameters.
  696. * @param array Additional client options.
  697. * @return Client client instance.
  698. */
  699. protected function getClient(array $parameters = array(), array $options = array())
  700. {
  701. return $this->createClient($parameters, $options);
  702. }
  703. }