ZSetUnionStoreTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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-zset
  15. */
  16. class ZSetUnionStoreTest extends CommandTestCase
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected function getExpectedCommand()
  22. {
  23. return 'Predis\Commands\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 connected
  92. */
  93. public function testStoresUnionInNewSortedSet()
  94. {
  95. $redis = $this->getClient();
  96. $redis->zadd('letters:1st', 1, 'a', 2, 'b', 3, 'c');
  97. $redis->zadd('letters:2nd', 1, 'b', 2, 'c', 3, 'd');
  98. $this->assertSame(4, $redis->zunionstore('letters:out', 2, 'letters:1st', 'letters:2nd'));
  99. $this->assertSame(
  100. array(array('a', '1'), array('b', '3'), array('d', '3'), array('c', '5')),
  101. $redis->zrange('letters:out', 0, -1, 'withscores')
  102. );
  103. $this->assertSame(3, $redis->zunionstore('letters:out', 2, 'letters:1st', 'letters:void'));
  104. $this->assertSame(3, $redis->zunionstore('letters:out', 2, 'letters:void', 'letters:2nd'));
  105. $this->assertSame(0, $redis->zunionstore('letters:out', 2, 'letters:void', 'letters:void'));
  106. }
  107. /**
  108. * @group connected
  109. */
  110. public function testStoresUnionWithAggregateModifier()
  111. {
  112. $redis = $this->getClient();
  113. $redis->zadd('letters:1st', 1, 'a', 2, 'b', 3, 'c');
  114. $redis->zadd('letters:2nd', 1, 'b', 2, 'c', 3, 'd');
  115. $options = array('aggregate' => 'min');
  116. $this->assertSame(4, $redis->zunionstore('letters:min', 2, 'letters:1st', 'letters:2nd', $options));
  117. $this->assertSame(
  118. array(array('a', '1'), array('b', '1'), array('c', '2'), array('d', '3')),
  119. $redis->zrange('letters:min', 0, -1, 'withscores')
  120. );
  121. $options = array('aggregate' => 'max');
  122. $this->assertSame(4, $redis->zunionstore('letters:max', 2, 'letters:1st', 'letters:2nd', $options));
  123. $this->assertSame(
  124. array(array('a', '1'), array('b', '2'), array('c', '3'), array('d', '3')),
  125. $redis->zrange('letters:max', 0, -1, 'withscores')
  126. );
  127. $options = array('aggregate' => 'sum');
  128. $this->assertSame(4, $redis->zunionstore('letters:sum', 2, 'letters:1st', 'letters:2nd', $options));
  129. $this->assertSame(
  130. array(array('a', '1'), array('b', '3'), array('d', '3'), array('c', '5')),
  131. $redis->zrange('letters:sum', 0, -1, 'withscores')
  132. );
  133. }
  134. /**
  135. * @group connected
  136. */
  137. public function testStoresUnionWithWeightsModifier()
  138. {
  139. $redis = $this->getClient();
  140. $redis->zadd('letters:1st', 1, 'a', 2, 'b', 3, 'c');
  141. $redis->zadd('letters:2nd', 1, 'b', 2, 'c', 3, 'd');
  142. $options = array('weights' => array(2, 3));
  143. $this->assertSame(4, $redis->zunionstore('letters:out', 2, 'letters:1st', 'letters:2nd', $options));
  144. $this->assertSame(
  145. array(array('a', '2'), array('b', '7'), array('d', '9'), array('c', '12')),
  146. $redis->zrange('letters:out', 0, -1, 'withscores')
  147. );
  148. }
  149. /**
  150. * @group connected
  151. */
  152. public function testStoresUnionWithCombinedModifiers()
  153. {
  154. $redis = $this->getClient();
  155. $redis->zadd('letters:1st', 1, 'a', 2, 'b', 3, 'c');
  156. $redis->zadd('letters:2nd', 1, 'b', 2, 'c', 3, 'd');
  157. $options = array('aggregate' => 'max', 'weights' => array(10, 15));
  158. $this->assertSame(4, $redis->zunionstore('letters:out', 2, 'letters:1st', 'letters:2nd', $options));
  159. $this->assertSame(
  160. array(array('a', '10'), array('b', '20'), array('c', '30'), array('d', '45')),
  161. $redis->zrange('letters:out', 0, -1, 'withscores')
  162. );
  163. }
  164. /**
  165. * @group connected
  166. * @expectedException Predis\ServerException
  167. * @expectedExceptionMessage ERR Operation against a key holding the wrong kind of value
  168. */
  169. public function testThrowsExceptionOnWrongType()
  170. {
  171. $redis = $this->getClient();
  172. $redis->set('foo', 'bar');
  173. $redis->zunionstore('zset:destination', 1, 'foo');
  174. }
  175. }