StringBitOpTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 commands
  14. * @group realm-string
  15. */
  16. class StringBitOpTest extends CommandTestCase
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected function getExpectedCommand()
  22. {
  23. return 'Predis\Commands\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 connected
  77. */
  78. public function testCanPerformBitwiseAND()
  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('AND', 'key:dst', 'key:src:1', 'key:src:2'));
  84. $this->assertSame("@\x00", $redis->get('key:dst'));
  85. }
  86. /**
  87. * @group connected
  88. */
  89. public function testCanPerformBitwiseOR()
  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('OR', 'key:dst', 'key:src:1', 'key:src:2'));
  95. $this->assertSame("z\x80", $redis->get('key:dst'));
  96. }
  97. /**
  98. * @group connected
  99. */
  100. public function testCanPerformBitwiseXOR()
  101. {
  102. $redis = $this->getClient();
  103. $redis->set('key:src:1', "h\x80");
  104. $redis->set('key:src:2', "R");
  105. $this->assertSame(2, $redis->bitop('XOR', 'key:dst', 'key:src:1', 'key:src:2'));
  106. $this->assertSame(":\x80", $redis->get('key:dst'));
  107. }
  108. /**
  109. * @group connected
  110. */
  111. public function testCanPerformBitwiseNOT()
  112. {
  113. $redis = $this->getClient();
  114. $redis->set('key:src:1', "h\x80");
  115. $this->assertSame(2, $redis->bitop('NOT', 'key:dst', 'key:src:1'));
  116. $this->assertSame("\x97\x7f", $redis->get('key:dst'));
  117. }
  118. /**
  119. * @group connected
  120. * @expectedException Predis\ServerException
  121. * @expectedExceptionMessage ERR BITOP NOT must be called with a single source key.
  122. */
  123. public function testBitwiseNOTAcceptsOnlyOneSourceKey()
  124. {
  125. $this->getClient()->bitop('NOT', 'key:dst', 'key:src:1', 'key:src:2');
  126. }
  127. /**
  128. * @group connected
  129. * @expectedException Predis\ServerException
  130. * @expectedExceptionMessage ERR syntax error
  131. */
  132. public function testThrowsExceptionOnInvalidOperation()
  133. {
  134. $this->getClient()->bitop('NOOP', 'key:dst', 'key:src:1', 'key:src:2');
  135. }
  136. /**
  137. * @group connected
  138. * @expectedException Predis\ServerException
  139. * @expectedExceptionMessage ERR Operation against a key holding the wrong kind of value
  140. */
  141. public function testThrowsExceptionOnInvalidSourceKey()
  142. {
  143. $redis = $this->getClient();
  144. $redis->lpush('key:src:1', 'list');
  145. $redis->bitop('AND', 'key:dst', 'key:src:1', 'key:src:2');
  146. }
  147. /**
  148. * @group connected
  149. */
  150. public function testDoesNotThrowExceptionOnInvalidDestinationKey()
  151. {
  152. $redis = $this->getClient();
  153. $redis->lpush('key:dst', 'list');
  154. $redis->bitop('AND', 'key:dst', 'key:src:1', 'key:src:2');
  155. $this->assertSame('none', $redis->type('key:dst'));
  156. }
  157. }