ZSetUnionStoreTest.php 6.6 KB

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