StringBitOpTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 commands
  14. * @group realm-string
  15. */
  16. class StringBitOpTest extends CommandTestCase
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected function getExpectedCommand()
  22. {
  23. return 'Predis\Command\StringBitOp';
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. protected function getExpectedId()
  29. {
  30. return 'BITOP';
  31. }
  32. /**
  33. * @group disconnected
  34. */
  35. public function testFilterArguments()
  36. {
  37. $arguments = array('AND', 'key:dst', 'key:01', 'key:02');
  38. $expected = array('AND', 'key:dst', 'key:01', 'key:02');
  39. $command = $this->getCommand();
  40. $command->setArguments($arguments);
  41. $this->assertSame($expected, $command->getArguments());
  42. }
  43. /**
  44. * @group disconnected
  45. */
  46. public function testFilterArgumentsKeysAsSingleArray()
  47. {
  48. $arguments = array('AND', 'key:dst', array('key:01', 'key:02'));
  49. $expected = array('AND', 'key:dst', 'key:01', 'key:02');
  50. $command = $this->getCommand();
  51. $command->setArguments($arguments);
  52. $this->assertSame($expected, $command->getArguments());
  53. }
  54. /**
  55. * @group disconnected
  56. */
  57. public function testParseResponse()
  58. {
  59. $raw = 10;
  60. $expected = 10;
  61. $command = $this->getCommand();
  62. $this->assertSame($expected, $command->parseResponse($raw));
  63. }
  64. /**
  65. * @group connected
  66. */
  67. public function testCanPerformBitwiseAND()
  68. {
  69. $redis = $this->getClient();
  70. $redis->set('key:src:1', "h\x80");
  71. $redis->set('key:src:2', "R");
  72. $this->assertSame(2, $redis->bitop('AND', 'key:dst', 'key:src:1', 'key:src:2'));
  73. $this->assertSame("@\x00", $redis->get('key:dst'));
  74. }
  75. /**
  76. * @group connected
  77. */
  78. public function testCanPerformBitwiseOR()
  79. {
  80. $redis = $this->getClient();
  81. $redis->set('key:src:1', "h\x80");
  82. $redis->set('key:src:2', "R");
  83. $this->assertSame(2, $redis->bitop('OR', 'key:dst', 'key:src:1', 'key:src:2'));
  84. $this->assertSame("z\x80", $redis->get('key:dst'));
  85. }
  86. /**
  87. * @group connected
  88. */
  89. public function testCanPerformBitwiseXOR()
  90. {
  91. $redis = $this->getClient();
  92. $redis->set('key:src:1', "h\x80");
  93. $redis->set('key:src:2', "R");
  94. $this->assertSame(2, $redis->bitop('XOR', 'key:dst', 'key:src:1', 'key:src:2'));
  95. $this->assertSame(":\x80", $redis->get('key:dst'));
  96. }
  97. /**
  98. * @group connected
  99. */
  100. public function testCanPerformBitwiseNOT()
  101. {
  102. $redis = $this->getClient();
  103. $redis->set('key:src:1', "h\x80");
  104. $this->assertSame(2, $redis->bitop('NOT', 'key:dst', 'key:src:1'));
  105. $this->assertSame("\x97\x7f", $redis->get('key:dst'));
  106. }
  107. /**
  108. * @group connected
  109. * @expectedException Predis\Response\ServerException
  110. * @expectedExceptionMessage ERR BITOP NOT must be called with a single source key.
  111. */
  112. public function testBitwiseNOTAcceptsOnlyOneSourceKey()
  113. {
  114. $this->getClient()->bitop('NOT', 'key:dst', 'key:src:1', 'key:src:2');
  115. }
  116. /**
  117. * @group connected
  118. * @expectedException Predis\Response\ServerException
  119. * @expectedExceptionMessage ERR syntax error
  120. */
  121. public function testThrowsExceptionOnInvalidOperation()
  122. {
  123. $this->getClient()->bitop('NOOP', 'key:dst', 'key:src:1', 'key:src:2');
  124. }
  125. /**
  126. * @group connected
  127. * @expectedException Predis\Response\ServerException
  128. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  129. */
  130. public function testThrowsExceptionOnInvalidSourceKey()
  131. {
  132. $redis = $this->getClient();
  133. $redis->lpush('key:src:1', 'list');
  134. $redis->bitop('AND', 'key:dst', 'key:src:1', 'key:src:2');
  135. }
  136. /**
  137. * @group connected
  138. */
  139. public function testDoesNotThrowExceptionOnInvalidDestinationKey()
  140. {
  141. $redis = $this->getClient();
  142. $redis->lpush('key:dst', 'list');
  143. $redis->bitop('AND', 'key:dst', 'key:src:1', 'key:src:2');
  144. $this->assertSame('none', $redis->type('key:dst'));
  145. }
  146. }