ScriptCommandTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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\Command;
  11. use PredisTestCase;
  12. /**
  13. * @group realm-scripting
  14. */
  15. class ScriptCommandTest extends PredisTestCase
  16. {
  17. const LUA_SCRIPT = 'return { KEYS[1], KEYS[2], ARGV[1], ARGV[2] }';
  18. const LUA_SCRIPT_SHA1 = '6e07f61f502e36d123fe28523076af588f5c315e';
  19. /**
  20. * @group disconnected
  21. */
  22. public function testGetId()
  23. {
  24. $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript'));
  25. $this->assertSame('EVALSHA', $command->getId());
  26. }
  27. /**
  28. * @group disconnected
  29. */
  30. public function testGetScriptHash()
  31. {
  32. $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount'));
  33. $command
  34. ->expects($this->exactly(2))
  35. ->method('getScript')
  36. ->will($this->returnValue(self::LUA_SCRIPT));
  37. $command
  38. ->expects($this->once())
  39. ->method('getKeysCount')
  40. ->will($this->returnValue(2));
  41. $command->setArguments($arguments = array('key1', 'key2', 'value1', 'value2'));
  42. $this->assertSame(self::LUA_SCRIPT_SHA1, $command->getScriptHash());
  43. }
  44. /**
  45. * @group disconnected
  46. */
  47. public function testGetKeys()
  48. {
  49. $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount'));
  50. $command
  51. ->expects($this->once())
  52. ->method('getScript')
  53. ->will($this->returnValue(self::LUA_SCRIPT));
  54. $command
  55. ->expects($this->exactly(2))
  56. ->method('getKeysCount')
  57. ->will($this->returnValue(2));
  58. $command->setArguments($arguments = array('key1', 'key2', 'value1', 'value2'));
  59. $this->assertSame(array('key1', 'key2'), $command->getKeys());
  60. }
  61. /**
  62. * @group disconnected
  63. */
  64. public function testGetKeysWithZeroKeysCount()
  65. {
  66. $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript'));
  67. $command
  68. ->expects($this->once())
  69. ->method('getScript')
  70. ->will($this->returnValue(self::LUA_SCRIPT));
  71. $command->setArguments($arguments = array('value1', 'value2', 'value3'));
  72. $this->assertSame(array(), $command->getKeys());
  73. }
  74. /**
  75. * @group disconnected
  76. */
  77. public function testGetKeysWithNegativeKeysCount()
  78. {
  79. $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount'));
  80. $command
  81. ->expects($this->once())
  82. ->method('getScript')
  83. ->will($this->returnValue(self::LUA_SCRIPT));
  84. $command
  85. ->expects($this->exactly(2))
  86. ->method('getKeysCount')
  87. ->will($this->returnValue(-2));
  88. $command->setArguments($arguments = array('key1', 'key2', 'value1', 'value2'));
  89. $this->assertSame(array('key1', 'key2'), $command->getKeys());
  90. }
  91. /**
  92. * @group disconnected
  93. */
  94. public function testGetArguments()
  95. {
  96. $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount'));
  97. $command
  98. ->expects($this->once())
  99. ->method('getScript')
  100. ->will($this->returnValue(self::LUA_SCRIPT));
  101. $command
  102. ->expects($this->once())
  103. ->method('getKeysCount')
  104. ->will($this->returnValue(2));
  105. $command->setArguments($arguments = array('key1', 'key2', 'value1', 'value2'));
  106. $this->assertSame(array_merge(array(self::LUA_SCRIPT_SHA1, 2), $arguments), $command->getArguments());
  107. }
  108. /**
  109. * @group disconnected
  110. */
  111. public function testGetArgumentsWithZeroKeysCount()
  112. {
  113. $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount'));
  114. $command
  115. ->expects($this->once())
  116. ->method('getScript')
  117. ->will($this->returnValue(self::LUA_SCRIPT));
  118. $command->setArguments($arguments = array('key1', 'key2', 'value1', 'value2'));
  119. $this->assertSame(array_merge(array(self::LUA_SCRIPT_SHA1, 0), $arguments), $command->getArguments());
  120. }
  121. /**
  122. * @group disconnected
  123. */
  124. public function testGetArgumentsWithNegativeKeysCount()
  125. {
  126. $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount'));
  127. $command
  128. ->expects($this->once())
  129. ->method('getScript')
  130. ->will($this->returnValue(self::LUA_SCRIPT));
  131. $command
  132. ->expects($this->once())
  133. ->method('getKeysCount')
  134. ->will($this->returnValue(-2));
  135. $command->setArguments($arguments = array('key1', 'key2', 'value1', 'value2'));
  136. $this->assertSame(array_merge(array(self::LUA_SCRIPT_SHA1, 2), $arguments), $command->getArguments());
  137. }
  138. /**
  139. * @group disconnected
  140. */
  141. public function testGetEvalArguments()
  142. {
  143. $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount'));
  144. $command
  145. ->expects($this->exactly(2))
  146. ->method('getScript')
  147. ->will($this->returnValue(self::LUA_SCRIPT));
  148. $command
  149. ->expects($this->once())
  150. ->method('getKeysCount')
  151. ->will($this->returnValue(2));
  152. $command->setArguments($arguments = array('key1', 'key2', 'value1', 'value2'));
  153. $this->assertSame(array_merge(array(self::LUA_SCRIPT, 2), $arguments), $command->getEvalArguments());
  154. }
  155. /**
  156. * @group disconnected
  157. */
  158. public function testGetEvalCommand()
  159. {
  160. $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount'));
  161. $command
  162. ->expects($this->exactly(2))
  163. ->method('getScript')
  164. ->will($this->returnValue(self::LUA_SCRIPT));
  165. $command
  166. ->expects($this->once())
  167. ->method('getKeysCount')
  168. ->will($this->returnValue(2));
  169. $command->setArguments($arguments = array('key1', 'key2', 'value1', 'value2'));
  170. $evalCMD = new RawCommand('EVAL', array_merge(array(self::LUA_SCRIPT, 2), $arguments));
  171. $this->assertRedisCommand($evalCMD, $command->getEvalCommand());
  172. }
  173. }