ZSetIntersectionStoreTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. /**
  12. * @group commands
  13. * @group realm-zset
  14. */
  15. class ZSetIntersectionStoreTest extends PredisCommandTestCase
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function getExpectedCommand()
  21. {
  22. return 'Predis\Command\ZSetIntersectionStore';
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function getExpectedId()
  28. {
  29. return 'ZINTERSTORE';
  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. */
  75. public function testStoresIntersectionInNewSortedSet()
  76. {
  77. $redis = $this->getClient();
  78. $redis->zadd('letters:1st', 1, 'a', 2, 'b', 3, 'c');
  79. $redis->zadd('letters:2nd', 1, 'b', 2, 'c', 3, 'd');
  80. $this->assertSame(2, $redis->zinterstore('letters:out', 2, 'letters:1st', 'letters:2nd'));
  81. $this->assertSame(array('b' => '3', 'c' => '5'), $redis->zrange('letters:out', 0, -1, 'withscores'));
  82. $this->assertSame(0, $redis->zinterstore('letters:out', 2, 'letters:1st', 'letters:void'));
  83. $this->assertSame(0, $redis->zinterstore('letters:out', 2, 'letters:void', 'letters:2nd'));
  84. $this->assertSame(0, $redis->zinterstore('letters:out', 2, 'letters:void', 'letters:void'));
  85. }
  86. /**
  87. * @group connected
  88. */
  89. public function testStoresIntersectionWithAggregateModifier()
  90. {
  91. $redis = $this->getClient();
  92. $redis->zadd('letters:1st', 1, 'a', 2, 'b', 3, 'c');
  93. $redis->zadd('letters:2nd', 1, 'b', 2, 'c', 3, 'd');
  94. $options = array('aggregate' => 'min');
  95. $this->assertSame(2, $redis->zinterstore('letters:min', 2, 'letters:1st', 'letters:2nd', $options));
  96. $this->assertSame(array('b' => '1', 'c' => '2'), $redis->zrange('letters:min', 0, -1, 'withscores'));
  97. $options = array('aggregate' => 'max');
  98. $this->assertSame(2, $redis->zinterstore('letters:max', 2, 'letters:1st', 'letters:2nd', $options));
  99. $this->assertSame(array('b' => '2', 'c' => '3'), $redis->zrange('letters:max', 0, -1, 'withscores'));
  100. $options = array('aggregate' => 'sum');
  101. $this->assertSame(2, $redis->zinterstore('letters:sum', 2, 'letters:1st', 'letters:2nd', $options));
  102. $this->assertSame(array('b' => '3', 'c' => '5'), $redis->zrange('letters:sum', 0, -1, 'withscores'));
  103. }
  104. /**
  105. * @group connected
  106. */
  107. public function testStoresIntersectionWithWeightsModifier()
  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('weights' => array(2, 3));
  113. $this->assertSame(2, $redis->zinterstore('letters:out', 2, 'letters:1st', 'letters:2nd', $options));
  114. $this->assertSame(array('b' => '7', 'c' => '12'), $redis->zrange('letters:out', 0, -1, 'withscores'));
  115. }
  116. /**
  117. * @group connected
  118. */
  119. public function testStoresIntersectionWithCombinedModifiers()
  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' => 'max', 'weights' => array(10, 15));
  125. $this->assertSame(2, $redis->zinterstore('letters:out', 2, 'letters:1st', 'letters:2nd', $options));
  126. $this->assertSame(array('b' => '20', 'c' => '30'), $redis->zrange('letters:out', 0, -1, 'withscores'));
  127. }
  128. /**
  129. * @group connected
  130. * @expectedException \Predis\Response\ServerException
  131. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  132. */
  133. public function testThrowsExceptionOnWrongType()
  134. {
  135. $redis = $this->getClient();
  136. $redis->set('foo', 'bar');
  137. $redis->zinterstore('zset:destination', '1', 'foo');
  138. }
  139. }