ZUNIONSTORE_Test.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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\Redis;
  11. /**
  12. * @group commands
  13. * @group realm-zset
  14. */
  15. class ZUNIONSTORE_Test extends PredisCommandTestCase
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function getExpectedCommand()
  21. {
  22. return 'Predis\Command\Redis\ZUNIONSTORE';
  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 connected
  74. * @requiresRedisVersion >= 2.0.0
  75. */
  76. public function testStoresUnionInNewSortedSet()
  77. {
  78. $redis = $this->getClient();
  79. $redis->zadd('letters:1st', 1, 'a', 2, 'b', 3, 'c');
  80. $redis->zadd('letters:2nd', 1, 'b', 2, 'c', 3, 'd');
  81. $this->assertSame(4, $redis->zunionstore('letters:out', 2, 'letters:1st', 'letters:2nd'));
  82. $this->assertSame(
  83. array('a' => '1', 'b' => '3', 'd' => '3', 'c' => '5'),
  84. $redis->zrange('letters:out', 0, -1, 'withscores')
  85. );
  86. $this->assertSame(3, $redis->zunionstore('letters:out', 2, 'letters:1st', 'letters:void'));
  87. $this->assertSame(3, $redis->zunionstore('letters:out', 2, 'letters:void', 'letters:2nd'));
  88. $this->assertSame(0, $redis->zunionstore('letters:out', 2, 'letters:void', 'letters:void'));
  89. }
  90. /**
  91. * @group connected
  92. * @requiresRedisVersion >= 2.0.0
  93. */
  94. public function testStoresUnionWithAggregateModifier()
  95. {
  96. $redis = $this->getClient();
  97. $redis->zadd('letters:1st', 1, 'a', 2, 'b', 3, 'c');
  98. $redis->zadd('letters:2nd', 1, 'b', 2, 'c', 3, 'd');
  99. $options = array('aggregate' => 'min');
  100. $this->assertSame(4, $redis->zunionstore('letters:min', 2, 'letters:1st', 'letters:2nd', $options));
  101. $this->assertSame(
  102. array('a' => '1', 'b' => '1', 'c' => '2', 'd' => '3'),
  103. $redis->zrange('letters:min', 0, -1, 'withscores')
  104. );
  105. $options = array('aggregate' => 'max');
  106. $this->assertSame(4, $redis->zunionstore('letters:max', 2, 'letters:1st', 'letters:2nd', $options));
  107. $this->assertSame(
  108. array('a' => '1', 'b' => '2', 'c' => '3', 'd' => '3'),
  109. $redis->zrange('letters:max', 0, -1, 'withscores')
  110. );
  111. $options = array('aggregate' => 'sum');
  112. $this->assertSame(4, $redis->zunionstore('letters:sum', 2, 'letters:1st', 'letters:2nd', $options));
  113. $this->assertSame(
  114. array('a' => '1', 'b' => '3', 'd' => '3', 'c' => '5'),
  115. $redis->zrange('letters:sum', 0, -1, 'withscores')
  116. );
  117. }
  118. /**
  119. * @group connected
  120. * @requiresRedisVersion >= 2.0.0
  121. */
  122. public function testStoresUnionWithWeightsModifier()
  123. {
  124. $redis = $this->getClient();
  125. $redis->zadd('letters:1st', 1, 'a', 2, 'b', 3, 'c');
  126. $redis->zadd('letters:2nd', 1, 'b', 2, 'c', 3, 'd');
  127. $options = array('weights' => array(2, 3));
  128. $this->assertSame(4, $redis->zunionstore('letters:out', 2, 'letters:1st', 'letters:2nd', $options));
  129. $this->assertSame(
  130. array('a' => '2', 'b' => '7', 'd' => '9', 'c' => '12'),
  131. $redis->zrange('letters:out', 0, -1, 'withscores')
  132. );
  133. }
  134. /**
  135. * @group connected
  136. * @requiresRedisVersion >= 2.0.0
  137. */
  138. public function testStoresUnionWithCombinedModifiers()
  139. {
  140. $redis = $this->getClient();
  141. $redis->zadd('letters:1st', 1, 'a', 2, 'b', 3, 'c');
  142. $redis->zadd('letters:2nd', 1, 'b', 2, 'c', 3, 'd');
  143. $options = array('aggregate' => 'max', 'weights' => array(10, 15));
  144. $this->assertSame(4, $redis->zunionstore('letters:out', 2, 'letters:1st', 'letters:2nd', $options));
  145. $this->assertSame(
  146. array('a' => '10', 'b' => '20', 'c' => '30', 'd' => '45'),
  147. $redis->zrange('letters:out', 0, -1, 'withscores')
  148. );
  149. }
  150. /**
  151. * @group connected
  152. * @requiresRedisVersion >= 2.0.0
  153. * @expectedException \Predis\Response\ServerException
  154. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  155. */
  156. public function testThrowsExceptionOnWrongType()
  157. {
  158. $redis = $this->getClient();
  159. $redis->set('foo', 'bar');
  160. $redis->zunionstore('zset:destination', '1', 'foo');
  161. }
  162. }