ZSetUnionStoreTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 ZSetUnionStoreTest extends PredisCommandTestCase
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function getExpectedCommand()
  21. {
  22. return 'Predis\Command\ZSetUnionStore';
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function getExpectedId()
  28. {
  29. return 'ZUNIONSTORE';
  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 testStoresUnionInNewSortedSet()
  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(4, $redis->zunionstore('letters:out', 2, 'letters:1st', 'letters:2nd'));
  107. $this->assertSame(
  108. array(array('a', '1'), array('b', '3'), array('d', '3'), array('c', '5')),
  109. $redis->zrange('letters:out', 0, -1, 'withscores')
  110. );
  111. $this->assertSame(3, $redis->zunionstore('letters:out', 2, 'letters:1st', 'letters:void'));
  112. $this->assertSame(3, $redis->zunionstore('letters:out', 2, 'letters:void', 'letters:2nd'));
  113. $this->assertSame(0, $redis->zunionstore('letters:out', 2, 'letters:void', 'letters:void'));
  114. }
  115. /**
  116. * @group connected
  117. */
  118. public function testStoresUnionWithAggregateModifier()
  119. {
  120. $redis = $this->getClient();
  121. $redis->zadd('letters:1st', 1, 'a', 2, 'b', 3, 'c');
  122. $redis->zadd('letters:2nd', 1, 'b', 2, 'c', 3, 'd');
  123. $options = array('aggregate' => 'min');
  124. $this->assertSame(4, $redis->zunionstore('letters:min', 2, 'letters:1st', 'letters:2nd', $options));
  125. $this->assertSame(
  126. array(array('a', '1'), array('b', '1'), array('c', '2'), array('d', '3')),
  127. $redis->zrange('letters:min', 0, -1, 'withscores')
  128. );
  129. $options = array('aggregate' => 'max');
  130. $this->assertSame(4, $redis->zunionstore('letters:max', 2, 'letters:1st', 'letters:2nd', $options));
  131. $this->assertSame(
  132. array(array('a', '1'), array('b', '2'), array('c', '3'), array('d', '3')),
  133. $redis->zrange('letters:max', 0, -1, 'withscores')
  134. );
  135. $options = array('aggregate' => 'sum');
  136. $this->assertSame(4, $redis->zunionstore('letters:sum', 2, 'letters:1st', 'letters:2nd', $options));
  137. $this->assertSame(
  138. array(array('a', '1'), array('b', '3'), array('d', '3'), array('c', '5')),
  139. $redis->zrange('letters:sum', 0, -1, 'withscores')
  140. );
  141. }
  142. /**
  143. * @group connected
  144. */
  145. public function testStoresUnionWithWeightsModifier()
  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('weights' => array(2, 3));
  151. $this->assertSame(4, $redis->zunionstore('letters:out', 2, 'letters:1st', 'letters:2nd', $options));
  152. $this->assertSame(
  153. array(array('a', '2'), array('b', '7'), array('d', '9'), array('c', '12')),
  154. $redis->zrange('letters:out', 0, -1, 'withscores')
  155. );
  156. }
  157. /**
  158. * @group connected
  159. */
  160. public function testStoresUnionWithCombinedModifiers()
  161. {
  162. $redis = $this->getClient();
  163. $redis->zadd('letters:1st', 1, 'a', 2, 'b', 3, 'c');
  164. $redis->zadd('letters:2nd', 1, 'b', 2, 'c', 3, 'd');
  165. $options = array('aggregate' => 'max', 'weights' => array(10, 15));
  166. $this->assertSame(4, $redis->zunionstore('letters:out', 2, 'letters:1st', 'letters:2nd', $options));
  167. $this->assertSame(
  168. array(array('a', '10'), array('b', '20'), array('c', '30'), array('d', '45')),
  169. $redis->zrange('letters:out', 0, -1, 'withscores')
  170. );
  171. }
  172. /**
  173. * @group connected
  174. * @expectedException Predis\ServerException
  175. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  176. */
  177. public function testThrowsExceptionOnWrongType()
  178. {
  179. $redis = $this->getClient();
  180. $redis->set('foo', 'bar');
  181. $redis->zunionstore('zset:destination', 1, 'foo');
  182. }
  183. }