ZSetUnionStoreTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 connected
  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(array('a', '1'), array('b', '3'), array('d', '3'), array('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. */
  93. public function testStoresUnionWithAggregateModifier()
  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. $options = array('aggregate' => 'min');
  99. $this->assertSame(4, $redis->zunionstore('letters:min', 2, 'letters:1st', 'letters:2nd', $options));
  100. $this->assertSame(
  101. array(array('a', '1'), array('b', '1'), array('c', '2'), array('d', '3')),
  102. $redis->zrange('letters:min', 0, -1, 'withscores')
  103. );
  104. $options = array('aggregate' => 'max');
  105. $this->assertSame(4, $redis->zunionstore('letters:max', 2, 'letters:1st', 'letters:2nd', $options));
  106. $this->assertSame(
  107. array(array('a', '1'), array('b', '2'), array('c', '3'), array('d', '3')),
  108. $redis->zrange('letters:max', 0, -1, 'withscores')
  109. );
  110. $options = array('aggregate' => 'sum');
  111. $this->assertSame(4, $redis->zunionstore('letters:sum', 2, 'letters:1st', 'letters:2nd', $options));
  112. $this->assertSame(
  113. array(array('a', '1'), array('b', '3'), array('d', '3'), array('c', '5')),
  114. $redis->zrange('letters:sum', 0, -1, 'withscores')
  115. );
  116. }
  117. /**
  118. * @group connected
  119. */
  120. public function testStoresUnionWithWeightsModifier()
  121. {
  122. $redis = $this->getClient();
  123. $redis->zadd('letters:1st', 1, 'a', 2, 'b', 3, 'c');
  124. $redis->zadd('letters:2nd', 1, 'b', 2, 'c', 3, 'd');
  125. $options = array('weights' => array(2, 3));
  126. $this->assertSame(4, $redis->zunionstore('letters:out', 2, 'letters:1st', 'letters:2nd', $options));
  127. $this->assertSame(
  128. array(array('a', '2'), array('b', '7'), array('d', '9'), array('c', '12')),
  129. $redis->zrange('letters:out', 0, -1, 'withscores')
  130. );
  131. }
  132. /**
  133. * @group connected
  134. */
  135. public function testStoresUnionWithCombinedModifiers()
  136. {
  137. $redis = $this->getClient();
  138. $redis->zadd('letters:1st', 1, 'a', 2, 'b', 3, 'c');
  139. $redis->zadd('letters:2nd', 1, 'b', 2, 'c', 3, 'd');
  140. $options = array('aggregate' => 'max', 'weights' => array(10, 15));
  141. $this->assertSame(4, $redis->zunionstore('letters:out', 2, 'letters:1st', 'letters:2nd', $options));
  142. $this->assertSame(
  143. array(array('a', '10'), array('b', '20'), array('c', '30'), array('d', '45')),
  144. $redis->zrange('letters:out', 0, -1, 'withscores')
  145. );
  146. }
  147. /**
  148. * @group connected
  149. * @expectedException Predis\Response\ServerException
  150. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  151. */
  152. public function testThrowsExceptionOnWrongType()
  153. {
  154. $redis = $this->getClient();
  155. $redis->set('foo', 'bar');
  156. $redis->zunionstore('zset:destination', 1, 'foo');
  157. }
  158. }