ScriptCommandTest.php 6.5 KB

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