ScriptedCommandTest.php 5.2 KB

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