ZSetIntersectionStoreTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. /**
  12. * @group commands
  13. * @group realm-zset
  14. */
  15. class ZSetIntersectionStoreTest extends PredisCommandTestCase
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function getExpectedCommand()
  21. {
  22. return 'Predis\Command\ZSetIntersectionStore';
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function getExpectedId()
  28. {
  29. return 'ZINTERSTORE';
  30. }
  31. /**
  32. * @group disconnected
  33. */
  34. public function testFilterArguments()
  35. {
  36. $modifiers = array(
  37. 'aggregate' => 'sum',
  38. 'weights' => array(10, 100),
  39. );
  40. $arguments = array('zset:destination', 2, 'zset1', 'zset2', $modifiers);
  41. $expected = array(
  42. 'zset:destination', 2, 'zset1', 'zset2', 'WEIGHTS', 10, 100, 'AGGREGATE', 'sum'
  43. );
  44. $command = $this->getCommand();
  45. $command->setArguments($arguments);
  46. $this->assertSame($expected, $command->getArguments());
  47. }
  48. /**
  49. * @group disconnected
  50. */
  51. public function testFilterArgumentsSourceKeysAsSingleArray()
  52. {
  53. $modifiers = array(
  54. 'aggregate' => 'sum',
  55. 'weights' => array(10, 100),
  56. );
  57. $arguments = array('zset:destination', array('zset1', 'zset2'), $modifiers);
  58. $expected = array(
  59. 'zset:destination', 2, 'zset1', 'zset2', 'WEIGHTS', 10, 100, 'AGGREGATE', 'sum'
  60. );
  61. $command = $this->getCommand();
  62. $command->setArguments($arguments);
  63. $this->assertSame($expected, $command->getArguments());
  64. }
  65. /**
  66. * @group disconnected
  67. */
  68. public function testParseResponse()
  69. {
  70. $this->assertSame(1, $this->getCommand()->parseResponse(1));
  71. }
  72. /**
  73. * @group disconnected
  74. */
  75. public function testPrefixKeys()
  76. {
  77. $modifiers = array(
  78. 'aggregate' => 'sum',
  79. 'weights' => array(10, 100),
  80. );
  81. $arguments = array('zset:destination', 2, 'zset1', 'zset2', $modifiers);
  82. $expected = array(
  83. 'prefix:zset:destination', 2, 'prefix:zset1', 'prefix:zset2', 'WEIGHTS', 10, 100, 'AGGREGATE', 'sum'
  84. );
  85. $command = $this->getCommandWithArgumentsArray($arguments);
  86. $command->prefixKeys('prefix:');
  87. $this->assertSame($expected, $command->getArguments());
  88. }
  89. /**
  90. * @group disconnected
  91. */
  92. public function testPrefixKeysIgnoredOnEmptyArguments()
  93. {
  94. $command = $this->getCommand();
  95. $command->prefixKeys('prefix:');
  96. $this->assertSame(array(), $command->getArguments());
  97. }
  98. /**
  99. * @group connected
  100. */
  101. public function testStoresIntersectionInNewSortedSet()
  102. {
  103. $redis = $this->getClient();
  104. $redis->zadd('letters:1st', 1, 'a', 2, 'b', 3, 'c');
  105. $redis->zadd('letters:2nd', 1, 'b', 2, 'c', 3, 'd');
  106. $this->assertSame(2, $redis->zinterstore('letters:out', 2, 'letters:1st', 'letters:2nd'));
  107. $this->assertSame(array(array('b', '3'), array('c', '5')), $redis->zrange('letters:out', 0, -1, 'withscores'));
  108. $this->assertSame(0, $redis->zinterstore('letters:out', 2, 'letters:1st', 'letters:void'));
  109. $this->assertSame(0, $redis->zinterstore('letters:out', 2, 'letters:void', 'letters:2nd'));
  110. $this->assertSame(0, $redis->zinterstore('letters:out', 2, 'letters:void', 'letters:void'));
  111. }
  112. /**
  113. * @group connected
  114. */
  115. public function testStoresIntersectionWithAggregateModifier()
  116. {
  117. $redis = $this->getClient();
  118. $redis->zadd('letters:1st', 1, 'a', 2, 'b', 3, 'c');
  119. $redis->zadd('letters:2nd', 1, 'b', 2, 'c', 3, 'd');
  120. $options = array('aggregate' => 'min');
  121. $this->assertSame(2, $redis->zinterstore('letters:min', 2, 'letters:1st', 'letters:2nd', $options));
  122. $this->assertSame(array(array('b', '1'), array('c', '2')), $redis->zrange('letters:min', 0, -1, 'withscores'));
  123. $options = array('aggregate' => 'max');
  124. $this->assertSame(2, $redis->zinterstore('letters:max', 2, 'letters:1st', 'letters:2nd', $options));
  125. $this->assertSame(array(array('b', '2'), array('c', '3')), $redis->zrange('letters:max', 0, -1, 'withscores'));
  126. $options = array('aggregate' => 'sum');
  127. $this->assertSame(2, $redis->zinterstore('letters:sum', 2, 'letters:1st', 'letters:2nd', $options));
  128. $this->assertSame(array(array('b', '3'), array('c', '5')), $redis->zrange('letters:sum', 0, -1, 'withscores'));
  129. }
  130. /**
  131. * @group connected
  132. */
  133. public function testStoresIntersectionWithWeightsModifier()
  134. {
  135. $redis = $this->getClient();
  136. $redis->zadd('letters:1st', 1, 'a', 2, 'b', 3, 'c');
  137. $redis->zadd('letters:2nd', 1, 'b', 2, 'c', 3, 'd');
  138. $options = array('weights' => array(2, 3));
  139. $this->assertSame(2, $redis->zinterstore('letters:out', 2, 'letters:1st', 'letters:2nd', $options));
  140. $this->assertSame(array(array('b', '7'), array('c', '12')), $redis->zrange('letters:out', 0, -1, 'withscores'));
  141. }
  142. /**
  143. * @group connected
  144. */
  145. public function testStoresIntersectionWithCombinedModifiers()
  146. {
  147. $redis = $this->getClient();
  148. $redis->zadd('letters:1st', 1, 'a', 2, 'b', 3, 'c');
  149. $redis->zadd('letters:2nd', 1, 'b', 2, 'c', 3, 'd');
  150. $options = array('aggregate' => 'max', 'weights' => array(10, 15));
  151. $this->assertSame(2, $redis->zinterstore('letters:out', 2, 'letters:1st', 'letters:2nd', $options));
  152. $this->assertSame(array(array('b', '20'), array('c', '30')), $redis->zrange('letters:out', 0, -1, 'withscores'));
  153. }
  154. /**
  155. * @group connected
  156. * @expectedException Predis\ServerException
  157. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  158. */
  159. public function testThrowsExceptionOnWrongType()
  160. {
  161. $redis = $this->getClient();
  162. $redis->set('foo', 'bar');
  163. $redis->zinterstore('zset:destination', 1, 'foo');
  164. }
  165. }