PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/predis/predis/tests/Predis/Replication/ReplicationStrategyTest.php

https://gitlab.com/xolotsoft/pumasruiz
PHP | 382 lines | 260 code | 55 blank | 67 comment | 1 complexity | 7323984dbb428cd8b8db00bae54e19fa MD5 | raw file
  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\Replication;
  11. use PredisTestCase;
  12. use Predis\Profile\ServerProfile;
  13. /**
  14. *
  15. */
  16. class ReplicationStrategyTest extends PredisTestCase
  17. {
  18. /**
  19. * @group disconnected
  20. */
  21. public function testReadCommands()
  22. {
  23. $profile = ServerProfile::getDevelopment();
  24. $strategy = new ReplicationStrategy();
  25. foreach ($this->getExpectedCommands('read') as $commandId) {
  26. $command = $profile->createCommand($commandId);
  27. $this->assertTrue($strategy->isReadOperation($command));
  28. }
  29. }
  30. /**
  31. * @group disconnected
  32. */
  33. public function testWriteCommands()
  34. {
  35. $profile = ServerProfile::getDevelopment();
  36. $strategy = new ReplicationStrategy();
  37. foreach ($this->getExpectedCommands('write') as $commandId) {
  38. $command = $profile->createCommand($commandId);
  39. $this->assertFalse($strategy->isReadOperation($command), $commandId);
  40. }
  41. }
  42. /**
  43. * @group disconnected
  44. */
  45. public function testDisallowedCommands()
  46. {
  47. $profile = ServerProfile::getDevelopment();
  48. $strategy = new ReplicationStrategy();
  49. foreach ($this->getExpectedCommands('disallowed') as $commandId) {
  50. $command = $profile->createCommand($commandId);
  51. $this->assertTrue($strategy->isDisallowedOperation($command), $commandId);
  52. }
  53. }
  54. /**
  55. * @group disconnected
  56. */
  57. public function testSortCommand()
  58. {
  59. $profile = ServerProfile::getDevelopment();
  60. $strategy = new ReplicationStrategy();
  61. $cmdReadSort = $profile->createCommand('SORT', array('key:list'));
  62. $this->assertTrue($strategy->isReadOperation($cmdReadSort), 'SORT [read-only]');
  63. $cmdWriteSort = $profile->createCommand('SORT', array('key:list', array('store' => 'key:stored')));
  64. $this->assertFalse($strategy->isReadOperation($cmdWriteSort), 'SORT [write with STORE]');
  65. }
  66. /**
  67. * @group disconnected
  68. * @expectedException Predis\NotSupportedException
  69. * @expectedExceptionMessage The command INFO is not allowed in replication mode
  70. */
  71. public function testUsingDisallowedCommandThrowsException()
  72. {
  73. $profile = ServerProfile::getDevelopment();
  74. $strategy = new ReplicationStrategy();
  75. $command = $profile->createCommand('INFO');
  76. $strategy->isReadOperation($command);
  77. }
  78. /**
  79. * @group disconnected
  80. */
  81. public function testDefaultIsWriteOperation()
  82. {
  83. $strategy = new ReplicationStrategy();
  84. $command = $this->getMock('Predis\Command\CommandInterface');
  85. $command->expects($this->any())
  86. ->method('getId')
  87. ->will($this->returnValue('CMDTEST'));
  88. $this->assertFalse($strategy->isReadOperation($command));
  89. }
  90. /**
  91. * @group disconnected
  92. */
  93. public function testCanSetCommandAsReadOperation()
  94. {
  95. $strategy = new ReplicationStrategy();
  96. $command = $this->getMock('Predis\Command\CommandInterface');
  97. $command->expects($this->any())
  98. ->method('getId')
  99. ->will($this->returnValue('CMDTEST'));
  100. $strategy->setCommandReadOnly('CMDTEST', true);
  101. $this->assertTrue($strategy->isReadOperation($command));
  102. }
  103. /**
  104. * @group disconnected
  105. */
  106. public function testCanSetCommandAsWriteOperation()
  107. {
  108. $strategy = new ReplicationStrategy();
  109. $command = $this->getMock('Predis\Command\CommandInterface');
  110. $command->expects($this->any())
  111. ->method('getId')
  112. ->will($this->returnValue('CMDTEST'));
  113. $strategy->setCommandReadOnly('CMDTEST', false);
  114. $this->assertFalse($strategy->isReadOperation($command));
  115. $strategy->setCommandReadOnly('GET', false);
  116. $this->assertFalse($strategy->isReadOperation($command));
  117. }
  118. /**
  119. * @group disconnected
  120. */
  121. public function testCanUseCallableToCheckCommand()
  122. {
  123. $strategy = new ReplicationStrategy();
  124. $profile = ServerProfile::getDevelopment();
  125. $strategy->setCommandReadOnly('SET', function ($command) {
  126. return $command->getArgument(1) === true;
  127. });
  128. $command = $profile->createCommand('SET', array('trigger', false));
  129. $this->assertFalse($strategy->isReadOperation($command));
  130. $command = $profile->createCommand('SET', array('trigger', true));
  131. $this->assertTrue($strategy->isReadOperation($command));
  132. }
  133. /**
  134. * @group disconnected
  135. */
  136. public function testSetLuaScriptAsReadOperation()
  137. {
  138. $strategy = new ReplicationStrategy();
  139. $profile = ServerProfile::getDevelopment();
  140. $writeScript = 'redis.call("set", "foo", "bar")';
  141. $readScript = 'return true';
  142. $strategy->setScriptReadOnly($readScript, true);
  143. $cmdEval = $profile->createCommand('EVAL', array($writeScript));
  144. $cmdEvalSHA = $profile->createCommand('EVALSHA', array(sha1($writeScript)));
  145. $this->assertFalse($strategy->isReadOperation($cmdEval));
  146. $this->assertFalse($strategy->isReadOperation($cmdEvalSHA));
  147. $cmdEval = $profile->createCommand('EVAL', array($readScript));
  148. $cmdEvalSHA = $profile->createCommand('EVALSHA', array(sha1($readScript)));
  149. $this->assertTrue($strategy->isReadOperation($cmdEval));
  150. $this->assertTrue($strategy->isReadOperation($cmdEvalSHA));
  151. }
  152. /**
  153. * @group disconnected
  154. */
  155. public function testSetLuaScriptAsReadOperationWorksWithScriptedCommand()
  156. {
  157. $strategy = new ReplicationStrategy();
  158. $command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript'));
  159. $command->expects($this->any())
  160. ->method('getScript')
  161. ->will($this->returnValue($script = 'return true'));
  162. $strategy->setScriptReadOnly($script, function ($command) {
  163. return $command->getArgument(2) === true;
  164. });
  165. $command->setArguments(array(false));
  166. $this->assertFalse($strategy->isReadOperation($command));
  167. $command->setArguments(array(true));
  168. $this->assertTrue($strategy->isReadOperation($command));
  169. }
  170. /**
  171. * @group disconnected
  172. */
  173. public function testSetLuaScriptAsReadOperationWorksWithScriptedCommandAndCallableCheck()
  174. {
  175. $strategy = new ReplicationStrategy();
  176. $command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript'));
  177. $command->expects($this->any())
  178. ->method('getScript')
  179. ->will($this->returnValue($script = 'return true'));
  180. $command->setArguments(array('trigger', false));
  181. $strategy->setScriptReadOnly($script, true);
  182. $this->assertTrue($strategy->isReadOperation($command));
  183. }
  184. // ******************************************************************** //
  185. // ---- HELPER METHODS ------------------------------------------------ //
  186. // ******************************************************************** //
  187. /**
  188. * Returns the list of expected supported commands.
  189. *
  190. * @param string $type Optional type of command (based on its keys)
  191. * @return array
  192. */
  193. protected function getExpectedCommands($type = null)
  194. {
  195. $commands = array(
  196. /* commands operating on the connection */
  197. 'AUTH' => 'read',
  198. 'SELECT' => 'read',
  199. 'ECHO' => 'read',
  200. 'QUIT' => 'read',
  201. 'OBJECT' => 'read',
  202. 'BITCOUNT' => 'read',
  203. 'TIME' => 'read',
  204. 'SHUTDOWN' => 'disallowed',
  205. 'INFO' => 'disallowed',
  206. 'DBSIZE' => 'disallowed',
  207. 'LASTSAVE' => 'disallowed',
  208. 'CONFIG' => 'disallowed',
  209. 'MONITOR' => 'disallowed',
  210. 'SLAVEOF' => 'disallowed',
  211. 'SAVE' => 'disallowed',
  212. 'BGSAVE' => 'disallowed',
  213. 'BGREWRITEAOF' => 'disallowed',
  214. 'SLOWLOG' => 'disallowed',
  215. /* commands operating on the key space */
  216. 'EXISTS' => 'read',
  217. 'DEL' => 'write',
  218. 'TYPE' => 'read',
  219. 'EXPIRE' => 'write',
  220. 'EXPIREAT' => 'write',
  221. 'PERSIST' => 'write',
  222. 'PEXPIRE' => 'write',
  223. 'PEXPIREAT' => 'write',
  224. 'TTL' => 'read',
  225. 'PTTL' => 'write',
  226. 'SORT' => 'variable',
  227. 'KEYS' => 'read',
  228. 'SCAN' => 'read',
  229. 'RANDOMKEY' => 'read',
  230. /* commands operating on string values */
  231. 'APPEND' => 'write',
  232. 'DECR' => 'write',
  233. 'DECRBY' => 'write',
  234. 'GET' => 'read',
  235. 'GETBIT' => 'read',
  236. 'MGET' => 'read',
  237. 'SET' => 'write',
  238. 'GETRANGE' => 'read',
  239. 'GETSET' => 'write',
  240. 'INCR' => 'write',
  241. 'INCRBY' => 'write',
  242. 'INCRBYFLOAT' => 'write',
  243. 'SETBIT' => 'write',
  244. 'SETEX' => 'write',
  245. 'MSET' => 'write',
  246. 'MSETNX' => 'write',
  247. 'SETNX' => 'write',
  248. 'SETRANGE' => 'write',
  249. 'STRLEN' => 'read',
  250. 'SUBSTR' => 'read',
  251. /* commands operating on lists */
  252. 'LINSERT' => 'write',
  253. 'LINDEX' => 'read',
  254. 'LLEN' => 'read',
  255. 'LPOP' => 'write',
  256. 'RPOP' => 'write',
  257. 'BLPOP' => 'write',
  258. 'BRPOP' => 'write',
  259. 'LPUSH' => 'write',
  260. 'LPUSHX' => 'write',
  261. 'RPUSH' => 'write',
  262. 'RPUSHX' => 'write',
  263. 'LRANGE' => 'read',
  264. 'LREM' => 'write',
  265. 'LSET' => 'write',
  266. 'LTRIM' => 'write',
  267. /* commands operating on sets */
  268. 'SADD' => 'write',
  269. 'SCARD' => 'read',
  270. 'SISMEMBER' => 'read',
  271. 'SMEMBERS' => 'read',
  272. 'SSCAN' => 'read',
  273. 'SRANDMEMBER' => 'read',
  274. 'SPOP' => 'write',
  275. 'SREM' => 'write',
  276. 'SINTER' => 'read',
  277. 'SUNION' => 'read',
  278. 'SDIFF' => 'read',
  279. /* commands operating on sorted sets */
  280. 'ZADD' => 'write',
  281. 'ZCARD' => 'read',
  282. 'ZCOUNT' => 'read',
  283. 'ZINCRBY' => 'write',
  284. 'ZRANGE' => 'read',
  285. 'ZRANGEBYSCORE' => 'read',
  286. 'ZRANK' => 'read',
  287. 'ZREM' => 'write',
  288. 'ZREMRANGEBYRANK' => 'write',
  289. 'ZREMRANGEBYSCORE' => 'write',
  290. 'ZREVRANGE' => 'read',
  291. 'ZREVRANGEBYSCORE' => 'read',
  292. 'ZREVRANK' => 'read',
  293. 'ZSCORE' => 'read',
  294. 'ZSCAN' => 'read',
  295. 'ZLEXCOUNT' => 'read',
  296. 'ZRANGEBYLEX' => 'read',
  297. 'ZREMRANGEBYLEX' => 'write',
  298. /* commands operating on hashes */
  299. 'HDEL' => 'write',
  300. 'HEXISTS' => 'read',
  301. 'HGET' => 'read',
  302. 'HGETALL' => 'read',
  303. 'HMGET' => 'read',
  304. 'HINCRBY' => 'write',
  305. 'HINCRBYFLOAT' => 'write',
  306. 'HKEYS' => 'read',
  307. 'HLEN' => 'read',
  308. 'HSET' => 'write',
  309. 'HSETNX' => 'write',
  310. 'HVALS' => 'read',
  311. 'HSCAN' => 'read',
  312. /* commands operating on HyperLogLog */
  313. 'PFADD' => 'write',
  314. 'PFMERGE' => 'write',
  315. 'PFCOUNT' => 'read',
  316. /* scripting */
  317. 'EVAL' => 'write',
  318. 'EVALSHA' => 'write',
  319. );
  320. if (isset($type)) {
  321. $commands = array_filter($commands, function ($expectedType) use ($type) {
  322. return $expectedType === $type;
  323. });
  324. }
  325. return array_keys($commands);
  326. }
  327. }