ScriptedCommandTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 \PHPUnit_Framework_TestCase as StandardTestCase;
  12. /**
  13. * @group realm-scripting
  14. */
  15. class ScriptedCommandTest extends StandardTestCase
  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\ScriptedCommand', 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\ScriptedCommand', 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 testGetKeys()
  55. {
  56. $arguments = array('key1', 'key2', 'value1', 'value2');
  57. $command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript', 'getKeysCount'));
  58. $command->expects($this->once())
  59. ->method('getScript')
  60. ->will($this->returnValue(self::LUA_SCRIPT));
  61. $command->expects($this->exactly(2))
  62. ->method('getKeysCount')
  63. ->will($this->returnValue(2));
  64. $command->setArguments($arguments);
  65. $this->assertSame(array('key1', 'key2'), $command->getKeys());
  66. }
  67. /**
  68. * @group disconnected
  69. */
  70. public function testGetKeysWithNegativeKeysCount()
  71. {
  72. $arguments = array('key1', 'key2', 'value1', 'value2');
  73. $command = $this->getMock('Predis\Command\ScriptedCommand', 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 testPrefixKeys()
  87. {
  88. $arguments = array('foo', 'hoge', 'bar', 'piyo');
  89. $expected = array('prefix:foo', 'prefix:hoge');
  90. $command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript', 'getKeysCount'));
  91. $command->expects($this->once())
  92. ->method('getScript')
  93. ->will($this->returnValue(self::LUA_SCRIPT));
  94. $command->expects($this->exactly(2))
  95. ->method('getKeysCount')
  96. ->will($this->returnValue(2));
  97. $command->setArguments($arguments);
  98. $command->prefixKeys('prefix:');
  99. $this->assertSame($expected, $command->getKeys());
  100. }
  101. /**
  102. * @group disconnected
  103. */
  104. public function testPrefixKeysWithNegativeKeysCount()
  105. {
  106. $arguments = array('foo', 'hoge', 'bar', 'piyo');
  107. $expected = array('prefix:foo', 'prefix:hoge');
  108. $command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript', 'getKeysCount'));
  109. $command->expects($this->once())
  110. ->method('getScript')
  111. ->will($this->returnValue(self::LUA_SCRIPT));
  112. $command->expects($this->exactly(2))
  113. ->method('getKeysCount')
  114. ->will($this->returnValue(-2));
  115. $command->setArguments($arguments);
  116. $command->prefixKeys('prefix:');
  117. $this->assertSame($expected, $command->getKeys());
  118. }
  119. /**
  120. * @group disconnected
  121. */
  122. public function testGetScriptHash()
  123. {
  124. $arguments = array('key1', 'key2', 'value1', 'value2');
  125. $command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript', 'getKeysCount'));
  126. $command->expects($this->once())
  127. ->method('getScript')
  128. ->will($this->returnValue(self::LUA_SCRIPT));
  129. $command->expects($this->once())
  130. ->method('getKeysCount')
  131. ->will($this->returnValue(2));
  132. $command->setArguments($arguments);
  133. $this->assertSame(self::LUA_SCRIPT_SHA1, $command->getScriptHash());
  134. }
  135. }