ZSetAddTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 ZSetAddTest extends PredisCommandTestCase
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function getExpectedCommand()
  21. {
  22. return 'Predis\Command\ZSetAdd';
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function getExpectedId()
  28. {
  29. return 'ZADD';
  30. }
  31. /**
  32. * @group disconnected
  33. */
  34. public function testFilterArguments()
  35. {
  36. $arguments = array('key', 1, 'member1', 2, 'member2');
  37. $expected = array('key', 1, 'member1', 2, 'member2');
  38. $command = $this->getCommand();
  39. $command->setArguments($arguments);
  40. $this->assertSame($expected, $command->getArguments());
  41. }
  42. /**
  43. * @group disconnected
  44. */
  45. public function testFilterArgumentsMembersScoresAsSingleArray()
  46. {
  47. $arguments = array('key', array('member1' => 1, 'member2' => 2));
  48. $expected = array('key', 1, 'member1', 2, 'member2');
  49. $command = $this->getCommand();
  50. $command->setArguments($arguments);
  51. $this->assertSame($expected, $command->getArguments());
  52. }
  53. /**
  54. * @group disconnected
  55. */
  56. public function testFilterArgumentsMembersScoresAsSingleArrayWithModifiers()
  57. {
  58. $arguments = array('key', 'NX', 'CH', array('member1' => 1, 'member2' => 2));
  59. $expected = array('key', 'NX', 'CH', 1, 'member1', 2, 'member2');
  60. $command = $this->getCommand();
  61. $command->setArguments($arguments);
  62. $this->assertSame($expected, $command->getArguments());
  63. }
  64. /**
  65. * @group disconnected
  66. */
  67. public function testParseResponse()
  68. {
  69. $this->assertSame(1, $this->getCommand()->parseResponse(1));
  70. }
  71. /**
  72. * @group connected
  73. */
  74. public function testAddsOrUpdatesMembersOrderingByScore()
  75. {
  76. $redis = $this->getClient();
  77. $this->assertSame(5, $redis->zadd('letters', 1, 'a', 2, 'b', 3, 'c', 4, 'd', 5, 'e'));
  78. $this->assertSame(array('a', 'b', 'c', 'd', 'e'), $redis->zrange('letters', 0, -1));
  79. $this->assertSame(1, $redis->zadd('letters', 1, 'e', 8, 'c', 6, 'f'));
  80. $this->assertSame(array('a', 'e', 'b', 'd', 'f', 'c'), $redis->zrange('letters', 0, -1));
  81. }
  82. /**
  83. * @group connected
  84. * @requiresRedisVersion >= 3.0.2
  85. */
  86. public function testOnlyAddsNonExistingMembersWithModifierNX()
  87. {
  88. $redis = $this->getClient();
  89. $this->assertSame(5, $redis->zadd('letters', 1, 'a', 2, 'b', 3, 'c', 4, 'd', 5, 'e'));
  90. $this->assertSame(array('a', 'b', 'c', 'd', 'e'), $redis->zrange('letters', 0, -1));
  91. $this->assertSame(2, $redis->zadd('letters', 'NX', 8, 'a', 1, 'f', 8, 'g', 4, 'e'));
  92. $this->assertSame(array('a', 'f', 'b', 'c', 'd', 'e', 'g'), $redis->zrange('letters', 0, -1));
  93. }
  94. /**
  95. * @group connected
  96. * @requiresRedisVersion >= 3.0.2
  97. */
  98. public function testOnlyUpdatesExistingMembersWithModifierXX()
  99. {
  100. $redis = $this->getClient();
  101. $this->assertSame(5, $redis->zadd('letters', 1, 'a', 2, 'b', 3, 'c', 4, 'd', 5, 'e'));
  102. $this->assertSame(array('a', 'b', 'c', 'd', 'e'), $redis->zrange('letters', 0, -1));
  103. $this->assertSame(0, $redis->zadd('letters', 'XX', 1, 'd', 2, 'c', 3, 'b', 1, 'x', 0, 'y'));
  104. $this->assertSame(array('a', 'd', 'c', 'b', 'e'), $redis->zrange('letters', 0, -1));
  105. }
  106. /**
  107. * @group connected
  108. * @requiresRedisVersion >= 3.0.2
  109. */
  110. public function testReturnsNumberOfAddedAndUpdatedElementsWithModifierCH()
  111. {
  112. $redis = $this->getClient();
  113. $this->assertSame(5, $redis->zadd('letters', 'CH', 1, 'a', 2, 'b', 3, 'c', 4, 'd', 5, 'e'));
  114. $this->assertSame(array('a', 'b', 'c', 'd', 'e'), $redis->zrange('letters', 0, -1));
  115. $this->assertSame(2, $redis->zadd('letters', 'NX', 'CH', 8, 'a', 1, 'f', 8, 'g', 4, 'e'));
  116. $this->assertSame(array('a', 'f', 'b', 'c', 'd', 'e', 'g'), $redis->zrange('letters', 0, -1));
  117. $this->assertSame(3, $redis->zadd('letters', 'XX', 'CH', 1, 'd', 2, 'c', 3, 'b', 1, 'x', 0, 'y'));
  118. $this->assertSame(array('a', 'd', 'f', 'c', 'b', 'e', 'g'), $redis->zrange('letters', 0, -1));
  119. }
  120. /**
  121. * @group connected
  122. * @requiresRedisVersion >= 3.0.2
  123. */
  124. public function testActsLikeZINCRBYWithModifierINCR()
  125. {
  126. $redis = $this->getClient();
  127. $this->assertSame('1', $redis->zadd('letters', 'INCR', 1, 'a'));
  128. $this->assertSame('0', $redis->zadd('letters', 'INCR', -1, 'a'));
  129. $this->assertSame('0.5', $redis->zadd('letters', 'INCR', 0.5, 'a'));
  130. $this->assertSame('-10', $redis->zadd('letters', 'INCR', -10.5, 'a'));
  131. }
  132. /**
  133. * @group connected
  134. * @requiresRedisVersion >= 3.0.2
  135. * @expectedException \Predis\Response\ServerException
  136. * @expectedExceptionMessage INCR option supports a single increment-element pair
  137. */
  138. public function testDoesNotAcceptMultipleScoreElementPairsWithModifierINCR()
  139. {
  140. $redis = $this->getClient();
  141. $redis->zadd('letters', 'INCR', 1, 'a', 2, 'b');
  142. }
  143. /**
  144. * @group connected
  145. */
  146. public function testAcceptsFloatValuesAsScore()
  147. {
  148. $redis = $this->getClient();
  149. $redis->zadd('letters', 0.2, 'b', 0.3, 'a', 0.1, 'c');
  150. $this->assertSame(array('c', 'b', 'a'), $redis->zrange('letters', 0, -1));
  151. }
  152. /**
  153. * @group connected
  154. * @expectedException \Predis\Response\ServerException
  155. * @expectedExceptionMessage Operation against a key holding the wrong kind of value
  156. */
  157. public function testThrowsExceptionOnWrongType()
  158. {
  159. $redis = $this->getClient();
  160. $redis->set('foo', 'bar');
  161. $redis->zadd('foo', 10, 'bar');
  162. }
  163. }