ScriptCommandTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 testGetArguments()
  23. {
  24. $arguments = array('key1', 'key2', 'value1', 'value2');
  25. $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount'));
  26. $command->expects($this->once())
  27. ->method('getScript')
  28. ->will($this->returnValue(self::LUA_SCRIPT));
  29. $command->expects($this->once())
  30. ->method('getKeysCount')
  31. ->will($this->returnValue(2));
  32. $command->setArguments($arguments);
  33. $this->assertSame(array_merge(array(self::LUA_SCRIPT_SHA1, 2), $arguments), $command->getArguments());
  34. }
  35. /**
  36. * @group disconnected
  37. */
  38. public function testGetArgumentsWithNegativeKeysCount()
  39. {
  40. $arguments = array('key1', 'key2', 'value1', 'value2');
  41. $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount'));
  42. $command->expects($this->once())
  43. ->method('getScript')
  44. ->will($this->returnValue(self::LUA_SCRIPT));
  45. $command->expects($this->once())
  46. ->method('getKeysCount')
  47. ->will($this->returnValue(-2));
  48. $command->setArguments($arguments);
  49. $this->assertSame(array_merge(array(self::LUA_SCRIPT_SHA1, 2), $arguments), $command->getArguments());
  50. }
  51. /**
  52. * @group disconnected
  53. */
  54. public function testGetArgumentsWithZeroKeysCount()
  55. {
  56. $arguments = array('value1', 'value2', 'value3');
  57. $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount'));
  58. $command->expects($this->once())
  59. ->method('getScript')
  60. ->will($this->returnValue(self::LUA_SCRIPT));
  61. $command->expects($this->once())
  62. ->method('getKeysCount')
  63. ->will($this->returnValue(0));
  64. $command->setArguments($arguments);
  65. $this->assertSame(array_merge(array(self::LUA_SCRIPT_SHA1, 0), $arguments), $command->getArguments());
  66. }
  67. /**
  68. * @group disconnected
  69. */
  70. public function testGetKeys()
  71. {
  72. $arguments = array('key1', 'key2', 'value1', 'value2');
  73. $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount'));
  74. $command->expects($this->once())
  75. ->method('getScript')
  76. ->will($this->returnValue(self::LUA_SCRIPT));
  77. $command->expects($this->exactly(2))
  78. ->method('getKeysCount')
  79. ->will($this->returnValue(2));
  80. $command->setArguments($arguments);
  81. $this->assertSame(array('key1', 'key2'), $command->getKeys());
  82. }
  83. /**
  84. * @group disconnected
  85. */
  86. public function testGetKeysWithZeroKeysCount()
  87. {
  88. $arguments = array('value1', 'value2', 'value3');
  89. $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount'));
  90. $command->expects($this->once())
  91. ->method('getScript')
  92. ->will($this->returnValue(self::LUA_SCRIPT));
  93. $command->expects($this->exactly(2))
  94. ->method('getKeysCount')
  95. ->will($this->returnValue(0));
  96. $command->setArguments($arguments);
  97. $this->assertSame(array(), $command->getKeys());
  98. }
  99. /**
  100. * @group disconnected
  101. */
  102. public function testGetKeysWithNegativeKeysCount()
  103. {
  104. $arguments = array('key1', 'key2', 'value1', 'value2');
  105. $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount'));
  106. $command->expects($this->once())
  107. ->method('getScript')
  108. ->will($this->returnValue(self::LUA_SCRIPT));
  109. $command->expects($this->exactly(2))
  110. ->method('getKeysCount')
  111. ->will($this->returnValue(-2));
  112. $command->setArguments($arguments);
  113. $this->assertSame(array('key1', 'key2'), $command->getKeys());
  114. }
  115. /**
  116. * @group disconnected
  117. */
  118. public function testGetScriptHash()
  119. {
  120. $arguments = array('key1', 'key2', 'value1', 'value2');
  121. $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount'));
  122. $command->expects($this->once())
  123. ->method('getScript')
  124. ->will($this->returnValue(self::LUA_SCRIPT));
  125. $command->expects($this->once())
  126. ->method('getKeysCount')
  127. ->will($this->returnValue(2));
  128. $command->setArguments($arguments);
  129. $this->assertSame(self::LUA_SCRIPT_SHA1, $command->getScriptHash());
  130. }
  131. }