StringBitOpTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 disconnected
  66. */
  67. public function testPrefixKeys()
  68. {
  69. $arguments = array('AND', 'key:dst', 'key:01', 'key:02');
  70. $expected = array('AND', 'prefix:key:dst', 'prefix:key:01', 'prefix:key:02');
  71. $command = $this->getCommandWithArgumentsArray($arguments);
  72. $command->prefixKeys('prefix:');
  73. $this->assertSame($expected, $command->getArguments());
  74. }
  75. /**
  76. * @group disconnected
  77. */
  78. public function testPrefixKeysIgnoredOnEmptyArguments()
  79. {
  80. $command = $this->getCommand();
  81. $command->prefixKeys('prefix:');
  82. $this->assertSame(array(), $command->getArguments());
  83. }
  84. /**
  85. * @group connected
  86. */
  87. public function testCanPerformBitwiseAND()
  88. {
  89. $redis = $this->getClient();
  90. $redis->set('key:src:1', "h\x80");
  91. $redis->set('key:src:2', "R");
  92. $this->assertSame(2, $redis->bitop('AND', 'key:dst', 'key:src:1', 'key:src:2'));
  93. $this->assertSame("@\x00", $redis->get('key:dst'));
  94. }
  95. /**
  96. * @group connected
  97. */
  98. public function testCanPerformBitwiseOR()
  99. {
  100. $redis = $this->getClient();
  101. $redis->set('key:src:1', "h\x80");
  102. $redis->set('key:src:2', "R");
  103. $this->assertSame(2, $redis->bitop('OR', 'key:dst', 'key:src:1', 'key:src:2'));
  104. $this->assertSame("z\x80", $redis->get('key:dst'));
  105. }
  106. /**
  107. * @group connected
  108. */
  109. public function testCanPerformBitwiseXOR()
  110. {
  111. $redis = $this->getClient();
  112. $redis->set('key:src:1', "h\x80");
  113. $redis->set('key:src:2', "R");
  114. $this->assertSame(2, $redis->bitop('XOR', 'key:dst', 'key:src:1', 'key:src:2'));
  115. $this->assertSame(":\x80", $redis->get('key:dst'));
  116. }
  117. /**
  118. * @group connected
  119. */
  120. public function testCanPerformBitwiseNOT()
  121. {
  122. $redis = $this->getClient();
  123. $redis->set('key:src:1', "h\x80");
  124. $this->assertSame(2, $redis->bitop('NOT', 'key:dst', 'key:src:1'));
  125. $this->assertSame("\x97\x7f", $redis->get('key:dst'));
  126. }
  127. /**
  128. * @group connected
  129. * @expectedException Predis\Response\ServerException
  130. * @expectedExceptionMessage ERR BITOP NOT must be called with a single source key.
  131. */
  132. public function testBitwiseNOTAcceptsOnlyOneSourceKey()
  133. {
  134. $this->getClient()->bitop('NOT', 'key:dst', 'key:src:1', 'key:src:2');
  135. }
  136. /**
  137. * @group connected
  138. * @expectedException Predis\Response\ServerException
  139. * @expectedExceptionMessage ERR syntax error
  140. */
  141. public function testThrowsExceptionOnInvalidOperation()
  142. {
  143. $this->getClient()->bitop('NOOP', 'key:dst', 'key:src:1', 'key:src:2');
  144. }
  145. /**
  146. * @group connected
  147. * @expectedException Predis\Response\ServerException
  148. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  149. */
  150. public function testThrowsExceptionOnInvalidSourceKey()
  151. {
  152. $redis = $this->getClient();
  153. $redis->lpush('key:src:1', 'list');
  154. $redis->bitop('AND', 'key:dst', 'key:src:1', 'key:src:2');
  155. }
  156. /**
  157. * @group connected
  158. */
  159. public function testDoesNotThrowExceptionOnInvalidDestinationKey()
  160. {
  161. $redis = $this->getClient();
  162. $redis->lpush('key:dst', 'list');
  163. $redis->bitop('AND', 'key:dst', 'key:src:1', 'key:src:2');
  164. $this->assertSame('none', $redis->type('key:dst'));
  165. }
  166. }