ScriptedCommandTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 testGetKeys()
  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->exactly(2))
  45. ->method('getKeysCount')
  46. ->will($this->returnValue(2));
  47. $command->setArguments($arguments);
  48. $this->assertSame(array('key1', 'key2'), $command->getKeys());
  49. }
  50. /**
  51. * @group disconnected
  52. */
  53. public function testPrefixKeys()
  54. {
  55. $arguments = array('foo', 'hoge', 'bar', 'piyo');
  56. $expected = array('prefix:foo', 'prefix:hoge');
  57. $command = $this->getMock('Predis\Commands\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. $command->prefixKeys('prefix:');
  66. $this->assertSame($expected, $command->getKeys());
  67. }
  68. /**
  69. * @group disconnected
  70. */
  71. public function testGetScriptHash()
  72. {
  73. $arguments = array('key1', 'key2', 'value1', 'value2');
  74. $command = $this->getMock('Predis\Commands\ScriptedCommand', array('getScript', 'getKeysCount'));
  75. $command->expects($this->once())
  76. ->method('getScript')
  77. ->will($this->returnValue(self::LUA_SCRIPT));
  78. $command->expects($this->once())
  79. ->method('getKeysCount')
  80. ->will($this->returnValue(2));
  81. $command->setArguments($arguments);
  82. $this->assertSame(sha1(self::LUA_SCRIPT), $command->getScriptHash());
  83. }
  84. }