ZSetIntersectionStoreTest.php 5.3 KB

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