ZSetIntersectionStoreTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 ZSetIntersectionStoreTest extends CommandTestCase
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected function getExpectedCommand()
  22. {
  23. return 'Predis\Commands\ZSetIntersectionStore';
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. protected function getExpectedId()
  29. {
  30. return 'ZINTERSTORE';
  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 testStoresIntersectionInNewSortedSet()
  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(2, $redis->zinterstore('letters:out', 2, 'letters:1st', 'letters:2nd'));
  99. $this->assertSame(array(array('b', '3'), array('c', '5')), $redis->zrange('letters:out', 0, -1, 'withscores'));
  100. $this->assertSame(0, $redis->zinterstore('letters:out', 2, 'letters:1st', 'letters:void'));
  101. $this->assertSame(0, $redis->zinterstore('letters:out', 2, 'letters:void', 'letters:2nd'));
  102. $this->assertSame(0, $redis->zinterstore('letters:out', 2, 'letters:void', 'letters:void'));
  103. }
  104. /**
  105. * @group connected
  106. */
  107. public function testStoresIntersectionWithAggregateModifier()
  108. {
  109. $redis = $this->getClient();
  110. $redis->zadd('letters:1st', 1, 'a', 2, 'b', 3, 'c');
  111. $redis->zadd('letters:2nd', 1, 'b', 2, 'c', 3, 'd');
  112. $options = array('aggregate' => 'min');
  113. $this->assertSame(2, $redis->zinterstore('letters:min', 2, 'letters:1st', 'letters:2nd', $options));
  114. $this->assertSame(array(array('b', '1'), array('c', '2')), $redis->zrange('letters:min', 0, -1, 'withscores'));
  115. $options = array('aggregate' => 'max');
  116. $this->assertSame(2, $redis->zinterstore('letters:max', 2, 'letters:1st', 'letters:2nd', $options));
  117. $this->assertSame(array(array('b', '2'), array('c', '3')), $redis->zrange('letters:max', 0, -1, 'withscores'));
  118. $options = array('aggregate' => 'sum');
  119. $this->assertSame(2, $redis->zinterstore('letters:sum', 2, 'letters:1st', 'letters:2nd', $options));
  120. $this->assertSame(array(array('b', '3'), array('c', '5')), $redis->zrange('letters:sum', 0, -1, 'withscores'));
  121. }
  122. /**
  123. * @group connected
  124. */
  125. public function testStoresIntersectionWithWeightsModifier()
  126. {
  127. $redis = $this->getClient();
  128. $redis->zadd('letters:1st', 1, 'a', 2, 'b', 3, 'c');
  129. $redis->zadd('letters:2nd', 1, 'b', 2, 'c', 3, 'd');
  130. $options = array('weights' => array(2, 3));
  131. $this->assertSame(2, $redis->zinterstore('letters:out', 2, 'letters:1st', 'letters:2nd', $options));
  132. $this->assertSame(array(array('b', '7'), array('c', '12')), $redis->zrange('letters:out', 0, -1, 'withscores'));
  133. }
  134. /**
  135. * @group connected
  136. */
  137. public function testStoresIntersectionWithCombinedModifiers()
  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('aggregate' => 'max', 'weights' => array(10, 15));
  143. $this->assertSame(2, $redis->zinterstore('letters:out', 2, 'letters:1st', 'letters:2nd', $options));
  144. $this->assertSame(array(array('b', '20'), array('c', '30')), $redis->zrange('letters:out', 0, -1, 'withscores'));
  145. }
  146. /**
  147. * @group connected
  148. * @expectedException Predis\ServerException
  149. * @expectedExceptionMessage ERR Operation against a key holding the wrong kind of value
  150. */
  151. public function testThrowsExceptionOnWrongType()
  152. {
  153. $redis = $this->getClient();
  154. $redis->set('foo', 'bar');
  155. $redis->zinterstore('zset:destination', 1, 'foo');
  156. }
  157. }